Create custom annotaion class
package com.arvind;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(value=RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
String[] customValues();
}
Create Test class for testing
package com.arvind; import java.lang.reflect.Method; public class Test { @CustomAnnotation(customValues = { "First Value", "Second Value" }) public void testing() { } public static void main(String[] args) { Class clazz = Test.class; Method[] methods = clazz.getMethods(); for (Method method : methods) { if ("testing".equals(method.getName())) { if (method.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation ca = method.getAnnotation(CustomAnnotation.class); if (ca != null){ for (String value : ca.customValues()) { System.out.println(value); } } } } } } }Output will be
First Value Second Value
can you also post an example of Class or Type level custom annotation. Thank you in advance.
ReplyDeletehttp://mujenahipata.blogspot.com/2013/05/class-level-custom-annotation-java.html
Delete