Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] privileged aspects and Load-Time Weaving

The problem is not privileged access, you are rather dealing with a hen-and-egg type of problem here. Let's imagine you want a setup like this:

  • Aspect library (to be used via LTW)
    • AspectPrivate
  • Application
    • ClassWithPrivate
    • TestPrivate

Now probably you don't want any dependencies between aspect library and application, but

  • the aspect imports an application class,
  • the test program is trying to access methods unavailable during its compile time,
  • thus the aspect library has a dependency on the application library and vice versa.

Ergo: It simply does not make sense to use AspectJ's introduction feature (also known as ITD or inter-type declaration) like this in an LTW scenario. In most cases AOP is about adding cross-cutting behaviour and there you can work with jokers like "*" or "..", i.e. you do not need to use precise class names. Even if you do, the worst that could happen is an "advice does not match" warning during compilation of the aspect library. It would still work during runtime after aspect weaving. But member access and/or method introduction only work with specific class names, so if you need that you should use compile-time weaving. Think about it, you are not just decorating classes with behaviour before or after existing methods, you are introducing new members or methods, i.e. changing the class structure itself. This is to be done during compile time.

So much for the technical part. I still think your question might be suffering from the XY problem. Maybe you should rather explain what you want to achieve and not how you think it should best be achieved. Ask yourself questions like:

  • Why would I want to access private class members in the first place?
  • What happens if I refactor the target class, e.g. rename a member? (In your example the aspect would break and need to be refactored too.)
  • Why am I limited to LTW here? Or maybe I just think I am and there are other possible solutions.
  • What is it I want to achieve? (Maybe your answer is: "I want to reduce boiler-plate code and auto-generate getters and setters for all or some of my beans." And maybe then you find out that you could do that via AspectJ's annotation processing feature or by using another tool such as Lombok or by using Groovy instead of Java or ...)
Kind regards
--
Alexander Kriegisch
https://scrum-master.de
 

REV Tamas schrieb am 17.06.2019 18:35:

Hello,
 
I would like to use a privileged access and weave it at load-time.
I did not see anywhere that it was not possible to do so.
On the other hand, I could only make it work with compile-time weaving so far.
Could you please help me to make it work (or just tell me it is not possible)?
 
Code excerpt:
public class ClassWithPrivate {
    private String s = "myStr";
}

==========

package com.example.aspect;

import com.example.ClassWithPrivate;

privileged public aspect AccessPrivate {

    public String ClassWithPrivate.getS() {
        return this.s;
    }

    public void ClassWithPrivate.setS(String str) {
        this.s = str;
    }
}

==========

package com.example;

public class TestPrivate {

    public static void main(String[] args) {

        ClassWithPrivate test = new ClassWithPrivate();
        System.out.println(test.getS());
        test.setS("hello");
        System.out.println(test.getS());
    }
}
 
I installed ADT to eclipse and now I can see the aspect weaved into the bytecode.
I saw this when I decompiled the code:
public class ClassWithPrivate
{
  public void setS(String paramString)
  {
    AccessPrivate.ajc$interMethod$com_example_aspect_AccessPrivate$com_example_ClassWithPrivate$setS(this, paramString);
  }
 
  public String getS()
  {
    return AccessPrivate.ajc$interMethod$com_example_aspect_AccessPrivate$com_example_ClassWithPrivate$getS(this);
  }
 
  private String s = "mystr";
}
So here is the question: Is it possible to do it with load-time weaving?
I disabled ADT, then I added the aspect to the aop.xml as I normally do. However,
the class that tried to access the privileged methods, that gave a compile error.

Thanks,
Tamas

Back to the top