Richard Zowalla created OPENJPA-3097:
----------------------------------------

             Summary: Runtime enhancement of already loaded entity classes 
fails with 'attempted to change superclass or interfaces' since 4.2.0
                 Key: OPENJPA-3097
                 URL: https://issues.apache.org/jira/browse/OPENJPA-3097
             Project: OpenJPA
          Issue Type: Bug
    Affects Versions: 4.2.0
            Reporter: Richard Zowalla


Since 4.2.0, runtime enhancement of entity classes that are already loaded 
unenhanced when the EntityManagerFactory starts fails instead of falling back 
to subclass enhancement:

{noformat}
 org.apache.openjpa.persistence.PersistenceException: class redefinition 
failed: attempted to change superclass or interfaces
Caused by: java.lang.UnsupportedOperationException: class redefinition failed: 
attempted to change superclass or interfaces
        at 
org.apache.openjpa.enhance.ClassRedefiner.redefineClasses\(ClassRedefiner.java:73\)
        at 
org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses\(ManagedClassSubclasser.java:186\)
        at 
org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes\(AbstractBrokerFactory.java:300\)
        at 
org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker\(AbstractBrokerFactory.java:239\)
        at 
org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker\(AbstractBrokerFactory.java:215\)
        ...
        at 
org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager\(EntityManagerFactoryImpl.java:83\)
{noformat}

The same code works with 4.1.1.

h3. Reproducer
Apache TomEE \(11.0.0\-SNAPSHOT, which moved to OpenJPA 4.2.0\) has several 
examples where the entity class is loaded before the persistence unit enhances 
it \(Groovy, JAXB/CXF, Jakarta Data repositories, ApplicationComposer\). For 
instance:
{noformat}
git clone 
[https://github.com/apache/tomee.git|https://github.com/apache/tomee.git]
mvn test \-f tomee/examples/groovy\-jpa/pom.xml
{noformat}
fails with the exception above. Pinning org.apache.openjpa:openjpa to 4.1.1 in 
that pom makes the test pass again. Also affected: 
examples/webservice\-inheritance, examples/jpa\-and\-jakarta\-data and the 
ApplicationComposer MoviesTest example.

h3. Analysis
In 4.1.1 ClassRedefiner.redefineClasses used Instrumentation.retransformClasses 
with a temporary transformer. In 4.2.0 it uses Instrumentation.redefineClasses 
and gained a fallback:
{code:java}
} catch \(UnsupportedOperationException e\) {
    throw new InternalException\(e\);
} catch \(Throwable e\) {
    // ... falling back to subclass enhancement
}
{code}
The JVM reports a schema change \("attempted to change superclass or 
interfaces"\) as UnsupportedOperationException, so exactly this case is 
rethrown instead of using the subclass fallback.

The schema change itself seems to come from the 4.2.0 PCEnhancer changes for 
redefine \+ createSubclass \(the original class is now rewritten in place, see 
the new getRedefine\(\) && getCreateSubclass\(\) branches\): the bytes handed 
to redefineClasses must not add interfaces or change the superclass of an 
already loaded class.

h3. Possible fix
\* Make sure the redefine bytecode never changes the class schema \(no added 
interfaces such as PersistenceCapable / AttributeTranslator, no new fields or 
methods\), and/or
\* treat UnsupportedOperationException from redefineClasses like the other 
failures and fall back to subclass enhancement, as 4.1.1 effectively did.





--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to