I have this annotation type class:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RemoteTcpAccess {
public int port();
}
and apply it on another class like this:
@RemoteTcpAccess(port = 444)
public class CalculatorService {
public int Add(int a, int b) {
return a + b;
}
public void DisplayText(String text) {
System.out.println(text);
}
}
Now I get the CalculatorService Class object and try to get the information about RemoteTcpAccess annotation:
private static void CheckRemoteTcpAccess(List<Class> classes) {
for (Class class1 : classes) {
for (Annotation annotation : class1.getDeclaredAnnotations()) {
if (AnnotationEquals(annotation, ComponentsProtocol.REMOTE_TCP_ACCESS)) {
//GET INFORMATION
}
}
}
}
private static boolean AnnotationEquals(Annotation classAnnotation, String protocolAnnotation) {
return classAnnotation.toString()
.substring(0, classAnnotation.toString().indexOf("("))
.equals(protocolAnnotation);
}
I am able to recognize if the class has applied RemoteTcpAccess annotation on it, but I cant get inforamtion about what fields has the annotation and what values have those fields, like :
Field port - value 444
Is there any way how to get those inforamtion via reflection?
thanks