[
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>
> public static interface MyAnnoInterface {<br>
> public int getPermission();<br>
> }<br>
> private int MyAnnoInterface.permission;<br>
> public int MyAnnoInterface.getPermission() {
> return permission; }<br> public void
> MyAnnoInterface.setPermission(int p) { permission = p; }<br> <br>
> declare parents: (@MyAnnotation *) implements
> MyAnnoInterface;<br> <br>
> private int count = 0;<br>
> void around(MyAnnotation myAnnotation, MyAnnoInterface
> myAnnoInterface):<br>
> execution(*
> com.aspectprogramming.aspects.experiments.annos.*.*(..)) <br>
> &&
> @annotation(myAnnotation) && target(myAnnoInterface) {<br>
>
> myAnnoInterface.setPermission(myAnnotation.permission() + count);<br>
> count++;<br>
> System.out.println("Permission =
> "+myAnnoInterface.getPermission()+" in
> "+thisJoinPointStaticPart.toShortString());<br>
> <br>
> proceed(myAnnotation,
> myAnnoInterface);<br> }<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>
> 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 1/4/2006 2:46 PM :#
> <br>
> <blockquote type="cite">Hello,
> <br>
> <br>
> 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>
> 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>
> The source code would be something like this:
> <br>
> <br>
> My Annotation:
> <br>
> <br>
> import java.lang.annotation.ElementType;
> <br>
> import java.lang.annotation.Retention;
> <br>
> import java.lang.annotation.RetentionPolicy;
> <br>
> import java.lang.annotation.Target;
> <br>
> <br>
> @Retention(RetentionPolicy.RUNTIME)
> <br>
> @Target( { ElementType.METHOD, ElementType.TYPE })
> <br>
> public @interface MyAnnotation {
> <br>
> int permission() default -1;
> <br>
> }
> <br>
> <br>
> Next, there's the class:
> <br>
> <br>
> import java.io.IOException;
> <br>
> import java.util.Properties;
> <br>
> <br>
> @MyAnnotation(id = 3)
> <br>
> public class A {
> <br>
> <br>
> @MyAnnotation
> <br>
> public void foo() {
> <br>
> <br>
> }
> <br>
> <br>
> public static void main(String
> args[]) { <br>
> A a =
> new A(); <br>
> a.foo();
> <br>
> }
> <br>
> }
> <br>
> <br>
> And finally, here is the aspect:
> <br>
> <br>
> public aspect CaptureAnnotationValueAspect {
> <br>
> <br>
> void around(MyAnnotation
> myAnnotation): <br>
>
> execution(* *.*(..)) && @annotation(myAnnotation) { <br>
> <br>
>
> System.out.println(myAnnotation.id()); <br>
> <br>
> // How
> can I change the value for the Annotation, if possible?
> <br>
> <br>
>
> proceed(myAnnotation); <br>
> }
> <br>
> }
> <br>
> <br>
> <br>
> Thanks in advance. Best regards,
> <br>
> <br>
> 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"> </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>