Home > Java > javaTutorial > body text

Java custom annotation interface implementation solution

高洛峰
Release: 2017-01-23 15:26:55
Original
1861 people have browsed it

Java annotations are some meta-information attached to the code, which are used by some tools to parse and use during compilation and runtime, and serve the function of explanation and configuration.
Annotations will not and cannot affect the actual logic of the code, they only play a supporting role. Contained in the java.lang.annotation package.
1. Meta-annotation
Meta-annotation refers to the annotation of annotation. There are four types including @Retention @Target @Document @Inherited.
1.1. @Retention: Define the retention policy of annotations
Java code

@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含 
@Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得, 
@Retention(RetentionPolicy.RUNTIME)//注解会在class字节码文件中存在,在运行时可以通过反射获取到
Copy after login

1.2. @Target: Define the purpose of annotations
Java code

@Target(ElementType.TYPE) //接口、类、枚举、注解 
@Target(ElementType.FIELD) //字段、枚举的常量 
@Target(ElementType.METHOD) //方法 
@Target(ElementType.PARAMETER) //方法参数 
@Target(ElementType.CONSTRUCTOR) //构造函数 
@Target(ElementType.LOCAL_VARIABLE)//局部变量 
@Target(ElementType.ANNOTATION_TYPE)//注解 
@Target(ElementType.PACKAGE) ///包
Copy after login

elementType can be Multiple, an annotation can be of a class, a method, a field, etc.
1.3. @Document: Indicates that the annotation will be included in javadoc
1.4. @Inherited: Indicates that subclasses can inherit from parent classes The annotation
The following is an example of a custom annotation
2. Customization of annotations
Java code

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface HelloWorld { 
public String name() default ""; 
}
Copy after login

3. Use of annotations, test class
Java code

public class SayHello { 
@HelloWorld(name = " 小明 ") 
public void sayHello(String name) { 
System.out.println(name + "say hello world!"); 
}//www.heatpress123.net 
}
Copy after login

4. Parse annotations
java’s reflection mechanism can help to get annotations. The code is as follows:
Java code

public class AnnTest { 
public void parseMethod(Class<?> clazz) { 
Object obj; 
try { 
// 通过默认构造方法创建一个新的对象 
obj = clazz.getConstructor(new Class[] {}).newInstance( 
new Object[] {}); 
for (Method method : clazz.getDeclaredMethods()) { 
HelloWorld say = method.getAnnotation(HelloWorld.class); 
String name = ""; 
if (say != null) { 
name = say.name(); 
System.out.println(name); 
method.invoke(obj, name); 
} 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
public static void main(String[] args) { 
AnnTest t = new AnnTest(); 
t.parseMethod(SayHello.class); 
} 
}
Copy after login

Please pay attention to more articles related to java custom annotation interface implementation solutions PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!