So I want to inject some testing and logging functionality in my libgdx game.
So I added the following to my the main desktop gradle dependencies.
compile 'org.aspectj:aspectjweaver:1.8.2'
compile "org.aspectj:aspectjrt:1.8.2"
Initially it wasn't finding the dependencies, but that was solved by both turning offline mode off and closing and reopening my IntelliJ project (Gradle sync button wasn't working).
My understanding is that the aspectjweaver has to be loaded with as a java agent. So I found where it appears gradle downloaded it and added the following to my VM runtime config options
-javaagent:/Users/daniel/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjweaver/1.8.2/4963c0bef4748d5ad039cc26c1ac32a082eb755e/aspectjweaver-1.8.2.jar
Surprisingly this gives me the following warning message
objc[66447]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
That message is absent without the -javaagent line.
I was trying to follow this example for a load weaving example. http://andrewclement.blogspot.co.uk/2009/02/load-time-weaving-basics.html
However, I don't need a separate reusable aspect, and so I just created a basic aop.xml file in the src directory with the following contents.
<aspectj>
<weaver options="-verbose"/>
</aspectj>
Obviously I don't have anything set up yet, I just want to confirm that the setup is working. There are enough differences between that tutorial and my target environment that I imagine a lot can go wrong.
I don't really mind whether the solution is compile time or class loading, as long as it works predictably in the libgdx/gradle environment. I opted for looking into the class loading solution because of my unfamiliarity with libgdx/gradle's building environment & requirements.
Thanx in advance.
Update: Will try work my way through this http://www.breskeby.com/2010/02/using-gradle-with-aspectj/
...but not really familiar iajc mentioned in the tutorial, and knowing how to use this in the libgdx build scripts also seems complex.
I'm not gonna provide a gradle implementation but a Maven one, using AspectJ. Obviously, you will have to customize/adapt to fit your needs.
Below, an aspect on a bar.foo
method taking two parameters (String, Boolean)
with an @Around
advice and a ProceedingJoinPoint
.
Any advice or Pointcut can be obviously implemented.
JAVA
@Aspect
@ajcPrivileged
public class MyAspect {
@Pointcut("execution(* bar.foo(String, Boolean)) && args(string, bool)")
void foo(String string, Boolean bool) {}
@Around(value = "loadFile(filePathName, toGray)", argNames = "filePathName, toGray")
public MyReturnType foo(final ProceedingJoinPoint joinPoint, final String string, final Boolean bool) throws MyEx {
//impl
}
}
POM
See Weave third party dependency comment to define the third party lib you want to weave.
<project ..>
..
<properties>
<aspectj-maven-plugin.version>1.7</aspectj-maven-plugin.version>
<aspectjrt.version>1.8.2</aspectjrt.version>
..
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectjrt.version}</version>
</dependency>
..
</dependencies>
<build>
..
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- Weave third party dependency -->
<weaveDependencies>
<weaveDependency>
<groupId>yourThirdPartyDepGroupId</groupId>
<artifactId>yourThirdPartyDepGroupIdArtifactId</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<!-- Weave on compile -->
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>${lifecycle-mapping.version}</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With