Reputation: 43
I don't have any experience with AspectJ but recently I've got task to implement and I've read several AspectJ tutorials so I think that AspectJ can be my solution.
The task is the following: I have a class A with some method b() and objects of this class are included as fields in other classes. I'd like to annotate these fields with some annotation @C and to get this annotation value each time I call the method b() on field with type A and annotation @C.
Simplified code:
class A{
field1;
field2;
field3;
void b(String[] fieldsToIgnore){
doSomething with fields 1,2,3 (excluding fields from fieldsToIgnore array)
}
}
class B{
@C(value="field1,field2")
A fieldA;
}
//Here when I want to weaver my aspect - before I call to method b() on fieldA with annotation @C - I want to get annotation value and to pass it as an argument to method b()
new B.fieldA.b()
Please help me to write correct pointcut for me advise. I also can't quite understand how can I transfer data from my advise method to my method b() - is it possible at all?
Will appreciate any help - even if you just tell mt "No, it's not possible" - it will save me a lot of time and nerves :)
Upvotes: 0
Views: 564
Reputation: 67457
Actually, if you are already using reflection massively anyway - which is not an excuse for not refactoring the code, BTW - and wish to continue doing so, actually you do not really need AspectJ in order to make the mess even worse. You can just do it like this:
Helper class for reflective field access:
package de.scrum_master.app;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
public class FieldHelper {
public static Field getField(Class<?> clazz, String fieldName) {
Field field;
try {
field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException | SecurityException e) {
throw new RuntimeException("Reflection problem", e);
}
field.setAccessible(true);
return field;
}
public static Field[] getFields(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields)
field.setAccessible(true);
return fields;
}
public static List<String> extractIgnoredFieldsList(Class<?> clazz, String fieldName) {
return Arrays.asList(
getField(clazz, fieldName)
.getAnnotation(IgnoreFields.class)
.fieldNames()
);
}
}
Marker annotation:
package de.scrum_master.app;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface IgnoreFields {
public String[] fieldNames();
}
Class with method to be called:
package de.scrum_master.app;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import static de.scrum_master.app.FieldHelper.*;
public class A {
int number = 11;
String text = "Hi there!";
Date date = new Date();
String optionalText = "I am not really always needed";
int optionalNumber = 123;
public void doSomething(List<String> ignoredFields) {
for (Field field : getFields(this.getClass())) {
if (!ignoredFields.contains(field.getName())) {
try {
System.out.println(field.getName() + " = " + field.get(this));
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("Reflection problem", e);
}
}
}
}
}
Class with annotated member fields:
package de.scrum_master.app;
import static de.scrum_master.app.FieldHelper.*;
public class B {
@IgnoreFields(fieldNames = { "optionalText", "optionalNumber" })
A noOptionalsA = new A();
@IgnoreFields(fieldNames = { "text", "number", "date" })
A onlyOptionalsA = new A();
public static void main(String[] args) {
B b = new B();
b.noOptionalsA.doSomething(extractIgnoredFieldsList(b.getClass(), "noOptionalsA"));
System.out.println("----------------------------------------");
b.onlyOptionalsA.doSomething(extractIgnoredFieldsList(b.getClass(), "onlyOptionalsA"));
}
}
Console log:
number = 11
text = Hi there!
date = Wed Dec 27 18:54:44 ICT 2017
----------------------------------------
optionalText = I am not really always needed
optionalNumber = 123
If you do not understand what is happening here or if you still insist in an AspectJ solution, please let me know and I will explain and/or provide extra aspect code.
Upvotes: 1