0

Imagine that, in an application I'm developing, there is a need to sort each of the returning objects, from any method with these characteristics:

  • the method is inside the package my.sorted or any of its subpackages
  • the returning object implements the List interface
  • the method has the @Sorted annotation

I want to create an Aspect called SortingAspect in which implements:

  • A pointcut for the desired points of interruption
  • An advice to perform the intended functionality

With this case I came to this code:

MyClass:

public class MyClass {

    @Sorted
    public List<Integer> myFunction(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(20);
        list.add(3);
        list.add(-4);
        list.add(5);
        list.add(200);
        list.add(34);
        return list;
    }
}

Sorted.

public @interface Sorted {

}

SortedAspect:

@Aspect
public class SortingAspect {
    @Pointcut ("call(* my.sorted..* (..)) && @annotation(Sorted)")
    public void sortFuncPointCut() {}

    @Around("sortFuncPointCut()")
    public List<Integer> sortAdvice(ProceedingJoinPoint pjp) throws Throwable {
        List<Integer> list = (List<Integer>)pjp.proceed();
        Collections.sort(list);
        return list;
    }

}

Problem: The problem is that the @Around is not catch any Poincut and I can not understand why.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
  • how you build you code and what agent do you use when run it ? – xyz Jul 03 '17 at 06:03
  • @sbjavateam This is a AspectJ project and what I do is, in eclipse IDE, click on the project and build it. About the agent, I'm not understanding what you mean. – Ricardo Rocha Jul 03 '17 at 08:12
  • 1
    Is there a reason to use call instead of execution? That way you define the call to come from the package my.sorted..., not the called method to reside in it. Thus a class outside of my.sorted... calling one inside that package structure will not be advised. – sheltem Jul 03 '17 at 11:56
  • @sheltem there is no reason. Want I want is, at the final of any method execution with the characteristics described, I could sort the result of the function, and send back to proceed with the normal flow. So, you are telling me that if I change it to "execution" this may work? – Ricardo Rocha Jul 03 '17 at 13:05
  • 1
    It won't work yet, because *I think* you are not referencing the annotation correctly either, but it will get you closer to what you want. I don't have time for a complete answer now, but have a look at the difference between call and execution for now: https://stackoverflow.com/questions/18132822/execution-vs-call-join-point/18149106#18149106 – sheltem Jul 03 '17 at 13:36
  • The annotation is correct because I test the same pointcut only with the annotation code. And thank you for the help – Ricardo Rocha Jul 03 '17 at 17:18
  • Just tried it out myself. So long as your Sorted annotation has runtime-retention just switching to an execution Pointcut should work. Have you tried with execution? – sheltem Jul 04 '17 at 09:34
  • @sheltem Yes, and is not working :S. Afternoon I will put more info like the package structure etc. – Ricardo Rocha Jul 04 '17 at 10:05

0 Answers0