Is there any way in Kotlin to weave in code before / after / around a function, as it is with AspectJ in Java?

I tried using AspectJ to weave in aspects of Kotlin functions, but without success. Maybe I just misconfigured something, or maybe AspectJ does not support this.

Does anyone know if this is possible, for example, maven and Eclipse (or IntelliJ)? Or explain why this is not possible?

+5
source share
2 answers

This is what I did to weave the binary way in aspects, after which Java and Kotlin code was compiled.

I could not get aspectj-maven-plugin to weave the aspect of the binary path correctly, so instead I used the jcabi-maven-plugin . See http://plugin.jcabi.com/example-ajc.html

The assistant who worked for me:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>my.group.id</groupId> <artifactId>my.artifact.id</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <complianceLevel>1.6</complianceLevel> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId> <version>3.3</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>1.0.3</version> <configuration> <sourceDirs> <sourceDir>src/main/kotlin</sourceDir> <sourceDir>src/test/kotlin</sourceDir> </sourceDirs> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.jcabi</groupId> <artifactId>jcabi-maven-plugin</artifactId> <version>0.14.1</version> <executions> <execution> <goals> <goal>ajc</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.5.0</version> <configuration> <mainClass>my.package.MyMainClassKt</mainClass> </configuration> </plugin> </plugins> </build> 

So, this works with aspects and a few annotations defined in Java, and then using these annotations to annotate Kotlin methods and classes so that the aspects are successfully introduced into Kotlin code.

Note that if a Kotlin file has both a main method and a class defined in the same file, the Kotlin compiler creates two class files. One class with the class name and one class with "Kt" is added to its name. It is useful to know if you are trying to use exec-maven-plugin to run Kotlin code.

However, this did not reflect well on the eclipse. Perhaps IntelliJ will work better.

+3
source

In addition to other comments and answers, I think it is worth noting that you can "twist" the code before / after / around the function code using inline functions . eg:.

 fun main(vararg args: String) = nanoTimeAppendedTo(System.out, name = "main") { /* do something, eg: */ Thread.sleep(0) } inline fun nanoTimeAppendedTo(appendable: Appendable, name: String, block: () -> Unit) { val nanoTime = measureNanoTime(block) appendable.appendln("`$name` took $nanoTime ns") } 

You won’t have access to all the information that AspectJ provides, but for simple cases where you just want to reuse the execution of some code before / after / around any other code, this works fine.

+3
source

All Articles