美文网首页
Java注解实现浅析

Java注解实现浅析

作者: huapro | 来源:发表于2019-05-15 09:30 被阅读0次

Java注解实现浅析https://blog.csdn.net/kisimple/article/details/43709505

Java字节码(.class文件)格式详解https://www.xuebuyuan.com/1778079.html

java注解——最通俗易懂的解释https://zhuanlan.zhihu.com/p/41554884

Java奇技淫巧-插件化注解处理API(Pluggable Annotation Processing API)https://www.cnblogs.com/throwable/p/9139908.html

TestMain

import java.io.IOException; 
@TestAnnotation(count = 0x7fffffff)
public class TestMain {
    public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalAccessException, IOException{
    TestAnnotation annotation = TestMain.class.getAnnotation(TestAnnotation.class);
    System.out.println(annotation.count());
    System.in.read();
   }
}

TestAnnotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by Administrator on 2015/1/18.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {

    int count() default 1;

}

TestMain 反编译

javap -v TestMain.class
Classfile /Users/yanglihua/Desktop/temp/TestMain.class
  Last modified May 15, 2019; size 877 bytes
  MD5 checksum fdee1cc47020ac40a5f89847c88a9e64
  Compiled from "TestMain.java"
public class TestMain
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #10.#28        // java/lang/Object."<init>":()V
   #2 = Class              #29            // TestMain
   #3 = Class              #30            // TestAnnotation
   #4 = Methodref          #31.#32        // java/lang/Class.getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
   #5 = Fieldref           #33.#34        // java/lang/System.out:Ljava/io/PrintStream;
   #6 = InterfaceMethodref #3.#35         // TestAnnotation.count:()I
   #7 = Methodref          #36.#37        // java/io/PrintStream.println:(I)V
   #8 = Fieldref           #33.#38        // java/lang/System.in:Ljava/io/InputStream;
   #9 = Methodref          #39.#40        // java/io/InputStream.read:()I
  #10 = Class              #41            // java/lang/Object
  #11 = Utf8               <init>
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               main
  #16 = Utf8               ([Ljava/lang/String;)V
  #17 = Utf8               Exceptions
  #18 = Class              #42            // java/lang/InterruptedException
  #19 = Class              #43            // java/lang/NoSuchFieldException
  #20 = Class              #44            // java/lang/IllegalAccessException
  #21 = Class              #45            // java/io/IOException
  #22 = Utf8               SourceFile
  #23 = Utf8               TestMain.java
  #24 = Utf8               RuntimeVisibleAnnotations
  #25 = Utf8               LTestAnnotation;
  #26 = Utf8               count
  #27 = Integer            2147483647
  #28 = NameAndType        #11:#12        // "<init>":()V
  #29 = Utf8               TestMain
  #30 = Utf8               TestAnnotation
  #31 = Class              #46            // java/lang/Class
  #32 = NameAndType        #47:#48        // getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
  #33 = Class              #49            // java/lang/System
  #34 = NameAndType        #50:#51        // out:Ljava/io/PrintStream;
  #35 = NameAndType        #26:#52        // count:()I
  #36 = Class              #53            // java/io/PrintStream
  #37 = NameAndType        #54:#55        // println:(I)V
  #38 = NameAndType        #56:#57        // in:Ljava/io/InputStream;
  #39 = Class              #58            // java/io/InputStream
  #40 = NameAndType        #59:#52        // read:()I
  #41 = Utf8               java/lang/Object
  #42 = Utf8               java/lang/InterruptedException
  #43 = Utf8               java/lang/NoSuchFieldException
  #44 = Utf8               java/lang/IllegalAccessException
  #45 = Utf8               java/io/IOException
  #46 = Utf8               java/lang/Class
  #47 = Utf8               getAnnotation
  #48 = Utf8               (Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
  #49 = Utf8               java/lang/System
  #50 = Utf8               out
  #51 = Utf8               Ljava/io/PrintStream;
  #52 = Utf8               ()I
  #53 = Utf8               java/io/PrintStream
  #54 = Utf8               println
  #55 = Utf8               (I)V
  #56 = Utf8               in
  #57 = Utf8               Ljava/io/InputStream;
  #58 = Utf8               java/io/InputStream
  #59 = Utf8               read
{
  public TestMain();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0

  public static void main(java.lang.String[]) throws java.lang.InterruptedException, java.lang.NoSuchFieldException, java.lang.IllegalAccessException, java.io.IOException;
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: ldc           #2                  // class TestMain
         2: ldc           #3                  // class TestAnnotation
         4: invokevirtual #4                  // Method java/lang/Class.getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
         7: checkcast     #3                  // class TestAnnotation
        10: astore_1
        11: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
        14: aload_1
        15: invokeinterface #6,  1            // InterfaceMethod TestAnnotation.count:()I
        20: invokevirtual #7                  // Method java/io/PrintStream.println:(I)V
        23: getstatic     #8                  // Field java/lang/System.in:Ljava/io/InputStream;
        26: invokevirtual #9                  // Method java/io/InputStream.read:()I
        29: pop
        30: return
      LineNumberTable:
        line 5: 0
        line 6: 11
        line 7: 23
        line 8: 30
    Exceptions:
      throws java.lang.InterruptedException, java.lang.NoSuchFieldException, java.lang.IllegalAccessException, java.io.IOException
}
SourceFile: "TestMain.java"
RuntimeVisibleAnnotations:
  0: #25(#26=I#27)

