Java 什么';这是ClassVisitor#visitTypeAnnotation和MethodVisitor#visitTypeAnnotation的示例

Java 什么';这是ClassVisitor#visitTypeAnnotation和MethodVisitor#visitTypeAnnotation的示例,java,java-8,java-bytecode-asm,Java,Java 8,Java Bytecode Asm,我正在学习asm,我发现了两个有趣的api 在org.objectweb.asm.ClassVisitor /** * Visits an annotation on a type in the class signature. */ public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible); /** * Visits

我正在学习asm,我发现了两个有趣的api

org.objectweb.asm.ClassVisitor

/**
 * Visits an annotation on a type in the class signature.
 */
public AnnotationVisitor visitTypeAnnotation(int typeRef,
        TypePath typePath, String desc, boolean visible);
/**
 * Visits an annotation on a type in the method signature.
 * 
 */
public AnnotationVisitor visitTypeAnnotation(int typeRef,
        TypePath typePath, String desc, boolean visible);
org.objectweb.asm.MethodVisitor

/**
 * Visits an annotation on a type in the class signature.
 */
public AnnotationVisitor visitTypeAnnotation(int typeRef,
        TypePath typePath, String desc, boolean visible);
/**
 * Visits an annotation on a type in the method signature.
 * 
 */
public AnnotationVisitor visitTypeAnnotation(int typeRef,
        TypePath typePath, String desc, boolean visible);
但在什么情况下我们将使用这两种方法

在java中,我们如何在类/方法签名中的类型上生成带有注释的类

我试着

 public @Z Integer testMethod(String testParam)
但是,
@Z
仍然由VisitAnotation调用,而不是visitTypeAnnotation

asm将调用visitTypeAnnotation的情况是什么

thx~

是Java8的一个新特性。为了使注释能够在类型上下文中使用,注释类型本身必须使用
@Target(
进行注释,但请注意,当注释同时支持Target
方法时,如下声明

public @Z Integer testMethod(String testParam)
public Integer testMethod(@Z String testParam)
这是含糊不清的。好的,注释将同时记录方法和返回类型,然后。类似地,类似于

public @Z Integer testMethod(String testParam)
public Integer testMethod(@Z String testParam)
如果
@Z
同时支持
参数
目标,则不明确


只有类型注释才能出现的独特用法示例如下

public Integer testMethod(List<@Z String> testParam) throws @Z RuntimeException {
    return new @Z Integer(testParam.get((@Z int)0));
}

在本例中,
instanceMethod()
的方法接收方类型是
@Z example
而不是
example
,尽管这种差异对Java语言本身没有意义。

Z
是否有
@Target(ElementType.type\u USE)
注释?@immibis Oh~~,thx immibis,我明白了
Type Annotation
是Java 8的一项新功能,我学习了一些
Type Annotation
信息并升级到Java 8,将
@Target(ElementType.Type_USE)
标记为@Z它可以工作…谢谢你(我仍然使用jdk7)