error at ::0 formal unbound in pointcut - java

All the classes in project are in com.aspect package.
The main Aspect :
#Aspect
public class MainAspect {
#Pointcut("within(com.aspect..*)")
public void standaloneLayer(){}
}
Another aspect for joinpoints having account object as argument:
#Aspect
public class AccountAspect {
#After("com.aspect.MainAspect.standaloneLayer() && args(account)")
public void pointCutForAccount(JoinPoint joinPoint, Account account){
}
}
Service layer class:
#Service
public class Customer {
public void setAccountBalance(Account account) {}
}
While running application I am getting below exception:
Caused by:
java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)

Crosscheck your code once more whether you have imported org.aopalliance.intercept.Joinpoint instead of org.aspectj.lang.JoinPoint
Note for AspectJ AOP you need to import org.aspectj.lang.JoinPoint.

In my case the reason was that I imported the wrong class. Make sure that the class you import is org.aspectj.lang.JoinPoint and not any thing else.

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
#Aspect
public class LoggingAspect {
#Before("allCircleMethods()")
public void LogginAdvice(JoinPoint joinpoint)
{
System.out.println("advice is run");
System.out.println(joinpoint.toString());
}
#Before("args(String)")
public void StringArguementMethods()
{
System.out.println("A method that takes String arguement has been called");
}
#Pointcut("execution(* get*())")
public void allgetters()
{}
#Pointcut("within(as.model.Circle)")
public void allCircleMethods()
{}
}
when i imported import org.aopalliance.intercept.Joinpoint; i got 0 ubound in poincut error , so i changed the import to org.aspect.lang.JoinPoint and my program ran successfully

Related

Why the Pointcut expression is not working

If i keep #Pointcut("within(org.example.ShoppingCart.*)") in AuthenticationAspect.java then the authenticate method is NOT getting invoked BUT when i change to #Pointcut("within(org.example..*)") then it does get invoked.
Doesn't the 2nd PointCut below automatically include the first one?
#Pointcut("within(org.example.ShoppingCart.*)")
#Pointcut("within(org.example..*)")
i believe both of these should work.
Following is the code :
ShoppingCart.java
package org.example;
import org.springframework.stereotype.Component;
#Component
public class ShoppingCart {
public void checkout(String status)
{
System.out.println("Checkout method called");
}
}
AuthenticationAspect.java
package org.example;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
#Aspect
#Component
public class AuthenticationAspect {
#Pointcut("within(org.example.ShoppingCart.*)")
public void authenticationPointCut()
{
}
#Before("authenticationPointCut()")
public void authenticate()
{
System.out.println("Authentication is being performed");
}
}
Main.java
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
ShoppingCart cart = context.getBean(ShoppingCart.class);
cart.checkout("CANCELLED");
}
}
BeanConfig.java
package org.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
#Configuration // Indicates this is a configuration class
#ComponentScan(basePackages = "org.example") // Indicates where to find the components/beans
#EnableAspectJAutoProxy // Enables the AspectJ features in project
public class BeanConfig {
}
i am learning spring aspect and unsure why the pointcut expression is not working .
Since the within pointcut designator limits matching to join points of certain types, the problem is your pointcut expression is matching types within ShoppingCart because of .* in the end. If you remove those characters it should work.
#Pointcut("within(org.example.ShoppingCart)")
public void authenticationPointCut(){}

Proxy for abstract class without changing the usage

