Skip to content

Commit

Permalink
avoid unnecessarry reflection calls,
Browse files Browse the repository at this point in the history
improve codegen to handle Map related mappings

Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Oct 3, 2023
1 parent 676506b commit ba8b71e
Show file tree
Hide file tree
Showing 30 changed files with 377 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -28,7 +28,7 @@ public class IsIsolatedTest extends ProjectClassGeneratorResultFileTest {

public IsIsolatedTest() {
super(new org.eclipse.persistence.testing.models.employee.relational.EmployeeProject(),
"descriptor.setIsIsolated(true);");
"descriptor.setCacheIsolation(CacheIsolationType.ISOLATED);");
setDescription("Test addDescriptorPropertyLines method -> the setIsIsolated");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ protected boolean isSingleKey(KeyElementAccessor[] pkElementArray){

/**
* INTERNAL:
* This is the interface used to encapsulate the the type of key class element
* This is the interface used to encapsulate the type of key class element
*/
protected interface KeyElementAccessor {
String getAttributeName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1475,15 +1475,15 @@ public Object clone() {
public void convertClassNamesToClasses(ClassLoader classLoader) {
//Class<?> redirectorClass = null;

if (getJavaClassName() != null) {
if (getJavaClass() == null && getJavaClassName() != null) {
final Class<?> descriptorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
() -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getJavaClassName(), true, classLoader),
(ex) -> ValidationException.classNotFoundWhileConvertingClassNames(getJavaClassName(), ex)
);
setJavaClass(descriptorClass);
}

if (getAmendmentClassName() != null) {
if (getAmendmentClass() == null && getAmendmentClassName() != null) {
final Class<?> amendmentClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
() -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getAmendmentClassName(), true, classLoader),
(ex) -> ValidationException.classNotFoundWhileConvertingClassNames(getAmendmentClassName(), ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@
*/
public class ClassDefinition extends CodeDefinition {
protected String packageName;
protected List<String> imports;
protected Set<String> imports;
protected int type;
public static final int CLASS_TYPE = 1;
public static final int INTERFACE_TYPE = 2;
protected String superClass;
protected List<String> interfaces;
protected List<AttributeDefinition> attributes;
protected List<MethodDefinition> methods;
protected List<ClassDefinition> innerClasses;
protected Set<ClassDefinition> innerClasses;

public ClassDefinition() {
this.packageName = "";
this.imports = new ArrayList<>(3);
this.imports = new TreeSet<>();
this.type = CLASS_TYPE;
this.interfaces = new ArrayList<>(3);
this.attributes = new ArrayList<>();
this.methods = new ArrayList<>();
this.innerClasses = new ArrayList<>(3);
this.innerClasses = new TreeSet<>(Comparator.comparing(CodeDefinition::getName));
}

public void addAttribute(AttributeDefinition attribute) {
Expand All @@ -60,9 +60,7 @@ public void addAttribute(AttributeDefinition attribute) {
* "{packageName}.{shortName or '*'}"
*/
public void addImport(String importStatement) {
if (!getImports().contains(importStatement)) {
getImports().add(importStatement);
}
getImports().add(importStatement);
}

private void addImports(Map<String, Set<String>> typeNameMap) {
Expand All @@ -80,11 +78,11 @@ private void addImports(Map<String, Set<String>> typeNameMap) {
}
}
}

sortImports();
}

public void addInnerClass(ClassDefinition classDefinition) {
imports.addAll(classDefinition.imports);
classDefinition.imports.clear();
getInnerClasses().add(classDefinition);
}

Expand Down Expand Up @@ -163,11 +161,11 @@ protected List<AttributeDefinition> getAttributes() {
return attributes;
}

protected List<String> getImports() {
protected Set<String> getImports() {
return imports;
}

protected List<ClassDefinition> getInnerClasses() {
protected Set<ClassDefinition> getInnerClasses() {
return innerClasses;
}

Expand Down Expand Up @@ -203,7 +201,7 @@ protected void replaceInterface(String oldInterfaceName, String newInterfaceName
}
}

private void setImports(List<String> imports) {
private void setImports(Set<String> imports) {
this.imports = imports;
}

Expand All @@ -227,10 +225,6 @@ public void setType(int type) {
this.type = type;
}

protected void sortImports() {
setImports(new ArrayList<>(new TreeSet<>(getImports())));
}

protected void sortMethods() {
Comparator<MethodDefinition> comparison = (first, second) -> {
if (first.isConstructor()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setOutput(Writer output) {
}

public void tab() {
write("\t");
write(" ");
}

public void tab(int indent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,6 @@ public void forEach(Consumer<? super E> action) {
throw new ConcurrentModificationException();
}

/**
* @throws NullPointerException {@inheritDoc}
*/
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ public Object containerInstance() {
public Object containerInstance(int initialCapacity) {
return new ArrayList<>(initialCapacity);
}

@Override
public Class<?> getPolicyContainerClass() {
return ArrayList.class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ public Object containerInstance() {
public Object containerInstance(int initialCapacity) {
return IndirectCollectionsFactory.createIndirectList(initialCapacity);
}

@Override
public Class<?> getPolicyContainerClass() {
return IndirectCollectionsFactory.IndirectList_Class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Object cloneFor(Object container) {
@Override
public void convertClassNamesToClasses(ClassLoader classLoader){
super.convertClassNamesToClasses(classLoader);
if (getContainerClassName() == null){
if (getContainerClass() != null) {
return;
}
Class<?> containerClass = null;
Expand Down Expand Up @@ -360,7 +360,9 @@ public void setCloneMethod(Method cloneMethod) {
@Override
public void setContainerClass(Class<?> containerClass) {
this.containerClass = containerClass;
initializeConstructor();
if (getPolicyContainerClass() == null || containerClass != getPolicyContainerClass()) {
initializeConstructor();
}
}

@Override
Expand All @@ -385,4 +387,13 @@ public Object buildContainerFromVector(Vector vector, AbstractSession session) {
protected Object toStringInfo() {
return getContainerClass();
}

/**
* INTERNAL:
* Get the default {@code Collection} class used by the container policy.
*/
protected Class<?> getPolicyContainerClass() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public Object cloneFor(Object container) {
}

if (container.getClass() == Vector.class) {
return ((Vector) container).clone();
return ((Vector<?>) container).clone();
}

// Could potentially be another Collection type as well.
return new Vector<>((Collection) container);
return new Vector<>((Collection<?>) container);
}

/**
Expand Down Expand Up @@ -91,4 +91,9 @@ public Object containerInstance() {
public Object containerInstance(int initialCapacity) {
return new Vector<>(initialCapacity);
}

@Override
public Class<?> getPolicyContainerClass() {
return Vector.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
*/
public class SecurableObjectHolder {

/** The JCE encryption class name */
private final static String JCE_ENCRYPTION_CLASS_NAME = "org.eclipse.persistence.internal.security.JCEEncryptor";

/** The encryption class name **/
private String m_securableClassName;

Expand Down Expand Up @@ -73,28 +70,29 @@ public boolean hasSecurableObject() {
*/
private void initSecurableObject() {
boolean initPassThroughEncryptor = false;

if (m_securableClassName == null) {
// Since we are defaulting, hence, assuming they can initialize the JCE
// libraries, if the init fails, this flag tells us to assume no encryption.
// However, if the JCE init does work, the JCEEncryptor will need to
// determine that a password was not encrypted by it, therefore, assume
// clear text. See JCEEncryptor.
initPassThroughEncryptor = true;
m_securableClassName = JCE_ENCRYPTION_CLASS_NAME;
}

try {
if (m_securableClassName == null || JCEEncryptor.class.getName().equals(m_securableClassName)) {
// Since we are defaulting, hence, assuming they can initialize the JCE
// libraries, if the init fails, this flag tells us to assume no encryption.
// However, if the JCE init does work, the JCEEncryptor will need to
// determine that a password was not encrypted by it, therefore, assume
// clear text. See JCEEncryptor.
initPassThroughEncryptor = m_securableClassName == null;
m_securableObject = new JCEEncryptor();
return;
}

ConversionManager cm = ConversionManager.getDefaultManager();
Class<?> securableClass = cm.convertObject(m_securableClassName, Class.class);
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
@SuppressWarnings({"unchecked"})
Class<? extends Securable> securableClass = cm.convertObject(m_securableClassName, Class.class);
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
m_securableObject = (Securable)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(securableClass));
m_securableObject = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(securableClass));
} catch (PrivilegedActionException exception) {
throw exception.getException();
}
} else {
m_securableObject = (Securable)PrivilegedAccessHelper.newInstanceFromClass(securableClass);
m_securableObject = PrivilegedAccessHelper.newInstanceFromClass(securableClass);
}
} catch (Throwable e) {
if (initPassThroughEncryptor) {// default failed, so perform no encryption.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
/**
* Implementation of org.eclipse.persistence.sessions.Session
* The public interface should be used.
* @see org.eclipse.persistence.sessions.Session
*
* <p>
* <b>Purpose</b>: Define the interface and common protocol of an EclipseLink compliant session.
Expand All @@ -161,6 +160,7 @@
* <li> Identity maps and caching.
* </ul>
* @see DatabaseSessionImpl
* @see org.eclipse.persistence.sessions.Session
*/
public abstract class AbstractSession extends CoreAbstractSession<ClassDescriptor, Login, Platform, Project, SessionEventManager> implements org.eclipse.persistence.sessions.Session, CommandProcessor, Serializable, Cloneable {
/** ExceptionHandler handles database exceptions. */
Expand Down Expand Up @@ -438,7 +438,7 @@ public JPAQueryBuilder getQueryBuilder() {

/**
* INTERNAL
* Set the query builder used to parser JPQL.
* Set the query builder used to parse JPQL.
*/
public void setQueryBuilder(JPAQueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,20 @@ public class AggregateObjectMapping extends AggregateMapping implements Relation
*/
protected List<DatabaseMapping> mapsIdMappings;

/**
* List of map id attributes that need thir mapping to be set to read only at initialize
* time on their cloned aggregate mappings.
*/
protected List<String> mapsIdMappingAttributes;

/**
* Default constructor.
*/
public AggregateObjectMapping() {
aggregateToSourceFields = new HashMap(5);
aggregateToSourceFields = new HashMap<>(5);
nestedFieldTranslations = new HashMap<>();
mapsIdMappings = new ArrayList<>();
mapsIdMappingAttributes = new ArrayList<>();
overrideManyToManyMappings = new ArrayList<>();
overrideUnidirectionalOneToManyMappings = new ArrayList<>();
converters = new HashMap<>();
Expand Down Expand Up @@ -246,6 +253,17 @@ public void addFieldTranslation(DatabaseField sourceField, String aggregateField
*/
public void addMapsIdMapping(DatabaseMapping mapping) {
mapsIdMappings.add(mapping);
addMapsIdMappingAttribute(mapping.getAttributeName());
}

/**
* INTERNAL:
* In JPA users may specify a maps id attribute mapping on a shared embeddable
* descriptor. Mappings for these attributes need to be set to read-only
* at initialize time, after the reference descriptor is cloned.
*/
public void addMapsIdMappingAttribute(String attribute) {
mapsIdMappingAttributes.add(attribute);
}

/**
Expand Down Expand Up @@ -1269,6 +1287,15 @@ protected Object getMatchingBackupAttributeValue(WriteObjectQuery query, Object
return getAttributeValueFromObject(query.getBackupClone());
}

/**
* INTERNAL:
* Return the list of map id attributes that need their mapping to be set to read only
* at initialize time on their cloned aggregate mappings.
*/
public List<String> getMapsIdMappingAttributes() {
return mapsIdMappingAttributes;
}

/**
* INTERNAL:
* Return the query that is used when this mapping is part of a joined relationship
Expand Down Expand Up @@ -1412,8 +1439,8 @@ public void initialize(AbstractSession session) throws DescriptorException {
}

// Mark any mapsId mappings as read-only.
for (DatabaseMapping mapsIdMapping : mapsIdMappings) {
DatabaseMapping mapping = clonedDescriptor.getMappingForAttributeName(mapsIdMapping.getAttributeName());
for (String attribute : getMapsIdMappingAttributes()) {
DatabaseMapping mapping = clonedDescriptor.getMappingForAttributeName(attribute);

if (mapping != null) {
mapping.setIsReadOnly(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public void convertClassNamesToClasses(ClassLoader classLoader){
super.convertClassNamesToClasses(classLoader);

// DirectCollection mappings don't require a reference class.
if (getReferenceClassName() != null) {
if (getReferenceClass() == null && getReferenceClassName() != null) {
Class<?> referenceClass = null;
try{
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
Expand Down

0 comments on commit ba8b71e

Please sign in to comment.