Hi,
I started implementing the parsing of the new Signature attribute that
describes the 1.5 generic signatures. Not everything is there yet, but I
will continue to flesh out the implementation.
Regards,
Jeroen
2005-09-25 Jeroen Frijters <[EMAIL PROTECTED]>
* gnu/java/lang/reflect/ClassSignatureParser.java,
gnu/java/lang/reflect/GenericSignatureParser.java,
gnu/java/lang/reflect/MethodSignatureParser.java: New files.
* java/lang/Class.java
(constructor): Changed type to generic type.
(cast, getEnumConstants): Added cast.
(getGenericInterfaces, getGenericSuperclass, getTypeParameters):
Implemented.
* vm/reference/java/lang/VMClass.java
(getSimpleName, getDeclaredAnnotations, getCanonicalName,
getEnclosingClass, getEnclosingConstructor, getEnclosingMethod,
isAnonymousClass, isLocalClass, isMemberClass):
Removed generic types from signatures.
(getGenericInterfaces, getGenericSuperclass, getTypeParameters):
Removed.
(getClassSignature): New method.
* vm/reference/java/lang/reflect/Constructor.java
(getTypeParameters): Implemented.
* vm/reference/java/lang/reflect/Method.java
(getTypeParameters, getSignature): New methods.
Index: java/lang/Class.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Class.java,v
retrieving revision 1.22.2.18
diff -u -r1.22.2.18 Class.java
--- java/lang/Class.java 20 Sep 2005 18:46:27 -0000 1.22.2.18
+++ java/lang/Class.java 26 Sep 2005 11:21:02 -0000
@@ -39,6 +39,7 @@
package java.lang;
import gnu.classpath.VMStackWalker;
+import gnu.java.lang.reflect.ClassSignatureParser;
import java.io.InputStream;
import java.io.Serializable;
@@ -124,7 +125,7 @@
final transient Object vmdata;
/** newInstance() caches the default constructor */
- private transient Constructor constructor;
+ private transient Constructor<T> constructor;
/**
* Class is non-instantiable from Java code; only the VM can create
@@ -1298,7 +1299,7 @@
*/
public T cast(Object obj)
{
- return VMClass.cast(obj, this);
+ return (T)VMClass.cast(obj, this);
}
/**
@@ -1368,7 +1369,7 @@
*/
public T[] getEnumConstants()
{
- return VMClass.getEnumConstants(this);
+ return (T[])VMClass.getEnumConstants(this);
}
/**
@@ -1602,7 +1603,15 @@
*/
public Type[] getGenericInterfaces()
{
- return VMClass.getGenericInterfaces(this);
+ if (isPrimitive())
+ return new Type[0];
+
+ String sig = VMClass.getClassSignature(this);
+ if (sig == null)
+ return getInterfaces();
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getInterfaceTypes();
}
/**
@@ -1636,7 +1645,18 @@
*/
public Type getGenericSuperclass()
{
- return VMClass.getGenericSuperclass(this);
+ if (isArray())
+ return Object.class;
+
+ if (isPrimitive() || isInterface() || this == Object.class)
+ return null;
+
+ String sig = VMClass.getClassSignature(this);
+ if (sig == null)
+ return getSuperclass();
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getSuperclassType();
}
/**
@@ -1653,7 +1673,12 @@
*/
public TypeVariable<Class<T>>[] getTypeParameters()
{
- return VMClass.getTypeParameters(this);
+ String sig = VMClass.getClassSignature(this);
+ if (sig == null)
+ return (TypeVariable<Class<T>>[])new TypeVariable[0];
+
+ ClassSignatureParser p = new ClassSignatureParser(this, sig);
+ return p.getTypeParameters();
}
/**
Index: vm/reference/java/lang/VMClass.java
===================================================================
RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMClass.java,v
retrieving revision 1.10.2.9
diff -u -r1.10.2.9 VMClass.java
--- vm/reference/java/lang/VMClass.java 7 Aug 2005 18:34:13 -0000 1.10.2.9
+++ vm/reference/java/lang/VMClass.java 26 Sep 2005 11:21:10 -0000
@@ -325,7 +325,7 @@
* @param klass the class whose simple name should be returned.
* @return the simple name for this class.
*/
- static String getSimpleName(Class<?> klass)
+ static String getSimpleName(Class klass)
{
if (isArray(klass))
{
@@ -387,7 +387,7 @@
* @return the annotations directly defined by the specified class.
* @since 1.5
*/
- static native Annotation[] getDeclaredAnnotations(Class<?> klass);
+ static native Annotation[] getDeclaredAnnotations(Class klass);
/**
* <p>
@@ -424,7 +424,7 @@
* class doesn't have a canonical name.
* @since 1.5
*/
- static String getCanonicalName(Class<?> klass)
+ static String getCanonicalName(Class klass)
{
if (isArray(klass))
{
@@ -452,7 +452,7 @@
* a top-level class.
* @since 1.5
*/
- static native Class<?> getEnclosingClass(Class<?> klass);
+ static native Class getEnclosingClass(Class klass);
/**
* Returns the constructor which immediately encloses the specified class.
@@ -466,7 +466,7 @@
* is returned.
* @since 1.5
*/
- static native Constructor<?> getEnclosingConstructor(Class<?> klass);
+ static native Constructor getEnclosingConstructor(Class klass);
/**
* Returns the method which immediately encloses the specified class. If
@@ -480,92 +480,18 @@
* is returned.
* @since 1.5
*/
- static native Method getEnclosingMethod(Class<?> klass);
+ static native Method getEnclosingMethod(Class klass);
/**
- * <p>
- * Returns an array of <code>Type</code> objects which represent the
- * interfaces directly implemented by the specified class or extended by the
- * specified interface.
- * </p>
- * <p>
- * If one of the superinterfaces is a parameterized type, then the
- * object returned for this interface reflects the actual type
- * parameters used in the source code. Type parameters are created
- * using the semantics specified by the <code>ParameterizedType</code>
- * interface, and only if an instance has not already been created.
- * </p>
- * <p>
- * The order of the interfaces in the array matches the order in which
- * the interfaces are declared. For classes which represent an array,
- * an array of two interfaces, <code>Cloneable</code> and
- * <code>Serializable</code>, is always returned, with the objects in
- * that order. A class representing a primitive type or void always
- * returns an array of zero size.
- * </p>
+ * Returns the class signature as specified in Class File Format
+ * chapter in the VM specification, or null if the class is not
+ * generic.
*
- * @param klass the class whose generic interfaces should be retrieved.
- * @return an array of interfaces implemented or extended by the specified
- * class.
- * @throws GenericSignatureFormatError if the generic signature of one
- * of the interfaces does not comply with that specified by the Java
- * Virtual Machine specification, 3rd edition.
- * @throws TypeNotPresentException if any of the superinterfaces refers
- * to a non-existant type.
- * @throws MalformedParameterizedTypeException if any of the interfaces
- * refer to a parameterized type that can not be instantiated for
- * some reason.
- * @since 1.5
- * @see java.lang.reflect.ParameterizedType
- */
- static native Type[] getGenericInterfaces(Class<?> klass);
-
- /**
- * <p>
- * Returns a <code>Type</code> object representing the direct superclass,
- * whether class, interface, primitive type or void, of the specified class.
- * If the class is an array class, then a class instance representing
- * the <code>Object</code> class is returned. If the class is primitive,
- * an interface, or a representation of either the <code>Object</code>
- * class or void, then <code>null</code> is returned.
- * </p>
- * <p>
- * If the superclass is a parameterized type, then the
- * object returned for this interface reflects the actual type
- * parameters used in the source code. Type parameters are created
- * using the semantics specified by the <code>ParameterizedType</code>
- * interface, and only if an instance has not already been created.
- * </p>
- *
- * @param klass the class whose generic superclass should be obtained.
- * @return the superclass of the specified class.
- * @throws GenericSignatureFormatError if the generic signature of the
- * class does not comply with that specified by the Java
- * Virtual Machine specification, 3rd edition.
- * @throws TypeNotPresentException if the superclass refers
- * to a non-existant type.
- * @throws MalformedParameterizedTypeException if the superclass
- * refers to a parameterized type that can not be instantiated for
- * some reason.
- * @since 1.5
- * @see java.lang.reflect.ParameterizedType
- */
- static native Type getGenericSuperclass(Class<?> klass);
-
- /**
- * Returns an array of <code>TypeVariable</code> objects that represents
- * the type variables declared by the specified class, in declaration order.
- * An array of size zero is returned if the specified class has no type
- * variables.
- *
- * @param klass the class whose type variables should be returned.
- * @return the type variables associated with this class.
- * @throws GenericSignatureFormatError if the generic signature does
- * not conform to the format specified in the Virtual Machine
- * specification, version 3.
+ * @param klass the klass to test.
+ * @return a ClassSignature string.
* @since 1.5
*/
- static native <T> TypeVariable<Class<T>>[] getTypeParameters(Class<T> klass);
+ static native String getClassSignature(Class klass);
/**
* Returns true if the specified class represents an anonymous class.
@@ -574,7 +500,7 @@
* @return true if the specified class represents an anonymous class.
* @since 1.5
*/
- static native boolean isAnonymousClass(Class<?> klass);
+ static native boolean isAnonymousClass(Class klass);
/**
* Returns true if the specified class represents an local class.
@@ -583,7 +509,7 @@
* @return true if the specified class represents an local class.
* @since 1.5
*/
- static native boolean isLocalClass(Class<?> klass);
+ static native boolean isLocalClass(Class klass);
/**
* Returns true if the specified class represents an member class.
@@ -592,6 +518,6 @@
* @return true if the specified class represents an member class.
* @since 1.5
*/
- static native boolean isMemberClass(Class<?> klass);
+ static native boolean isMemberClass(Class klass);
} // class VMClass
Index: vm/reference/java/lang/reflect/Constructor.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/vm/reference/java/lang/reflect/Constructor.java,v
retrieving revision 1.11.2.6
diff -u -r1.11.2.6 Constructor.java
--- vm/reference/java/lang/reflect/Constructor.java 2 Aug 2005 20:12:48
-0000 1.11.2.6
+++ vm/reference/java/lang/reflect/Constructor.java 26 Sep 2005 11:21:10
-0000
@@ -1,5 +1,5 @@
/* java.lang.reflect.Constructor - reflection of Java constructors
- Copyright (C) 1998, 2001, 2004 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -38,6 +38,7 @@
package java.lang.reflect;
+import gnu.java.lang.reflect.MethodSignatureParser;
import java.util.Arrays;
/**
@@ -260,6 +261,12 @@
* specification, version 3.
* @since 1.5
*/
- public native TypeVariable<?>[] getTypeParameters();
+ public TypeVariable<?>[] getTypeParameters()
+ {
+ String sig = getSignature();
+ MethodSignatureParser p = new MethodSignatureParser(this, sig);
+ return p.getTypeParameters();
+ }
+ private native String getSignature();
}
Index: vm/reference/java/lang/reflect/Method.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/vm/reference/java/lang/reflect/Method.java,v
retrieving revision 1.12.2.3
diff -u -r1.12.2.3 Method.java
--- vm/reference/java/lang/reflect/Method.java 2 Aug 2005 20:12:48 -0000
1.12.2.3
+++ vm/reference/java/lang/reflect/Method.java 26 Sep 2005 11:21:14 -0000
@@ -1,5 +1,5 @@
/* java.lang.reflect.Method - reflection of Java methods
- Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2001, 2002, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -38,6 +38,7 @@
package java.lang.reflect;
+import gnu.java.lang.reflect.MethodSignatureParser;
import java.util.Arrays;
/**
@@ -74,7 +75,7 @@
* @status updated to 1.4
*/
public final class Method
-extends AccessibleObject implements Member
+extends AccessibleObject implements Member, GenericDeclaration
{
Class declaringClass;
String name;
@@ -329,11 +330,28 @@
return invokeNative(o, args, declaringClass, slot);
}
- /*
- * NATIVE HELPERS
- */
-
private native Object invokeNative(Object o, Object[] args,
Class declaringClass, int slot)
throws IllegalAccessException, InvocationTargetException;
+
+ /**
+ * Returns an array of <code>TypeVariable</code> objects that represents
+ * the type variables declared by this constructor, in declaration order.
+ * An array of size zero is returned if this class has no type
+ * variables.
+ *
+ * @return the type variables associated with this class.
+ * @throws GenericSignatureFormatError if the generic signature does
+ * not conform to the format specified in the Virtual Machine
+ * specification, version 3.
+ * @since 1.5
+ */
+ public TypeVariable<?>[] getTypeParameters()
+ {
+ String sig = getSignature();
+ MethodSignatureParser p = new MethodSignatureParser(this, sig);
+ return p.getTypeParameters();
+ }
+
+ private native String getSignature();
}
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches