You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2007/12/10 00:46:32 UTC
svn commit: r602754 - in
/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src:
main/java/org/apache/cayenne/gen/ main/resources/dotemplates/v1_2/
test/java/org/apache/cayenne/gen/
Author: aadamchik
Date: Sun Dec 9 15:46:31 2007
New Revision: 602754
URL: http://svn.apache.org/viewvc?rev=602754&view=rev
Log:
CAY-927 Switch to version 1.2 as default generation mechanism , deprecate 1.1 generator
(fixing collections imports)
Added:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ImportUtils.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/resources/dotemplates/v1_2/superclass.vm
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java?rev=602754&r1=602753&r2=602754&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java Sun Dec 9 15:46:31 2007
@@ -336,9 +336,10 @@
}
/**
- * Opens a Writer to write generated output. Writer encoding is determined from the
- * value of the "encoding" property. Output file is determined from the current state
- * of VelocityContext and the TemplateType passed as a parameter.
+ * Opens a Writer to write generated output. Returned Writer is mapped to a filesystem
+ * file (although subclasses may override that). File location is determined from the
+ * current state of VelocityContext and the TemplateType passed as a parameter. Writer
+ * encoding is determined from the value of the "encoding" property.
*/
protected Writer openWriter(TemplateType templateType) throws Exception {
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ImportUtils.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ImportUtils.java?rev=602754&r1=602753&r2=602754&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ImportUtils.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ImportUtils.java Sun Dec 9 15:46:31 2007
@@ -17,7 +17,6 @@
* under the License.
****************************************************************/
-
package org.apache.cayenne.gen;
import java.util.ArrayList;
@@ -37,93 +36,108 @@
public class ImportUtils {
public static final String importOrdering[] = new String[] {
- "java.", "javax.", "org.", "com." };
+ "java.", "javax.", "org.", "com."
+ };
static final String primitives[] = new String[] {
"long", "double", "byte", "boolean", "float", "short", "int"
};
-
+
static final String primitiveClasses[] = new String[] {
Long.class.getName(), Double.class.getName(), Byte.class.getName(),
Boolean.class.getName(), Float.class.getName(), Short.class.getName(),
Integer.class.getName()
};
-
- static Map<String, String> classesForPrimitives = Util.toMap(primitives, primitiveClasses);
- static Map<String, String> primitivesForClasses = Util.toMap(primitiveClasses, primitives);
+
+ static Map<String, String> classesForPrimitives = Util.toMap(
+ primitives,
+ primitiveClasses);
+ static Map<String, String> primitivesForClasses = Util.toMap(
+ primitiveClasses,
+ primitives);
protected Map<String, String> importTypesMap = new HashMap<String, String>();
- protected Map<String, String> reservedImportTypesMap = new HashMap<String, String>(); // Types forced to be FQN
-
+ protected Map<String, String> reservedImportTypesMap = new HashMap<String, String>(); // Types
+ // forced
+ // to
+ // be
+ // FQN
+
protected String packageName;
-
- public ImportUtils()
- {
+
+ public ImportUtils() {
super();
}
-
- protected boolean canRegisterType(String typeName)
- {
+
+ protected boolean canRegisterType(String typeName) {
// Not sure why this would ever happen, but it did
- if (null == typeName) return false;
-
+ if (null == typeName)
+ return false;
+
StringUtils stringUtils = StringUtils.getInstance();
String typeClassName = stringUtils.stripPackageName(typeName);
String typePackageName = stringUtils.stripClass(typeName);
-
- if (typePackageName.length() == 0) return false; // disallow non-packaged types (primitives, probably)
- if ("java.lang".equals(typePackageName)) return false;
-
+
+ if (typePackageName.length() == 0)
+ return false; // disallow non-packaged types (primitives, probably)
+ if ("java.lang".equals(typePackageName))
+ return false;
+
// Can only have one type -- rest must use fqn
- if (reservedImportTypesMap.containsKey(typeClassName)) return false;
- if (importTypesMap.containsKey(typeClassName)) return false;
-
+ if (reservedImportTypesMap.containsKey(typeClassName))
+ return false;
+ if (importTypesMap.containsKey(typeClassName))
+ return false;
+
return true;
}
-
+
/**
- * Reserve a fully-qualified data type class name so it cannot be used by another class.
- * No import statements will be generated for reserved types.
- * Typically, this is the fully-qualified class name of the class being generated.
+ * Reserve a fully-qualified data type class name so it cannot be used by another
+ * class. No import statements will be generated for reserved types. Typically, this
+ * is the fully-qualified class name of the class being generated.
+ *
* @param typeName FQ data type class name.
*/
- public void addReservedType(String typeName)
- {
- if (! canRegisterType(typeName)) return;
-
+ public void addReservedType(String typeName) {
+ if (!canRegisterType(typeName))
+ return;
+
StringUtils stringUtils = StringUtils.getInstance();
String typeClassName = stringUtils.stripPackageName(typeName);
-
+
reservedImportTypesMap.put(typeClassName, typeName);
}
-
+
/**
- * Register a fully-qualified data type class name.
- * For example, org.apache.cayenne.CayenneDataObject
+ * Register a fully-qualified data type class name. For example,
+ * org.apache.cayenne.CayenneDataObject.
+ *
* @param typeName FQ data type class name.
*/
- public void addType(String typeName)
- {
- if (! canRegisterType(typeName)) return;
-
+ public void addType(String typeName) {
+ if (!canRegisterType(typeName))
+ return;
+
StringUtils stringUtils = StringUtils.getInstance();
String typePackageName = stringUtils.stripClass(typeName);
String typeClassName = stringUtils.stripPackageName(typeName);
-
- if (typePackageName.equals(packageName)) return;
+
+ if (typePackageName.equals(packageName))
+ return;
importTypesMap.put(typeClassName, typeName);
}
-
+
/**
* Add the package name to use for this importUtil invocation.
+ *
* @param packageName
*/
- public void setPackage(String packageName)
- {
+ public void setPackage(String packageName) {
this.packageName = packageName;
}
-
+
/**
* Performs processing similar to <code>formatJavaType(String)</code>, with special
* handling of primitive types and their Java class counterparts. This method allows
@@ -142,32 +156,33 @@
: formatJavaType(typeName);
}
}
-
+
/**
- * Removes registered package and non-reserved registered type name prefixes from java types
+ * Removes registered package and non-reserved registered type name prefixes from java
+ * types
*/
public String formatJavaType(String typeName) {
if (typeName != null) {
StringUtils stringUtils = StringUtils.getInstance();
String typeClassName = stringUtils.stripPackageName(typeName);
-
- if (! reservedImportTypesMap.containsKey(typeClassName))
- {
- if (importTypesMap.containsKey(typeClassName))
- {
- if (typeName.equals(importTypesMap.get(typeClassName))) return typeClassName;
+
+ if (!reservedImportTypesMap.containsKey(typeClassName)) {
+ if (importTypesMap.containsKey(typeClassName)) {
+ if (typeName.equals(importTypesMap.get(typeClassName)))
+ return typeClassName;
}
}
-
+
String typePackageName = stringUtils.stripClass(typeName);
- if ("java.lang".equals(typePackageName)) return typeClassName;
+ if ("java.lang".equals(typePackageName))
+ return typeClassName;
if ((null != packageName) && (packageName.equals(typePackageName)))
- return typeClassName;
+ return typeClassName;
}
return typeName;
}
-
+
/**
* @since 3.0
*/
@@ -175,30 +190,28 @@
String value = ImportUtils.classesForPrimitives.get(type);
return formatJavaType(value != null ? value : type);
}
-
+
/**
* @since 3.0
*/
public boolean isNonBooleanPrimitive(String type) {
return ImportUtils.classesForPrimitives.containsKey(type) && !isBoolean(type);
}
-
+
/**
* @since 3.0
*/
public boolean isBoolean(String type) {
return "boolean".equals(type);
}
-
+
/**
* Generate package and list of import statements based on the registered types.
*/
- public String generate()
- {
+ public String generate() {
StringBuffer outputBuffer = new StringBuffer();
- if (null != packageName)
- {
+ if (null != packageName) {
outputBuffer.append("package ");
outputBuffer.append(packageName);
outputBuffer.append(';');
@@ -210,27 +223,28 @@
Collections.sort(typesList, new Comparator<String>() {
public int compare(String s1, String s2) {
-
+
for (int index = 0; index < importOrdering.length; index++) {
String ordering = importOrdering[index];
-
- if ( (s1.startsWith(ordering)) && (!s2.startsWith(ordering)) )
+
+ if ((s1.startsWith(ordering)) && (!s2.startsWith(ordering)))
return -1;
- if ( (!s1.startsWith(ordering)) && (s2.startsWith(ordering)) )
+ if ((!s1.startsWith(ordering)) && (s2.startsWith(ordering)))
return 1;
}
-
+
return s1.compareTo(s2);
}
});
-
+
String lastStringPrefix = null;
boolean firstIteration = true;
for (String typeName : typesList) {
if (firstIteration) {
firstIteration = false;
- } else {
+ }
+ else {
outputBuffer.append(System.getProperty("line.separator"));
}
// Output another newline if we're in a different root package.
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/resources/dotemplates/v1_2/superclass.vm
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/resources/dotemplates/v1_2/superclass.vm?rev=602754&r1=602753&r2=602754&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/resources/dotemplates/v1_2/superclass.vm (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/resources/dotemplates/v1_2/superclass.vm Sun Dec 9 15:46:31 2007
@@ -21,51 +21,54 @@
## Sub class - class of entity, ie, org.apache.cayenne.art.Artist
##
## Classes available in template
-## objEntity - the ObjEntity class: See org.apache.cayenne.map.ObjectEntity
+## object (duplicated as 'objEntity') - the ObjEntity class: See org.apache.cayenne.map.ObjectEntity
## stringUtils - class for string "helper" functions: See org.apache.cayenne.gen.StringUtils
## entityUtils - class for entity "helper" functions: See org.apache.cayenne.gen.EntityUtils
## importUtils - class for import statement management: See org.apache.cayenne.gen.ImportUtils
+## superClassName
+## superPackageName
+## subClassName
+## subPackageName
+## baseClassName
+## basePackageName
##
-##
-${importUtils.setPackage($entityUtils.superPackageName)}##
-${importUtils.addReservedType("${entityUtils.superPackageName}.${entityUtils.superClassName}")}##
-${importUtils.addType("${entityUtils.basePackageName}.${entityUtils.baseClassName}")}##
-#foreach( $attr in ${objEntity.DeclaredAttributes} )
+${importUtils.setPackage($superPackageName)}##
+${importUtils.addReservedType("${superPackageName}.${superClassName}")}##
+${importUtils.addType("${basePackageName}.${baseClassName}")}##
+#foreach( $attr in ${object.DeclaredAttributes} )
$importUtils.addType(${attr.Type})##
#end
-#foreach( $rel in ${objEntity.DeclaredRelationships} )
+#foreach( $rel in ${object.DeclaredRelationships} )
$importUtils.addType(${rel.TargetEntity.ClassName})##
-#end
-#if( ${entityUtils.hasToManyDeclaredRelationships()} )
-${importUtils.addType('java.util.List')}##
+$importUtils.addType(${rel.CollectionType})##
#end
${importUtils.generate()}
/**
- * Class ${entityUtils.superClassName} was generated by Cayenne.
+ * Class ${superClassName} was generated by Cayenne.
* It is probably a good idea to avoid changing this class manually,
* since it may be overwritten next time code is regenerated.
* If you need to make any customizations, please use subclass.
*/
-public abstract class ${entityUtils.superClassName} extends ${entityUtils.baseClassName} {
+public abstract class ${superClassName} extends ${baseClassName} {
## Create property names
-#foreach( $attr in ${objEntity.DeclaredAttributes} )
+#foreach( $attr in ${object.DeclaredAttributes} )
public static final String ${stringUtils.capitalizedAsConstant($attr.Name)}_PROPERTY = "${attr.Name}";
#end
-#foreach( $rel in ${objEntity.DeclaredRelationships} )
+#foreach( $rel in ${object.DeclaredRelationships} )
public static final String ${stringUtils.capitalizedAsConstant($rel.Name)}_PROPERTY = "${rel.Name}";
#end
-#if( $objEntity.DbEntity )
-#foreach( $idAttr in ${objEntity.DbEntity.PrimaryKey} )
+#if( $object.DbEntity )
+#foreach( $idAttr in ${object.DbEntity.PrimaryKey} )
public static final String ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = "${idAttr.Name}";
#end
#end
## Create attribute set/get methods
-#foreach( $attr in ${objEntity.DeclaredAttributes} )
-#if ("true" != "${objEntity.isReadOnly()}")
+#foreach( $attr in ${object.DeclaredAttributes} )
+#if ("true" != "${object.isReadOnly()}")
public void set${stringUtils.capitalized($attr.Name)}($importUtils.formatJavaType(${attr.Type}) $stringUtils.formatVariableName(${attr.Name})) {
writeProperty("${attr.Name}", $stringUtils.formatVariableName(${attr.Name}));
}
@@ -89,7 +92,7 @@
#end
##
## Create list add/remove/get methods
-#foreach( $rel in ${objEntity.DeclaredRelationships} )
+#foreach( $rel in ${object.DeclaredRelationships} )
#if( $rel.ToMany )
#if ( ! $rel.ReadOnly )
public void addTo${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) obj) {
@@ -99,11 +102,11 @@
removeToManyTarget("${rel.name}", obj, true);
}
#end
- public $rel.CollectionType get${stringUtils.capitalized($rel.Name)}() {
- return ($rel.CollectionType)readProperty("${rel.name}");
+ public $importUtils.formatJavaType($rel.CollectionType) get${stringUtils.capitalized($rel.Name)}() {
+ return ($importUtils.formatJavaType($rel.CollectionType))readProperty("${rel.name}");
}
#else
-#if ( !${objEntity.isReadOnly()} && !$rel.ReadOnly )
+#if ( !${object.isReadOnly()} && !$rel.ReadOnly )
public void set${stringUtils.capitalized($rel.Name)}($importUtils.formatJavaType(${rel.TargetEntity.ClassName}) $stringUtils.formatVariableName(${rel.name})) {
setToOneTarget("${rel.name}", $stringUtils.formatVariableName(${rel.name}), true);
}
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java?rev=602754&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/gen/ClassGenerationActionTest.java Sun Dec 9 15:46:31 2007
@@ -0,0 +1,127 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.gen;
+
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.cayenne.map.Entity;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.unit.BasicCase;
+
+public class ClassGenerationActionTest extends BasicCase {
+
+ protected ClassGenerationAction action;
+ protected Collection<StringWriter> writers;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ this.writers = new ArrayList<StringWriter>(3);
+ this.action = new ClassGenerationAction() {
+
+ @Override
+ protected Writer openWriter(TemplateType templateType) throws Exception {
+ StringWriter writer = new StringWriter();
+ writers.add(writer);
+ return writer;
+ }
+ };
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ action = null;
+ writers = null;
+ }
+
+ public void testExecuteArtifactPairsImports() throws Exception {
+
+ ObjEntity testEntity1 = new ObjEntity("TE1");
+ testEntity1.setClassName("org.example.TestClass1");
+
+ action.setMakePairs(true);
+ action.setSuperPkg("org.example.auto");
+
+ List<String> generated = execute(new EntityArtifact(testEntity1));
+ assertNotNull(generated);
+ assertEquals(2, generated.size());
+
+ String superclass = generated.get(0);
+ assertTrue(superclass, superclass.indexOf("package org.example.auto;") >= 0);
+ assertTrue(superclass, superclass
+ .indexOf("import org.apache.cayenne.CayenneDataObject;") >= 0);
+
+ String subclass = generated.get(1);
+ assertTrue(subclass, subclass.indexOf("package org.example;") >= 0);
+ assertTrue(
+ subclass,
+ subclass.indexOf("import org.example.auto._TestClass1;") >= 0);
+ }
+
+ public void testExecuteArtifactPairsMapRelationships() throws Exception {
+
+ ObjEntity testEntity1 = new ObjEntity("TE1");
+ testEntity1.setClassName("org.example.TestClass1");
+
+ final ObjEntity testEntity2 = new ObjEntity("TE1");
+ testEntity2.setClassName("org.example.TestClass2");
+
+ ObjRelationship relationship = new ObjRelationship("xMap") {
+
+ @Override
+ public boolean isToMany() {
+ return true;
+ }
+
+ @Override
+ public Entity getTargetEntity() {
+ return testEntity2;
+ }
+ };
+ relationship.setCollectionType("java.util.Map");
+ testEntity1.addRelationship(relationship);
+
+ action.setMakePairs(true);
+
+ List<String> generated = execute(new EntityArtifact(testEntity1));
+ assertNotNull(generated);
+ assertEquals(2, generated.size());
+
+ String superclass = generated.get(0);
+ assertTrue(superclass, superclass.indexOf("import java.util.Map;") >= 0);
+ }
+
+ protected List<String> execute(Artifact artifact) throws Exception {
+
+ action.execute(artifact);
+
+ List<String> strings = new ArrayList<String>(writers.size());
+ for (StringWriter writer : writers) {
+ strings.add(writer.toString());
+ }
+ return strings;
+ }
+}