AspectJ pointcuts in Scala using SBT

2.5k Views Asked by At

I'm trying to build a hello world example of using AspectJ pointcuts in Scala using the sbt-aspectj plugin. In my plugins.sbt I've added the plugin:

addSbtPlugin("com.typesafe.sbt" % "sbt-aspectj" % "0.10.2")

and in my build.sbt, I've set it up as follows:

import com.typesafe.sbt.SbtAspectj._

aspectjSettings

/* project settings */

fork in run := true

javaOptions in run <++= AspectjKeys.weaverOptions in Aspectj

products in Compile <++= products in Aspectj

Then I create a very simple AspectJ file defining a pointcut around one of my server classes:

package io.bigsense;

public aspect AspectLogger {

  private long start;

  pointcut conn() : call(* io.bigsense.server.TomcatServer.startServer(..));

  void around() : conn() {
    start = System.currentTimeMillis();
    Throwable t = null;
    try {
      proceed();
    }
    catch(Throwable _t) {
      t = _t;
    }
    long spent = System.currentTimeMillis() - start;
    System.out.println("Send time: " + spent);
    if (t != null) {
      throw new RuntimeException(t);
    }
  }

}

So that class and function do exist. But whenever I run sbt, I get the following message:

[info] Weaving 1 AspectJ source to /home/cassius/sensespace/BigSense/target/scala-2.10/aspectj/classes...
[warn] warning at /home/cassius/sensespace/BigSense/src/main/aspectj/AspectLogger.aj:11::0 advice defined in io.bigsense.AspectLogger has not been applied [Xlint:adviceDidNotMatch]

I've looked at a lot of examples and my pointcut looks correct. Why is it not being applied/weaved into my code at runtime?

EDIT: adding the command line arguments that sbt is launching java with:

/opt/oracle-jdk-bin-1.7.0.80/jre/bin/java
-javaagent:/home/cassius/.ivy2/cache/org.aspectj/aspectjweaver/jars/aspectjweaver-1.8.5.jar
-classpath
/home/cassius/sensespace/BigSense/target/scala-2.10/classes:/home/cassius/sensespace/BigSense/target/scala-2.10/aspectj/classes:/home/cassius/sensespace/BigSense/lib/ij.jar:/home/cassius/sensespace/BigSense/lib/eproperties-1.1.3.jar:/home/cassius/.sbt/boot/scala-2.10.4/lib/scala-library.jar:/home/cassius/.ivy2/cache/com.typesafe.play/twirl-api_2.10/jars/twirl-api_2.10-1.0.4.jar:/home/cassius/.ivy2/cache/org.apache.commons/commons-lang3/jars/commons-lang3-3.1.jar:/home/cassius/.ivy2/cache/org.aspectj/aspectjrt/jars/aspectjrt-1.8.5.jar:/home/cassius/.ivy2/cache/org.springframework/spring-beans/jars/spring-beans-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-core/jars/spring-core-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-asm/jars/spring-asm-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar:/home/cassius/.ivy2/cache/org.springframework/spring-context/jars/spring-context-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-aop/jars/spring-aop-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/aopalliance/aopalliance/jars/aopalliance-1.0.jar:/home/cassius/.ivy2/cache/org.springframework/spring-expression/jars/spring-expression-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/cglib/cglib/jars/cglib-2.2.2.jar:/home/cassius/.ivy2/cache/asm/asm/jars/asm-3.3.1.jar:/home/cassius/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.6.jar:/home/cassius/.ivy2/cache/commons-io/commons-io/jars/commons-io-2.4.jar:/home/cassius/.ivy2/cache/net.sourceforge.jtds/jtds/jars/jtds-1.2.4.jar:/home/cassius/.ivy2/cache/com.jolbox/bonecp/bundles/bonecp-0.7.1.RELEASE.jar:/home/cassius/.ivy2/cache/com.google.guava/guava/jars/guava-r08.jar:/home/cassius/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.12.jar:/home/cassius/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.7.12.jar:/home/cassius/.ivy2/cache/log4j/log4j/bundles/log4j-1.2.17.jar:/home/cassius/.ivy2/cache/org.scalaj/scalaj-collection_2.10/jars/scalaj-collection_2.10-1.5.jar:/home/cassius/.ivy2/cache/bouncycastle/bcprov-jdk15/jars/bcprov-jdk15-140.jar:/home/cassius/.ivy2/cache/org.postgresql/postgresql/jars/postgresql-9.4-1201-jdbc41.jar:/home/cassius/.ivy2/cache/org.postgis/postgis-jdbc/jars/postgis-jdbc-1.3.3.jar:/home/cassius/.ivy2/cache/org.postgis/postgis-stubs/jars/postgis-stubs-1.3.3.jar:/home/cassius/.ivy2/cache/postgresql/postgresql/jars/postgresql-8.3-603.jdbc4.jar:/home/cassius/.ivy2/cache/mysql/mysql-connector-java/jars/mysql-connector-java-5.1.30.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-server/jars/jetty-server-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/javax.servlet/javax.servlet-api/jars/javax.servlet-api-3.1.0.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-http/jars/jetty-http-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-util/jars/jetty-util-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-io/jars/jetty-io-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-servlet/jars/jetty-servlet-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-security/jars/jetty-security-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-webapp/jars/jetty-webapp-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-xml/jars/jetty-xml-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.rogach/scallop_2.10/jars/scallop_2.10-0.9.5.jar:/home/cassius/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.3.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-core/jars/tomcat-embed-core-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-logging-juli/jars/tomcat-embed-logging-juli-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-jasper/jars/tomcat-embed-jasper-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-el/jars/tomcat-embed-el-7.0.53.jar:/home/cassius/.ivy2/cache/org.eclipse.jdt.core.compiler/ecj/jars/ecj-P20140317-1600.jar:/home/cassius/.ivy2/cache/org.apache.commons/commons-compress/jars/commons-compress-1.6.jar:/home/cassius/.ivy2/cache/org.tukaani/xz/jars/xz-1.4.jar
io.bigsense.server.BigSenseServer
-c
../bs-env/mssql-vm.conf
3

There are 3 best solutions below

1
On BEST ANSWER

I looked through some of the other example projects and discovered that I was missing the following from my build.sbt

AspectjKeys.inputs in Aspectj <+= compiledClasses

products in Compile <<= products in Aspectj

products in Runtime <<= products in Compile

This places the compiled sources within the inpath for AspectJ.

6
On

[Xlint:adviceDidNotMatch] always means that the AspectJ weaver/compiler does not see any joinpoints matching your pointcut on its inpath.

In this case, it means that no line of code doing a call to * io.bigsense.server.TomcatServer.startServer(..) is found. Probably the call is not made from your own source code but from some 3rd party library not woven by AspectJ. Consequently, you either need to weave the calling code or you should not be surprised that you get this warning. ;-)

If you think that I am wrong, please provide an SSCCE for me to reproduce your problem.

0
On

For sbt 0.13.16 now it's

addSbtPlugin("com.lightbend.sbt" % "sbt-aspectj" % "0.11.0")

in plugins.sbt and

import com.lightbend.sbt.SbtAspectj._

scalaVersion := "2.12.3"

aspectjSettings

aspectjInputs in Aspectj += (aspectjCompiledClasses in Aspectj).value

products in Compile := (products in Aspectj).value

products in Runtime := (products in Compile).value

in build.sbt.