AOP and Kotlin

Sorry for not replying sooner. You were right that Guice implements aspects at runtime. At least for the cases you sent in attached project Injector.getInstance() returns generated subclasses with the overriden methods containing injected bytecode. But it can't do that for Kotlin because unlike Java, subclassing and overriding is forbidden in Kotlin for classes and functions by default. So the solution is to add open keyword into Kotlin code:

public open class SimpleKotlin {   AlwaysThrowException   open fun hello() {   println("Hello, Kotlin")   } }

public open class PersistKotlin [Inject] (val em : EntityManager) {   Transactional   open fun hello() {   println("Kotlin: In Transaction? ${em.getTransaction()?.isActive()}")

    val p = Person()
    p.name = "Kotlin"
    em.persist(p)

  }
}


Or you can emulate the same bug with lack of aspects behaviour in Java by adding final:

public final class SimpleJava {   @AlwaysThrowException   public final void hello() {   System.out.println("Hello, Java");   } }

Interesting thing, that if class is final, but method is not, Guice provide a very helpfull message that it can't subclass a particular class.

1 Like