1
votes

My glassfish throws an exception when I try to run.

[2018-02-22T17:07:04.135+0100] [glassfish 5.0] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=46 _ThreadName=admin-listener(4)] [timeMillis: 1519315624135] [levelValue: 1000] [[ Exception while loading the app : EJB Container initialization error java.lang.RuntimeException: Could not invoke defineClass!

I uploaded the server log here: https://pastebin.com/cB0EgG4Y

UserDao

@Stateless
class UserDao : Abstract<User>() {
    override fun getEntityClass(): Class<User> {
        return User::class.java
    }
}

Abstract Dao

@Stateless
abstract class Abstract<T : Model> {
    @PersistenceContext(unitName = "kwetter")
    private lateinit var entityManager: EntityManager

    abstract fun getEntityClass(): Class<T>

    fun find(id: Int): T {
        return entityManager.find(getEntityClass(), id)
    }
}

Service

@Stateless
class UserService {
    @Inject private var userDao: UserDao? = null

    fun find(id: Int): User {
        return userDao!!.find(id)
    }
}

API endpoint

@Stateless
@Named
@Path("/users")
class Users {
    @Inject private var userService: UserService? = null

    @GET
    @Path("/{id}")
    @Produces("application/json")
    fun single(@PathParam("id") id: Int): User {
        return userService!!.find(id)
    }
}
2

2 Answers

0
votes

In Kotlin, all classes are implicitly final by default, hence this error in your logs:

Caused by: java.lang.VerifyError: Cannot inherit from final class at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

Mark your classes as open if you want to extend them:

The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.

https://kotlinlang.org/docs/reference/classes.html#inheritance

0
votes

You can also use the all-open-compiler-plugins : https://kotlinlang.org/docs/reference/compiler-plugins.html#all-open-compiler-plugin

Kotlin has classes and their members final by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be open. The all-open compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicit open keyword

See also : https://dzone.com/articles/the-state-of-kotlin-for-jakarta-eemicroprofile-tra