You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by kw...@apache.org on 2011/11/11 14:52:40 UTC

svn commit: r1200884 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/ openjpa-persistence/src/main/resources/org/apache/openjpa/p...

Author: kwsutter
Date: Fri Nov 11 13:52:39 2011
New Revision: 1200884

URL: http://svn.apache.org/viewvc?rev=1200884&view=rev
Log:
OPENJPA-2070.  Clean up the processing in the MetamodelImpl.populate() method.  The exception processing and messaging were not correct/accurate.  Also, changed the testcase slightly to allow for the error checking when needed.

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/BlogUser_.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestByteArray.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java
    openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/BlogUser_.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/BlogUser_.java?rev=1200884&r1=1200883&r2=1200884&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/BlogUser_.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/BlogUser_.java Fri Nov 11 13:52:39 2011
@@ -22,6 +22,7 @@
 
 package org.apache.openjpa.persistence.criteria;
 
+import javax.persistence.metamodel.ListAttribute;
 import javax.persistence.metamodel.SingularAttribute;
 
 @javax.persistence.metamodel.StaticMetamodel
@@ -29,9 +30,14 @@ import javax.persistence.metamodel.Singu
 @javax.annotation.Generated
 (value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Wed Jul 06 09:09:40 CDT 2011")
 public class BlogUser_ {
+    // The use of ListAttribute for arrays needs a special property setting.
+    // Reference TestByteArray testcase.
+//    public static volatile ListAttribute<BlogUser,Character> characters;
     public static volatile SingularAttribute<BlogUser,Character[]> characters;
+//    public static volatile ListAttribute<BlogUser,Character> chars;
     public static volatile SingularAttribute<BlogUser,char[]> chars;
     public static volatile SingularAttribute<BlogUser,Integer> id;
+//    public static volatile ListAttribute<BlogUser,Byte> pic;
     public static volatile SingularAttribute<BlogUser,byte[]> pic;
     public static volatile SingularAttribute<BlogUser,String> username;
     public static volatile SingularAttribute<BlogUser,Integer> version;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestByteArray.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestByteArray.java?rev=1200884&r1=1200883&r2=1200884&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestByteArray.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestByteArray.java Fri Nov 11 13:52:39 2011
@@ -35,7 +35,9 @@ public class TestByteArray extends Singl
     Character[] chars = new Character[] { new Character('a'), new Character('b') };
     public void setUp() {
         super.setUp(BlogUser.class
-//            ,"openjpa.Compatibility", "UseListAttributeForArrays=false"
+            // Using the following property will require corresponding
+            // changes in BlogUser_.java
+//            ,"openjpa.Compatibility", "UseListAttributeForArrays=true"
             );
         cb = emf.getCriteriaBuilder();
         populate();

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java?rev=1200884&r1=1200883&r2=1200884&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java Fri Nov 11 13:52:39 2011
@@ -21,8 +21,7 @@ package org.apache.openjpa.persistence.m
 import static javax.persistence.metamodel.Type.PersistenceType.BASIC;
 import static javax.persistence.metamodel.Type.PersistenceType.EMBEDDABLE;
 import static javax.persistence.metamodel.Type.PersistenceType.ENTITY;
-import static 
- javax.persistence.metamodel.Type.PersistenceType.MAPPED_SUPERCLASS;
+import static javax.persistence.metamodel.Type.PersistenceType.MAPPED_SUPERCLASS;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.GenericArrayType;
@@ -41,7 +40,6 @@ import javax.persistence.metamodel.Manag
 import javax.persistence.metamodel.MappedSuperclassType;
 import javax.persistence.metamodel.Attribute;
 import javax.persistence.metamodel.Metamodel;
-import javax.persistence.metamodel.PluralAttribute;
 import javax.persistence.metamodel.Type;
 import javax.persistence.metamodel.StaticMetamodel;
 import javax.persistence.metamodel.PluralAttribute.CollectionType;
@@ -290,48 +288,38 @@ public class MetamodelImpl implements Me
      * Populate the static fields of the canonical type.
      */
     public <X> void populate(AbstractManagedType<X> type) {
-		Class<X> cls = type.getJavaType();
-		Class<?> mcls = repos.getMetaModel(cls, true);
-		if (mcls == null)
-		    return;
+        Class<X> cls = type.getJavaType();
+        Class<?> mcls = repos.getMetaModel(cls, true);
+        if (mcls == null)
+            return;
         StaticMetamodel anno = mcls.getAnnotation(StaticMetamodel.class);
-		if (anno == null)
+        if (anno == null)
             throw new IllegalArgumentException(_loc.get("meta-class-no-anno", 
-               mcls.getName(), cls.getName(), StaticMetamodel.class.getName()).getMessage());
-		
+                    mcls.getName(), cls.getName(), StaticMetamodel.class.getName()).getMessage());
+
         if (cls != anno.value()) {
             throw new IllegalStateException(_loc.get("meta-class-mismatch",
-            mcls.getName(), cls.getName(), anno.value()).getMessage());
+                    mcls.getName(), cls.getName(), anno.value()).getMessage());
         }
         
