openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r881529 - in /openjpa/trunk/openjpa-persistence/src/main: java/org/apache/openjpa/persistence/meta/ resources/org/apache/openjpa/persistence/meta/
Date Tue, 17 Nov 2009 21:16:24 GMT
Author: ppoddar
Date: Tue Nov 17 21:16:23 2009
New Revision: 881529

URL: http://svn.apache.org/viewvc?rev=881529&view=rev
Log:
OPENJPA-1391: Handle untyped collection

Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java
    openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java?rev=881529&r1=881528&r2=881529&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AbstractManagedType.java
Tue Nov 17 21:16:23 2009
@@ -58,7 +58,7 @@
 public abstract class AbstractManagedType<X> extends Types.BaseType<X> 
     implements ManagedType<X> {
     
-    private static final Localizer _loc = Localizer.forPackage(Types.class);
+    private static final Localizer _loc = Localizer.forPackage(AbstractManagedType.class);
     public final MetamodelImpl model;
     public final ClassMetaData meta;
 
@@ -248,7 +248,7 @@
                 new AttributeTypeFilter<X, Y>(type), 
                 singularAttributeFilter);
         if (result == null)
-            notFoundException("attr-not-found-single",name, type);
+            notFoundException("attr-not-found-single", name, type);
          
         return (SingularAttribute<? super X, Y>) result;
     }
@@ -317,7 +317,7 @@
                 new ElementTypeFilter<X, E>(elementType),
                 new AttributeNameFilter<X>(name));
         if (result == null)
-            notFoundException("attr-not-found-coll",name, elementType);
+            notFoundException("attr-not-found-coll", name, elementType);
 
         return (CollectionAttribute<? super X, E>) result;
     }
