I have these 4 classes -
- Component.java -> which will be annotated using custom annotation,
- Properties.java -> the annotation class
- Utilities.java -> class where the annotation is used
- AnnotationProcessor.java -> where the annotation logic using reflection is built What I want is, based on the annotation, I will set a property of the Component object. This is my code -
//class which will be annotated using custom annotation
public class Component { private String name; private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }
//annotation class
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Properties {
String compName();
}
//class where the annotation is used
public class Utility {
@Properties(compName = "myName")
Component comp1;
@Properties(compName = "myPassword")
Component comp2;
public void login() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
comp1 = new Component();
comp2 = new Component();
comp1.setId("1111");
comp2.setId("2222");
AnnotationProcessor annotationProcessor = new AnnotationProcessor();
annotationProcessor.process(this, comp1);
annotationProcessor.process(this, comp2);
}}
//class where the annotation is processed
public class AnnotationProcessor {
public void process (Object callingClass, Component c){
System.out.println("In AnnotationProcessor:process");
Class<? extends Object> aClass = callingClass.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field: declaredFields) {
if(field.isAnnotationPresent(Properties.class)){
if (field.get(callingClass) != null) { //Added newly
Properties annotation = field.getAnnotation(Properties.class);
String val = annotation.compName();
c.setName(val);
field.set(callingClass, c); //Added newly
System.out.println("For field " + field.getName() + ", component id is : " + c.getId() + " and name is : --> " + c.getName());
break; //Added newly
}
}
}
}
I want to achieve to set the name of "comp1" to be "myName" and name of "comp2" to be "myPassword". Basically, the output I expect is this -
For field comp1, component id is : 1111 and name is : --> myName
For field comp2, component id is : 2222 and name is : --> myPassword
But the actual output is this (after //added newly changes)-
In AnnotationProcessor:process
For field comp1, component id is : 1111 and name is : --> myName
In AnnotationProcessor:process
For field comp1, component id is : 2222 and name is : --> myName
My problem is, how to assign the "name" of the component as per the annotation for that component object only and not for all the fields the Utility class has. I feel there is some if condition in the middle that might help achieve this, but not able to find it out.