+        ParameterizedType mfType = null;
+        Attribute<? super X, ?> f = null;
         Field[] mfields = AccessController.doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(mcls));
-    	for (Field mf : mfields) {
+        for (Field mf : mfields) {
             try {
-                ParameterizedType mfType = getParameterziedType(mf);
+                mfType = getParameterizedType(mf); // metamodel type
                 if (mfType == null) {
-                	continue;
+                    continue;
                 }
-    	        Attribute<? super X, ?> f = type.getAttribute(mf.getName());
-    	        Class<?> fClass = f.getJavaType();
-    	       java.lang.reflect.Type[] args = mfType.getActualTypeArguments();
-    	       if (args.length < 2)
-    	           throw new IllegalStateException(
-    	               _loc.get("meta-field-no-para", mf).getMessage());
-    	       java.lang.reflect.Type ftype = args[1];
-    	       if (fClass.isPrimitive() 
-    	        || Collection.class.isAssignableFrom(fClass) 
-    	        || Map.class.isAssignableFrom(fClass)) {
-    	        ;
-    	    } else if (ftype != args[1]) {
-    	        throw new RuntimeException(_loc.get("meta-field-mismatch", 
-    	            new Object[]{mf.getName(), mcls.getName(), 
-    	                toTypeName(mfType), toTypeName(ftype)}).getMessage());
-    	    }
-            mf.set(null, f);
-	} catch (Exception e) {
-	    e.printStackTrace();
-		throw new RuntimeException(mf.toString());
-	}
+                f = type.getAttribute(mf.getName()); // persistent type
+
+                // populate the static field with persistent type information
+                mf.set(null, f);
+            } catch (Exception e) {
+                throw new RuntimeException(_loc.get("meta-field-mismatch",
+                        new Object[] { mf.getName(), mcls.getName(), toTypeName(mfType), f.getJavaType().toString() })
+                        .getMessage(), e);
+            }
         }
     }
     
@@ -343,7 +331,7 @@ public class MetamodelImpl implements Me
      * metamodel or weaving process introducing synthetic fields),
      * returns null.
      */
-    ParameterizedType getParameterziedType(Field mf) {
+    ParameterizedType getParameterizedType(Field mf) {
         java.lang.reflect.Type t = mf.getGenericType();
         if (t instanceof ParameterizedType == false) {
         	repos.getLog().warn(_loc.get("meta-field-not-param", 
@@ -356,7 +344,7 @@ public class MetamodelImpl implements Me
             throw new IllegalStateException(_loc.get("meta-field-less-param", 
             mf.getDeclaringClass(), mf.getName(), toTypeName(t)).getMessage());
         }
-        
+
         return mfType;
     }
     
@@ -396,7 +384,6 @@ public class MetamodelImpl implements Me
     }
     
     <X,Y> void validate(Field mField, Member<X, Y> member) {
-        Class<?> fType = member.getJavaType();
         if (!ParameterizedType.class.isInstance(mField.getGenericType())) {
             throw new IllegalArgumentException(_loc.get("meta-bad-field", 
                 mField).getMessage());
@@ -407,9 +394,6 @@ public class MetamodelImpl implements Me
         if (member.getDeclaringType().getJavaType() != owner)
             throw new IllegalArgumentException(_loc.get("meta-bad-field-owner", 
                     mField, owner).getMessage());
-        java.lang.reflect.Type elementType = args[1];
-        if (fType.isPrimitive())
-            return;
     }
 
     public Class classForName(String name, String[] imports) {

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=1200884&r1=1200883&r2=1200884&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 Fri Nov 11 13:52:39 2011
@@ -52,10 +52,14 @@ mmg-bad-log: Log level specified in -Alo
 
 meta-class-no-anno: Meta class "{0}" registered for "{1}" is not annotated \
 	with "{2}" annotation.	
+meta-class-mismatch: Value provided for the StaticMetamodel annotation "{2}" \
+    in Meta class "{0}" does not match the expected Entity class "{1}".
 meta-field-mismatch: The field "{0}" in meta class "{1}" is of type "{2}" \
-	which does not match the persistent property type "{3}".  
-meta-field-not-param: Encountered field " {2} {1}" in "{0}". This field is \
+	which does not support the persistent property type "{3}".  
+meta-field-not-param: Encountered field "{2} {1}" in "{0}". This field is \
 	not recognized as canonical field. Ignoring.
+meta-field-less-param: Encountered field "{2} {1}" in "{0}". This field has \
+	less than two parameters and is not recognized as a canonical field.
 type-wrong-category:"{0}" categorized as "{1}" should be a "{2}"
 mmg-asl-header: Licensed to the Apache Software Foundation (ASF) under one \
 	or more contributor license agreements.  See the NOTICE file distributed \