0

I am using the AspectJ Maven plugin to build my project and use an AspectLibrary, which is a jar in which I have my aspects defined.

Here is the Aspect that I am trying to use

@Around("execution(* *(..))&&@annotation(com.cisco.commerce.pricing.lp.commons.util.annotations.TimeMe)")
public Object timeMeAroundAspect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {// NOSONAR
    Timer timer = Timer.instance().start();
    MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
    Method method = signature.getMethod();
    TimeMe timeMeAnnotation = method.getAnnotation(TimeMe.class);
    String name = timeMeAnnotation.name();
    boolean log = timeMeAnnotation.log();
    boolean addToMetrics = timeMeAnnotation.addToMetrics();
    Object response = null;
    try {
        response = proceedingJoinPoint.proceed();
    } finally {
        try {
            Long timeTaken = timer.timeTaken();
            if (log) {
                LOGGER.info("MethodName: {} Time taken: {}", name, timeTaken);
            }
            if (addToMetrics) {
                ExecutionDetailsUtil.addMethodExecutionTime(name, timeTaken);
            }
        } catch (Exception e) {
            LOGGER.warn("Exception while trying to log time", e);
        }
    }
    return response;
}

This code is in a jar file, which I am using as the aspectLibrary in my pom

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <encoding>UTF-8</encoding>
                <source>${java.source-target.version}</source>
                <target>${java.source-target.version}</target>
                <Xlint>ignore</Xlint>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
                        <artifactId>lp-commons</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <complianceLevel>${java.source-target.version}</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

Below is my annotation defintion

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeMe {

  public String name();

  public boolean log() default true;

  public boolean addToMetrics() default true;

}

Here is the snippet where I am trying to use this annotation (in a different code base which uses the above jar as a dependency)

@TimeMe(name = "classifyLine")
private void classifyLine(PricingObject pricingObject, 
   PricingLineObject pricingLineObject, LineTypes lineTypes) {
//logic
}

My build runs fine and prints the following in the MAVEN Console

[INFO] Join point 'method-execution(void com.cisco.pricing.lps.main.ListPriceService.classifyLine(com.cisco.pricing.lps.bean.PricingObject, com.cisco.pricing.lps.bean.PricingLineObject, com.cisco.pricing.lps.dto.LineTypes))' in Type 'com.cisco.pricing.lps.main.ListPriceService' (ListPriceService.java:235) advised by around advice from 'com.cisco.commerce.pricing.lp.commons.util.logging.LoggingAspectDefiner' (lp-commons-2019.03.01-SNAPSHOT.jar!LoggingAspectDefiner.class(from LoggingAspectDefiner.java))

I exploded the war file and looked at the class files generated. I have the following AjcClosure1 class generated for the java file where I used the annotation.

public class ListPriceService$AjcClosure1 extends AroundClosure {

  public Object run(Object[] paramArrayOfObject) {
    Object[] arrayOfObject = this.state;
    ListPriceService.classifyLine_aroundBody0((ListPriceService) 
    arrayOfObject[0],
    (PricingObject)arrayOfObject[1],
    (PricingLineObject)arrayOfObject[2], (LineTypes)arrayOfObject[3], 
    (JoinPoint)arrayOfObject[4]);return null;
  }

  public ListPriceService$AjcClosure1(Object[] paramArrayOfObject)
  {
     super(paramArrayOfObject);
  }
}

And in the java class file, where I use the annotation, I see no changes to the classifyLine method.

However, when I run my application, the annotation is not working. It doesn't execute the Aspect I have defined in the jar.

I have no clue why. Is my pattern not matching? It matches and works fine in a Spring application but not in this non Spring application.

Arnav Sengupta
  • 423
  • 1
  • 7
  • 19
  • after maven build success, can you refresh the project and check once. – Phani Kumar Dec 20 '18 at 13:25
  • I have enabled auto-refresh once build is complete. Anyway, it doesn't work. – Arnav Sengupta Dec 20 '18 at 13:31
  • Why don't you edit your original question instead of creating one that comes quite close to a duplicate? Do you think you will make progress that way? As I already said in a comment below my [answer here](https://stackoverflow.com/a/53851377/1082681): Please provide a complete [MCVE](http://stackoverflow.com/help/mcve) and publish it on GitHub in order to enable the people who are willing to help you (me too!) to reproduce and solve your problem. It would have been solved already by me if you had done that. I know quite a lot about AspectJ + Maven, but do not feel so inclined to just guess. – kriegaex Dec 21 '18 at 07:11

0 Answers0