schmutterer / gradle-openjpa

gradle-plugin that enhances entity-classes with OpenJPA code
Apache License 2.0
5 stars 2 forks source link

Support for Java 8 #6

Open centic9 opened 8 years ago

centic9 commented 8 years ago

When trying to use Java 8 class file format, I get the following error while enhancing via the gradle-openjpa plugin:

Caused by: java.lang.IllegalArgumentException
        at org.apache.xbean.asm4.ClassReader.<init>(Unknown Source)
        at org.apache.xbean.asm4.ClassReader.<init>(Unknown Source)
        at org.apache.xbean.asm4.ClassReader.<init>(Unknown Source)
        at org.apache.openjpa.enhance.AsmAdaptor.toJava7ByteArray(AsmAdaptor.java:93)
        at org.apache.openjpa.enhance.AsmAdaptor.writeJava7(AsmAdaptor.java:84)
        at org.apache.openjpa.enhance.AsmAdaptor.write(AsmAdaptor.java:54)
        at org.apache.openjpa.enhance.PCEnhancer.record(PCEnhancer.java:633)
        at org.apache.openjpa.enhance.PCEnhancer.record(PCEnhancer.java:619)
        at org.apache.openjpa.enhance.PCEnhancer.run(PCEnhancer.java:4900)
        at org.apache.openjpa.enhance.PCEnhancer.run(PCEnhancer.java:4830)
        at org.apache.openjpa.enhance.PCEnhancer$1.run(PCEnhancer.java:4800)
        at org.apache.openjpa.lib.conf.Configurations.launchRunnable(Configurations.java:764)
        at org.apache.openjpa.lib.conf.Configurations.runAgainstAllAnchors(Configurations.java:754)
        at org.apache.openjpa.enhance.PCEnhancer.run(PCEnhancer.java:4795)
        at at.schmutterer.oss.gradle.openjpa.OpenJpaPlugin$_apply_closure1.doCall(OpenJpaPlugin.groovy:44)
        at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:554)
        at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:535)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
        ... 60 more

See http://stackoverflow.com/questions/28476227/upgrade-application-using-openjpa-to-java-8 and https://issues.apache.org/jira/browse/OPENJPA-2386 for some related discussion.

It seems OpenJPA 2.3.0 does not support Java 8, but the newer OpenJPA 2.4.0 does.

Any chance of updating the plugin to use OpenJPA 2.4.0 so we can use Java 8 here?

ChristophGr commented 8 years ago

Well, is upgrading the openjpa-version to 2.4.0 all it takes? We moved to Java8 some time ago, and migrated all JPA-code to eclipselink, so we have no current projects to try this on.

centic9 commented 8 years ago

Hm, when I tried it gradle-openjpa compiles,but I get the following error, so it seems there might be a bit more to it, unfortunately I lack the knowledge to find out why this happens. I tried to print out the URLs that are used as classpath and it seems to include the openjpa-2.4.0.jar correctly, so not sure why it cannot load the class here...

Anything that I could check additionally?

22:10:36.850 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.lang.NoClassDefFoundError: org/apache/openjpa/enhance/PCEnhancer
22:10:36.850 [ERROR] [org.gradle.BuildExceptionReporter]    at at.schmutterer.oss.gradle.openjpa.OpenJpaPlugin$_apply_closure1.doCall(OpenJpaPlugin.groovy:43)
22:10:36.850 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:554)
22:10:36.850 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:535)
22:10:36.851 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
22:10:36.851 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
22:10:36.851 [ERROR] [org.gradle.BuildExceptionReporter]    ... 60 more
22:10:36.851 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.lang.ClassNotFoundException: org.apache.openjpa.enhance.PCEnhancer
22:10:36.851 [ERROR] [org.gradle.BuildExceptionReporter]    ... 65 more
ChristophGr commented 8 years ago

It seems OpenJPA changed the signature of the classes responsible for enhancing.

Back then, I copied the code from the openjpa-maven-plugin

centic9 commented 8 years ago

Hm, doesn't look like upon a quick look. The interface is still the same and the openjpa-gradle plugin still seems to call it correctly.

Somehow classes like PCEnhancer and Options are reported as not found although the classloader is set correctly.

In fact I could get it to work when using Reflection instead of using the classes directly:

                String[] args = tree.files as String[];
                Class clazz = loader.loadClass("org.apache.openjpa.lib.util.Options")
                Constructor<?> constr = clazz.getConstructor(java.util.Properties.class);
                Object options = constr.newInstance(target.openjpa.toProperties());
        Class enhClass = loader.loadClass("org.apache.openjpa.enhance.PCEnhancer")
        Method method = enhClass.getMethod("run", String[].class, clazz)
        method.invoke(null, args, options)

However it looks quite ugly and I do not understand why it is necessary, maybe it's because I am using a newer Gradle (which pulls in a newer Groovy) in my projects and this somehow breaks the Thread.currentThread().setContextClassLoader(loader)?

centic9 commented 8 years ago

Ok, maybe I am on the wrong track with the NoClassDefFoundException as it also fails when I keep things at OpenJPA 2.3.0, seems the way I am loading the local version of the plugin in my tests via

classpath files('gradle-openjpa/build/libs/gradle-openjpa-0.3.0-SNAPSHOT.jar')

causes the classpath to break in Gradle...

centic9 commented 8 years ago

BTW, I have pushed my collected changes to the fork at https://github.com/centic9/gradle-openjpa, albeit it is not in a PR-able state as it mixes other changes into it as well and I am not sure if the reflection stuff is even necessary when the plugin is released correctly.

I am using this to run OpenJPA 2.4.0 on Java 8 successfully, as said, the reflection part seems to be caused when using jar for the plugin, let me know if you would like a separated PR for some of the things in here.