Skip to content

Commit

Permalink
move to Lists in codegen
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Aug 21, 2023
1 parent d10676f commit 352a1e6
Show file tree
Hide file tree
Showing 39 changed files with 397 additions and 531 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 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 @@ -46,11 +46,11 @@ public void test() throws Exception {
// passwords
// The first one will preserve the password and test the forced
// encryption on the getPassword() call
m_sessionConfig1 = (DatabaseSessionConfig)m_sessions.getSessionConfigs().firstElement();
m_sessionConfig1 = (DatabaseSessionConfig)m_sessions.getSessionConfigs().get(0);

// The second one will blank out the password and test that the
// getPassword() call doesn't throw a null pointer exception
m_sessionConfig2 = (DatabaseSessionConfig)m_sessions.getSessionConfigs().lastElement();
m_sessionConfig2 = (DatabaseSessionConfig)m_sessions.getSessionConfigs().get(1);
m_sessionConfig2.getLoginConfig().setEncryptedPassword(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 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 All @@ -17,8 +17,10 @@
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.Vector;

import org.eclipse.persistence.internal.sessions.factories.model.project.ProjectConfig;
import org.eclipse.persistence.oxm.XMLContext;
import org.eclipse.persistence.oxm.XMLUnmarshaller;
import org.eclipse.persistence.testing.framework.AutoVerifyTestCase;
Expand Down Expand Up @@ -189,7 +191,7 @@ public void test() {
XMLContext context = new XMLContext(new XMLSessionConfigProject_11_1_1());
XMLUnmarshaller unmarshaller = context.createUnmarshaller();
SessionConfigs eclipseLinkSessions = (SessionConfigs)unmarshaller.unmarshal(reader);
m_session = (DatabaseSessionConfig)eclipseLinkSessions.getSessionConfigs().firstElement();
m_session = (DatabaseSessionConfig)eclipseLinkSessions.getSessionConfigs().get(0);
} catch (Exception exception) {
m_session = null;
}
Expand Down Expand Up @@ -304,18 +306,18 @@ protected void verify() {
check("InitialContextFactoryName", namingConfig.getInitialContextFactoryName(), "new_initial_context_factory_name");

// Properties
Vector propertyConfigs = namingConfig.getPropertyConfigs();
List<PropertyConfig> propertyConfigs = namingConfig.getPropertyConfigs();

if (propertyConfigs == null) {
throw new TestErrorException("PropertyConfigs were null");
} else if (propertyConfigs.size() != 2) {
throw new TestErrorException("PropertyConfigs were not the correct size");
} else {
PropertyConfig one = (PropertyConfig)propertyConfigs.firstElement();
PropertyConfig one = propertyConfigs.get(0);
check("Property name", one.getName(), "name1");
check("Property value", one.getValue(), "value1");

PropertyConfig two = (PropertyConfig)propertyConfigs.lastElement();
PropertyConfig two = propertyConfigs.get(1);
check("Property name", two.getName(), "name2");
check("Property value", two.getValue(), "value2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.internal.codegen;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -44,8 +43,7 @@ private void adjustInitialValue(Map<String, Set<String>> typeNameMap) {
StringBuilder initialValue = new StringBuilder(getInitialValue());
Set<String> typeNames = parseForTypeNames(initialValue.toString());

for (Iterator<String> i = typeNames.iterator(); i.hasNext();) {
String typeName = i.next();
for (String typeName : typeNames) {
String adjustedTypeName = adjustTypeName(typeName, typeNameMap);

if (!typeName.equals(adjustedTypeName)) {
Expand Down Expand Up @@ -77,8 +75,8 @@ public String getInitialValue() {
protected void putTypeNamesInMap(Map<String, Set<String>> typeNameMap) {
putTypeNameInMap(getTypeName(), typeNameMap);

for (Iterator<String> i = parseForTypeNames(getInitialValue()).iterator(); i.hasNext();) {
putTypeNameInMap(i.next(), typeNameMap);
for (String s : parseForTypeNames(getInitialValue())) {
putTypeNameInMap(s, typeNameMap);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 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 All @@ -14,15 +14,13 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.internal.codegen;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

/**
* INTERNAL:
Expand All @@ -33,28 +31,28 @@
*/
public class ClassDefinition extends CodeDefinition {
protected String packageName;
protected Vector<String> imports;
protected List<String> imports;
protected int type;
public static final int CLASS_TYPE = 1;
public static final int INTERFACE_TYPE = 2;
protected String superClass;
protected Vector<String> interfaces;
protected Vector<AttributeDefinition> attributes;
protected Vector<MethodDefinition> methods;
protected Vector<ClassDefinition> innerClasses;
protected List<String> interfaces;
protected List<AttributeDefinition> attributes;
protected List<MethodDefinition> methods;
protected List<ClassDefinition> innerClasses;

public ClassDefinition() {
this.packageName = "";
this.imports = new Vector<>(3);
this.imports = new ArrayList<>(3);
this.type = CLASS_TYPE;
this.interfaces = new Vector<>(3);
this.attributes = new Vector<>();
this.methods = new Vector<>();
this.innerClasses = new Vector<>(3);
this.interfaces = new ArrayList<>(3);
this.attributes = new ArrayList<>();
this.methods = new ArrayList<>();
this.innerClasses = new ArrayList<>(3);
}

public void addAttribute(AttributeDefinition attribute) {
getAttributes().addElement(attribute);
getAttributes().add(attribute);
}

/**
Expand All @@ -63,7 +61,7 @@ public void addAttribute(AttributeDefinition attribute) {
*/
public void addImport(String importStatement) {
if (!getImports().contains(importStatement)) {
getImports().addElement(importStatement);
getImports().add(importStatement);
}
}

Expand All @@ -77,7 +75,7 @@ private void addImports(Map<String, Set<String>> typeNameMap) {
}

for (String packageName : packageNames) {
if (!packageName.equals(JAVA_LANG_PACKAGE_NAME) && !packageName.equals(getPackageName()) && !packageName.equals("")) {
if (!packageName.equals(JAVA_LANG_PACKAGE_NAME) && !packageName.equals(getPackageName()) && !packageName.isEmpty()) {
addImport(packageName + "." + shortName);
}
}
Expand All @@ -91,43 +89,42 @@ public void addInnerClass(ClassDefinition classDefinition) {
}

public void addInterface(String interfaceClassName) {
getInterfaces().addElement(interfaceClassName);
getInterfaces().add(interfaceClassName);
}

public void addMethod(MethodDefinition method) {
getMethods().addElement(method);
getMethods().add(method);
}

private void addTypeNamesToMap(HashMap<String, Set<String>> typeNameMap) {
putTypeNameInMap(getSuperClass(), typeNameMap);

for (Iterator<String> i = getInterfaces().iterator(); i.hasNext();) {
putTypeNameInMap(i.next(), typeNameMap);
for (String s : getInterfaces()) {
putTypeNameInMap(s, typeNameMap);
}

for (Iterator<AttributeDefinition> i = getAttributes().iterator(); i.hasNext();) {
(i.next()).putTypeNamesInMap(typeNameMap);
for (AttributeDefinition attributeDefinition : getAttributes()) {
attributeDefinition.putTypeNamesInMap(typeNameMap);
}

for (Iterator<MethodDefinition> i = getMethods().iterator(); i.hasNext();) {
i.next().putTypeNamesInMap(typeNameMap);
for (MethodDefinition methodDefinition : getMethods()) {
methodDefinition.putTypeNamesInMap(typeNameMap);
}
}

private void adjustTypeNames(HashMap<String, Set<String>> typeNameMap) {
setSuperClass(adjustTypeName(getSuperClass(), typeNameMap));

for (Iterator<String> i = new Vector<>(getInterfaces()).iterator(); i.hasNext();) {
String interfaceName = i.next();
for (String interfaceName : new ArrayList<>(getInterfaces())) {
replaceInterface(interfaceName, adjustTypeName(interfaceName, typeNameMap));
}

for (Iterator<AttributeDefinition> i = getAttributes().iterator(); i.hasNext();) {
(i.next()).adjustTypeNames(typeNameMap);
for (AttributeDefinition attributeDefinition : getAttributes()) {
attributeDefinition.adjustTypeNames(typeNameMap);
}

for (Iterator<MethodDefinition> i = getMethods().iterator(); i.hasNext();) {
i.next().adjustTypeNames(typeNameMap);
for (MethodDefinition methodDefinition : getMethods()) {
methodDefinition.adjustTypeNames(typeNameMap);
}
}

Expand All @@ -141,7 +138,7 @@ private void adjustTypeNames(HashMap<String, Set<String>> typeNameMap) {
* - Will not add imports for classes in the same package.
* - Will not parse method bodies, but will unqualify types it finds.
*
* ?? - Should unqualification occur during writing? That way, reflective definitions could take advantage.
* ?? - Should un-qualification occur during writing? That way, reflective definitions could take advantage.
*
*/
public void calculateImports() {
Expand All @@ -162,23 +159,23 @@ public boolean containsMethod(MethodDefinition method) {
return getMethods().contains(method);
}

protected Vector<AttributeDefinition> getAttributes() {
protected List<AttributeDefinition> getAttributes() {
return attributes;
}

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

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

protected Vector<String> getInterfaces() {
protected List<String> getInterfaces() {
return interfaces;
}

protected Vector<MethodDefinition> getMethods() {
protected List<MethodDefinition> getMethods() {
return methods;
}

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

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

private void setMethods(Vector<MethodDefinition> methods) {
private void setMethods(List<MethodDefinition> methods) {
this.methods = methods;
}

Expand All @@ -231,34 +228,20 @@ public void setType(int type) {
}

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

protected void sortMethods() {
//Object methodArray[] = getMethods().toArray();
MethodDefinition[] methodArray = getMethods().toArray(new MethodDefinition[0]);

Comparator<MethodDefinition> comparison = new Comparator<MethodDefinition>() {
@Override
public int compare(MethodDefinition first, MethodDefinition second) {
if (first.isConstructor()) {
return -1;
} else if (second.isConstructor()) {
return 1;
} else {
return first.getName().compareTo(second.getName());
}
Comparator<MethodDefinition> comparison = (first, second) -> {
if (first.isConstructor()) {
return -1;
} else if (second.isConstructor()) {
return 1;
} else {
return first.getName().compareTo(second.getName());
}
};

Arrays.sort(methodArray, comparison);

Vector<MethodDefinition> sortedMethods = new Vector<>(getMethods().size());
for (int index = 0; index < methodArray.length; index++) {
sortedMethods.addElement(methodArray[index]);
}

setMethods(sortedMethods);
getMethods().sort(comparison);
}

/**
Expand All @@ -273,8 +256,7 @@ public void write(CodeGenerator generator) {
generator.cr();
}

for (Enumeration<String> importsEnum = getImports().elements(); importsEnum.hasMoreElements();) {
String importLine = importsEnum.nextElement();
for (String importLine : getImports()) {
generator.write("import ");
generator.write(importLine);
generator.writeln(";");
Expand Down Expand Up @@ -306,10 +288,7 @@ public void writeBody(CodeGenerator generator) {
}

boolean isFirst = true;
for (Enumeration<String> interfacesEnum = getInterfaces().elements();
interfacesEnum.hasMoreElements();) {
String interfaceName = interfacesEnum.nextElement();

for (String interfaceName : getInterfaces()) {
if (isFirst) {
if (isInterface()) {
generator.write(" extends");
Expand All @@ -329,27 +308,25 @@ public void writeBody(CodeGenerator generator) {
generator.writeln(" {");
generator.cr();

for (Enumeration<AttributeDefinition> attributesEnum = getAttributes().elements();
attributesEnum.hasMoreElements();) {
for (AttributeDefinition attributeDefinition : getAttributes()) {
generator.tab();
(attributesEnum.nextElement()).write(generator);
attributeDefinition.write(generator);
generator.cr();
}

if (!getAttributes().isEmpty()) {
generator.cr();
}

for (Enumeration<MethodDefinition> methodsEnum = getMethods().elements(); methodsEnum.hasMoreElements();) {
methodsEnum.nextElement().write(generator);
for (MethodDefinition methodDefinition : getMethods()) {
methodDefinition.write(generator);
generator.cr();
generator.cr();
}

//used for Oc4j code gen
for (Enumeration<ClassDefinition> innerClassesEnum = getInnerClasses().elements();
innerClassesEnum.hasMoreElements();) {
(innerClassesEnum.nextElement()).write(generator);
for (ClassDefinition classDefinition : getInnerClasses()) {
classDefinition.write(generator);
generator.cr();
generator.cr();
}
Expand Down

0 comments on commit 352a1e6

Please sign in to comment.