I have an abstract class (database mapping) implementing an interface where default implementations are injected at runtime (this is part of another library and cannot be changed).
I want to override one of the default implementation via a proxy (as that seems like the way to override this).
public abstract class Table1 implements Storable<Table1>
{
#Sequence("ID_SEQUENCE")
#Alias("ID")
public abstract String getID();
public abstract void setID(String ID);
#Alias("NAME")
public abstract String getAvailabilityZone();
public abstract void setAvailabilityZone(String value);
}
public interface Storable<S extends Storable<S>> {
//a bunch of method definition.
boolean tryLoad() throws Exception;
}
Let's say I want to override tryLoad() method to do my own things instead of what the generated code provides. Given the nature of the library, it is not something I can achieve by simple #Override.
The simple way this is currently used is as following:
public void method() {
Table1 t = Repository.storageFor(Table1.class).prepare();
t.setName( "temp" );
if (!t.tryLoad())
t.tryInsert();
}
I want to proxy tryLoad() without making changes in all the methods across the whole codebase - that would be to get proxied instance instead of actual one and perform the operation on that.
Is there any recommended way to achieve this?
Thanks!
I woke up last night and felt bored, so despite your lack of feedback I created a little Carbonado showcase project and shared it on GitHub. I made three commits:
Initial commit with Maven project already prepared for AspectJ and a JUnit test for me to find out how Carbonado actually works, because I had never used it before.
Add failing unit test for behaviour of tryLoad() expected to be provided by aspect.
Add aspect to make unit test pass. Aspect hooks into tryLoad() and auto-creates non-existent record. I do not know if I guessed right what you actually wanted to achieve, but if it was a different thing, just change the aspect implementation.
Sample code
Carbonado storable:
package de.scrum_master.app;
import com.amazon.carbonado.Nullable;
import com.amazon.carbonado.PrimaryKey;
import com.amazon.carbonado.Storable;
#PrimaryKey("ID")
public interface StoredMessage extends Storable<StoredMessage> {
long getID();
void setID(long id);
#Nullable String getMessage();
void setMessage(String message);
}
Aspect:
package de.scrum_master.aspect;
import com.amazon.carbonado.Storable;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
#Aspect
public class CarbonadoAspect {
#Around("call(boolean tryLoad()) && target(storable)")
public boolean tryInsertIfNotFound(ProceedingJoinPoint thisJoinPoint, Storable storable) throws Throwable {
System.out.println(thisJoinPoint);
if ((boolean) thisJoinPoint.proceed())
return true;
System.out.println("Not found: " + storable + " -> inserting");
return storable.tryInsert();
}
}
JUnit test:
package de.scrum_master.app;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.repo.map.MapRepositoryBuilder;
import de.scrum_master.app.StoredMessage;
public class CarbonadoTest {
private Repository repo;
private Storage<StoredMessage> storage;
StoredMessage message;
#Before
public void setUp() throws Exception {
repo = MapRepositoryBuilder.newRepository();
storage = repo.storageFor(StoredMessage.class);
message = storage.prepare();
}
#After
public void tearDown() throws Exception {
repo.close();
repo = null;
storage = null;
message = null;
}
// (...)
#Test
public void aspectCreatesNonExistentRecord() throws SupportException, RepositoryException {
message.setID(1);
// Without the aspect this would be false
assertTrue(message.tryLoad());
assertEquals(message.getID(), 1);
assertEquals(message.getMessage(), null);
}
}
Enjoy!

Capturing advice execution (advising advice) in AspectJ