@@ -729,12 +729,14 @@
     // Exception handling
     // -------------------------------------------------------------------------
     private void notFoundException(String msg, String name, Class<?> t1) {
-        throw new IllegalArgumentException(msg + " Attribute " + name + " of type " + 
-                (t1 == null ? "any" : t1.getName()) + " not found in " + meta);
+        throw new IllegalArgumentException(
+            _loc.get(msg, name, (t1 == null ? "any" : t1.getName()), meta).getMessage());
     }
     
     private void notFoundException(String msg, String name, Class<?> t1, Class<?>
t2) {
-        throw new IllegalArgumentException("Attribute " + name + " not found");
+        throw new IllegalArgumentException(
+            _loc.get(msg, new Object[]{name, (t1 == null ? "any" : t1.getName()), 
+                    (t2 == null ? "any" : t1.getName()), meta}).getMessage());
     }
     // --------------------------------------------------------------------------
     // Attribute filtering

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java?rev=881529&r1=881528&r2=881529&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
Tue Nov 17 21:16:23 2009
@@ -244,12 +244,10 @@
             Set<? extends Element> members = handler.getPersistentMembers(e);
             
             for (Element m : members) {
-                TypeMirror decl = handler.getDeclaredType(m);
-                decl.getKind();
+                TypeMirror decl  = handler.getDeclaredType(m);
                 String fieldName = handler.getPersistentMemberName(m);
                 String fieldType = handler.getDeclaredTypeName(decl);
-                TypeCategory typeCategory = toMetaModelTypeCategory(decl, 
-                        fieldType);
+                TypeCategory typeCategory = toMetaModelTypeCategory(decl, fieldType);
                 String metaModelType = typeCategory.getMetaModelType();
                 SourceCode.Field modelField = null;
                 switch (typeCategory) {
@@ -261,15 +259,15 @@
                 case COLLECTION:
                 case LIST:
                 case SET:
-                    TypeMirror param = handler.getTypeParameter(decl, 0);
+                    TypeMirror param   = handler.getTypeParameter(m, decl, 0, true);
                     String elementType = handler.getDeclaredTypeName(param);
                     modelField = modelClass.addField(fieldName, metaModelType);
                     modelField.addParameter(originalSimpleClass)
                               .addParameter(elementType);
                     break;
                 case MAP:
-                    TypeMirror key = handler.getTypeParameter(decl, 0);
-                    TypeMirror value = handler.getTypeParameter(decl, 1);
+                    TypeMirror key   = handler.getTypeParameter(m, decl, 0, false);
+                    TypeMirror value = handler.getTypeParameter(m, decl, 1, true);
                     String keyType = handler.getDeclaredTypeName(key);
                     String valueType = handler.getDeclaredTypeName(value);
                     modelField = modelClass.addField(fieldName, metaModelType);

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java?rev=881529&r1=881528&r2=881529&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/SourceAnnotationHandler.java
Tue Nov 17 21:16:23 2009
@@ -47,7 +47,11 @@
 
 import javax.persistence.Embeddable;
 import javax.persistence.Entity;
+import javax.persistence.ManyToMany;
+import javax.persistence.ManyToOne;
 import javax.persistence.MappedSuperclass;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
 import javax.persistence.Transient;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.AccessCode;
@@ -65,6 +69,7 @@
     implements MetadataProcessor<TypeElement, Element> {
 	
 	private final ProcessingEnvironment processingEnv;
+	private final Types typeUtility;
 	private final CompileTimeLogger logger;
 	/**
      * Set of Inclusion Filters based on member type, access type or transient
@@ -81,8 +86,14 @@
     protected GetterFilter getterFilter = new GetterFilter();
     protected SetterFilter setterFilter = new SetterFilter();
     
-    private static Localizer _loc = Localizer.forPackage(
-    		SourceAnnotationHandler.class);
+    protected static List<Class<? extends Annotation>> mappingAnnos = new ArrayList<Class<?
extends Annotation>>();
+    static {
+        mappingAnnos.add(OneToOne.class);
+        mappingAnnos.add(OneToMany.class);
+        mappingAnnos.add(ManyToOne.class);
+        mappingAnnos.add(ManyToMany.class);
+    }
+    private static Localizer _loc = Localizer.forPackage(SourceAnnotationHandler.class);
     
 	/**
 	 * Construct with JDK6 annotation processing environment.
@@ -92,6 +103,7 @@
         CompileTimeLogger logger) {
 		super();
 		this.processingEnv = processingEnv;
+		this.typeUtility   = processingEnv.getTypeUtils();
 		this.logger = logger;
 	}
 
@@ -187,15 +199,12 @@
     
     private int getImplicitAccessType(TypeElement type) {
         List<? extends Element> allMembers = type.getEnclosedElements();
-        Set<VariableElement> allFields = (Set<VariableElement>) 
-           filter(allMembers, fieldFilter, nonTransientFilter);
-        Set<ExecutableElement> allMethods = (Set<ExecutableElement>) 
-          filter(allMembers, methodFilter, nonTransientFilter);
+        Set<VariableElement> allFields = (Set<VariableElement>) filter(allMembers,
fieldFilter, nonTransientFilter);
+        Set<ExecutableElement> allMethods = (Set<ExecutableElement>) filter(allMembers,
methodFilter, 
+                nonTransientFilter);
 
-        Set<VariableElement> annotatedFields = filter(allFields, 
-            annotatedFilter);
-        Set<ExecutableElement> getters = filter(allMethods, getterFilter, 
-            annotatedFilter);
+        Set<VariableElement> annotatedFields = filter(allFields, annotatedFilter);
+        Set<ExecutableElement> getters = filter(allMethods, getterFilter, annotatedFilter);
         Set<ExecutableElement> setters = filter(allMethods, setterFilter);
         getters = matchGetterAndSetter(getters, setters);
         
@@ -242,9 +251,7 @@
      */
     private Set<ExecutableElement> matchGetterAndSetter(
         Set<ExecutableElement> getters,  Set<ExecutableElement> setters) {
-        Collection<ExecutableElement> unmatched =
-            new ArrayList<ExecutableElement>();
-        Types typeUtils = processingEnv.getTypeUtils();
+        Collection<ExecutableElement> unmatched =  new ArrayList<ExecutableElement>();
         
         for (ExecutableElement getter : getters) {
             String getterName = getter.getSimpleName().toString();
@@ -257,13 +264,12 @@
                                      .iterator().next().asType();
                 String actualSetterName = setter.getSimpleName().toString();
                 matched = actualSetterName.equals(expectedSetterName)
-                    && typeUtils.isSameType(setterArgType, getterReturnType);
+                       && typeUtility.isSameType(setterArgType, getterReturnType);
                 if (matched)
                     break;
             }
             if (!matched) {
-                logger.warn(_loc.get("getter-unmatched", getter, 
-                    getter.getEnclosingElement()));
+                logger.warn(_loc.get("getter-unmatched", getter, getter.getEnclosingElement()));
                 unmatched.add(getter);
             }
 
@@ -524,6 +530,17 @@
         }
     }
     
+    TypeMirror getTargetEntityType(Element e) {
+        for (Class<? extends Annotation> anno : mappingAnnos) {
+            Object target = getAnnotationValue(e, anno, "targetEntity");
+            if (target != null) {
+                return (TypeMirror)target;
+            }
+            
+        };
+        return null;
+    }
+    
     String getDeclaredTypeName(TypeMirror mirror) {
     	return getDeclaredTypeName(mirror, true);
     }
@@ -537,6 +554,8 @@
      * return <code>java.util.Set</code>.
      */
     String getDeclaredTypeName(TypeMirror mirror, boolean box) {
+        if (mirror == null || mirror.getKind() == TypeKind.NULL || mirror.getKind() == TypeKind.WILDCARD)
+            return "java.lang.Object";
     	if (mirror.getKind() == TypeKind.ARRAY) {
     		TypeMirror comp = ((ArrayType)mirror).getComponentType();
     		return getDeclaredTypeName(comp, false);
@@ -544,7 +563,7 @@
     	mirror = box ? box(mirror) : mirror;
     	if (isPrimitive(mirror))
     		return ((PrimitiveType)mirror).toString();
-    	Element elem = processingEnv.getTypeUtils().asElement(mirror);
+    	Element elem = typeUtility.asElement(mirror);
     	if (elem == null)
     	    throw new RuntimeException(_loc.get("mmg-no-type", mirror).getMessage());
         return elem.toString();
@@ -600,15 +619,23 @@
      * @return if the given type represents a parameterized type, then the
      *         indexed parameter type argument. Otherwise null.
      */
-    TypeMirror getTypeParameter(TypeMirror mirror, int index) {
+    TypeMirror getTypeParameter(Element e, TypeMirror mirror, int index, boolean checkTarget)
{
         if (mirror.getKind() == TypeKind.ARRAY)
             return ((ArrayType)mirror).getComponentType();
     	if (mirror.getKind() != TypeKind.DECLARED)
     		return null;
-        List<? extends TypeMirror> params = ((DeclaredType)mirror)
-        	.getTypeArguments();
-        return (params == null || params.size() < index+1) 
-            ? null : params.get(index);
+    	if (checkTarget) {
+    	    TypeMirror target = getTargetEntityType(e);
+    	    if (target != null)
+    	        return target;
+    	}
+        List<? extends TypeMirror> params = ((DeclaredType)mirror).getTypeArguments();
+        TypeMirror param = (params == null || params.size() < index+1) 
+            ? typeUtility.getNullType() : params.get(index);
+        if (param.getKind() == TypeKind.NULL || param.getKind() == TypeKind.WILDCARD) {
+            logger.warn(_loc.get("generic-type-param", e, getDeclaredType(e), e.getEnclosingElement()));
+        }
+        return param;
     }
 
     public TypeElement getPersistentSupertype(TypeElement cls) {

Modified: openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties?rev=881529&r1=881528&r2=881529&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties
Tue Nov 17 21:16:23 2009
@@ -79,4 +79,6 @@
 attr-not-found-map: Map attribute "{0}" of {1} key type and {2} value type not found in {3}
 attr-not-found-decl-map: Declared Map attribute "{0}" of {1} key type and {2} value type
not found in {3}
 decl-version-not-found: Declared version attribute of {1} type not found in {2} 
-version-not-found: Version attribute of {1} type not found in {2}
\ No newline at end of file
+version-not-found: Version attribute of {1} type not found in {2}
+generic-type-param: Can not determine generic type parameter for field {0} of type {1} in
{2}. \
+	Using java.lang.Object.
\ No newline at end of file



Mime
View raw message