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

Hello,

	I just want to say thanks. It really seems to work for what I intended with 
it.

	Best regards,

		Paulo Zenida

Em Quarta, 4 de Janeiro de 2006 15:59, o Dean Wampler escreveu:
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
>   <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
>   <title></title>
> </head>
> <body bgcolor="#ffffff" text="#000000">
> In this particular example, it seems like you don't really need to
> change the annotation value itself. You just need to make sure you use
> either it's declared "permission" value or an override you read
> elsewhere. You could look for an updated value in the advice every time
> or cache it somehow.<br>
> <br>
> For example, use an intertype declaration to insert an int field in the
> class annotated with MyAnnotation. Store the "mutable" value there
> instead:<br>
> <br>
> public aspect CaptureAnnotationValueAspect {<br>
> &nbsp;&nbsp;&nbsp; public static interface MyAnnoInterface {<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public int getPermission();<br>
> &nbsp;&nbsp;&nbsp; }<br>
> &nbsp;&nbsp;&nbsp; private int MyAnnoInterface.permission;<br>
> &nbsp;&nbsp;&nbsp; public int&nbsp; MyAnnoInterface.getPermission() {
> return permission; }<br> &nbsp;&nbsp;&nbsp; public void
> MyAnnoInterface.setPermission(int p) { permission = p; }<br> <br>
> &nbsp;&nbsp;&nbsp; declare parents: (@MyAnnotation *) implements
> MyAnnoInterface;<br> <br>
> &nbsp;&nbsp;&nbsp; private int count = 0;<br>
> &nbsp;&nbsp;&nbsp; void around(MyAnnotation myAnnotation, MyAnnoInterface
> myAnnoInterface):<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; execution(*
> com.aspectprogramming.aspects.experiments.annos.*.*(..)) <br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &amp;&amp;
> @annotation(myAnnotation) &amp;&amp; target(myAnnoInterface) {<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> myAnnoInterface.setPermission(myAnnotation.permission() + count);<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; count++;<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Permission =
> "+myAnnoInterface.getPermission()+" in
> "+thisJoinPointStaticPart.toShortString());<br>
> <br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; proceed(myAnnotation,
> myAnnoInterface);<br> &nbsp;&nbsp;&nbsp; }<br>
> }<br>
> <br>
> I declared an interface and used ITD to add it to any class that is
> annotated with MyAnnotation (which means that this example will only
> work if the annotation is on both the class and the method!). I also
> added a default permission field and accessor methods to the
> interface. (You could just make the new
> permission field public and skip the accessors....)<br>
> <br>
> Note that you can't do something like <br>
> <br>
> &nbsp;&nbsp;&nbsp; private MyAnnotation.mutablePermission;<br>
> <br>
> because this would attempt to inject the value on the annotation class
> itself, not the target class.<br>
> <br>
> In the advice, I set the interface's permission value to the
> annotation's value, then for demonstration purposes, I added an
> incrementing count. Finally, the "println" statement uses the
> interface's permission, not the annotation's value.<br>
> <br>
> If you change the test driver to call "a.foo()" several times, you'll
> see that the permission value counts up from -1.<br>
> <br>
> Refinements would include moving the "MyAnnoInterface.setPermission()"
> call into advice on the constructor, since it only needs to happen
> once.<br> <br>
> Hope this helps.<br>
> dean<br>
> <br>
> Alexandru Popescu wrote:
> <blockquote cite="mid43BBC645.6000505@xxxxxxxxx" type="cite">#: Paulo
> Alexandre Corigo Zenida changed the world a bit at a time by saying
> on&nbsp; 1/4/2006 2:46 PM :#
>   <br>
>   <blockquote type="cite">Hello,
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;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"?
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;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?
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;The source code would be something like this:
>     <br>
>     <br>
> My Annotation:
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.lang.annotation.ElementType;
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.lang.annotation.Retention;
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.lang.annotation.RetentionPolicy;
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.lang.annotation.Target;
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;@Retention(RetentionPolicy.RUNTIME)
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;@Target( { ElementType.METHOD, ElementType.TYPE })
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;public @interface MyAnnotation {
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int permission() default -1;
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;}
>     <br>
>     <br>
> Next, there's the class:
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.io.IOException;
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;import java.util.Properties;
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;@MyAnnotation(id = 3)
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;public class A {
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @MyAnnotation
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void foo() {
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String
> args[]) { <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A a =
> new A(); <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.foo();
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;}
>     <br>
>     <br>
> And finally, here is the aspect:
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;public aspect CaptureAnnotationValueAspect {
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void around(MyAnnotation
> myAnnotation): <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> execution(* *.*(..)) &amp;&amp; @annotation(myAnnotation) { <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> System.out.println(myAnnotation.id()); <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // How
> can I change the value for the Annotation, if possible?
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> proceed(myAnnotation); <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;}
>     <br>
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;Thanks in advance. Best regards,
>     <br>
>     <br>
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paulo Zenida
>     <br>
> _______________________________________________
>     <br>
> aspectj-users mailing list
>     <br>
> <a class="moz-txt-link-abbreviated"
> href="mailto:aspectj-users@xxxxxxxxxxx";>aspectj-users@xxxxxxxxxxx</a> <br>
> <a class="moz-txt-link-freetext"
> href="https://dev.eclipse.org/mailman/listinfo/aspectj-users";>https://dev.e
>clipse.org/mailman/listinfo/aspectj-users</a> <br>
>     <br>
>   </blockquote>
>   <br>
> 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).
>   <br>
>   <br>
> hope i'm not wrong,
>   <br>
>   <br>
> ./alex
>   <br>
> --
>   <br>
> .w( the_mindstorm )p.
>   <br>
>   <br>
> _______________________________________________
>   <br>
> aspectj-users mailing list
>   <br>
> <a class="moz-txt-link-abbreviated"
> href="mailto:aspectj-users@xxxxxxxxxxx";>aspectj-users@xxxxxxxxxxx</a> <br>
> <a class="moz-txt-link-freetext"
> href="https://dev.eclipse.org/mailman/listinfo/aspectj-users";>https://dev.e
>clipse.org/mailman/listinfo/aspectj-users</a> <br>
> </blockquote>
> <br>
> <br>
> <div class="moz-signature">-- <br>
> <title>Dean Wampler's Signature</title>
> <style type="text/css">
> 		FONT.mail { font-size:small; color:dark-grey;
> font-family:palatino,sans-serif;} FONT.name { font-size:medium;
> color:black; font-family: times;}
> 		FONT.quote2 { font-size:medium; color:black; font-family: times;}
> 		FONT.quote1 { font-size:medium; color:black; font-family: times;
> font-style:italic;} </style>
> <form>
>   <table border="0">
>     <tbody>
>       <tr>
>         <td colspan="3"><font class="name">Dean Wampler, Ph.D.</font></td>
>       </tr>
>       <tr>
>         <td colspan="3"><font class="mail">dean at
> aspectprogramming.com</font></td> </tr>
>       <tr>
>         <td colspan="3"><font class="mail"><a class="moz-txt-link-freetext"
> href="http://www.aspectprogramming.com";>http://www.aspectprogramming.com</a
>></font></td> </tr>
>       <tr>
>         <td colspan="3"><font class="mail"><a class="moz-txt-link-freetext"
> href="http://www.contract4j.org";>http://www.contract4j.org</a></font></td>
> </tr>
>       <tr>
>         <td colspan="3" align="deft"><font class="quote1">I want my
> tombstone to say:</font></td>
>       </tr>
>       <tr>
>         <td colspan="3" align="center" valign="top"><font
> class="quote2">Unknown Application Error in Dean Wampler.exe.<br>
> Application Terminated.</font></td>
>       </tr>
>       <tr>
>         <td align="center" valign="top"><font class="quote2"><button
>  type="button" text="Okay" name="Okay">Okay</button></font></td>
>         <td align="center" valign="top">&nbsp;</td>
>         <td align="center" valign="top"><font class="quote2"><button
>  type="button" text="Cancel" name="Cancel">Cancel</button></font></td>
>       </tr>
>     </tbody>
>   </table>
> </form>
> </div>
> </body>
> </html>