Quantcast

How can I make ITD available in the code of the aspect subject?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How can I make ITD available in the code of the aspect subject?

Mark
Hi.
I am not sure if my terminology is right, but here is what I want. I have an aspect that injects a Log field into certain types, here is the aspect code:
public aspect LoggingAspect {
  private interface HttpHandlerType {}
  declare parents: (@Path *) implements HttpHandlerType;
  
  private Logger HttpHandlerType.Log = Logger.getLogger(getClass());
  
  pointcut httpHandlerMethods(HttpHandlerType o) : within(HttpHandlerType+) && 
    execution(@(GET || PUT || POST || DELETE) public * *.*(..)) && this(o);
  
  before(HttpHandlerType o): httpHandlerMethods(o) {
    if (o.Log.isInfoEnabled()) {
      o.Log.info(logMethod(thisJoinPoint));
    }
  }
  
  after(HttpHandlerType o) returning (Object result): httpHandlerMethods(o) {
    if (o.Log.isDebugEnabled()) {
      o.Log.debug(logMethod(thisJoinPoint, result));
    }
  }
  
  after(HttpHandlerType o) throwing (Exception e): httpHandlerMethods(o) {
    if (o.Log.isEnabledFor(Level.ERROR)) {
      o.Log.error(logMethod(thisJoinPoint), e);
    }
  }
  
  private static String logMethod(JoinPoint jp) {
    ...
  }

  private static String logMethod(JoinPoint jp, Object result) {
    ...
  }
}

The question is how the aspect subjects can make use of this field. For instance, here is a sample class affected by this aspect:
@Path("user")
public class UserHandler {
  @GET
  @Path("{id}")
  public User getUser(@PathParam("id") int id) {
    ...
  }
}

The question is how the code of getUser can utilize the Log field injected by the aspect?

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I make ITD available in the code of the aspect subject?

Andy Clement
Hi,

I can't seem to see the code in your message below.  However, assuming
it is something like this:

====
aspect X {
  interface Marker {}
  declare parents: Code implements Marker;

  String Marker.log = "abc";

}


public class Code {
  public static void main(String []argv) {
    new Code().bar();
  }
  public void bar() {
    System.out.println(log);
  }
}
====
You see it just works if you compile with ajc. (and prints 'abc' when
you run Code).  This is because ajc knows that the log reference in
bar refers to the introduced field.

It gets complicated if you want to have user code exploit log and yet
be compiled with javac.

cheers,
Andy

On 7 December 2011 10:04, Mark <[hidden email]> wrote:

> Hi.
> I am not sure if my terminology is right, but here is what I want. I have an
> aspect that injects a Log field into certain types, here is the aspect code:
>
>
> The question is how the aspect subjects can make use of this field. For
> instance, here is a sample class affected by this aspect:
>
>
> The question is how the code of getUser can utilize the Log field injected
> by the aspect?
>
> Thanks.
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/How-can-I-make-ITD-available-in-the-code-of-the-aspect-subject-tp4169901p4169901.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> [hidden email]
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I make ITD available in the code of the aspect subject?

Mark
Well, I have problems with it. Please, consider the following aspect:
public aspect EnrolerHelperAspect {
  public static void Enroler.collectAllRoles(Collection<Role> roles) {
    final int expectedModifiers = Modifier.PUBLIC | Modifier.STATIC;
    for (Field f : Enroler.class.getFields()) {
      if ((f.getModifiers() & expectedModifiers) == expectedModifiers &&
          Role.class.isAssignableFrom(f.getType())) {
        try {
          roles.add((Role)f.get(null));
        } catch (Exception e) {
        }
      }
    }
  }
}

Once created, the Cross Reference view reports that the `collectAllRoles` static method has indeed been added to the Enroler class.

Now, please observer the Enroler class:
public class Enroler implements org.restlet.security.Enroler {
  public final static Role USER = new Role("user", "A limited user.");
  public final static Role ADMINISTRATOR = new Role("admin", "The system administrator.");

  public static Collection<? extends Role> getAllRoles() {
    ArrayList<Role> result = new ArrayList<Role>(2);
    // collectAllRoles(result);
    return result;
  }

  ...
}

