db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1489314 - in /db/derby/code/trunk/java: engine/org/apache/derby/ engine/org/apache/derby/iapi/services/loader/ engine/org/apache/derby/impl/services/reflect/ engine/org/apache/derby/loc/ shared/org/apache/derby/shared/common/reference/ tes...
Date Tue, 04 Jun 2013 08:03:37 GMT
Author: kahatlen
Date: Tue Jun  4 08:03:37 2013
New Revision: 1489314

URL: http://svn.apache.org/r1489314
Log:
DERBY-6243: Fold Java5ClassFactory into ReflectClassesJava2

Removed:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/Java5ClassInspector.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/Java5ClassFactory.java
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/ClassInspector.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/DatabaseClasses.java
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/engine/org/apache/derby/modules.properties
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UngroupedAggregatesNegativeTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/ClassInspector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/ClassInspector.java?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/ClassInspector.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/ClassInspector.java
Tue Jun  4 08:03:37 2013
@@ -28,6 +28,8 @@ import org.apache.derby.iapi.error.Stand
 import org.apache.derby.iapi.reference.SQLState;
 
 import java.lang.reflect.*;
+import java.util.ArrayList;
+import java.util.HashMap;
 
 /**
 	Methods to find out relationships between classes and methods within a class.
@@ -501,36 +503,83 @@ public class ClassInspector
 
 	/**
 	 * Given an implementation of a parameterized interface, return
-     * the bounds on the type variables. This method raises an exception if the
-     * JVM does not support generics. May return null if type resolution fails.
+     * the bounds on the type variables. May return null if type resolution
+     * fails.
 	 */
 	public Class[][] getTypeBounds( Class parameterizedInterface, Class implementation )
         throws StandardException
 	{
-		throw StandardException.newException( SQLState.VM_LEVEL_TOO_LOW, "Java 5" );
+        if ( implementation == null ) { return null; }
+
+        Type[]  genericInterfaces = implementation.getGenericInterfaces();
+        for ( Type genericInterface : genericInterfaces )
+        {
+            //
+            // Look for the generic interface whose raw type is the
+            // parameterized interface we're interested in.
+            //
+            if ( genericInterface instanceof ParameterizedType )
+            {
+                ParameterizedType   pt = (ParameterizedType) genericInterface;
+                Type    rawType = pt.getRawType();
+
+                // found it!
+                if ( parameterizedInterface == rawType )
+                {
+                    return findTypeBounds( pt );
+                }
+            }
+        }
+
+        // couldn't find the interface we're looking for. check our superclass.
+        return getTypeBounds( parameterizedInterface, implementation.getSuperclass() );
     }
-    
+
 	/**
 	 * Return true if the method or constructor supports varargs.
 	 */
 	public boolean  isVarArgsMethod( Member member )
 	{
-        // Varargs were introduced by Java 5. So this 1.4 ClassInspector always
-        // return false;
-        return false;
+        if (member instanceof Method) {
+            return ((Method) member).isVarArgs();
+        } else if (member instanceof Constructor) {
+            return ((Constructor) member).isVarArgs();
+        } else {
+            return false;
+        }
     }
-    
+
 	/**
 	 * Given an implementation of a parameterized interface, return
-     * the actual types of the interface type variables. This method raises an exception
if the
-     * JVM does not support generics. May return null or an array of nulls if type resolution
fails.
+     * the actual types of the interface type variables.
+     * May return null or an array of nulls if type resolution fails.
 	 */
     public Class[] getGenericParameterTypes( Class parameterizedType, Class implementation
)
         throws StandardException
 	{
-		throw StandardException.newException( SQLState.VM_LEVEL_TOO_LOW, "Java 5" );
+        // construct the inheritance chain stretching from the parameterized
+        // type down to the concrete implemention
+        ArrayList<Class<?>> chain =
+                getTypeChain(parameterizedType, implementation);
+
+        // walk the chain, filling in a map of generic types to their
+        // resolved types
+        HashMap<Type, Type> resolvedTypes = getResolvedTypes(chain);
+
+        // compose the resolved types together in order to compute the actual
+        // classes which are plugged into the variables of the parameterized
+        // type
+        ArrayList<Class<?>> parameterTypes =
+                getParameterTypes(parameterizedType, resolvedTypes);
+
+        // turn the list into an array
+        if (parameterTypes == null) {
+            return null;
+        }
+
+        return parameterTypes.toArray(new Class[parameterTypes.size()]);
     }
