Skip to content

Commit

Permalink
Bug 466271: Abstract MappedSuperclass in separate JAR is not weaved s…
Browse files Browse the repository at this point in the history
…tatically

Signed-off-by: Lukas Jungmann <[email protected]>
Reviewed-by: Petros, MartinG
  • Loading branch information
lukasj committed Oct 2, 2015
1 parent 100e361 commit ca7f96b
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.MappedSuperclass;
import javax.persistence.Persistence;

import org.eclipse.persistence.annotations.Cache;
Expand Down Expand Up @@ -1860,9 +1861,23 @@ public class PersistenceUnitProperties {
*/
public static final String WEAVING_FETCHGROUPS = "eclipselink.weaving.fetchgroups";

/**
* The "<code>eclipselink.weaving.mappedsuperclass</code>" property configures
* whether {@link MappedSuperclass}es with no direct sub-classes will be woven.
* <p>
* This property will only be considered if weaving is enabled.
* <p>
* <b>Allowed Values:</b>
* <ul>
* <li>"<code>true</code>" (DEFAULT)
* <li>"<code>false</code>"
* </ul>
*/
public static final String WEAVING_MAPPEDSUPERCLASS = "eclipselink.weaving.mappedsuperclass";

/**
* The "<code>eclipselink.weaving.rest</code>" property configures
* whether classes will be weaved to support EclipseLink JPA_RS functionality
* whether classes will be woven to support EclipseLink JPA_RS functionality
* <p>
* This property will only be considered if weaving is enabled.
* <p>
Expand Down Expand Up @@ -2479,6 +2494,7 @@ public class PersistenceUnitProperties {
*
* @deprecated replaced by {@link #ID_VALIDATION} property with value "<code>NULL</code>".
*/
@Deprecated
public static final String ALLOW_ZERO_ID = "eclipselink.allow-zero-id";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,16 @@
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

<!-- A test model for bug #466271. -->
<persistence-unit name="pu-with-mappedsuperclass" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<shared-cache-mode>NONE</shared-cache-mode>
<class>org.eclipse.persistence.testing.models.jpa21.advanced.Athlete</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.weaving" value="true" />
<property name="eclipselink.ddl-generation" value="none" />
</properties>
</persistence-unit>

</persistence>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static Test suite() {
fullSuite.addTest(XMLForeignKeyTestSuite.suite());
fullSuite.addTest(XMLIndexTestSuite.suite());
fullSuite.addTest(XMLEntityGraphTestSuite.suite());
fullSuite.addTest(WeaverTestSuite.suite());
return fullSuite;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.eclipse.persistence.testing.tests.jpa21.advanced;

import java.util.Arrays;

import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.ManagedType;

import org.eclipse.persistence.internal.descriptors.PersistenceEntity;
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase;
import org.eclipse.persistence.testing.models.jpa21.advanced.Athlete;
import org.junit.Assert;

import junit.framework.Test;
import junit.framework.TestSuite;

public class WeaverTestSuite extends JUnitTestCase {

public WeaverTestSuite(String name) {
super(name);
}

@Override
public String getPersistenceUnitName() {
return "pu-with-mappedsuperclass";
}


public static Test suite() {
TestSuite suite = new TestSuite();
suite.setName("WeaverTestSuite");

suite.addTest(new WeaverTestSuite("testMappedSuperclassWeaving"));

return suite;
}

//bug #466271 - @MappedSuperclass with no implementations should be woven
public void testMappedSuperclassWeaving() {
EntityManagerFactory emf = getEntityManagerFactory();
ManagedType<Athlete> managedType = emf.getMetamodel().managedType(Athlete.class);
Class<Athlete> javaClass = emf.getMetamodel().managedType(Athlete.class).getJavaType();
Assert.assertTrue(Arrays.asList(javaClass.getInterfaces()).contains(PersistenceEntity.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import java.util.ArrayList;
import java.util.Collection;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.persistence.internal.jpa.metadata.MetadataLogger;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAsmFactory;
import org.eclipse.persistence.internal.jpa.weaving.PersistenceWeaver;
Expand All @@ -33,6 +29,10 @@
import org.eclipse.persistence.testing.models.weaving.SimpleObject;
import org.eclipse.persistence.testing.models.weaving.SimpleProject;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SimpleWeaverTestSuite extends TestCase {

// fixtures
Expand Down Expand Up @@ -64,20 +64,26 @@ public static Test suite() {
TestSuite suite = new TestSuite("Simple TopLinkWeaver Tests");
suite.addTest(new SimpleWeaverTestSuite(
"test TopLinkWeaver with null Session") {
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@Override
public void runTest() {
nullSessionTest();
}
});
suite.addTest(new SimpleWeaverTestSuite(
"test TopLinkWeaver with null Project") {
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@Override
public void runTest() {
Session session = new ServerSession();
session.setLogLevel(SessionLog.OFF);
Expand All @@ -86,10 +92,13 @@ public void runTest() {
});
suite.addTest(new SimpleWeaverTestSuite(
"test TopLinkWeaver with null list of entities") {
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@Override
public void runTest() {
Session session = new ServerSession(new SimpleProject());
session.setLogLevel(SessionLog.OFF);
Expand All @@ -98,21 +107,27 @@ public void runTest() {
});
suite.addTest(new SimpleWeaverTestSuite(
"test TopLinkWeaver with empty list of entities") {
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@Override
public void runTest() {
Session session = new ServerSession(new SimpleProject());
session.setLogLevel(SessionLog.OFF);
emptyEntitiesTest(session);
}
});
suite.addTest(new SimpleWeaverTestSuite("build TopLinkWeaver") {
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@Override
public void runTest() {
Session session = new ServerSession(new SimpleProject());
session.setLogLevel(SessionLog.OFF);
Expand All @@ -125,7 +140,7 @@ public void runTest() {
public void nullSessionTest() {
boolean expectedFailure = false;
try {
TransformerFactory.createTransformerAndModifyProject(null, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true);
TransformerFactory.createTransformerAndModifyProject(null, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true, false);
}
catch (IllegalArgumentException iae) {
expectedFailure = true;
Expand All @@ -138,7 +153,7 @@ public void nullSessionTest() {
public void nullProjectTest(Session session) {
boolean expectedFailure = false;
try {
TransformerFactory.createTransformerAndModifyProject(session, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true);
TransformerFactory.createTransformerAndModifyProject(session, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true, false);
}
catch (IllegalArgumentException iae) {
expectedFailure = true;
Expand All @@ -150,7 +165,7 @@ public void nullProjectTest(Session session) {

public void nullEntitiesTest(Session session) {
try {
TransformerFactory.createTransformerAndModifyProject(session, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true);
TransformerFactory.createTransformerAndModifyProject(session, null, Thread.currentThread().getContextClassLoader(), true, false, true, true, true, false);
}
catch (Exception e) {
fail(getName() + " failed: " + e.toString());
Expand All @@ -159,7 +174,7 @@ public void nullEntitiesTest(Session session) {

public void emptyEntitiesTest(Session session) {
try {
TransformerFactory.createTransformerAndModifyProject(session, new ArrayList(), Thread.currentThread().getContextClassLoader(), true, false, true, true, true);
TransformerFactory.createTransformerAndModifyProject(session, new ArrayList(), Thread.currentThread().getContextClassLoader(), true, false, true, true, true, false);
}
catch (Exception e) {
fail(getName() + " failed: " + e.toString());
Expand All @@ -170,7 +185,7 @@ public PersistenceWeaver buildWeaver(Session session, Collection entities) {

PersistenceWeaver tw = null;
try {
tw = TransformerFactory.createTransformerAndModifyProject(session, entities, Thread.currentThread().getContextClassLoader(), true, false, true, true, true);
tw = TransformerFactory.createTransformerAndModifyProject(session, entities, Thread.currentThread().getContextClassLoader(), true, false, true, true, true, false);
}
catch (Exception e) {
fail(getName() + " failed: " + e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public class EntityManagerSetupImpl implements MetadataRefreshListener {
boolean weaveFetchGroups;
boolean weaveInternal;
boolean weaveRest;
boolean weaveMappedSuperClass;

/**
* Used to indicate that an EntityManagerFactoryImpl based on this
Expand Down Expand Up @@ -1922,13 +1923,15 @@ public synchronized ClassTransformer predeploy(PersistenceUnitInfo info, Map ext
weaveFetchGroups = false;
weaveInternal = false;
weaveRest = false;
weaveMappedSuperClass = false;
if (enableWeaving) {
weaveChangeTracking = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_CHANGE_TRACKING, predeployProperties, "true", session));
weaveLazy = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_LAZY, predeployProperties, "true", session));
weaveEager = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_EAGER, predeployProperties, "false", session));
weaveFetchGroups = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_FETCHGROUPS, predeployProperties, "true", session));
weaveInternal = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_INTERNAL, predeployProperties, "true", session));
weaveRest = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_REST, predeployProperties, "true", session));
weaveMappedSuperClass = "true".equalsIgnoreCase(EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.WEAVING_MAPPEDSUPERCLASS, predeployProperties, "true", session));
}

}
Expand Down Expand Up @@ -1987,7 +1990,7 @@ public synchronized ClassTransformer predeploy(PersistenceUnitInfo info, Map ext
if (enableWeaving) {
// build a list of entities the persistence unit represented by this EntityManagerSetupImpl will use
Collection entities = PersistenceUnitProcessor.buildEntityList(processor, classLoaderToUse);
this.weaver = TransformerFactory.createTransformerAndModifyProject(session, entities, classLoaderToUse, weaveLazy, weaveChangeTracking, weaveFetchGroups, weaveInternal, weaveRest);
this.weaver = TransformerFactory.createTransformerAndModifyProject(session, entities, classLoaderToUse, weaveLazy, weaveChangeTracking, weaveFetchGroups, weaveInternal, weaveRest, weaveMappedSuperClass);
session.getProject().setClassNamesForWeaving(new ArrayList(processor.getProject().getWeavableClassNames()));
}

Expand Down Expand Up @@ -2015,7 +2018,7 @@ public synchronized ClassTransformer predeploy(PersistenceUnitInfo info, Map ext
persistenceClasses.add(factory.getMetadataClass(className));
}
}
this.weaver = TransformerFactory.createTransformerAndModifyProject(session, persistenceClasses, classLoaderToUse, weaveLazy, weaveChangeTracking, weaveFetchGroups, weaveInternal, weaveRest);
this.weaver = TransformerFactory.createTransformerAndModifyProject(session, persistenceClasses, classLoaderToUse, weaveLazy, weaveChangeTracking, weaveFetchGroups, weaveInternal, weaveRest, weaveMappedSuperClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ConverterAccessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EmbeddableAccessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.classes.MappedSuperclassAccessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAsmFactory;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataFactory;
Expand Down Expand Up @@ -372,9 +373,10 @@ protected void initPersistenceUnitClasses() {
m_project.addConverterAccessor(new ConverterAccessor(PersistenceUnitProcessor.getConverterAnnotation(candidateClass), candidateClass, m_project));
} else if (PersistenceUnitProcessor.isMappedSuperclass(candidateClass) && ! m_project.hasMappedSuperclass(candidateClass)) {
// ensure mapped superclasses will be added to the metamodel even if they do not have entity subclasses
MetadataDescriptor metadataDescriptor = new MetadataDescriptor(candidateClass);
// add the mapped superclass to keep track of it in case it is not processed later (has no subclasses).
m_session.getProject().addMappedSuperclass(candidateClass.getName(), metadataDescriptor.getClassDescriptor(), false);
m_project.addMappedSuperclass(new MappedSuperclassAccessor(
PersistenceUnitProcessor.getMappedSuperclassAnnotation(candidateClass),
candidateClass, m_project));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,13 @@ public List<StructConverterMetadata> getStructConverters(){
/**
* INTERNAL:
* Returns all those classes in this project that are available for
* weaving. This list currently includes entity and embeddables classes.
* weaving. This list currently includes entity, embeddables
* and mappedsuperclass with no children classes.
*/
public Collection<String> getWeavableClassNames() {
return Collections.unmodifiableCollection(m_allAccessors.keySet());
Set<String> weavableClassNames = new HashSet<String>(m_allAccessors.keySet());
weavableClassNames.addAll(m_mappedSuperclasseAccessors.keySet());
return Collections.unmodifiableCollection(weavableClassNames);
}

/**
Expand Down Expand Up @@ -1794,8 +1797,8 @@ public void processStage1() {
* @see processStage3
*/
public void processStage2() {
// 266912: process metamodel mappedSuperclasses separately from entity descriptors
for (MappedSuperclassAccessor msAccessor : m_metamodelMappedSuperclasses.values()) {
// process metamodel mappedSuperclasses separately from entity descriptors
for (MappedSuperclassAccessor msAccessor : getMappedSuperclasses()) {
if (! msAccessor.isProcessed()) {
msAccessor.processMetamodelDescriptor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ protected void addPotentialMappedSuperclass(MetadataClass metadataClass, boolean
// project for later use by the Metamodel API Note: we must
// again reload our accessor from XML or we will be sharing
// instances of the descriptor
getProject().addMetamodelMappedSuperclass(reloadMappedSuperclass(accessor, new MetadataDescriptor(metadataClass)), getDescriptor());
getProject().addMetamodelMappedSuperclass(reloadMappedSuperclass(accessor, new MetadataDescriptor(metadataClass, getDescriptor().getClassAccessor())), getDescriptor());
} else {
m_mappedSuperclasses.add(accessor);
}
Expand Down
Loading

0 comments on commit ca7f96b

Please sign in to comment.