1

I know AspectJ can declare inter-type, but I want to declare a variable inner TestMain.test method, thus only that method can get it but all of other methods can't get the variable.

public aspect GetParams {
    public int TestMain.anInt = 2; // I know this is accessed in the whole scope of a TestMain instance
    // How to declare a local variable only for TestMain.test
}

public class TestMain {

    public void test(int a)
    {
        System.out.println(anInt);
    }

    public void test2() {
        // cannot accessed here
    }
}

--------------- Update ------------------

I describe what I want.

The scenario is in a method for Jersey API, I wants to get a mapper in the method that contains all but only the parameters and the values the API needs. Thus I hope there's is an annotation on the method which does this job automatically.

Cuero
  • 1,169
  • 4
  • 21
  • 42
  • What you want is not technically possible with AspectJ because what you describe is not a cross-cutting concern. I assume that what you really want to achieve is possible with AOP though. So would you mind describing **what** you want to achieve instead of **how** you think it should be done? Then somebody might actually be able to help you. – kriegaex Sep 18 '16 at 15:47
  • @kriegaex Thank you. I've updated. – Cuero Sep 19 '16 at 02:21
  • Again you describe *how* you want to solve a problem, but not *what* you actually want to achieve. Is is really so hard? Do you weave via ITD into your own or Jersey's code? If you would declare a local variable in one method, why via AOP and not directly? How would the method know of the variable's existence? What would be so dangerous about a class member? The other methods would not use it anyway. Really, I think you are just trying to do a very contrived thing. Which friggin' problem are you trying to solve? – kriegaex Sep 19 '16 at 07:26

0 Answers0