Skip to main content

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

#: Paulo Alexandre Corigo Zenida changed the world a bit at a time by saying on  1/4/2006 2:46 PM :#
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
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


afaik they are read-only (and I think i see a technical reason in this: they are written in the bytecode, so if changing the value, you should modify the bytecode).

hope i'm not wrong,

./alex
--
.w( the_mindstorm )p.



Back to the top