TestAnnotation.class 反编译

javap -v TestAnnotation.class
Classfile /Users/yanglihua/Desktop/temp/TestAnnotation.class
  Last modified May 15, 2019; size 435 bytes
  MD5 checksum c5a4be1e3536f21be7b8a71cf354073a
  Compiled from "TestAnnotation.java"
public interface TestAnnotation extends java.lang.annotation.Annotation
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT, ACC_ANNOTATION
Constant pool:
   #1 = Class              #18            // TestAnnotation
   #2 = Class              #19            // java/lang/Object
   #3 = Class              #20            // java/lang/annotation/Annotation
   #4 = Utf8               count
   #5 = Utf8               ()I
   #6 = Utf8               AnnotationDefault
   #7 = Integer            1
   #8 = Utf8               SourceFile
   #9 = Utf8               TestAnnotation.java
  #10 = Utf8               RuntimeVisibleAnnotations
  #11 = Utf8               Ljava/lang/annotation/Target;
  #12 = Utf8               value
  #13 = Utf8               Ljava/lang/annotation/ElementType;
  #14 = Utf8               TYPE
  #15 = Utf8               Ljava/lang/annotation/Retention;
  #16 = Utf8               Ljava/lang/annotation/RetentionPolicy;
  #17 = Utf8               RUNTIME
  #18 = Utf8               TestAnnotation
  #19 = Utf8               java/lang/Object
  #20 = Utf8               java/lang/annotation/Annotation
{
  public abstract int count();
    descriptor: ()I
    flags: ACC_PUBLIC, ACC_ABSTRACT
    AnnotationDefault:
      default_value: I#7}
SourceFile: "TestAnnotation.java"
RuntimeVisibleAnnotations:
  0: #11(#12=[e#13.#14])
  1: #15(#12=e#16.#17)

相关文章

  • Java注解实现浅析

    Java注解实现浅析https://blog.csdn.net/kisimple/article/details/...

  • Java注解浅析

    注解(Annotation)是JDK1.5引入的机制,我们平时经常会遇到一些注解,比如@Override,@Tes...

  • java注解浅析

    自定义注解的定义方式 public @interface Class Preamble 一般来说,定义注解需要指定...

  • Spring注解原理探索(三)

    之 Java如何识别注解 关键词:Java 反射java.lang.reflect 包,实现反射功能的工具类。注解...

  • java配置 [配置扫描]

    实现功能 java注解扫描 服务 测试

  • 注解

    Java 注解原理 下面来看看Java中注解是如何实现的 创建注解类Inter: 创建测试类Test: 在程序第二...

  • 浅谈java注解

    Java注解用于为 Java代码提供元数据, 通过关键字@interface来定义一个注解,借助元注解实现一个注解...

  • Java注解Annotation浅析

    转载注明出处:http://www.jianshu.com/p/898d5893cda5 1. 概述 Java在1...

  • Java中的注解&反射

    注解 注解声明 声明一个注解类型 Java中所有的注解,默认是实现Annotation接口: 注解的声明使用@in...

  • Java注解

    在写java代码的过程中,经常会遇到注解,但是没有去理解注解背后的原理,也没有实现过注解。网上关于java注解的文...

网友评论

      本文标题:Java注解实现浅析

      本文链接:https://www.haomeiwen.com/subject/ecyuaqtx.html