Pointcut not well Formatted - java

What is the format problem with this pointcut?
#Around("execution(* #myPackage.SafetyCritical.*(*))&& #annotation(deny)")
.i forgot to add: exception is "Pointcut is not well-formed: expecting 'name pattern' (last closing bracket before &&)
for an example the pointcut should work with this class:
#SafetyCritical
public class SecureClass
{
public SecureClass(){
}
#Deny
public void isNotAllowed(){
System.out.println("This should not happen");
}
#Allow
public void isAllowed(){
System.out.println("Allowed");
}
}

EDIT:
I think the pointcut expression you are looking for would be more like this:
#Around("#target(myPackage.SafetyCritical) && #annotation(denyPackage.Deny)")
The #target designator is used to match classes that are marked with the given annoation, and the #annotation designator will filter to the methods annotated with the denyPackage.Deny annotation.
Again, a look through the Spring Documentation regarding AspectJ support would be helpful.
ORIGINAL:
To match on any number of arguments, the parameters definition passed to the execution pointcut designator should be '..'
#Around("execution(* myPackage.SafetyCritical.*(..)) && #annotation(deny)")
The Spring documentation has some examples of using this to denote accepting any number of arguments.
Also, I would venture to guess that that having the '#' symbol in front of your package name is not acceptable. You should remove it.

I've used a pointcut definition like this to match annotated methods:
#Around("execution(#myPackage.SafetyCritical * *(..)) && #annotation(deny)")
The last part #annotation(deny) (like you already know, but some others may not) is to bind the annotation to the advice method argument named "deny".
Edit: As per your update, I was not aware that SafetyCritical was an annotation on the class. I suppose that would be witin the target() goal then:
#Around("execution(* *(..)) && #target(myPackage.SafetyCritical) && #annotation(deny)")

Related

Pointcut for methods with at least one annotation with any type

I am trying to have the following logic;
Method within class x.b.Classy
Public method
It has at least one annotation of any type
I have been trying to use this but annotation with any type logic is failing, how do I represent class type with 100% wildcard?
#Pointcut("within(x.b.Classy) && execution(public * *(..)) && #annotation(*)")
Though getting the following;
java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'identifier'
within(x.b.Classy) && execution(public * *(..)) && #annotation(*)
^
How do I represent any type of annotation within my pointcut definition?
I think that what you want is something like:
#Pointcut("within(x.b.Classy) && execution(#(*) public * *(..))")

Getting value from annotation used at Method or Type level [duplicate]

I need a pointcut for methods in classes annotated with #X or methods annotated with #X. I also need the annotation object. If both the class and the method are annotated I prefer to get the method annotation as argument.
I tried the following, which creates an "inconsistent binding" warning. (Why not just set them null?)
#Around("#annotation(methodLevelX) || #within(classLevelX)")
public Object advise(ProceedingJoinPoint pjp, X methodLevelX, X classLevelX)
The following creates a "ambiguous binding of parameter(s) x across '||' in pointcut" warning. (Which does not necessarily make sense in my opinion: Why not bind the first short circuited evaluation?)
#Around("#annotation(x) || #within(x)")
public Object advise(ProceedingJoinPoint pjp, X x)
Splitting the previous attempt in two naturally results in two method calls if class and method annotations are present.
I know I could just get the method and class with reflection and my desired annotation with a pointcut like this:
#Around("#annotation(com.package.X) || #within(com.package.X)")
But I'd prefer not to.
Is there any "one pointcut, one method, one annotation argument", solution for my requirement that does not require reflection?
Not quite, but almost. You will need two pointcuts, two advices, but you can delegate the work to a single method. Here's how it would look like:
#Aspect
public class AnyAspectName {
#Pointcut("execution(#X * *.*(..))")
void annotatedMethod() {}
#Pointcut("execution(* (#X *).*(..))")
void methodOfAnnotatedClass() {}
#Around("annotatedMethod() && #annotation(methodLevelX)")
public Object adviseAnnotatedMethods(ProceedingJoinPoint pjp, X methodLevelX)
throws Throwable {
return aroundImplementation(pjp, methodLevelX);
}
#Around("methodOfAnnotatedClass() && !annotatedMethod() && #within(classLevelX)")
public Object adviseMethodsOfAnnotatedClass(ProceedingJoinPoint pjp, X classLevelX)
throws Throwable {
return aroundImplementation(pjp, classLevelX);
}
public Object aroundImplementation(ProceedingJoinPoint pjp, X annotation)
throws Throwable {
return pjp.proceed();
}
}
Note that besides splitting apart the #annotation() and #within() pointcuts, I added restrictions to the resulting pointcuts so that they aren't too broad. I suppose you want method execution join points, so I added the needed pointcut expressions that would restrict it to method execution. They are matching
execution of any method annotated with #X with any return type in any class being in any package for the first advice
execution of any method with any return type in any class annotated with #X for the second.
Further restricting #within(X) and #annotation(X) comes in handy, because #within(X) by itself would match
any join point where the associated code is defined in a type with
an annotation of type X
which would include method-execution, method-call, constructor-execution, constructor-call, pre-initialization, static initialization, initialization, field set, field get, exception-handler, lock type join points (not all join points are valid for around advices though). Similarly, #annotation(X) by itself would mean
any join point where the subject has an annotation of type X
which could also mean most of the previously mentioned join points, depending on the target type of your annotation.

Pointcut with a class name pattern

test.core and I want an aspect around every class in that or a sub-package with the name pattern Service.
sth like this: "execution(public de.test.core..Service.*(..)" but it doesn t seem to work.
Is aspectJ even able to match to a class pattern?
Match all methods defined in beans whose name ends with ‘Service’.
bean(*Service)
Match by Service pattern
#Pointcut("within(*..*Service)")
public void inServiceClass() {}

AspectJ Pointcut on Methods with Multiple Annotations

Use load-time weaving, pure AspectJ.
We have 2 annotations #Time and #Count, and a few annotated methods.
#Time (name="myMethod1Time")
#Count (name="myMethod1Count")
public void myMethod1(){..};
#Time (name="myMethod2Time")
public void myMethod2(){..};
#Count (name="myMethod3Count")
public void myMethod3(){..};
Now I am defining my own around aspect for myMethod1 which has multiple annotations:
// multiple annotations, not working
#Around("#annotation(time) && #annotation(count))
public Object myAspect(Time time, Count count) {..}
This doesn't work. However, capture method myMethod2 works fine with a single annotation:
// single annotation, is working
#Around("#annotation(time))
public Object myAnotherAspect(Time time) {..}
I want to capture only methods with both Time and Count annotations present in their signature, and I want to use the annotation value. Anyone know how to achieve this?
Maybe combine 2 pointcuts like:
#Around("call(#Time * *(..)) && call(#Count * *(..))");

How to match a method with an annotated argument in AspectJ

I'd like to match a method like this:
#Foo
public void boo(#Baz Bar bar) { ... }
Basically:
the method has a #Foo annotation (which I match with execution(#Foo * *(..)) && #annotation(foo)),
can have a variable amount of parameters,
and one of them should have a #Baz annotation,
I need to further work with that annotated argument (bar).
If a method has a #Foo annotation but is missing a #Baz annotation, I want to get an error as early as possible, if possible when weaving and not at runtime.
How can I do that?
public pointcut annArg(): execution(#Foo * *(.., #Baz (*),..));
declare error :execution(#Foo * *(..))&&!annArg() :"error";
Unfortunatly it is impossible to grab matched argument by args(..,arg,..). But you can use thisJoinPoint.getArgs() and reflection API to get the annotated argument. Or if you know the position of the argument you can use something like args(..,arg);

Categories