Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Setting values for Annotations fields in runtime

Hello,

	I'm new to Annotations and I have previously searched for examples and tried 
some tests but I didn't achieve my goals, so I was wondering if someone could 
tell me: is it possible to change a value of an Annotation "field"?

	I was trying to create an Annotation that would tell me the access level 
required for a method to be executed, and I would like to set that access 
level by using properties declared in a properties file. To do so, I would 
like to change the default value for the Annotation in runtime. Is this 
possible?

	The source code would be something like this:

My Annotation:

	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.METHOD, ElementType.TYPE })
	public @interface MyAnnotation {
		int permission() default -1;
	}

Next, there's the class:

	import java.io.IOException;
	import java.util.Properties;

	@MyAnnotation(id = 3)
	public class A {

		@MyAnnotation
		public void foo() {

		}

		public static void main(String args[]) {
			A a = new A();
			a.foo();
		}
	}

And finally, here is the aspect:

	public aspect CaptureAnnotationValueAspect {

		void around(MyAnnotation myAnnotation):
			execution(* *.*(..)) && @annotation(myAnnotation) {

			System.out.println(myAnnotation.id());

			// How can I change the value for the Annotation, if possible?

			proceed(myAnnotation);
		}
	}


	Thanks in advance. Best regards,

		Paulo Zenida


Back to the top