package be.ac.vub.objectAnnotations.contextCapturing; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import be.ac.vub.objectAnnotations.AnnotationManager; import be.ac.vub.objectAnnotations.ObjectAnnotation; public aspect ContextCapturingFieldsAspect { pointcut settingField(Object parent, Object value) : set(* *.*) && target(parent) && args(value); //this could be statically determined but aspectJ doesn't let me capture meta-annotations, and it complains that Annotation doesn't have runtime retention after(Object parent, Object value) : settingField(parent, value) { Class declaringClass = thisJoinPointStaticPart.getSignature().getDeclaringType(); String fieldName = thisJoinPointStaticPart.getSignature().getName(); try { Field field = declaringClass.getDeclaredField(fieldName); Annotation[] annotations = field.getAnnotations(); Annotation objectAnnotation = null; boolean isFieldObjectAnnotated = false; for (int i = 0; i < annotations.length && !isFieldObjectAnnotated; i++) { Annotation fieldAnnotation = annotations[i]; Class annotationType = fieldAnnotation.annotationType(); isFieldObjectAnnotated |= null != annotationType.getAnnotation(ObjectAnnotation.class); if(isFieldObjectAnnotated){ objectAnnotation = fieldAnnotation; } } if(isFieldObjectAnnotated){ // System.out.println("Setting context for "+value); AnnotationManager.setContext(value, parent,objectAnnotation.annotationType()); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }