summaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2005-06-08 23:24:51 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2005-06-08 23:24:51 +0000
commit88913bd165b7043de1b4e2eec5e37465321b8bb5 (patch)
tree9b1831d216a01f4fedb54df0147eecfa38c83938 /vm
parent9399c41550e32c6bd8ea2d299a09f7151f9dc7dd (diff)
downloadclasspath-88913bd165b7043de1b4e2eec5e37465321b8bb5.tar.gz
2005-06-09 Andrew John Hughes <gnu_andrew@member.fsf.org>
* java/lang/Class.java: (internalGetClasses()): Use collections with type parameters. (internalGetFields()): Likewise. (internalGetMethods()): Likewise. (getSuperclass()): Changed to new return type. (asSubclass(Class<U>)): Documented. (getEnumConstants()): Calls VMClass. (getAnnotation(Class<?>)): Implemented. (getAnnotations()): Implemented. (getCanonicalName()): Implemented. (getDeclaredAnnotations()): Implemented. (getEnclosingClass()): Implemented. (getEnclosingConstructor()): Implemented. (getEnclosingMethod()): Implemented. (getGenericInterfaces()): Implemented. (getGenericSuperclass()): Implemented. (getTypeParameters()): Implemented. (isAnnotationPresent(Class<?>)): Implemented. (isAnonymousClass()): Implemented. (isLocalClass()): Implemented. (isMemberClass()): Implemented. * java/lang/Package.java: (getAnnotation(Class<?>)): Implemented. (getAnnotations()): Implemented. (getDeclaredAnnotations()): Implemented. (isAnnotationPresent(Class<?>)): Implemented. * java/lang/annotation/AnnotationTypeMismatchException.java: Added serial version UID. * java/lang/annotation/ElementType.java: Likewise. * java/lang/annotation/RetentionPolicy.java: Likewise. * java/lang/reflect/AnnotatedElement.java: Documented. * java/lang/reflect/Modifier.java: (toString(int)): Switched to using StringBuilder. (toString(int,StringBuilder)): Likewise. * vm/reference/java/lang/VMClass.java: (getSuperClass(Class<T>)): Updated return type. (getSimpleName(Class<?>)): Use VM methods directly. (getEnumConstants(Class<T>)): Implementation moved from Class. (getDeclaredAnnotations(Class<?>)): New native method. (getCanonicalName(Class<?>)): Implemented. (getEnclosingClass(Class<?>)): New native method. (getEnclosingConstructor(Class<?>)): New native method. (getEnclosingMethod(Class<?>)): New native method. (getGenericInterfaces(Class<?>)): New native method. (getGenericSuperclass(Class<?>)): New native method. (getTypeParameters(Class<T>)): New native method. (isAnonymousClass(Class<?>)): New native method. (isLocalClass(Class<?>)): New native method. (isMemberClass(Class<?>)): New native method. * vm/reference/java/lang/VMPackage.java: New VM class corresponding to java.lang.Package. (getDeclaredAnnotations(Class<?>)): New native method. * vm/reference/java/lang/VMSystem.java: Removed unnecessary imports. * vm/reference/java/lang/reflect/Constructor.java: (toString()): Changed StringBuffer to StringBuilder. (getTypeParameters()): Changed to native method. * vm/reference/java/lang/reflect/Field.java: (toString()): Changed StringBuffer to StringBuilder. * vm/reference/java/lang/reflect/Method.java: (getReturnType()): Updated return type. (toString()): Changed StringBuffer to StringBuilder. (invoke(Object,...)): Updated arguments.
Diffstat (limited to 'vm')
-rw-r--r--vm/reference/java/lang/VMClass.java276
-rw-r--r--vm/reference/java/lang/VMPackage.java76
-rw-r--r--vm/reference/java/lang/VMSystem.java2
-rw-r--r--vm/reference/java/lang/reflect/Constructor.java19
-rw-r--r--vm/reference/java/lang/reflect/Field.java2
-rw-r--r--vm/reference/java/lang/reflect/Method.java6
6 files changed, 363 insertions, 18 deletions
diff --git a/vm/reference/java/lang/VMClass.java b/vm/reference/java/lang/VMClass.java
index e94299204..6c82dfdb5 100644
--- a/vm/reference/java/lang/VMClass.java
+++ b/vm/reference/java/lang/VMClass.java
@@ -37,9 +37,13 @@ exception statement from your version. */
package java.lang;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
/*
* This class is a reference version, mainly for compiling a class library
@@ -152,7 +156,7 @@ final class VMClass
* @param klass the Class object that's calling us
* @return the direct superclass of this class
*/
- static native Class getSuperclass(Class klass);
+ static native <T> Class<? super T> getSuperclass(Class<T> klass);
/**
* Get the interfaces this class <EM>directly</EM> implements, in the
@@ -325,7 +329,7 @@ final class VMClass
static native boolean isEnum(Class klass);
/**
- * Returns the simple name for this class, as used in the source
+ * Returns the simple name for the specified class, as used in the source
* code. For normal classes, this is the content returned by
* <code>getName()</code> which follows the last ".". Anonymous
* classes have no name, and so the result of calling this method is
@@ -334,16 +338,276 @@ final class VMClass
* component type of an anonymous class has a simple name of simply
* "[]".
*
+ * @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 (klass.isArray())
+ if (isArray(klass))
{
- return klass.getComponentType().getSimpleName() + "[]";
+ return getComponentType(klass).getSimpleName() + "[]";
}
- String fullName = klass.getName();
+ String fullName = getName(klass);
return fullName.substring(fullName.lastIndexOf(".") + 1);
}
+ /**
+ * Returns the enumeration constants of this class, or
+ * null if this class is not an <code>Enum</code>.
+ *
+ * @param klass the class whose enumeration constants should be returned.
+ * @return an array of <code>Enum</code> constants
+ * associated with this class, or null if this
+ * class is not an <code>enum</code>.
+ * @since 1.5
+ */
+ static <T> T[] getEnumConstants(Class<T> klass)
+ {
+ if (isEnum(klass))
+ {
+ try
+ {
+ return (T[])
+ klass.getMethod("values").invoke(null);
+ }
+ catch (NoSuchMethodException exception)
+ {
+ throw new Error("Enum lacks values() method");
+ }
+ catch (IllegalAccessException exception)
+ {
+ throw new Error("Unable to access Enum class");
+ }
+ catch (InvocationTargetException exception)
+ {
+ throw new
+ RuntimeException("The values method threw an exception",
+ exception);
+ }
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all annotations directly defined by the specified class. If
+ * there are no annotations associated with this class, then a zero-length
+ * array will be returned. The returned array may be modified by the client
+ * code, but this will have no effect on the annotation content of this
+ * class, and hence no effect on the return value of this method for
+ * future callers.
+ *
+ * @param klass the class whose annotations should be returned.
+ * @return the annotations directly defined by the specified class.
+ * @since 1.5
+ */
+ static native Annotation[] getDeclaredAnnotations(Class<?> klass);
+
+ /**
+ * <p>
+ * Returns the canonical name of the specified class, as defined by section
+ * 6.7 of the Java language specification. Each package, top-level class,
+ * top-level interface and primitive type has a canonical name. A member
+ * class has a canonical name, if its parent class has one. Likewise,
+ * an array type has a canonical name, if its component type does.
+ * Local or anonymous classes do not have canonical names.
+ * </p>
+ * <p>
+ * The canonical name for top-level classes, top-level interfaces and
+ * primitive types is always the same as the fully-qualified name.
+ * For array types, the canonical name is the canonical name of its
+ * component type with `[]' appended.
+ * </p>
+ * <p>
+ * The canonical name of a member class always refers to the place where
+ * the class was defined, and is composed of the canonical name of the
+ * defining class and the simple name of the member class, joined by `.'.
+ * For example, if a <code>Person</code> class has an inner class,
+ * <code>M</code>, then both its fully-qualified name and canonical name
+ * is <code>Person.M</code>. A subclass, <code>Staff</code>, of
+ * <code>Person</code> refers to the same inner class by the fully-qualified
+ * name of <code>Staff.M</code>, but its canonical name is still
+ * <code>Person.M</code>.
+ * </p>
+ * <p>
+ * Where no canonical name is present, <code>null</code> is returned.
+ * </p>
+ *
+ * @param klass the class whose canonical name should be retrieved.
+ * @return the canonical name of the class, or <code>null</code> if the
+ * class doesn't have a canonical name.
+ * @since 1.5
+ */
+ static String getCanonicalName(Class<?> klass)
+ {
+ if (isArray(klass))
+ {
+ String componentName = getComponentType(klass).getCanonicalName();
+ if (componentName != null)
+ return componentName + "[]";
+ }
+ if (isMemberClass(klass))
+ {
+ String memberName = getDeclaringClass(klass).getCanonicalName();
+ if (memberName != null)
+ return memberName + "." + getSimpleName(klass);
+ }
+ if (isLocalClass(klass) || isAnonymousClass(klass))
+ return null;
+ return getName(klass);
+ }
+
+ /**
+ * Returns the class which immediately encloses the specified class. If
+ * the class is a top-level class, this method returns <code>null</code>.
+ *
+ * @param klass the class whose enclosing class should be returned.
+ * @return the immediate enclosing class, or <code>null</code> if this is
+ * a top-level class.
+ * @since 1.5
+ */
+ static native Class<?> getEnclosingClass(Class<?> klass);
+
+ /**
+ * Returns the constructor which immediately encloses the specified class.
+ * If the class is a top-level class, or a local or anonymous class
+ * immediately enclosed by a type definition, instance initializer
+ * or static initializer, then <code>null</code> is returned.
+ *
+ * @param klass the class whose enclosing constructor should be returned.
+ * @return the immediate enclosing constructor if the specified class is
+ * declared within a constructor. Otherwise, <code>null</code>
+ * is returned.
+ * @since 1.5
+ */
+ static native Constructor<?> getEnclosingConstructor(Class<?> klass);
+
+ /**
+ * Returns the method which immediately encloses the specified class. If
+ * the class is a top-level class, or a local or anonymous class
+ * immediately enclosed by a type definition, instance initializer
+ * or static initializer, then <code>null</code> is returned.
+ *
+ * @param klass the class whose enclosing method should be returned.
+ * @return the immediate enclosing method if the specified class is
+ * declared within a method. Otherwise, <code>null</code>
+ * is returned.
+ * @since 1.5
+ */
+ 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>
+ *
+ * @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.
+ * @since 1.5
+ */
+ static native <T> TypeVariable<Class<T>>[] getTypeParameters(Class<T> klass);
+
+ /**
+ * Returns true if the specified class represents an anonymous class.
+ *
+ * @param klass the klass to test.
+ * @return true if the specified class represents an anonymous class.
+ * @since 1.5
+ */
+ static native boolean isAnonymousClass(Class<?> klass);
+
+ /**
+ * Returns true if the specified class represents an local class.
+ *
+ * @param klass the klass to test.
+ * @return true if the specified class represents an local class.
+ * @since 1.5
+ */
+ static native boolean isLocalClass(Class<?> klass);
+
+ /**
+ * Returns true if the specified class represents an member class.
+ *
+ * @param klass the klass to test.
+ * @return true if the specified class represents an member class.
+ * @since 1.5
+ */
+ static native boolean isMemberClass(Class<?> klass);
+
} // class VMClass
diff --git a/vm/reference/java/lang/VMPackage.java b/vm/reference/java/lang/VMPackage.java
new file mode 100644
index 000000000..051b05695
--- /dev/null
+++ b/vm/reference/java/lang/VMPackage.java
@@ -0,0 +1,76 @@
+/* VMPackage.java -- VM Specific Package methods
+ Copyright (C) 2005 Free Software Foundation
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package java.lang;
+
+import java.lang.annotation.Annotation;
+
+/*
+ * This class is a reference version, mainly for compiling a class library
+ * jar. It is likely that VM implementers replace this with their own
+ * version that can communicate effectively with the VM.
+ */
+
+/**
+ * This class provides static methods to be implemented by a VM in order
+ * to support the full functionality of the <code>Package</code> class.
+ *
+ * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
+ */
+final class VMPackage
+{
+
+ // Only static methods. Cannot be instantiated.
+ private VMPackage()
+ {
+ }
+
+ /**
+ * Returns all annotations directly defined by the specified package. If
+ * there are no annotations associated with this package, then a zero-length
+ * array will be returned. The returned array may be modified by the client
+ * code, but this will have no effect on the annotation content of this
+ * class, and hence no effect on the return value of this method for
+ * future callers.
+ *
+ * @param pack the package whose annotations should be returned.
+ * @return the annotations directly defined by the specified package.
+ * @since 1.5
+ */
+ static native Annotation[] getDeclaredAnnotations(Package pack);
+
+} // class VMPackage
diff --git a/vm/reference/java/lang/VMSystem.java b/vm/reference/java/lang/VMSystem.java
index e99e3daa4..34539ead7 100644
--- a/vm/reference/java/lang/VMSystem.java
+++ b/vm/reference/java/lang/VMSystem.java
@@ -38,8 +38,6 @@ exception statement from your version. */
package java.lang;
import java.util.List;
-import java.util.Map;
-import java.util.Properties;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
diff --git a/vm/reference/java/lang/reflect/Constructor.java b/vm/reference/java/lang/reflect/Constructor.java
index e2c5e65a0..0692f92f9 100644
--- a/vm/reference/java/lang/reflect/Constructor.java
+++ b/vm/reference/java/lang/reflect/Constructor.java
@@ -185,7 +185,7 @@ public final class Constructor<T>
public String toString()
{
// 128 is a reasonable buffer initial size for constructor
- StringBuffer sb = new StringBuffer(128);
+ StringBuilder sb = new StringBuilder(128);
Modifier.toString(getModifiers(), sb).append(' ');
sb.append(getDeclaringClass().getName()).append('(');
Class[] c = getParameterTypes();
@@ -248,11 +248,18 @@ public final class Constructor<T>
throws InstantiationException, IllegalAccessException,
InvocationTargetException;
- /** FIXME
+ /**
+ * 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()
- {
- return new TypeVariable<?>[0];
- }
+ public native TypeVariable<?>[] getTypeParameters();
+
}
diff --git a/vm/reference/java/lang/reflect/Field.java b/vm/reference/java/lang/reflect/Field.java
index 36469796b..f82fc4a58 100644
--- a/vm/reference/java/lang/reflect/Field.java
+++ b/vm/reference/java/lang/reflect/Field.java
@@ -169,7 +169,7 @@ extends AccessibleObject implements Member
public String toString()
{
// 64 is a reasonable buffer initial size for field
- StringBuffer sb = new StringBuffer(64);
+ StringBuilder sb = new StringBuilder(64);
Modifier.toString(getModifiers(), sb).append(' ');
sb.append(getType().getName()).append(' ');
sb.append(getDeclaringClass().getName()).append('.');
diff --git a/vm/reference/java/lang/reflect/Method.java b/vm/reference/java/lang/reflect/Method.java
index 693c31c9f..d6aea0b6d 100644
--- a/vm/reference/java/lang/reflect/Method.java
+++ b/vm/reference/java/lang/reflect/Method.java
@@ -124,7 +124,7 @@ extends AccessibleObject implements Member
* Gets the return type of this method.
* @return the type of this method
*/
- public native Class getReturnType();
+ public native Class<?> getReturnType();
/**
* Get the parameter list for this method, in declaration order. If the
@@ -210,7 +210,7 @@ extends AccessibleObject implements Member
public String toString()
{
// 128 is a reasonable buffer initial size for constructor
- StringBuffer sb = new StringBuffer(128);
+ StringBuilder sb = new StringBuilder(128);
Modifier.toString(getModifiers(), sb).append(' ');
sb.append(getUserTypeName(getReturnType().getName())).append(' ');
sb.append(getDeclaringClass().getName()).append('.');
@@ -323,7 +323,7 @@ extends AccessibleObject implements Member
* @throws ExceptionInInitializerError if accessing a static method triggered
* class initialization, which then failed
*/
- public Object invoke(Object o, Object[] args)
+ public Object invoke(Object o, Object... args)
throws IllegalAccessException, InvocationTargetException
{
return invokeNative(o, args, declaringClass, slot);