I get compilation error if I uncomment the call to `collectAllRoles`. It says:
Cannot make a static reference to the non-static method collectAllRoles(Collection<Role>) from the type Enroler

How come? The method is declared as static! What is even stranger, is that when I begin to type the name of the method it happily provides the intellisense where the method is indicated as static! Observe:



What is going on?

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I make ITD available in the code of the aspect subject?

Andy Clement
Hi Mark,

I'm afraid I still don't see any code in any of the messages you post
to the mailing list - so I can't comment specifically right now.  When
working in eclipse it is important to ensure 'jdt weaving' is turned
on (as that is how AJDT modifies eclipse at startup to open it up for
a better AspectJ experience).  Open workspace properties, jdtweaving,
check it is on.  Beyond that I'd need to see the actual code - perhaps
raise a bugzilla and i'll take a look at it in there.

cheers,
Andy

On 8 December 2011 07:58, Mark <[hidden email]> wrote:

> Well, I have problems with it. Please, consider the following aspect:
>
>
> Once created, the Cross Reference view reports that the `collectAllRoles`
> static method has indeed been added to the Enroler class.
>
> Now, please observer the Enroler class:
>
>
> I get compilation error if I uncomment the call to `collectAllRoles`. It
> says:
>
>
> How come? The method is declared as static! What is even stranger, is that
> when I begin to type the name of the method it happily provides the
> intellisense where the method is indicated as static! Observe:
>
> http://aspectj.2085585.n4.nabble.com/file/n4173193/StaticITD.png
>
> What is going on?
>
> Thanks.
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/How-can-I-make-ITD-available-in-the-code-of-the-aspect-subject-tp4169901p4173193.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> [hidden email]
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I make ITD available in the code of the aspect subject?

Mark
Please, view it on Nabble - http://aspectj.2085585.n4.nabble.com/How-can-I-make-ITD-available-in-the-code-of-the-aspect-subject-td4169901.html

Eclipse archives seems to remove everything between the <raw></raw> tags. I will cease using them to delimit the code samples.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I make ITD available in the code of the aspect subject?

Andy Clement
I finally found a minute to look at this.

Unfortunately it just works for me.  I distilled it down to these two files:

==== Enroler.java ====
import java.util.ArrayList;
import java.util.Collection;

public class Enroler {
        public static Collection<? extends Role> getAllRoles() {
                ArrayList<Role> result = new ArrayList<Role>(2);
                collectAllRoles(result);
                return result;
        }
}
====

==== EnrolerHelperAspect.aj ====
public class Enroler implements org.restlet.security.Enroler {
  public final static Role USER = new Role("user", "A limited user.");
  public final static Role ADMINISTRATOR = new Role("admin", "The
system administrator.");

  public static Collection<? extends Role> getAllRoles() {
    ArrayList<Role> result = new ArrayList<Role>(2);
    collectAllRoles(result);
    return result;
  }

}
====

and they compile OK.  Few things you can check:
- are you on a recent AJDT? (a dev build? they are at this update
site: http://download.eclipse.org/tools/ajdt/37/dev/update )
- have you tried doing a project clean, does it make a difference?
- have you tried removing the generics from the ITD declaration and
making the 'result' just a Collection (this is just to see what
happens)
- do the gutter annotations appear correct, i.e. an outgoing arrow
from the aspect and an incoming arrow into the target?

Rather than me try to recreate your scenario, if you can send me a zip
file of a broken project, that would mean I am setup exactly the same
as you and I can debug a bit further.  (I don't have this type:
"org.restlet.security.Enroler" which I suppose *could* make a
difference, although it shouldn't)

cheers,
Andy


On 8 December 2011 11:40, Mark <[hidden email]> wrote:

> Please, view it on Nabble -
> http://aspectj.2085585.n4.nabble.com/How-can-I-make-ITD-available-in-the-code-of-the-aspect-subject-td4169901.html
>
> Eclipse archives seems to remove everything between the
> &lt;raw&gt;&lt;/raw&gt; tags. I will cease using them to delimit the code
> samples.
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/How-can-I-make-ITD-available-in-the-code-of-the-aspect-subject-tp4169901p4173983.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> [hidden email]
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Loading...