Index: java/lang/Class.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/Class.java,v retrieving revision 1.22.2.14 diff -u -3 -p -u -r1.22.2.14 Class.java --- java/lang/Class.java 6 May 2005 00:54:18 -0000 1.22.2.14 +++ java/lang/Class.java 8 Jun 2005 23:16:27 -0000 @@ -42,13 +42,17 @@ import gnu.classpath.VMStackWalker; import java.io.InputStream; import java.io.Serializable; +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.GenericDeclaration; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.net.URL; import java.security.AccessController; import java.security.AllPermission; @@ -92,7 +96,7 @@ import java.util.HashSet; * @see ClassLoader */ public final class Class - implements Serializable, Type + implements Serializable, Type, AnnotatedElement, GenericDeclaration { /** * Compatible with JDK 1.0+. @@ -251,12 +255,12 @@ public final class Class */ private Class[] internalGetClasses() { - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); list.addAll(Arrays.asList(getDeclaredClasses(true))); Class superClass = getSuperclass(); if (superClass != null) list.addAll(Arrays.asList(superClass.internalGetClasses())); - return (Class[])list.toArray(new Class[list.size()]); + return list.toArray(new Class[list.size()]); } /** @@ -591,7 +595,7 @@ public final class Class */ private Field[] internalGetFields() { - HashSet set = new HashSet(); + HashSet set = new HashSet(); set.addAll(Arrays.asList(getDeclaredFields(true))); Class[] interfaces = getInterfaces(); for (int i = 0; i < interfaces.length; i++) @@ -599,7 +603,7 @@ public final class Class Class superClass = getSuperclass(); if (superClass != null) set.addAll(Arrays.asList(superClass.internalGetFields())); - return (Field[])set.toArray(new Field[set.size()]); + return set.toArray(new Field[set.size()]); } /** @@ -815,7 +819,7 @@ public final class Class */ private Method[] internalGetMethods() { - HashMap map = new HashMap(); + HashMap map = new HashMap(); Method[] methods; Class[] interfaces = getInterfaces(); for(int i = 0; i < interfaces.length; i++) @@ -840,7 +844,7 @@ public final class Class { map.put(new MethodKey(methods[i]), methods[i]); } - return (Method[])map.values().toArray(new Method[map.size()]); + return map.values().toArray(new Method[map.size()]); } /** @@ -994,7 +998,7 @@ public final class Class * * @return the direct superclass of this class */ - public Class getSuperclass() + public Class getSuperclass() { return VMClass.getSuperclass (this); } @@ -1270,18 +1274,43 @@ public final class Class } /** - * FIXME + *

+ * Casts this class to represent a subclass of the specified class. + * This method is useful for `narrowing' the type of a class so that + * the class object, and instances of that class, can match the contract + * of a more restrictive method. For example, if this class has the + * static type of Class<Object>, and a dynamic type of + * Class<Rectangle>, then, assuming Shape is + * a superclass of Rectangle, this method can be used on + * this class with the parameter, Class<Shape>, to retain + * the same instance but with the type + * Class<? extends Shape>. + *

+ *

+ * If this class can be converted to an instance which is parameterised + * over a subtype of the supplied type, U, then this method + * returns an appropriately cast reference to this object. Otherwise, + * a ClassCastException is thrown. + *

+ * + * @param klass the class object, the parameterized type (U) of + * which should be a superclass of the parameterized type of + * this instance. + * @return a reference to this object, appropriately cast. + * @throws ClassCastException if this class can not be converted to one + * which represents a subclass of the specified + * type, U. * @since 1.5 */ - public Class asSubclass(Class klass) + public Class asSubclass(Class klass) { if (! klass.isAssignableFrom(this)) throw new ClassCastException(); - return (Class) this; + return (Class) this; } /** - * Return object, cast to this Class' type. + * Returns the specified object, cast to this Class' type. * * @param obj the object to cast * @throws ClassCastException if obj is not an instance of this class @@ -1293,8 +1322,8 @@ public final class Class } /** - * Like getField(String) but without the security checks and returns null - * instead of throwing NoSuchFieldException. + * Like getField(String) but without the security checks and + * returns null instead of throwing NoSuchFieldException. */ private Field internalGetField(String name) { @@ -1355,41 +1384,18 @@ public final class Class * @return an array of Enum constants * associated with this class, or null if this * class is not an enum. + * @since 1.5 */ public T[] getEnumConstants() { - if (isEnum()) - { - try - { - return (T[]) - getMethod("values", null).invoke(null,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; - } + return VMClass.getEnumConstants(this); } /** * Returns true if this class is an Enum. * * @return true if this is an enumeration class. + * @since 1.5 */ public boolean isEnum() { @@ -1401,6 +1407,7 @@ public final class Class * the compiler. * * @return true if this is a synthetic class. + * @since 1.5 */ public boolean isSynthetic() { @@ -1411,6 +1418,7 @@ public final class Class * Returns true if this class is an Annotation. * * @return true if this is an annotation class. + * @since 1.5 */ public boolean isAnnotation() { @@ -1428,10 +1436,293 @@ public final class Class * "[]". * * @return the simple name for this class. + * @since 1.5 */ public String getSimpleName() { return VMClass.getSimpleName(this); } + /** + * Returns this class' annotation for the specified annotation type, + * or null if no such annotation exists. + * + * @param annotationClass the type of annotation to look for. + * @return this class' annotation for the specified type, or + * null if no such annotation exists. + * @since 1.5 + */ + public A getAnnotation(Class annotationClass) + { + A foundAnnotation = null; + Annotation[] annotations = getAnnotations(); + for (Annotation annotation : annotations) + if (annotation.annotationType() == annotationClass) + foundAnnotation = (A) annotation; + return foundAnnotation; + } + + /** + * Returns all annotations associated with this 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. + * + * @return this class' annotations. + * @since 1.5 + */ + public Annotation[] getAnnotations() + { + HashSet set = new HashSet(); + set.addAll(Arrays.asList(getDeclaredAnnotations())); + Class[] interfaces = getInterfaces(); + for (int i = 0; i < interfaces.length; i++) + set.addAll(Arrays.asList(interfaces[i].getAnnotations())); + Class superClass = getSuperclass(); + if (superClass != null) + set.addAll(Arrays.asList(superClass.getAnnotations())); + return set.toArray(new Annotation[set.size()]); + } + + /** + *

+ * Returns the canonical name of this 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. + *

+ *

+ * 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. + *

+ *

+ * 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 Person class has an inner class, + * M, then both its fully-qualified name and canonical name + * is Person.M. A subclass, Staff, of + * Person refers to the same inner class by the fully-qualified + * name of Staff.M, but its canonical name is still + * Person.M. + *

+ *

+ * Where no canonical name is present, null is returned. + *

+ * + * @return the canonical name of the class, or null if the + * class doesn't have a canonical name. + * @since 1.5 + */ + public String getCanonicalName() + { + return VMClass.getCanonicalName(this); + } + + /** + * Returns all annotations directly defined by this 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. + * + * @return the annotations directly defined by this class. + * @since 1.5 + */ + public Annotation[] getDeclaredAnnotations() + { + return VMClass.getDeclaredAnnotations(this); + } + + /** + * Returns the class which immediately encloses this class. If this class + * is a top-level class, this method returns null. + * + * @return the immediate enclosing class, or null if this is + * a top-level class. + * @since 1.5 + */ + public Class getEnclosingClass() + { + return VMClass.getEnclosingClass(this); + } + + /** + * Returns the constructor which immediately encloses this class. If + * this class is a top-level class, or a local or anonymous class + * immediately enclosed by a type definition, instance initializer + * or static initializer, then null is returned. + * + * @return the immediate enclosing constructor if this class is + * declared within a constructor. Otherwise, null + * is returned. + * @since 1.5 + */ + public Constructor getEnclosingConstructor() + { + return VMClass.getEnclosingConstructor(this); + } + + /** + * Returns the method which immediately encloses this class. If + * this class is a top-level class, or a local or anonymous class + * immediately enclosed by a type definition, instance initializer + * or static initializer, then null is returned. + * + * @return the immediate enclosing method if this class is + * declared within a method. Otherwise, null + * is returned. + * @since 1.5 + */ + public Method getEnclosingMethod() + { + return VMClass.getEnclosingMethod(this); + } + + /** + *

+ * Returns an array of Type objects which represent the + * interfaces directly implemented by this class or extended by this + * interface. + *

+ *

+ * 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 ParameterizedType + * interface, and only if an instance has not already been created. + *

+ *

+ * 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, Cloneable and + * Serializable, is always returned, with the objects in + * that order. A class representing a primitive type or void always + * returns an array of zero size. + *

+ * + * @return an array of interfaces implemented or extended by this 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 + */ + public Type[] getGenericInterfaces() + { + return VMClass.getGenericInterfaces(this); + } + + /** + *

+ * Returns a Type object representing the direct superclass, + * whether class, interface, primitive type or void, of this class. + * If this class is an array class, then a class instance representing + * the Object class is returned. If this class is primitive, + * an interface, or a representation of either the Object + * class or void, then null is returned. + *

+ *

+ * 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 ParameterizedType + * interface, and only if an instance has not already been created. + *

+ * + * @return the superclass of this 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 + */ + public Type getGenericSuperclass() + { + return VMClass.getGenericSuperclass(this); + } + + /** + * Returns an array of TypeVariable objects that represents + * the type variables declared by this class, 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 VMClass.getTypeParameters(this); + } + + /** + * Returns true if an annotation for the specified type is associated + * with this class. This is primarily a short-hand for using marker + * annotations. + * + * @param annotationClass the type of annotation to look for. + * @return true if an annotation exists for the specified type. + * @since 1.5 + */ + public boolean isAnnotationPresent(Class + annotationClass) + { + return getAnnotation(annotationClass) != null; + } + + /** + * Returns true if this object represents an anonymous class. + * + * @return true if this object represents an anonymous class. + * @since 1.5 + */ + public boolean isAnonymousClass() + { + return VMClass.isAnonymousClass(this); + } + + /** + * Returns true if this object represents an local class. + * + * @return true if this object represents an local class. + * @since 1.5 + */ + public boolean isLocalClass() + { + return VMClass.isLocalClass(this); + } + + /** + * Returns true if this object represents an member class. + * + * @return true if this object represents an member class. + * @since 1.5 + */ + public boolean isMemberClass() + { + return VMClass.isMemberClass(this); + } + + } Index: java/lang/Package.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/Package.java,v retrieving revision 1.11.2.2 diff -u -3 -p -u -r1.11.2.2 Package.java --- java/lang/Package.java 16 Jan 2005 15:15:12 -0000 1.11.2.2 +++ java/lang/Package.java 8 Jun 2005 23:16:28 -0000 @@ -39,6 +39,8 @@ package java.lang; import gnu.classpath.VMStackWalker; +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; import java.net.URL; import java.util.NoSuchElementException; import java.util.StringTokenizer; @@ -70,9 +72,10 @@ import java.util.StringTokenizer; * @see ClassLoader#definePackage(String, String, String, String, String, * String, String, URL) * @since 1.2 - * @status updated to 1.4 + * @status updated to 1.5 */ public class Package + implements AnnotatedElement { /** The name of the Package */ private final String name; @@ -233,7 +236,7 @@ public class Package * * @return true if the version is compatible, false otherwise * - * @Throws NumberFormatException if either version string is invalid + * @throws NumberFormatException if either version string is invalid * @throws NullPointerException if either version string is null */ public boolean isCompatibleWith(String version) @@ -315,4 +318,72 @@ public class Package return ("package " + name + (specTitle == null ? "" : ", " + specTitle) + (specVersion == null ? "" : ", version " + specVersion)); } + + /** + * Returns this package's annotation for the specified annotation type, + * or null if no such annotation exists. + * + * @param annotationClass the type of annotation to look for. + * @return this package's annotation for the specified type, or + * null if no such annotation exists. + * @since 1.5 + */ + public
A getAnnotation(Class annotationClass) + { + A foundAnnotation = null; + Annotation[] annotations = getAnnotations(); + for (Annotation annotation : annotations) + if (annotation.annotationType() == annotationClass) + foundAnnotation = (A) annotation; + return foundAnnotation; + } + + /** + * Returns all annotations associated with this 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 + * package, and hence no effect on the return value of this method for + * future callers. + * + * @return this package' annotations. + * @since 1.5 + */ + public Annotation[] getAnnotations() + { + /** All a package's annotations are declared within it. */ + return getDeclaredAnnotations(); + } + + /** + * Returns all annotations directly defined by this 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 + * package, and hence no effect on the return value of this method for + * future callers. + * + * @return the annotations directly defined by this package. + * @since 1.5 + */ + public Annotation[] getDeclaredAnnotations() + { + return VMPackage.getDeclaredAnnotations(this); + } + + /** + * Returns true if an annotation for the specified type is associated + * with this package. This is primarily a short-hand for using marker + * annotations. + * + * @param annotationClass the type of annotation to look for. + * @return true if an annotation exists for the specified type. + * @since 1.5 + */ + public boolean isAnnotationPresent(Class + annotationClass) + { + return getAnnotation(annotationClass) != null; + } + } // class Package Index: java/lang/annotation/AnnotationTypeMismatchException.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/annotation/AnnotationTypeMismatchException.java,v retrieving revision 1.1.2.3 diff -u -3 -p -u -r1.1.2.3 AnnotationTypeMismatchException.java --- java/lang/annotation/AnnotationTypeMismatchException.java 20 Feb 2005 21:10:36 -0000 1.1.2.3 +++ java/lang/annotation/AnnotationTypeMismatchException.java 8 Jun 2005 23:16:28 -0000 @@ -53,6 +53,11 @@ public class AnnotationTypeMismatchExcep { /** + * For compatability with Sun's JDK + */ + private static final long serialVersionUID = 8125925355765570191L; + + /** * Constructs an AnnotationTypeMismatchException * which is due to a mismatched type in the annotation * element, m. The erroneous type used for the Index: java/lang/annotation/ElementType.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/annotation/Attic/ElementType.java,v retrieving revision 1.1.2.1 diff -u -3 -p -u -r1.1.2.1 ElementType.java --- java/lang/annotation/ElementType.java 7 Aug 2004 19:01:27 -0000 1.1.2.1 +++ java/lang/annotation/ElementType.java 8 Jun 2005 23:16:28 -0000 @@ -49,5 +49,11 @@ public enum ElementType METHOD, PACKAGE, PARAMETER, - TYPE + TYPE; + + /** + * For compatability with Sun's JDK + */ + private static final long serialVersionUID = -7822481094880900790L; + } Index: java/lang/annotation/RetentionPolicy.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/annotation/Attic/RetentionPolicy.java,v retrieving revision 1.1.2.2 diff -u -3 -p -u -r1.1.2.2 RetentionPolicy.java --- java/lang/annotation/RetentionPolicy.java 18 Sep 2004 23:02:32 -0000 1.1.2.2 +++ java/lang/annotation/RetentionPolicy.java 8 Jun 2005 23:16:28 -0000 @@ -56,5 +56,11 @@ public enum RetentionPolicy * Indicates that the annotation should only be available when * parsing the source code. */ - SOURCE + SOURCE; + + /** + * For compatability with Sun's JDK + */ + private static final long serialVersionUID = 2098916047332259179L; + } Index: java/lang/reflect/AnnotatedElement.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/reflect/Attic/AnnotatedElement.java,v retrieving revision 1.1.2.2 diff -u -3 -p -u -r1.1.2.2 AnnotatedElement.java --- java/lang/reflect/AnnotatedElement.java 7 Jan 2005 03:42:30 -0000 1.1.2.2 +++ java/lang/reflect/AnnotatedElement.java 8 Jun 2005 23:16:28 -0000 @@ -41,14 +41,75 @@ package java.lang.reflect; import java.lang.annotation.Annotation; /** + *

+ * Represents an element that can be annotated. The methods of this interface + * provide reflection-based access to the annotations associated with a + * particular element, such as a class, field, method or package. Each + * annotation returned by these methods is both immutable and serializable. + * The returned arrays may be freely modified, without any effect on the + * arrays returned to future callers. + *

+ *

+ * If an annotation refers to a type or enumeration constant that is + * inaccessible, then a TypeNotPresentException or + * EnumConstantNotPresentException will be thrown. Likewise, + * invalid annotations will produce either a + * AnnotationTypeMismatchException or + * IncompleteAnnotationException. + *

+ * * @author Tom Tromey (address@hidden) * @author Andrew John Hughes (address@hidden) * @since 1.5 */ public interface AnnotatedElement { - T getAnnotation(Class annoType); + + /** + * Returns the element's annotation for the specified annotation type, + * or null if no such annotation exists. + * + * @param annotationClass the type of annotation to look for. + * @return this element's annotation for the specified type, or + * null if no such annotation exists. + * @throws NullPointerException if the annotation class is null. + */ + T getAnnotation(Class annotationClass); + + /** + * Returns all annotations associated with the element. If there are + * no annotations associated with the element, 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 the + * element, and hence no effect on the return value of this method for + * future callers. + * + * @return this element's annotations. + */ Annotation[] getAnnotations(); - Annotation[] getDeclaredAnnotation(); - boolean isAnnotationPresent(Class annoType); + + /** + * Returns all annotations directly defined by the element. If there are + * no annotations directly associated with the element, 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. + * + * @return the annotations directly defined by the element. + * @since 1.5 + */ + Annotation[] getDeclaredAnnotations(); + + /** + * Returns true if an annotation for the specified type is associated + * with the element. This is primarily a short-hand for using marker + * annotations. + * + * @param annotationClass the type of annotation to look for. + * @return true if an annotation exists for the specified type. + * @since 1.5 + */ + boolean isAnnotationPresent(Class annotationClass); + } Index: java/lang/reflect/Modifier.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/reflect/Modifier.java,v retrieving revision 1.8.2.1 diff -u -3 -p -u -r1.8.2.1 Modifier.java --- java/lang/reflect/Modifier.java 19 Feb 2005 10:50:37 -0000 1.8.2.1 +++ java/lang/reflect/Modifier.java 8 Jun 2005 23:16:28 -0000 @@ -288,7 +288,7 @@ public class Modifier */ public static String toString(int mod) { - return toString(mod, new StringBuffer()).toString(); + return toString(mod, new StringBuilder()).toString(); } /** @@ -297,7 +297,7 @@ public class Modifier * @param r the StringBuffer to which the String representation is appended * @return r, with information appended */ - static StringBuffer toString(int mod, StringBuffer r) + static StringBuilder toString(int mod, StringBuilder r) { if (isPublic(mod)) r.append("public "); Index: vm/reference/java/lang/VMClass.java =================================================================== RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMClass.java,v retrieving revision 1.10.2.6 diff -u -3 -p -u -r1.10.2.6 VMClass.java --- vm/reference/java/lang/VMClass.java 27 May 2005 00:01:46 -0000 1.10.2.6 +++ vm/reference/java/lang/VMClass.java 8 Jun 2005 23:16:29 -0000 @@ -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 Class getSuperclass(Class klass); /** * Get the interfaces this class directly 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 * getName() 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 Enum. + * + * @param klass the class whose enumeration constants should be returned. + * @return an array of Enum constants + * associated with this class, or null if this + * class is not an enum. + * @since 1.5 + */ + static T[] getEnumConstants(Class 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); + + /** + *

+ * 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. + *

+ *

+ * 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. + *

+ *

+ * 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 Person class has an inner class, + * M, then both its fully-qualified name and canonical name + * is Person.M. A subclass, Staff, of + * Person refers to the same inner class by the fully-qualified + * name of Staff.M, but its canonical name is still + * Person.M. + *

+ *

+ * Where no canonical name is present, null is returned. + *

+ * + * @param klass the class whose canonical name should be retrieved. + * @return the canonical name of the class, or null 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 null. + * + * @param klass the class whose enclosing class should be returned. + * @return the immediate enclosing class, or null 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 null 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, null + * 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 null 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, null + * is returned. + * @since 1.5 + */ + static native Method getEnclosingMethod(Class klass); + + /** + *

+ * Returns an array of Type objects which represent the + * interfaces directly implemented by the specified class or extended by the + * specified interface. + *

+ *

+ * 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 ParameterizedType + * interface, and only if an instance has not already been created. + *

+ *

+ * 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, Cloneable and + * Serializable, is always returned, with the objects in + * that order. A class representing a primitive type or void always + * returns an array of zero size. + *

+ * + * @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); + + /** + *

+ * Returns a Type 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 Object class is returned. If the class is primitive, + * an interface, or a representation of either the Object + * class or void, then null is returned. + *

+ *

+ * 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 ParameterizedType + * interface, and only if an instance has not already been created. + *

+ * + * @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 TypeVariable 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 TypeVariable>[] getTypeParameters(Class 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 Index: vm/reference/java/lang/VMPackage.java =================================================================== RCS file: vm/reference/java/lang/VMPackage.java diff -N vm/reference/java/lang/VMPackage.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ vm/reference/java/lang/VMPackage.java 8 Jun 2005 23:16:29 -0000 @@ -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 Package class. + * + * @author Andrew John Hughes (address@hidden) + */ +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 Index: vm/reference/java/lang/VMSystem.java =================================================================== RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMSystem.java,v retrieving revision 1.10.2.4 diff -u -3 -p -u -r1.10.2.4 VMSystem.java --- vm/reference/java/lang/VMSystem.java 20 Jan 2005 15:15:00 -0000 1.10.2.4 +++ vm/reference/java/lang/VMSystem.java 8 Jun 2005 23:16:29 -0000 @@ -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; 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.4 diff -u -3 -p -u -r1.11.2.4 Constructor.java --- vm/reference/java/lang/reflect/Constructor.java 27 May 2005 00:01:46 -0000 1.11.2.4 +++ vm/reference/java/lang/reflect/Constructor.java 8 Jun 2005 23:16:29 -0000 @@ -185,7 +185,7 @@ public final class Constructor 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 throws InstantiationException, IllegalAccessException, InvocationTargetException; - /** FIXME + /** + * Returns an array of TypeVariable 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(); + } Index: vm/reference/java/lang/reflect/Field.java =================================================================== RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/reflect/Field.java,v retrieving revision 1.9 diff -u -3 -p -u -r1.9 Field.java --- vm/reference/java/lang/reflect/Field.java 29 Mar 2004 07:07:40 -0000 1.9 +++ vm/reference/java/lang/reflect/Field.java 8 Jun 2005 23:16:29 -0000 @@ -169,7 +169,7 @@ extends AccessibleObject implements Memb 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('.'); 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.1 diff -u -3 -p -u -r1.12.2.1 Method.java --- vm/reference/java/lang/reflect/Method.java 15 Jan 2005 17:02:23 -0000 1.12.2.1 +++ vm/reference/java/lang/reflect/Method.java 8 Jun 2005 23:16:29 -0000 @@ -124,7 +124,7 @@ extends AccessibleObject implements Memb * 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 Memb 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 Memb * @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);