I'm trying to capture the execution of an advice using annotation in Maven, but it says that advice has not been applied. Here is the code:
package testMaven8;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
#Aspect
public class aspecter {
public static int a;
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
#Before("execution(* testMaven8.das.qwe(..))")
public void testBefore2(){
System.out.println("yoo2");
setA(102);
System.out.println(a);
}
#Before("execution (* testMaven8.aspecter.testBefore2(..))")
public void advice1(){
System.out.println("advicing advice testBefore2");
}
}
Is there something wrong with the code? I'm trying to avoid the usage of Aspect class if it's possible. Any help is appreciated.
Thanks
Advice execution is something special in AspectJ, not a normal method execution. Thus, if you want to capture advice execution you also need a special pointcut with the surprising name - adviceexecution() ;-)
Here is some sample code:
Driver application:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
new Application().doSomething();
}
public void doSomething() {
System.out.println("Doing something");
}
}
Aspect capturing method executions:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class MethodInterceptor {
#Before("execution(!static * *(..))")
public void interceptMethod(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
Aspect capturing advice executions:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class AdviceInterceptor {
#Before("!within(AdviceInterceptor) && adviceexecution()")
public void interceptAdvice(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
Please do not try to pack the advice execution interceptor into the same aspect as another advice because you can easily end up in an endless recursion if you cannot exclude the adviceexecution() pointcut from capturing itself via !within(AdviceInterceptor).
Console log:
adviceexecution(void de.scrum_master.aspect.MethodInterceptor.interceptMethod(JoinPoint))
execution(void de.scrum_master.app.Application.doSomething())
Doing something

How to write AspectJ AOP expression for this method?

I have tried many combinations but I am unable to invoke callback before execution of following method
#SomeAnnotation(...)
#Override
public void someMethod(Serializable id) {
}
I have tried many combinations similar to
#Before("execution(#com.full.name.of.SomeAnnotation * com.full.name.of.Class.someMethod(java.io.Serializable))")
public void beforeMethod() {
System.out.println("I am here.");
}
If I write a more generic expession, it hits the beforeMethod but I am unable to target a single specific method. What am I missing here?
Okay guys, let me prove that the pointcut actually works as written by the original poster Haris Hasan.
Sample annotation:
package com.full.name.of;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
#Retention(RetentionPolicy.RUNTIME)
public #interface SomeAnnotation {
int id();
String name();
}
Sample class using the annotation:
package com.full.name.of;
import java.io.Serializable;
public class Class {
#SomeAnnotation(id = 1, name = "John Doe")
public void someMethod(Serializable s) {}
public static void main(String[] args) {
new Class().someMethod("x");
}
}
Sample aspect with Haris Hasan's exact pointcut:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class SampleAspect {
#Before("execution(#com.full.name.of.SomeAnnotation * com.full.name.of.Class.someMethod(java.io.Serializable))")
public void yetAnotherPointcut(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
Console output:
execution(void com.full.name.of.Class.someMethod(Serializable))
Copy and paste everything exactly as is, then run it. Quod erat demonstrandum.
The expression below should work:
#Before("com.full.name.of.Class.someMethod(..) && args(java.io.Serializable)")
Correction by kriegaex:
#Before("execution(* com.full.name.of.Class.someMethod(*)) && args(java.io.Serializable)")
A few points worth considering: Is the method being advised public? Are you using cglib proxies or jdk proxies? Does your class implement any interface and is the method being advised declared in the interface contract?
Also do take a look at what spring doc has to say about writing good pointcuts
Hope this helps.
Why do you not just use
#Before("execution(* com.full.name.of.Class.someMethod(java.io.Serializable))")
That specifically targets the method you want, you do not need to put the annotation to specify the target

Aspectj and initialization

I would like to use initialization with aspectj.
#Aspect
public class TotoAspect
{
#Before("initialization( *.new(..))")
public void test(JoinPoint thisJoinPoint) throws AuditReactiveException
{
System.err.println("I AM HERE");
}
}
But, the message was not print with
new ABC();
I use the java agent for aspectj.
Where is the mistake ?
Well, actually on your console you should see something like this:
Exception in thread "main" org.aspectj.lang.NoAspectBoundException: Exception while initializing de.scrum_master.aspect.TotoAspect: org.aspectj.lang.NoAspectBoundException: de.scrum_master.aspect.TotoAspect
at de.scrum_master.aspect.TotoAspect.aspectOf(TotoAspect.java:1)
at de.scrum_master.app.ABC.<init>(ABC.java:4)
at de.scrum_master.app.ABC.main(ABC.java:10)
Caused by: org.aspectj.lang.NoAspectBoundException: de.scrum_master.aspect.TotoAspect
at de.scrum_master.aspect.TotoAspect.aspectOf(TotoAspect.java:1)
at de.scrum_master.aspect.TotoAspect.<init>(TotoAspect.java:10)
at de.scrum_master.aspect.TotoAspect.ajc$postClinit(TotoAspect.java:1)
at de.scrum_master.aspect.TotoAspect.<clinit>(TotoAspect.java:1)
... 2 more
This is because your advice is targetting all constructors, even its own aspect's one. You need to exclude it. Here is a compileable code sample:
Exception class:
This is to demonstrate that your pointcut also matches the exception's constructor.
package de.scrum_master.app;
public class AuditReactiveException extends Exception {
public AuditReactiveException(String message) {
super(message);
}
}
Sample driver class with main method:
package de.scrum_master.app;
public class ABC {
public ABC() throws AuditReactiveException {
System.err.println("Creating ABC object");
throw new AuditReactiveException("Something went wrong");
}
public static void main(String[] args) throws AuditReactiveException {
new ABC();
}
}
Aspect with modified pointcut:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import de.scrum_master.app.AuditReactiveException;
#Aspect
public class TotoAspect {
#Before("initialization(*.new(..)) && !within(TotoAspect)")
public void test(JoinPoint thisJoinPoint) throws AuditReactiveException {
System.err.println(thisJoinPoint);
}
}
Sample output:
initialization(de.scrum_master.app.ABC())
Creating ABC object
initialization(de.scrum_master.app.AuditReactiveException(String))
Exception in thread "main" de.scrum_master.app.AuditReactiveException: Something went wrong
at de.scrum_master.app.ABC.<init>(ABC.java:6)
at de.scrum_master.app.ABC.main(ABC.java:10)
You might also want to exclude the exception constructor, i.e. you should narrow down the pointcut's scope with package and/or class names. Check out the AspectJ documentation for pointcut syntax description, e.g. how to use jokers like * or ...

Categories