-    
+
 	/**
 	 * Get the parameter types for a method described by a Member as a String[].
 	 *
@@ -904,31 +953,6 @@ nextMethod:	for (int i = 0; i < methods.
 		return Array.newInstance(baseClass, new int[arrayDepth]).getClass();
 	}
 
-
-	/**
-		Is method/constructor T more or equally specific than method U.
-
-		See the Java Language Specification section 15.11.2.2.
-	*/
-	private boolean isMethodMoreSpecificOrEqual(Member T, Member U, boolean[] isParam) {
-
-		Class[] TC;
-		Class[] UC;
-
-		if (T instanceof Method) {
-			if (!classConvertableFromTo(T.getDeclaringClass(), U.getDeclaringClass(), true))
-				return false;
-
-			TC = ((Method) T).getParameterTypes();
-			UC = ((Method) U).getParameterTypes();
-		} else {
-			TC = ((Constructor) T).getParameterTypes();
-			UC = ((Constructor) U).getParameterTypes();
-		}
-
-		return signatureConvertableFromTo(TC, null, UC, isParam, true);
-	}
-
 	/**
 	 *  Can we convert a signature from fromTypes(primFromTypes) to toTypes.
 	 *  "mixTypes" is a flag to show if object/primitive type conversion is
@@ -1128,5 +1152,179 @@ nextMethod:	for (int i = 0; i < methods.
 		return method.getDeclaringClass().getName();
 	}		
 
+    /////////////////////////////////////////////////////////////////////////
+    //
+    // MINIONS FOR getTypeBounds()
+    //
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Get the type bounds for all of the type variables of the given
+     * parameterized type.
+     */
+    private Class[][] findTypeBounds(ParameterizedType pt) {
+        Type[] actualTypeArguments = pt.getActualTypeArguments();
+        int argCount = actualTypeArguments.length;
+        Class[][] retval = new Class[argCount][];
+
+        for (int i = 0; i < argCount; i++) {
+            retval[ i] = boundType(actualTypeArguments[ i]);
+        }
+
+        return retval;
+    }
+
+    /**
+     * Get the bounds for a single type variable.
+     */
+    private Class[] boundType(Type type) {
+        if (type instanceof Class) {
+            return new Class[]{(Class) type};
+        } else if (type instanceof TypeVariable) {
+            Type[] bounds = ((TypeVariable) type).getBounds();
+            int count = bounds.length;
+            Class[] retval = new Class[count];
+
+            for (int i = 0; i < count; i++) {
+                retval[ i] = getRawType(bounds[ i]);
+            }
+
+            return retval;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Get the raw type of a type bound.
+     */
+    private Class getRawType(Type bound) {
+        if (bound instanceof Class) {
+            return (Class) bound;
+        } else if (bound instanceof ParameterizedType) {
+            return getRawType(((ParameterizedType) bound).getRawType());
+        } else {
+            return null;
+        }
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+    //
+    // MINIONS FOR getGenericParameterTypes()
+    //
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Construct an inheritance chain of types stretching from a supertype down
+     * to a concrete implementation.
+     */
+    private ArrayList<Class<?>> getTypeChain(Class<?> chainEnd, Class<?>
start) {
+        ArrayList<Class<?>> result = null;
+
+        if (start == null) {
+            return null;
+        }
+
+        if (!chainEnd.isAssignableFrom(start)) {
+            return null;
+        }
+
+        if (start == chainEnd) {
+            result = new ArrayList<Class<?>>();
+        }
+
+        if (result == null) {
+            result = getTypeChain(chainEnd, start.getSuperclass());
+
+            if (result == null) {
+                for (Class<?> iface : start.getInterfaces()) {
+                    result = getTypeChain(chainEnd, iface);
+                    if (result != null) {
+                        break;
+                    }
+                }
+            }
+        }
+
+        if (result != null) {
+            result.add(start);
+        }
+
+        return result;
+    }
+
+    /**
+     * Given an inheritance chain of types, stretching from a superclass down
+     * to a terminal concrete class, construct a map of generic types to their
+     * resolved types.
+     */
+    private HashMap<Type, Type> getResolvedTypes(ArrayList<Class<?>> chain)
{
+        if (chain == null) {
+            return null;
+        }
+
+        HashMap<Type, Type> resolvedTypes = new HashMap<Type, Type>();
+
+        for (Class<?> klass : chain) {
+            addResolvedTypes(resolvedTypes, klass.getGenericSuperclass());
+
+            for (Type iface : klass.getGenericInterfaces()) {
+                addResolvedTypes(resolvedTypes, iface);
+            }
+        }
+
+        return resolvedTypes;
+    }
+
+    /**
+     * Given a generic type, add its parameter types to an evolving
+     * map of resolved types. Some of the resolved types may be
+     * generic type variables which will need further resolution from
+     * other generic types.
+     */
+    private void addResolvedTypes(HashMap<Type, Type> resolvedTypes,
+                                  Type genericType) {
+        if (genericType == null) {
+            return;
+        }
+
+        if (genericType instanceof ParameterizedType) {
+            ParameterizedType pt = (ParameterizedType) genericType;
+            Class rawType = (Class) pt.getRawType();
+
+            Type[] actualTypeArguments = pt.getActualTypeArguments();
+            TypeVariable[] typeParameters = rawType.getTypeParameters();
+            for (int i = 0; i < actualTypeArguments.length; i++) {
+                resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
+            }
+        }
+    }
+
+    /**
+     * Given a map of resolved types, compose them together in order
+     * to resolve the actual concrete types that are plugged into the
+     * parameterized type.
+     */
+    private ArrayList<Class<?>> getParameterTypes(
+            Class<?> parameterizedType, HashMap<Type, Type> resolvedTypes) {
+        if (resolvedTypes == null) {
+            return null;
+        }
+
+        Type[] actualTypeArguments = parameterizedType.getTypeParameters();
+
+        ArrayList<Class<?>> result = new ArrayList<Class<?>>();
+
+        // resolve types by composing type variables.
+        for (Type baseType : actualTypeArguments) {
+            while (resolvedTypes.containsKey(baseType)) {
+                baseType = resolvedTypes.get(baseType);
+            }
+
+            result.add(getRawType(baseType));
+        }
+
+        return result;
+    }
 }
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/DatabaseClasses.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/DatabaseClasses.java?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/DatabaseClasses.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/DatabaseClasses.java
Tue Jun  4 08:03:37 2013
@@ -117,8 +117,7 @@ abstract class DatabaseClasses
 	}
 
     /**
-     * For creating the class inspector. On Java 5 and higher, we have a more
-     * capable class inspector.
+     * For creating the class inspector.
      */
     protected   ClassInspector  makeClassInspector( DatabaseClasses dc )
     {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Tue Jun  4 08:03:37
2013
@@ -3686,12 +3686,6 @@ Guide.
                 <arg>limitDescriptor</arg>
             </msg>
 
-            <msg>
-                <name>XBCM5.S</name>
-                <text>This operation requires that the JVM level be at least {0}.</text>
-                <arg>vmLevel</arg>
-            </msg>
-
         </family>
 
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/modules.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/modules.properties?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/modules.properties (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/modules.properties Tue Jun  4 08:03:37
2013
@@ -151,11 +151,6 @@ cloudscape.config.lockManagerJ6=all
 derby.module.classManagerJ2=org.apache.derby.impl.services.reflect.ReflectClassesJava2
 cloudscape.config.classManagerJ2=derby
 
-# Java5ClassFactory requires JDK 1.5 (constant 6)
-derby.module.classManagerJ6=org.apache.derby.impl.services.reflect.Java5ClassFactory
-derby.env.jdk.classManagerJ6=6
-cloudscape.config.classManagerJ6=derby
-
 # cryptography - requires JDK 1.2 and greater and com.sun.crypto.provider.SunJCE
 #
 derby.module.cryptographyJ2=org.apache.derby.impl.services.jce.JCECipherFactoryBuilder

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
Tue Jun  4 08:03:37 2013
@@ -218,7 +218,6 @@ public interface SQLState {
 	String GENERATED_CLASS_INSTANCE_ERROR	= "XBCM2.S";
 	String GENERATED_CLASS_NO_SUCH_METHOD	= "XBCM3.S";
 	String GENERATED_CLASS_LIMIT_EXCEEDED	= "XBCM4.S";
-	String VM_LEVEL_TOO_LOW	= "XBCM5.S";
 
 	/*
 	** Cryptography

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UngroupedAggregatesNegativeTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UngroupedAggregatesNegativeTest.java?rev=1489314&r1=1489313&r2=1489314&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UngroupedAggregatesNegativeTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UngroupedAggregatesNegativeTest.java
Tue Jun  4 08:03:37 2013
@@ -25,7 +25,6 @@ import junit.framework.Test;
 
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.TestConfiguration;
-import org.apache.derbyTesting.junit.JDBC;
 
 /**
  * Test case for ungroupedAggregatesNegative.sql. 
@@ -104,36 +103,4 @@ public class UngroupedAggregatesNegative
         assertStatementError("21000", st, sql);
         st.close();
     }
-
-    /**
-     * Test that we get a reasonable error when trying to invoke
-     * a user-defined aggregate on a vm which doesn't support generics.
-     */
-    public  void    testUDAWithoutGenerics() throws Exception
-    {
-        if (JDBC.vmSupportsJDBC3()) { return; }
-        
-        Statement st = createStatement();
-
-        st.execute
-            (
-             "create derby aggregate bad_mode for int\n" +
-             "external name 'org.apache.derbyTesting.functionTests.tests.lang.ModeAggregate'"
-             );
-        
-        try {
-            st.execute
-                (
-                 "select bad_mode( columnnumber ) from sys.syscolumns" 
-                 );
-            fail( "Aggregate unexpectedly succeeded." );
-        } catch (SQLException se)
-        {
-            String  actualSQLState = se.getSQLState();
-            if ( !"XBCM5".equals( actualSQLState ) && !"XJ001".equals( actualSQLState
)  && !"42ZC8".equals( actualSQLState ) )
-            {
-                fail( "Unexpected SQLState: " + actualSQLState );
-            }
-        }
-    }
 }



Mime
View raw message