狂神说Java笔记–反射和注解部分笔记-java 怎么通过注解反射到

1、狂神说Java笔记-反射和注解部分笔记:java如何通过注解反射到类

1、狂神说Java笔记-反射和注解部分笔记:java如何通过注解反射到类

@Retention(RetentionPolicy.RUNTIME)

public@interfaceNewAnnotationType{

2,狂神说Java笔记-反思和注释部分笔记:Java需要学什么软件?

2,狂神说Java笔记-反思和注释部分笔记:Java需要学什么软件?

3、狂神说Java注解-反射和注解部分注解:Java注解是怎么实现的?

3、狂神说Java注解-反射和注解部分注解:Java注解是怎么实现的?

写一个使用这个注解的类:

;*CreatedbyAdministratoron/1/18、@TestAnnotation(count=)

publicclassTestMain{

publicstaticvoidmain(String[]args)throwsInterruptedException,NoSuchFieldException,IllegalAccessException, IOException{

TestAnnotationannotation=;

;反编译这段代码:

Classfile/e:/workspace/intellij/SpringTest/target/classes/TestMain.class

Lastmodified-1 -20;sizebytes

Compiledfrom”TestMain.java”

publicclassTestMain

minorversion:0

majorversion:52

flags:ACC_PUBLIC,ACC_SUPER

Constantpool:

#1 =Methodref#10、#34//java/lang/Object.” “:()V

#2=Class#35//TestMain

#3=Class#36//TestAnnotation
#4=Methodref#37、#38//java/lang/Class.getAnnotation :(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

#5=Fieldref#39、#40// java/lang/System.out:Ljava/io/PrintStream;

#6 =InterfaceMethodref#3、#41//TestAnnotation.count:()I

#7=Methodref#42、#43//java /io/PrintStream.println:(I)V

#8=Fieldref# 39、#44//java/lang/System.in:Ljava/io/InputStream;

#9=Methodref#45 .#46//java/io/InputStream.read:()I

#10 =Class#47//java/lang/Object

#11=Utf8

#12=Utf8( )V

#13=

#14=

#15=

#16=

#17=;

#18=

#19=Utf8([Ljava/lang/String;)V

#20=

#21=Utf8[Ljava /lang/String;

#22=

#23=;

#24=

#25 =Class#48//java/lang/InterruptedException

#26=Class#49//java/lang/NoSuchFieldException

#27=Class#50//java/ lang/IllegalAccessException

#28=Class #51//java/io/IOException

#29=

#30=.java

#31=

#32=

#33 =Integer

#34=NameAndType# 11:#12//” “:()V

#35=

#36=

#37=Class#52//java/ lang/Class

#38=NameAndType #53:#54//getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

#39=Class#55//java/lang/ System

#40=NameAndType#56 :#57//out:Ljava/io/PrintStream;

#41=NameAndType#32:#58//count:()I

#42=Class# 59//java/io/PrintStream

#43=NameAndType#60:#61//println:(I)V

#44=NameAndType#62:#63//in:Ljava/io/InputStream;

#45=Class#64/ /java/io/InputStream

#46=NameAndType#65:#58//read:()I

#47= /lang/Object

#48 =/lang/InterruptedException

#49=/lang/NoSuchFieldException

#50=/lang/IllegalAccessException

#51=/io/IOException

#52= /lang/Class

#53 =

#54=Utf8(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;#55=/lang/System

#56=

#57=/io/PrintStream;

#58=Utf8()I

#59=/io/PrintStream

# 60=

#61=Utf8(I)V

#62=

#63=/io/InputStream;

#64=/io/InputStream

#65=();

descriptor:()V

flags:ACC_PUBLIC

代码:

stack=1,locals=1,args_size=1

0:aload_0

1:invokespecial#1//Methodjava/ lang/Object.” “:()V

4:return

LineNumberTable:

line7:0

LocalVariableTable:

StartLengthSlotNameSignature

publicstaticvoidmain([] );

描述符:([Ljava/lang/String;) V

flags:ACC_PUBLIC,ACC_STATIC

Code:

stack=2,locals=2,args_size=1

0:ldc #2//classTestMain

2:ldc#3//classTestAnnotation

4:invokevirtual#4//Methodjava/lang/Class.getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

7:checkcast#3//classTestAn notation

10:astore_1

11:getstatic#5//Fieldjava/lang/System.out:Ljava/io/PrintStream;

14:aload_1
15:invokeinterface#6,1//InterfaceMethodTestAnnotation .count:()I

20:invokevirtual#7//Methodjava/io/PrintStream.pri ntln:(I)V

23:getstatic#8//Fieldjava/lang/System.in:Ljava/io/InputStream;

26:invokevirtual#9//Methodjava/io/InputStream.read:() I

29:pop

30:return

LineNumberTable:

:11

LocalVariableTable:

StartLengthSlotNameSignature

[Ljava/lang/String;

Exceptions:

:”TestMain.java”

RuntimeVisibleAnnotations:

0:#23(#32=I#33)

***行代码说明注解`TestAnnotation`的属性设置是在编译时确定的。 (属性的描述是[这里][1])。

然后,运行上面的程序,通过CLHSDB在eden区找到注解实例,

hsdb>scanoopsTestAnnotation

com/sun/proxy/$Proxy1

输入`com/sun/proxy/$ Proxy1` 是jdk动态代理生成对象时的默认类型,其中

”是默认包名,定义在`ReflectUtil`类的`PROXY_PACKAGE`字段中。代理类名`$PROXY1`

包含两部分,其中前缀`$PROXY`是jdk的默认代理类名前缀(参见“class javadoc),

后的1是自增。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注