I uploaded a maven artefact to the GitHub Package Registry (Beta) and want to add it as maven dependency. I'm already in the Regestry-Beta and activated it for my sample HalloMaven project. Also the mvn deploy was succesful, so the artifact is public available here:
https://github.com/TobseF/HelloMaven/packages
But how to include it as a maven dependency?
I tried to add it in a fresh sample project with this pom.xml:
<groupId>de.tfr.test</groupId>
<artifactId>maven-repo-test</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>github</id>
<name>GitHub TobseF Apache Maven Packages</name> …Run Code Online (Sandbox Code Playgroud) 如何在运行时使用ASM读取 Java 方法注释的值?
注释只有一个CLASS RetentionPolicy,因此不可能使用反射来做到这一点。
| 策略CLASS:注释将由编译器记录在类文件中,但不需要在运行时由VM保留
示例:我想在运行时从字段中
提取值:Carly Rae Jepsenartist
public class SampleClass {
@MyAnnotation(artist = "Carly Rae Jepsen")
public void callMeMaybe(){}
}
Run Code Online (Sandbox Code Playgroud)
@Documented
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String artist() default "";
}
Run Code Online (Sandbox Code Playgroud)
但为什么?
难道你不能只改变RetentionPolicytoRUNTIME并用反射来做吗?
简而言之:不。我使用modelmapper框架(简单、智能、对象映射)。在那里,我通过带注释的方法指定了 Java 类之间的双向映射。我不想重用此分层映射信息来进行更改事件传播。但提供的mapstruct org.mapstruct.Mapping注解有CLASS RetentionPolicy. 这就是为什么我需要从类文件中读取该信息 - 并且需要ASM。
我写了一个Kotlin扩展,它添加next()了枚举值。但是有更好的方法吗?
fun <T : Enum<*>> T.next(): T {
val values = this::class.java.getEnumConstants()
return if (this.ordinal < values.size - 1)
values[this.ordinal + 1]
else
values[0]
}
enum class Color {Red, Yellow, Green}
Color.Red.next() //Yellow
Run Code Online (Sandbox Code Playgroud)
想法是从枚举值迭代到下一个值。但仅适用于特定值,不适用于整个枚举values()列表。我要防止的事情是在每个枚举类型中添加以下内容:
fun next() = if (this.ordinal == Color.values().size - 1)
Color.values()[0]
else
Color.values()[this.ordinal + 1]
Run Code Online (Sandbox Code Playgroud) annotations ×1
enums ×1
github ×1
github-ci ×1
java ×1
kotlin ×1
maven ×1
modelmapper ×1
reflection ×1