summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ojluni/src/main/java/java/lang/Class.java992
-rw-r--r--ojluni/src/main/java/java/lang/reflect/AccessibleObject.java6
-rw-r--r--ojluni/src/main/java/java/lang/reflect/AnnotatedElement.java319
-rw-r--r--ojluni/src/main/java/java/lang/reflect/Constructor.java14
-rw-r--r--ojluni/src/main/java/java/lang/reflect/Field.java177
-rw-r--r--ojluni/src/main/java/java/lang/reflect/GenericDeclaration.java4
-rw-r--r--ojluni/src/main/java/java/lang/reflect/Modifier.java61
7 files changed, 966 insertions, 607 deletions
diff --git a/ojluni/src/main/java/java/lang/Class.java b/ojluni/src/main/java/java/lang/Class.java
index 7573fc39f1..43384f2943 100644
--- a/ojluni/src/main/java/java/lang/Class.java
+++ b/ojluni/src/main/java/java/lang/Class.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@ import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -47,6 +48,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
+import java.util.Objects;
import libcore.reflect.GenericSignatureParser;
import libcore.reflect.InternalNames;
import libcore.reflect.Types;
@@ -77,7 +79,7 @@ import sun.reflect.CallerSensitive;
* <p> The following example uses a {@code Class} object to print the
* class name of an object:
*
- * <p> <blockquote><pre>
+ * <blockquote><pre>
* void printClassName(Object obj) {
* System.out.println("The class of " + obj +
* " is " + obj.getClass().getName());
@@ -89,7 +91,7 @@ import sun.reflect.CallerSensitive;
* <cite>The Java&trade; Language Specification</cite>.
* For example:
*
- * <p> <blockquote>
+ * <blockquote>
* {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
* </blockquote>
*
@@ -102,13 +104,14 @@ import sun.reflect.CallerSensitive;
* @see java.lang.ClassLoader#defineClass(byte[], int, int)
* @since JDK1.0
*/
-public final
- class Class<T> implements java.io.Serializable,
- java.lang.reflect.GenericDeclaration,
- java.lang.reflect.Type,
- java.lang.reflect.AnnotatedElement {
- /** use serialVersionUID from JDK 1.1 for interoperability */
- private static final long serialVersionUID = 3206093459760846163L;
+public final class Class<T> implements java.io.Serializable,
+ GenericDeclaration,
+ Type,
+ AnnotatedElement {
+ private static final int ANNOTATION= 0x00002000;
+ private static final int ENUM = 0x00004000;
+ private static final int SYNTHETIC = 0x00001000;
+ private static final int FINALIZABLE = 0x80000000;
/** defining class loader, or null for the "bootstrap" system loader. */
private transient ClassLoader classLoader;
@@ -240,14 +243,10 @@ public final
/** Offset of the first virtual method defined in this class in the methods array. */
private transient short virtualMethodsOffset;
- private static final int ANNOTATION = 0x00002000;
- private static final int ENUM = 0x00004000;
- private static final int SYNTHETIC = 0x00001000;
- private static final int FINALIZABLE = 0x80000000;
-
/*
- * Constructor. Only the Java Virtual Machine creates Class
- * objects.
+ * Private constructor. Only the Java Virtual Machine creates Class objects.
+ * This constructor is not used and prevents the default constructor being
+ * generated.
*/
private Class() {}
@@ -268,6 +267,76 @@ public final
+ getName();
}
+ /**
+ * Returns a string describing this {@code Class}, including
+ * information about modifiers and type parameters.
+ *
+ * The string is formatted as a list of type modifiers, if any,
+ * followed by the kind of type (empty string for primitive types
+ * and {@code class}, {@code enum}, {@code interface}, or
+ * <code>&#64;</code>{@code interface}, as appropriate), followed
+ * by the type's name, followed by an angle-bracketed
+ * comma-separated list of the type's type parameters, if any.
+ *
+ * A space is used to separate modifiers from one another and to
+ * separate any modifiers from the kind of type. The modifiers
+ * occur in canonical order. If there are no type parameters, the
+ * type parameter list is elided.
+ *
+ * <p>Note that since information about the runtime representation
+ * of a type is being generated, modifiers not present on the
+ * originating source code or illegal on the originating source
+ * code may be present.
+ *
+ * @return a string describing this {@code Class}, including
+ * information about modifiers and type parameters
+ *
+ * @since 1.8
+ * @hide Hidden pending tests
+ */
+ public String toGenericString() {
+ if (isPrimitive()) {
+ return toString();
+ } else {
+ StringBuilder sb = new StringBuilder();
+
+ // Class modifiers are a superset of interface modifiers
+ int modifiers = getModifiers() & Modifier.classModifiers();
+ if (modifiers != 0) {
+ sb.append(Modifier.toString(modifiers));
+ sb.append(' ');
+ }
+
+ if (isAnnotation()) {
+ sb.append('@');
+ }
+ if (isInterface()) { // Note: all annotation types are interfaces
+ sb.append("interface");
+ } else {
+ if (isEnum())
+ sb.append("enum");
+ else
+ sb.append("class");
+ }
+ sb.append(' ');
+ sb.append(getName());
+
+ TypeVariable<?>[] typeparms = getTypeParameters();
+ if (typeparms.length > 0) {
+ boolean first = true;
+ sb.append('<');
+ for(TypeVariable<?> typeparm: typeparms) {
+ if (!first)
+ sb.append(',');
+ sb.append(typeparm.getTypeName());
+ first = false;
+ }
+ sb.append('>');
+ }
+
+ return sb.toString();
+ }
+ }
/**
* Returns the {@code Class} object associated with the class or
@@ -353,7 +422,8 @@ public final
* ensure it's ok to access the bootstrap class loader.
*
* @param name fully qualified name of the desired class
- * @param initialize whether the class must be initialized
+ * @param initialize if {@code true} the class will be initialized.
+ * See Section 12.4 of <em>The Java Language Specification</em>.
* @param loader class loader from which the class must be loaded
* @return class object representing the desired class
*
@@ -408,36 +478,24 @@ public final
* any exception thrown by the constructor in a (checked) {@link
* java.lang.reflect.InvocationTargetException}.
*
- * @return a newly allocated instance of the class represented by this
- * object.
- * @exception IllegalAccessException if the class or its nullary
- * constructor is not accessible.
- * @exception InstantiationException
- * if this {@code Class} represents an abstract class,
- * an interface, an array class, a primitive type, or void;
- * or if the class has no nullary constructor;
- * or if the instantiation fails for some other reason.
- * @exception ExceptionInInitializerError if the initialization
- * provoked by this method fails.
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * creation of new instances of this class
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
- *
+ * @return a newly allocated instance of the class represented by this
+ * object.
+ * @throws IllegalAccessException if the class or its nullary
+ * constructor is not accessible.
+ * @throws InstantiationException
+ * if this {@code Class} represents an abstract class,
+ * an interface, an array class, a primitive type, or void;
+ * or if the class has no nullary constructor;
+ * or if the instantiation fails for some other reason.
+ * @throws ExceptionInInitializerError if the initialization
+ * provoked by this method fails.
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
*/
public native T newInstance() throws InstantiationException, IllegalAccessException;
@@ -466,16 +524,16 @@ public final
* this {@code Class} object represents a primitive type, this method
* returns {@code false}.
*
- * @param object the object to check
+ * @param obj the object to check
* @return true if {@code obj} is an instance of this class
*
* @since JDK1.1
*/
- public boolean isInstance(Object object) {
- if (object == null) {
+ public boolean isInstance(Object obj) {
+ if (obj == null) {
return false;
}
- return isAssignableFrom(object.getClass());
+ return isAssignableFrom(obj.getClass());
}
@@ -496,23 +554,23 @@ public final
* or via a widening reference conversion. See <em>The Java Language
* Specification</em>, sections 5.1.1 and 5.1.4 , for details.
*
- * @param c the {@code Class} object to be checked
+ * @param cls the {@code Class} object to be checked
* @return the {@code boolean} value indicating whether objects of the
* type {@code cls} can be assigned to objects of this class
* @exception NullPointerException if the specified Class parameter is
* null.
* @since JDK1.1
*/
- public boolean isAssignableFrom(Class<?> c) {
- if (this == c) {
+ public boolean isAssignableFrom(Class<?> cls) {
+ if (this == cls) {
return true; // Can always assign to things of the same type.
} else if (this == Object.class) {
- return !c.isPrimitive(); // Can assign any reference to java.lang.Object.
+ return !cls.isPrimitive(); // Can assign any reference to java.lang.Object.
} else if (isArray()) {
- return c.isArray() && componentType.isAssignableFrom(c.componentType);
+ return cls.isArray() && componentType.isAssignableFrom(cls.componentType);
} else if (isInterface()) {
// Search iftable which has a flattened and uniqued list of interfaces.
- Object[] iftable = c.ifTable;
+ Object[] iftable = cls.ifTable;
if (iftable != null) {
for (int i = 0; i < iftable.length; i += 2) {
if (iftable[i] == this) {
@@ -522,9 +580,9 @@ public final
}
return false;
} else {
- if (!c.isInterface()) {
- for (c = c.superClass; c != null; c = c.superClass) {
- if (c == this) {
+ if (!cls.isInterface()) {
+ for (cls = cls.superClass; cls != null; cls = cls.superClass) {
+ if (cls == this) {
return true;
}
}
@@ -617,6 +675,7 @@ public final
* returns {@code false} otherwise.
* @return {@code true} if and only if this class is a synthetic class as
* defined by the Java Language Specification.
+ * @jls 13.1 The Form of a Binary
* @since 1.5
*/
public boolean isSynthetic() {
@@ -731,7 +790,8 @@ public final
* <cite>The Java&trade; Virtual Machine Specification</cite>
* @since 1.5
*/
- @Override public synchronized TypeVariable<Class<T>>[] getTypeParameters() {
+ @Override
+ public synchronized TypeVariable<Class<T>>[] getTypeParameters() {
String annotationSignature = getSignatureAttribute();
if (annotationSignature == null) {
return EmptyArray.TYPE_VARIABLE;
@@ -741,6 +801,7 @@ public final
return parser.formalTypeParameters;
}
+
/**
* Returns the {@code Class} representing the superclass of the entity
* (class, interface, primitive type or void) represented by this
@@ -883,6 +944,10 @@ public final
* <p> If this object represents a primitive type or void, the method
* returns an array of length 0.
*
+ * <p> If this {@code Class} object represents an array type, the
+ * interfaces {@code Cloneable} and {@code java.io.Serializable} are
+ * returned in that order.
+ *
* @return an array of interfaces implemented by this class.
*/
public Class<?>[] getInterfaces() {
@@ -1064,6 +1129,7 @@ public final
* that class is a local or anonymous class; otherwise {@code null}.
* @since 1.5
*/
+ // ANDROID-CHANGED: Removed SecurityException
public Method getEnclosingMethod() {
if (classNameImpliesTopLevel()) {
return null;
@@ -1085,6 +1151,7 @@ public final
* that class is a local or anonymous class; otherwise {@code null}.
* @since 1.5
*/
+ // ANDROID-CHANGED: Removed SecurityException
public Constructor<?> getEnclosingConstructor() {
if (classNameImpliesTopLevel()) {
return null;
@@ -1098,6 +1165,7 @@ public final
return !getName().contains("$");
}
+
/**
* If the class or interface represented by this {@code Class} object
* is a member of another class, returns the {@code Class} object
@@ -1109,6 +1177,7 @@ public final
* @return the declaring class for this class
* @since JDK1.1
*/
+ // ANDROID-CHANGED: Removed SecurityException
public native Class<?> getDeclaringClass();
/**
@@ -1118,6 +1187,7 @@ public final
* @return the immediately enclosing class of the underlying class
* @since 1.5
*/
+ // ANDROID-CHANGED: Removed SecurityException
public native Class<?> getEnclosingClass();
/**
@@ -1158,7 +1228,6 @@ public final
return simpleName;
}
-
/**
* Return an informative string for the name of this type.
*
@@ -1271,25 +1340,7 @@ public final
* class, or void.
*
* @return the array of {@code Class} objects representing the public
- * members of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} method
- * denies access to the classes within this class
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * members of this class
*
* @since JDK1.1
*/
@@ -1310,45 +1361,38 @@ public final
/**
* Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
- * this {@code Class} object. The elements in the array returned are
- * not sorted and are not in any particular order. This method returns an
- * array of length 0 if the class or interface has no accessible public
- * fields, or if it represents an array class, a primitive type, or void.
- *
- * <p> Specifically, if this {@code Class} object represents a class,
- * this method returns the public fields of this class and of all its
- * superclasses. If this {@code Class} object represents an
- * interface, this method returns the fields of this interface and of all
- * its superinterfaces.
+ * this {@code Class} object.
*
- * <p> The implicit length field for array class is not reflected by this
- * method. User code should use the methods of class {@code Array} to
- * manipulate arrays.
- *
- * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
+ * <p> If this {@code Class} object represents a class or interface with no
+ * no accessible public fields, then this method returns an array of length
+ * 0.
*
- * @return the array of {@code Field} objects representing the
- * public fields
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * <p> If this {@code Class} object represents a class, then this method
+ * returns the public fields of the class and of all its superclasses.
*
- * <ul>
+ * <p> If this {@code Class} object represents an interface, then this
+ * method returns the fields of the interface and of all its
+ * superinterfaces.
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the fields within this class
+ * <p> If this {@code Class} object represents an array type, a primitive
+ * type, or void, then this method returns an array of length 0.
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <p> The elements in the returned array are not sorted and are not in any
+ * particular order.
*
- * </ul>
+ * @return the array of {@code Field} objects representing the
+ * public fields
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
*
* @since JDK1.1
+ * @jls 8.2 Class Members
+ * @jls 8.3 Field Declarations
*/
@CallerSensitive
public Field[] getFields() throws SecurityException {
@@ -1377,45 +1421,54 @@ public final
}
/**
- * Returns an array containing {@code Method} objects reflecting all
- * the public <em>member</em> methods of the class or interface represented
- * by this {@code Class} object, including those declared by the class
- * or interface and those inherited from superclasses and
- * superinterfaces. Array classes return all the (public) member methods
- * inherited from the {@code Object} class. The elements in the array
- * returned are not sorted and are not in any particular order. This
- * method returns an array of length 0 if this {@code Class} object
- * represents a class or interface that has no public member methods, or if
- * this {@code Class} object represents a primitive type or void.
+ * Returns an array containing {@code Method} objects reflecting all the
+ * public methods of the class or interface represented by this {@code
+ * Class} object, including those declared by the class or interface and
+ * those inherited from superclasses and superinterfaces.
*
- * <p> The class initialization method {@code <clinit>} is not
- * included in the returned array. If the class declares multiple public
- * member methods with the same parameter types, they are all included in
- * the returned array.
+ * <p> If this {@code Class} object represents a type that has multiple
+ * public methods with the same name and parameter types, but different
+ * return types, then the returned array has a {@code Method} object for
+ * each such method.
*
- * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
+ * <p> If this {@code Class} object represents a type with a class
+ * initialization method {@code <clinit>}, then the returned array does
+ * <em>not</em> have a corresponding {@code Method} object.
*
- * @return the array of {@code Method} objects representing the
- * public methods of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * <p> If this {@code Class} object represents an array type, then the
+ * returned array has a {@code Method} object for each of the public
+ * methods inherited by the array type from {@code Object}. It does not
+ * contain a {@code Method} object for {@code clone()}.
*
- * <ul>
+ * <p> If this {@code Class} object represents an interface then the
+ * returned array does not contain any implicitly declared methods from
+ * {@code Object}. Therefore, if no methods are explicitly declared in
+ * this interface or any of its superinterfaces then the returned array
+ * has length 0. (Note that a {@code Class} object which represents a class
+ * always has public methods, inherited from {@code Object}.)
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the methods within this class
+ * <p> If this {@code Class} object represents a primitive type or void,
+ * then the returned array has length 0.
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <p> Static methods declared in superinterfaces of the class or interface
+ * represented by this {@code Class} object are not considered members of
+ * the class or interface.
*
- * </ul>
+ * <p> The elements in the returned array are not sorted and are not in any
+ * particular order.
*
+ * @return the array of {@code Method} objects representing the
+ * public methods of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
+ *
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -1471,25 +1524,14 @@ public final
* {@code Constructor<T>[]}.
*
* @return the array of {@code Constructor} objects representing the
- * public constructors of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the constructors within this class
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * public constructors of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
*
* @since JDK1.1
*/
@@ -1498,14 +1540,16 @@ public final
return getDeclaredConstructorsInternal(true);
}
+
/**
- * Returns a {@code Field} object that reflects the specified public
- * member field of the class or interface represented by this
- * {@code Class} object. The {@code name} parameter is a
- * {@code String} specifying the simple name of the desired field.
+ * Returns a {@code Field} object that reflects the specified public member
+ * field of the class or interface represented by this {@code Class}
+ * object. The {@code name} parameter is a {@code String} specifying the
+ * simple name of the desired field.
*
* <p> The field to be reflected is determined by the algorithm that
- * follows. Let C be the class represented by this object:
+ * follows. Let C be the class or interface represented by this object:
+ *
* <OL>
* <LI> If C declares a public field with the name specified, that is the
* field to be reflected.</LI>
@@ -1518,36 +1562,30 @@ public final
* is thrown.</LI>
* </OL>
*
- * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
+ * <p> If this {@code Class} object represents an array type, then this
+ * method does not find the {@code length} field of the array type.
*
* @param name the field name
- * @return the {@code Field} object of this class specified by
- * {@code name}
- * @exception NoSuchFieldException if a field with the specified name is
- * not found.
- * @exception NullPointerException if {@code name} is {@code null}
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the field
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * @return the {@code Field} object of this class specified by
+ * {@code name}
+ * @throws NoSuchFieldException if a field with the specified name is
+ * not found.
+ * @throws NullPointerException if {@code name} is {@code null}
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
*
* @since JDK1.1
+ * @jls 8.2 Class Members
+ * @jls 8.3 Field Declarations
*/
- public Field getField(String name) throws NoSuchFieldException {
+ // ANDROID-CHANGED: Removed SecurityException
+ public Field getField(String name)
+ throws NoSuchFieldException {
if (name == null) {
throw new NullPointerException("name == null");
}
@@ -1577,25 +1615,29 @@ public final
* order. If {@code parameterTypes} is {@code null}, it is
* treated as if it were an empty array.
*
- * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
+ * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
* {@code NoSuchMethodException} is raised. Otherwise, the method to
* be reflected is determined by the algorithm that follows. Let C be the
- * class represented by this object:
+ * class or interface represented by this object:
* <OL>
- * <LI> C is searched for any <I>matching methods</I>. If no matching
- * method is found, the algorithm of step 1 is invoked recursively on
- * the superclass of C.</LI>
- * <LI> If no method was found in step 1 above, the superinterfaces of C
- * are searched for a matching method. If any such method is found, it
- * is reflected.</LI>
+ * <LI> C is searched for a <I>matching method</I>, as defined below. If a
+ * matching method is found, it is reflected.</LI>
+ * <LI> If no matching method is found by step 1 then:
+ * <OL TYPE="a">
+ * <LI> If C is a class other than {@code Object}, then this algorithm is
+ * invoked recursively on the superclass of C.</LI>
+ * <LI> If C is the class {@code Object}, or if C is an interface, then
+ * the superinterfaces of C (if any) are searched for a matching
+ * method. If any such method is found, it is reflected.</LI>
+ * </OL></LI>
* </OL>
*
- * To find a matching method in a class C:&nbsp; If C declares exactly one
- * public method with the specified name and exactly the same formal
- * parameter types, that is the method reflected. If more than one such
- * method is found in C, and one of these methods has a return type that is
- * more specific than any of the others, that method is reflected;
- * otherwise one of the methods is chosen arbitrarily.
+ * <p> To find a matching method in a class or interface C:&nbsp; If C
+ * declares exactly one public method with the specified name and exactly
+ * the same formal parameter types, that is the method reflected. If more
+ * than one such method is found in C, and one of these methods has a
+ * return type that is more specific than any of the others, that method is
+ * reflected; otherwise one of the methods is chosen arbitrarily.
*
* <p>Note that there may be more than one matching method in a
* class because while the Java language forbids a class to
@@ -1608,34 +1650,30 @@ public final
* method and the method being overridden would have the same
* signature but different return types.
*
- * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
+ * <p> If this {@code Class} object represents an array type, then this
+ * method does not find the {@code clone()} method.
+ *
+ * <p> Static methods declared in superinterfaces of the class or interface
+ * represented by this {@code Class} object are not considered members of
+ * the class or interface.
*
* @param name the name of the method
* @param parameterTypes the list of parameters
* @return the {@code Method} object that matches the specified
- * {@code name} and {@code parameterTypes}
- * @exception NoSuchMethodException if a matching method is not found
- * or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
- * @exception NullPointerException if {@code name} is {@code null}
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the method
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
- *
+ * {@code name} and {@code parameterTypes}
+ * @throws NoSuchMethodException if a matching method is not found
+ * or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
+ * @throws NullPointerException if {@code name} is {@code null}
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
+ *
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -1662,26 +1700,15 @@ public final
*
* @param parameterTypes the parameter array
* @return the {@code Constructor} object of the public constructor that
- * matches the specified {@code parameterTypes}
- * @exception NoSuchMethodException if a matching method is not found.
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.PUBLIC)} denies
- * access to the constructor
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * matches the specified {@code parameterTypes}
+ * @throws NoSuchMethodException if a matching method is not found.
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and
+ * the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class.
*
* @since JDK1.1
*/
@@ -1690,6 +1717,7 @@ public final
return getConstructor0(parameterTypes, Member.PUBLIC);
}
+
/**
* Returns an array of {@code Class} objects reflecting all the
* classes and interfaces declared as members of the class represented by
@@ -1701,28 +1729,31 @@ public final
* primitive type, an array class, or void.
*
* @return the array of {@code Class} objects representing all the
- * declared members of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * declared members of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
*
- * <ul>
+ * <ul>
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared classes within this class
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared classes within this class
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
*
- * </ul>
+ * </ul>
*
* @since JDK1.1
*/
+ // ANDROID-CHANGED: Removed SecurityException
public native Class<?>[] getDeclaredClasses();
/**
@@ -1730,36 +1761,44 @@ public final
* declared by the class or interface represented by this
* {@code Class} object. This includes public, protected, default
* (package) access, and private fields, but excludes inherited fields.
- * The elements in the array returned are not sorted and are not in any
- * particular order. This method returns an array of length 0 if the class
- * or interface declares no fields, or if this {@code Class} object
- * represents a primitive type, an array class, or void.
*
- * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
+ * <p> If this {@code Class} object represents a class or interface with no
+ * declared fields, then this method returns an array of length 0.
+ *
+ * <p> If this {@code Class} object represents an array type, a primitive
+ * type, or void, then this method returns an array of length 0.
+ *
+ * <p> The elements in the returned array are not sorted and are not in any
+ * particular order.
*
- * @return the array of {@code Field} objects representing all the
- * declared fields of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * @return the array of {@code Field} objects representing all the
+ * declared fields of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
*
- * <ul>
+ * <ul>
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared fields within this class
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared fields within this class
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
*
- * </ul>
+ * </ul>
*
* @since JDK1.1
+ * @jls 8.2 Class Members
+ * @jls 8.3 Field Declarations
*/
+ // ANDROID-CHANGED: Removed SecurityException
public native Field[] getDeclaredFields();
/**
@@ -1772,42 +1811,55 @@ public final
public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly);
/**
- * Returns an array of {@code Method} objects reflecting all the
- * methods declared by the class or interface represented by this
- * {@code Class} object. This includes public, protected, default
- * (package) access, and private methods, but excludes inherited methods.
- * The elements in the array returned are not sorted and are not in any
- * particular order. This method returns an array of length 0 if the class
- * or interface declares no methods, or if this {@code Class} object
- * represents a primitive type, an array class, or void. The class
- * initialization method {@code <clinit>} is not included in the
- * returned array. If the class declares multiple public member methods
- * with the same parameter types, they are all included in the returned
- * array.
*
- * <p> See <em>The Java Language Specification</em>, section 8.2.
+ * Returns an array containing {@code Method} objects reflecting all the
+ * declared methods of the class or interface represented by this {@code
+ * Class} object, including public, protected, default (package)
+ * access, and private methods, but excluding inherited methods.
*
- * @return the array of {@code Method} objects representing all the
- * declared methods of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * <p> If this {@code Class} object represents a type that has multiple
+ * declared methods with the same name and parameter types, but different
+ * return types, then the returned array has a {@code Method} object for
+ * each such method.
*
- * <ul>
+ * <p> If this {@code Class} object represents a type that has a class
+ * initialization method {@code <clinit>}, then the returned array does
+ * <em>not</em> have a corresponding {@code Method} object.
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared methods within this class
+ * <p> If this {@code Class} object represents a class or interface with no
+ * declared methods, then the returned array has length 0.
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <p> If this {@code Class} object represents an array type, a primitive
+ * type, or void, then the returned array has length 0.
*
- * </ul>
+ * <p> The elements in the returned array are not sorted and are not in any
+ * particular order.
*
+ * @return the array of {@code Method} objects representing all the
+ * declared methods of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
+ *
+ * <ul>
+ *
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared methods within this class
+ *
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
+ *
+ * </ul>
+ *
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
public Method[] getDeclaredMethods() throws SecurityException {
@@ -1842,26 +1894,28 @@ public final
*
* <p> See <em>The Java Language Specification</em>, section 8.2.
*
- * @return the array of {@code Constructor} objects representing all the
- * declared constructors of this class
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
+ * @return the array of {@code Constructor} objects representing all the
+ * declared constructors of this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
*
- * <ul>
+ * <ul>
*
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared constructors within this class
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared constructors within this class
*
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
*
- * </ul>
+ * </ul>
*
* @since JDK1.1
*/
@@ -1869,6 +1923,7 @@ public final
return getDeclaredConstructorsInternal(false);
}
+
/**
* Returns the constructor with the given parameters if it is defined by this class;
* {@code null} otherwise. This may return a non-public member.
@@ -1878,37 +1933,44 @@ public final
/**
* Returns a {@code Field} object that reflects the specified declared
* field of the class or interface represented by this {@code Class}
- * object. The {@code name} parameter is a {@code String} that
- * specifies the simple name of the desired field. Note that this method
- * will not reflect the {@code length} field of an array class.
+ * object. The {@code name} parameter is a {@code String} that specifies
+ * the simple name of the desired field.
+ *
+ * <p> If this {@code Class} object represents an array type, then this
+ * method does not find the {@code length} field of the array type.
*
* @param name the name of the field
- * @return the {@code Field} object for the specified field in this
- * class
- * @exception NoSuchFieldException if a field with the specified name is
- * not found.
- * @exception NullPointerException if {@code name} is {@code null}
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared field
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * @return the {@code Field} object for the specified field in this
+ * class
+ * @throws NoSuchFieldException if a field with the specified name is
+ * not found.
+ * @throws NullPointerException if {@code name} is {@code null}
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
+ *
+ * <ul>
+ *
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared field
+ *
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
+ *
+ * </ul>
*
* @since JDK1.1
+ * @jls 8.2 Class Members
+ * @jls 8.3 Field Declarations
*/
+ // ANDROID-CHANGED: Removed SecurityException
public native Field getDeclaredField(String name) throws NoSuchFieldException;
/**
@@ -1930,31 +1992,38 @@ public final
* name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
* is raised.
*
+ * <p> If this {@code Class} object represents an array type, then this
+ * method does not find the {@code clone()} method.
+ *
* @param name the name of the method
* @param parameterTypes the parameter array
- * @return the {@code Method} object for the method of this class
- * matching the specified name and parameters
- * @exception NoSuchMethodException if a matching method is not found.
- * @exception NullPointerException if {@code name} is {@code null}
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared method
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
- *
+ * @return the {@code Method} object for the method of this class
+ * matching the specified name and parameters
+ * @throws NoSuchMethodException if a matching method is not found.
+ * @throws NullPointerException if {@code name} is {@code null}
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
+ *
+ * <ul>
+ *
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared method
+ *
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
+ *
+ * </ul>
+ *
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -2023,27 +2092,29 @@ public final
* include the explicit enclosing instance as the first parameter.
*
* @param parameterTypes the parameter array
- * @return The {@code Constructor} object for the constructor with the
- * specified parameter list
- * @exception NoSuchMethodException if a matching method is not found.
- * @exception SecurityException
- * If a security manager, <i>s</i>, is present and any of the
- * following conditions is met:
- *
- * <ul>
- *
- * <li> invocation of
- * {@link SecurityManager#checkMemberAccess
- * s.checkMemberAccess(this, Member.DECLARED)} denies
- * access to the declared constructor
- *
- * <li> the caller's class loader is not the same as or an
- * ancestor of the class loader for the current class and
- * invocation of {@link SecurityManager#checkPackageAccess
- * s.checkPackageAccess()} denies access to the package
- * of this class
- *
- * </ul>
+ * @return The {@code Constructor} object for the constructor with the
+ * specified parameter list
+ * @throws NoSuchMethodException if a matching method is not found.
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and any of the
+ * following conditions is met:
+ *
+ * <ul>
+ *
+ * <li> the caller's class loader is not the same as the
+ * class loader of this class and invocation of
+ * {@link SecurityManager#checkPermission
+ * s.checkPermission} method with
+ * {@code RuntimePermission("accessDeclaredMembers")}
+ * denies access to the declared constructor
+ *
+ * <li> the caller's class loader is not the same as or an
+ * ancestor of the class loader for the current class and
+ * invocation of {@link SecurityManager#checkPackageAccess
+ * s.checkPackageAccess()} denies access to the package
+ * of this class
+ *
+ * </ul>
*
* @since JDK1.1
*/
@@ -2209,6 +2280,10 @@ public final
return result;
}
+ /** use serialVersionUID from JDK 1.1 for interoperability */
+ private static final long serialVersionUID = 3206093459760846163L;
+
+
/**
* Returns the constructor with the given parameters if it is defined by this class;
* {@code null} otherwise. This may return a non-public member.
@@ -2306,6 +2381,7 @@ public final
*
* @since 1.5
*/
+ @SuppressWarnings("unchecked")
public T cast(Object obj) {
if (obj != null && !isInstance(obj))
throw new ClassCastException(cannotCastMsg(obj));
@@ -2318,7 +2394,7 @@ public final
/**
* Casts this {@code Class} object to represent a subclass of the class
- * represented by the specified class object. Checks that that the cast
+ * represented by the specified class object. Checks that the cast
* is valid, and throws a {@code ClassCastException} if it is not. If
* this method succeeds, it always returns a reference to this class object.
*
@@ -2329,6 +2405,8 @@ public final
* could not be checked at runtime (because generic types are implemented
* by erasure).
*
+ * @param <U> the type to cast this class object to
+ * @param clazz the class of the type to cast this class object to
* @return this {@code Class} object, cast to represent a subclass of
* the specified class object.
* @throws ClassCastException if this {@code Class} object does not
@@ -2336,6 +2414,7 @@ public final
* the class itself).
* @since 1.5
*/
+ @SuppressWarnings("unchecked")
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
if (clazz.isAssignableFrom(this))
return (Class<? extends U>) this;
@@ -2348,9 +2427,9 @@ public final
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
+ @SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
- if (annotationClass == null)
- throw new NullPointerException();
+ Objects.requireNonNull(annotationClass);
A annotation = getDeclaredAnnotation(annotationClass);
if (annotation != null) {
@@ -2369,18 +2448,24 @@ public final
return null;
}
- @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
- if (annotationType == null) {
- throw new NullPointerException("annotationType == null");
+ /**
+ * {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @since 1.5
+ */
+ @Override
+ public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
+ if (annotationClass == null) {
+ throw new NullPointerException("annotationClass == null");
}
- if (isDeclaredAnnotationPresent(annotationType)) {
+ if (isDeclaredAnnotationPresent(annotationClass)) {
return true;
}
- if (annotationType.isDeclaredAnnotationPresent(Inherited.class)) {
+ if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
- if (sup.isDeclaredAnnotationPresent(annotationType)) {
+ if (sup.isDeclaredAnnotationPresent(annotationClass)) {
return true;
}
}
@@ -2390,12 +2475,42 @@ public final
}
/**
- * Returns an array containing all the annotations of this class. If there are no annotations
- * then an empty array is returned.
- *
- * @see #getDeclaredAnnotations()
+ * @throws NullPointerException {@inheritDoc}
+ * @since 1.8
*/
- @Override public Annotation[] getAnnotations() {
+ @Override
+ public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
+ // Find any associated annotations [directly or repeatably (indirectly) present on this].
+ A[] annotations = GenericDeclaration.super.getAnnotationsByType(annotationClass);
+
+ if (annotations.length != 0) {
+ return annotations;
+ }
+
+ // Nothing was found, attempt looking for associated annotations recursively up to the root
+ // class if and only if:
+ // * The annotation class was marked with @Inherited.
+ //
+ // Inherited annotations are not coalesced into a single set: the first declaration found is
+ // returned.
+
+ if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
+ Class<?> superClass = getSuperclass(); // Returns null if klass's base is Object.
+
+ if (superClass != null) {
+ return superClass.getAnnotationsByType(annotationClass);
+ }
+ }
+
+ // Annotated was not marked with @Inherited, or no superclass.
+ return (A[]) Array.newInstance(annotationClass, 0); // Safe by construction.
+ }
+
+ /**
+ * @since 1.5
+ */
+ @Override
+ public Annotation[] getAnnotations() {
/*
* We need to get the annotations declared on this class, plus the
* annotations from superclasses that have the "@Inherited" annotation
@@ -2426,54 +2541,17 @@ public final
}
/**
- * Returns the annotations that are directly defined on the class
- * represented by this {@code Class}. Annotations that are inherited are not
- * included in the result. If there are no annotations at all, an empty
- * array is returned.
- *
- * @see #getAnnotations()
- */
- @Override public native Annotation[] getDeclaredAnnotations();
-
- /**
- * {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
* @since 1.8
*/
@Override
- public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
- // Find any associated annotations [directly or repeatably (indirectly) present on this].
- T[] annotations = AnnotatedElement.super.getAnnotationsByType(annotationClass);
-
- if (annotations.length != 0) {
- return annotations;
- }
-
- // Nothing was found, attempt looking for associated annotations recursively up to the root
- // class if and only if:
- // * The annotation class was marked with @Inherited.
- //
- // Inherited annotations are not coalesced into a single set: the first declaration found is
- // returned.
-
- if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
- Class<?> superClass = getSuperclass(); // Returns null if klass's base is Object.
-
- if (superClass != null) {
- return superClass.getAnnotationsByType(annotationClass);
- }
- }
-
- // Annotated was not marked with @Inherited, or no superclass.
- return (T[]) Array.newInstance(annotationClass, 0); // Safe by construction.
- }
+ public native <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass);
/**
- * {@inheritDoc}
- *
- * @since 1.8
+ * @since 1.5
*/
@Override
- public native <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass);
+ public native Annotation[] getDeclaredAnnotations();
/**
* Returns true if the annotation exists.
diff --git a/ojluni/src/main/java/java/lang/reflect/AccessibleObject.java b/ojluni/src/main/java/java/lang/reflect/AccessibleObject.java
index 22ecfb7489..08fc8cd638 100644
--- a/ojluni/src/main/java/java/lang/reflect/AccessibleObject.java
+++ b/ojluni/src/main/java/java/lang/reflect/AccessibleObject.java
@@ -122,8 +122,9 @@ public class AccessibleObject implements AnnotatedElement {
{
if (obj instanceof Constructor && flag == true) {
Constructor<?> c = (Constructor<?>)obj;
+ // Android-changed: Added additional checks below.
Class<?> clazz = c.getDeclaringClass();
- if (clazz == Class.class) {
+ if (c.getDeclaringClass() == Class.class) {
throw new SecurityException("Can not make a java.lang.Class" +
" constructor accessible");
} else if (clazz == Method.class) {
@@ -159,9 +160,6 @@ public class AccessibleObject implements AnnotatedElement {
// outside this package.
boolean override;
- // Reflection factory used by subclasses for creating field,
- // method, and constructor accessors. Note that this is called
- // very early in the bootstrapping process.
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.5
diff --git a/ojluni/src/main/java/java/lang/reflect/AnnotatedElement.java b/ojluni/src/main/java/java/lang/reflect/AnnotatedElement.java
index 96be4e4b4d..ddfbc1d7e2 100644
--- a/ojluni/src/main/java/java/lang/reflect/AnnotatedElement.java
+++ b/ojluni/src/main/java/java/lang/reflect/AnnotatedElement.java
@@ -30,13 +30,175 @@ import java.lang.annotation.Annotation;
import java.util.Objects;
import libcore.reflect.AnnotatedElements;
+// Android-changed: Removed some references to bytecode spec below that do not
+// apply and added a note about annotation ordering.
/**
* Represents an annotated element of the program currently running in this
* VM. This interface allows annotations to be read reflectively. All
* annotations returned by methods in this interface are immutable and
- * serializable. It is permissible for the caller to modify the
- * arrays returned by accessors for array-valued enum members; it will
- * have no affect on the arrays returned to other callers.
+ * serializable. The arrays returned by methods of this interface may be modified
+ * by callers without affecting the arrays returned to other callers.
+ *
+ * <p>Android note: methods that return multiple annotations of different types such as
+ * {@link #getAnnotations()} and {@link #getDeclaredAnnotations()} can be affected
+ * by the explicit character-code ordering of annotations types specified by the DEX format.
+ * Annotations of different types on a single element are not guaranteed to be returned in the order
+ * they are declared in source.
+ *
+ * <p>The {@link #getAnnotationsByType(Class)} and {@link
+ * #getDeclaredAnnotationsByType(Class)} methods support multiple
+ * annotations of the same type on an element. If the argument to
+ * either method is a repeatable annotation type (JLS 9.6), then the
+ * method will "look through" a container annotation (JLS 9.7), if
+ * present, and return any annotations inside the container. Container
+ * annotations may be generated at compile-time to wrap multiple
+ * annotations of the argument type.
+ *
+ * <p>The terms <em>directly present</em>, <em>indirectly present</em>,
+ * <em>present</em>, and <em>associated</em> are used throughout this
+ * interface to describe precisely which annotations are returned by
+ * methods:
+ *
+ * <ul>
+ *
+ * <li> An annotation <i>A</i> is <em>directly present</em> on an
+ * element <i>E</i> if <i>E</i> is annotated by <i>A</i> in the original source.
+ *
+ * <li>An annotation <i>A</i> is <em>indirectly present</em> on an
+ * element <i>E</i> if <i>E</i> is annotated by a container annotation
+ * of <i>A</i>.
+ *
+ * <li>An annotation <i>A</i> is present on an element <i>E</i> if either:
+ *
+ * <ul>
+ *
+ * <li><i>A</i> is directly present on <i>E</i>; or
+ *
+ * <li>No annotation of <i>A</i> 's type is directly present on
+ * <i>E</i>, and <i>E</i> is a class, and <i>A</i> 's type is
+ * inheritable, and <i>A</i> is present on the superclass of <i>E</i>.
+ *
+ * </ul>
+ *
+ * <li>An annotation <i>A</i> is <em>associated</em> with an element <i>E</i>
+ * if either:
+ *
+ * <ul>
+ *
+ * <li><i>A</i> is directly or indirectly present on <i>E</i>; or
+ *
+ * <li>No annotation of <i>A</i> 's type is directly or indirectly
+ * present on <i>E</i>, and <i>E</i> is a class, and <i>A</i>'s type
+ * is inheritable, and <i>A</i> is associated with the superclass of
+ * <i>E</i>.
+ *
+ * </ul>
+ *
+ * </ul>
+ *
+ * <p>The table below summarizes which kind of annotation presence
+ * different methods in this interface examine.
+ *
+ * <table border>
+ * <caption>Overview of kind of presence detected by different AnnotatedElement methods</caption>
+ * <tr><th colspan=2></th><th colspan=4>Kind of Presence</th>
+ * <tr><th colspan=2>Method</th><th>Directly Present</th><th>Indirectly Present</th><th>Present</th><th>Associated</th>
+ * <tr><td align=right>{@code T}</td><td>{@link #getAnnotation(Class) getAnnotation(Class&lt;T&gt;)}
+ * <td></td><td></td><td>X</td><td></td>
+ * </tr>
+ * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getAnnotations getAnnotations()}
+ * <td></td><td></td><td>X</td><td></td>
+ * </tr>
+ * <tr><td align=right>{@code T[]}</td><td>{@link #getAnnotationsByType(Class) getAnnotationsByType(Class&lt;T&gt;)}
+ * <td></td><td></td><td></td><td>X</td>
+ * </tr>
+ * <tr><td align=right>{@code T}</td><td>{@link #getDeclaredAnnotation(Class) getDeclaredAnnotation(Class&lt;T&gt;)}
+ * <td>X</td><td></td><td></td><td></td>
+ * </tr>
+ * <tr><td align=right>{@code Annotation[]}</td><td>{@link #getDeclaredAnnotations getDeclaredAnnotations()}
+ * <td>X</td><td></td><td></td><td></td>
+ * </tr>
+ * <tr><td align=right>{@code T[]}</td><td>{@link #getDeclaredAnnotationsByType(Class) getDeclaredAnnotationsByType(Class&lt;T&gt;)}
+ * <td>X</td><td>X</td><td></td><td></td>
+ * </tr>
+ * </table>
+ *
+ * <p>For an invocation of {@code get[Declared]AnnotationsByType( Class <
+ * T >)}, the order of annotations which are directly or indirectly
+ * present on an element <i>E</i> is computed as if indirectly present
+ * annotations on <i>E</i> are directly present on <i>E</i> in place
+ * of their container annotation, in the order in which they appear in
+ * the value element of the container annotation.
+ *
+ * <p>There are several compatibility concerns to keep in mind if an
+ * annotation type <i>T</i> is originally <em>not</em> repeatable and
+ * later modified to be repeatable.
+ *
+ * The containing annotation type for <i>T</i> is <i>TC</i>.
+ *
+ * <ul>
+ *
+ * <li>Modifying <i>T</i> to be repeatable is source and binary
+ * compatible with existing uses of <i>T</i> and with existing uses
+ * of <i>TC</i>.
+ *
+ * That is, for source compatibility, source code with annotations of
+ * type <i>T</i> or of type <i>TC</i> will still compile. For binary
+ * compatibility, class files with annotations of type <i>T</i> or of
+ * type <i>TC</i> (or with other kinds of uses of type <i>T</i> or of
+ * type <i>TC</i>) will link against the modified version of <i>T</i>
+ * if they linked against the earlier version.
+ *
+ * (An annotation type <i>TC</i> may informally serve as an acting
+ * containing annotation type before <i>T</i> is modified to be
+ * formally repeatable. Alternatively, when <i>T</i> is made
+ * repeatable, <i>TC</i> can be introduced as a new type.)
+ *
+ * <li>If an annotation type <i>TC</i> is present on an element, and
+ * <i>T</i> is modified to be repeatable with <i>TC</i> as its
+ * containing annotation type then:
+ *
+ * <ul>
+ *
+ * <li>The change to <i>T</i> is behaviorally compatible with respect
+ * to the {@code get[Declared]Annotation(Class<T>)} (called with an
+ * argument of <i>T</i> or <i>TC</i>) and {@code
+ * get[Declared]Annotations()} methods because the results of the
+ * methods will not change due to <i>TC</i> becoming the containing
+ * annotation type for <i>T</i>.
+ *
+ * <li>The change to <i>T</i> changes the results of the {@code
+ * get[Declared]AnnotationsByType(Class<T>)} methods called with an
+ * argument of <i>T</i>, because those methods will now recognize an
+ * annotation of type <i>TC</i> as a container annotation for <i>T</i>
+ * and will "look through" it to expose annotations of type <i>T</i>.
+ *
+ * </ul>
+ *
+ * <li>If an annotation of type <i>T</i> is present on an
+ * element and <i>T</i> is made repeatable and more annotations of
+ * type <i>T</i> are added to the element:
+ *
+ * <ul>
+ *
+ * <li> The addition of the annotations of type <i>T</i> is both
+ * source compatible and binary compatible.
+ *
+ * <li>The addition of the annotations of type <i>T</i> changes the results
+ * of the {@code get[Declared]Annotation(Class<T>)} methods and {@code
+ * get[Declared]Annotations()} methods, because those methods will now
+ * only see a container annotation on the element and not see an
+ * annotation of type <i>T</i>.
+ *
+ * <li>The addition of the annotations of type <i>T</i> changes the
+ * results of the {@code get[Declared]AnnotationsByType(Class<T>)}
+ * methods, because their results will expose the additional
+ * annotations of type <i>T</i> whereas previously they exposed only a
+ * single annotation of type <i>T</i>.
+ *
+ * </ul>
+ *
+ * </ul>
*
* <p>If an annotation returned by a method in this interface contains
* (directly or indirectly) a {@link Class}-valued member referring to
@@ -48,14 +210,20 @@ import libcore.reflect.AnnotatedElements;
* a {@link EnumConstantNotPresentException} if the enum constant in the
* annotation is no longer present in the enum type.
*
- * <p>Finally, Attempting to read a member whose definition has evolved
+ * <p>If an annotation type <i>T</i> is (meta-)annotated with an
+ * {@code @Repeatable} annotation whose value element indicates a type
+ * <i>TC</i>, but <i>TC</i> does not declare a {@code value()} method
+ * with a return type of <i>T</i>{@code []}, then an exception of type
+ * {@link java.lang.annotation.AnnotationFormatError} is thrown.
+ *
+ * <p>Finally, attempting to read a member whose definition has evolved
* incompatibly will result in a {@link
* java.lang.annotation.AnnotationTypeMismatchException} or an
* {@link java.lang.annotation.IncompleteAnnotationException}.
*
* @see java.lang.EnumConstantNotPresentException
* @see java.lang.TypeNotPresentException
- * @see java.lang.annotation.AnnotationFormatError
+ * @see AnnotationFormatError
* @see java.lang.annotation.AnnotationTypeMismatchException
* @see java.lang.annotation.IncompleteAnnotationException
* @since 1.5
@@ -64,9 +232,15 @@ import libcore.reflect.AnnotatedElements;
public interface AnnotatedElement {
/**
* Returns true if an annotation for the specified type
- * is present on this element, else false. This method
+ * is <em>present</em> on this element, else false. This method
* is designed primarily for convenient access to marker annotations.
*
+ * <p>The truth value returned by this method is equivalent to:
+ * {@code getAnnotation(annotationClass) != null}
+ *
+ * <p>The body of the default method is specified to be the code
+ * above.
+ *
* @param annotationClass the Class object corresponding to the
* annotation type
* @return true if an annotation for the specified annotation
@@ -74,14 +248,15 @@ public interface AnnotatedElement {
* @throws NullPointerException if the given annotation class is null
* @since 1.5
*/
- default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
- return getAnnotation(annotationClass) != null;
- }
+ default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
+ return getAnnotation(annotationClass) != null;
+ }
/**
* Returns this element's annotation for the specified type if
- * such an annotation is present, else null.
+ * such an annotation is <em>present</em>, else null.
*
+ * @param <T> the type of the annotation to query for and return if present
* @param annotationClass the Class object corresponding to the
* annotation type
* @return this element's annotation for the specified annotation type if
@@ -92,20 +267,52 @@ public interface AnnotatedElement {
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
/**
- * Returns all annotations present on this element. (Returns an array
- * of length zero if this element has no annotations.) The caller of
- * this method is free to modify the returned array; it will have no
- * effect on the arrays returned to other callers.
+ * Returns annotations that are <em>present</em> on this element.
+ *
+ * If there are no annotations <em>present</em> on this element, the return
+ * value is an array of length 0.
+ *
+ * The caller of this method is free to modify the returned array; it will
+ * have no effect on the arrays returned to other callers.
*
- * @return all annotations present on this element
+ * @return annotations present on this element
* @since 1.5
*/
Annotation[] getAnnotations();
/**
- * Returns an associated list of annotations on {@code this} element,
- * whose class is {@code annotationClass}, or an empty array if nothing was found.
+ * Returns annotations that are <em>associated</em> with this element.
*
+ * If there are no annotations <em>associated</em> with this element, the return
+ * value is an array of length 0.
+ *
+ * The difference between this method and {@link #getAnnotation(Class)}
+ * is that this method detects if its argument is a <em>repeatable
+ * annotation type</em> (JLS 9.6), and if so, attempts to find one or
+ * more annotations of that type by "looking through" a container
+ * annotation.
+ *
+ * The caller of this method is free to modify the returned array; it will
+ * have no effect on the arrays returned to other callers.
+ *
+ * @implSpec The default implementation first calls {@link
+ * #getDeclaredAnnotationsByType(Class)} passing {@code
+ * annotationClass} as the argument. If the returned array has
+ * length greater than zero, the array is returned. If the returned
+ * array is zero-length and this {@code AnnotatedElement} is a
+ * class and the argument type is an inheritable annotation type,
+ * and the superclass of this {@code AnnotatedElement} is non-null,
+ * then the returned result is the result of calling {@link
+ * #getAnnotationsByType(Class)} on the superclass with {@code
+ * annotationClass} as the argument. Otherwise, a zero-length
+ * array is returned.
+ *
+ * @param <T> the type of the annotation to query for and return if present
+ * @param annotationClass the Class object corresponding to the
+ * annotation type
+ * @return all this element's annotations for the specified annotation type if
+ * associated with this element, else an array of length zero
+ * @throws NullPointerException if the given annotation class is null
* @since 1.8
*/
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
@@ -118,9 +325,23 @@ public interface AnnotatedElement {
}
/**
- * Returns a directly-present annotation on {@code this} element, whose class is
- * {@code annotationClass}, or {@code null} if nothing was found.
+ * Returns this element's annotation for the specified type if
+ * such an annotation is <em>directly present</em>, else null.
*
+ * This method ignores inherited annotations. (Returns null if no
+ * annotations are directly present on this element.)
+ *
+ * @implSpec The default implementation first performs a null check
+ * and then loops over the results of {@link
+ * #getDeclaredAnnotations} returning the first annotation whose
+ * annotation type matches the argument type.
+ *
+ * @param <T> the type of the annotation to query for and return if directly present
+ * @param annotationClass the Class object corresponding to the
+ * annotation type
+ * @return this element's annotation for the specified annotation type if
+ * directly present on this element, else null
+ * @throws NullPointerException if the given annotation class is null
* @since 1.8
*/
default <T extends Annotation> Annotation getDeclaredAnnotation(Class<T> annotationClass) {
@@ -137,9 +358,47 @@ public interface AnnotatedElement {
}
/**
- * Returns a directly or indirectly present list of annotations on {@code this} element,
- * whose class is {@code annotationClass}, or an empty array if nothing was found.
+ * Returns this element's annotation(s) for the specified type if
+ * such annotations are either <em>directly present</em> or
+ * <em>indirectly present</em>. This method ignores inherited
+ * annotations.
+ *
+ * If there are no specified annotations directly or indirectly
+ * present on this element, the return value is an array of length
+ * 0.
+ *
+ * The difference between this method and {@link
+ * #getDeclaredAnnotation(Class)} is that this method detects if its
+ * argument is a <em>repeatable annotation type</em> (JLS 9.6), and if so,
+ * attempts to find one or more annotations of that type by "looking
+ * through" a container annotation if one is present.
+ *
+ * The caller of this method is free to modify the returned array; it will
+ * have no effect on the arrays returned to other callers.
*
+ * @implSpec The default implementation may call {@link
+ * #getDeclaredAnnotation(Class)} one or more times to find a
+ * directly present annotation and, if the annotation type is
+ * repeatable, to find a container annotation. If annotations of
+ * the annotation type {@code annotationClass} are found to be both
+ * directly and indirectly present, then {@link
+ * #getDeclaredAnnotations()} will get called to determine the
+ * order of the elements in the returned array.
+ *
+ * <p>Alternatively, the default implementation may call {@link
+ * #getDeclaredAnnotations()} a single time and the returned array
+ * examined for both directly and indirectly present
+ * annotations. The results of calling {@link
+ * #getDeclaredAnnotations()} are assumed to be consistent with the
+ * results of calling {@link #getDeclaredAnnotation(Class)}.
+ *
+ * @param <T> the type of the annotation to query for and return
+ * if directly or indirectly present
+ * @param annotationClass the Class object corresponding to the
+ * annotation type
+ * @return all this element's annotations for the specified annotation type if
+ * directly or indirectly present on this element, else an array of length zero
+ * @throws NullPointerException if the given annotation class is null
* @since 1.8
*/
default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
@@ -147,14 +406,16 @@ public interface AnnotatedElement {
}
/**
- * Returns all annotations that are directly present on this
- * element. Unlike the other methods in this interface, this method
- * ignores inherited annotations. (Returns an array of length zero if
- * no annotations are directly present on this element.) The caller of
- * this method is free to modify the returned array; it will have no
- * effect on the arrays returned to other callers.
- *
- * @return All annotations directly present on this element
+ * Returns annotations that are <em>directly present</em> on this element.
+ * This method ignores inherited annotations.
+ *
+ * If there are no annotations <em>directly present</em> on this element,
+ * the return value is an array of length 0.
+ *
+ * The caller of this method is free to modify the returned array; it will
+ * have no effect on the arrays returned to other callers.
+ *
+ * @return annotations directly present on this element
* @since 1.5
*/
Annotation[] getDeclaredAnnotations();
diff --git a/ojluni/src/main/java/java/lang/reflect/Constructor.java b/ojluni/src/main/java/java/lang/reflect/Constructor.java
index 76ecabf778..4ac1ca8d5d 100644
--- a/ojluni/src/main/java/java/lang/reflect/Constructor.java
+++ b/ojluni/src/main/java/java/lang/reflect/Constructor.java
@@ -115,6 +115,7 @@ public final class Constructor<T> extends AbstractMethod {
return (TypeVariable<Constructor<T>>[]) info.formalTypeParameters.clone();
}
+
/**
* {@inheritDoc}
*/
@@ -207,7 +208,7 @@ public final class Constructor<T> extends AbstractMethod {
* @jls 8.8.3. Constructor Modifiers
*/
public String toString() {
- // Android changed: Use getParameterTypes.
+ // Android changed: Use getParameterTypes().
return sharedToString(Modifier.constructorModifiers(),
false,
getParameterTypes(),
@@ -286,7 +287,7 @@ public final class Constructor<T> extends AbstractMethod {
* <p>If the constructor completes normally, returns the newly
* created and initialized instance.
*
- * @param args array of objects to be passed as arguments to
+ * @param initargs array of objects to be passed as arguments to
* the constructor call; values of primitive types are wrapped in
* a wrapper object of the appropriate type (e.g. a {@code float}
* in a {@link java.lang.Float Float})
@@ -311,11 +312,12 @@ public final class Constructor<T> extends AbstractMethod {
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
*/
- // Android changed param name s/initargs/args
- public T newInstance(Object... args) throws InstantiationException,
- IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ public T newInstance(Object ... initargs)
+ throws InstantiationException, IllegalAccessException,
+ IllegalArgumentException, InvocationTargetException
+ {
if (serializationClass == null) {
- return newInstance0(args);
+ return newInstance0(initargs);
} else {
return (T) newInstanceFromSerialization(serializationCtor, serializationClass);
}
diff --git a/ojluni/src/main/java/java/lang/reflect/Field.java b/ojluni/src/main/java/java/lang/reflect/Field.java
index db56ba4314..6eb49e6d82 100644
--- a/ojluni/src/main/java/java/lang/reflect/Field.java
+++ b/ojluni/src/main/java/java/lang/reflect/Field.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@ package java.lang.reflect;
import com.android.dex.Dex;
import java.lang.annotation.Annotation;
+import java.util.Objects;
import libcore.reflect.AnnotatedElements;
import libcore.reflect.GenericSignatureParser;
@@ -227,12 +228,15 @@ class Field extends AccessibleObject implements Member {
* {@code protected} or {@code private} first, and then other
* modifiers in the following order: {@code static}, {@code final},
* {@code transient}, {@code volatile}.
+ *
+ * @return a string describing this {@code Field}
+ * @jls 8.3.1 Field Modifiers
*/
public String toString() {
int mod = getModifiers();
return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
- + getTypeName(getType()) + " "
- + getTypeName(getDeclaringClass()) + "."
+ + getType().getTypeName() + " "
+ + getDeclaringClass().getTypeName() + "."
+ getName());
}
@@ -254,14 +258,14 @@ class Field extends AccessibleObject implements Member {
* its generic type
*
* @since 1.5
+ * @jls 8.3.1 Field Modifiers
*/
public String toGenericString() {
int mod = getModifiers();
Type fieldType = getGenericType();
return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
- + ((fieldType instanceof Class) ?
- getTypeName((Class)fieldType): fieldType.toString())+ " "
- + getTypeName(getDeclaringClass()) + "."
+ + fieldType.getTypeName() + " "
+ + getDeclaringClass().getTypeName() + "."
+ getName());
}
@@ -295,7 +299,7 @@ class Field extends AccessibleObject implements Member {
* <p>If the field is hidden in the type of {@code obj},
* the field's value is obtained according to the preceding rules.
*
- * @param object object from which the represented field's value is
+ * @param obj object from which the represented field's value is
* to be extracted
* @return the value of the represented field in object
* {@code obj}; primitive values are wrapped in an appropriate
@@ -312,13 +316,13 @@ class Field extends AccessibleObject implements Member {
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
*/
- public native Object get(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native Object get(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance {@code boolean} field.
*
- * @param object the object to extract the {@code boolean} value
+ * @param obj the object to extract the {@code boolean} value
* from
* @return the value of the {@code boolean} field
*
@@ -337,13 +341,13 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native boolean getBoolean(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native boolean getBoolean(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance {@code byte} field.
*
- * @param object the object to extract the {@code byte} value
+ * @param obj the object to extract the {@code byte} value
* from
* @return the value of the {@code byte} field
*
@@ -362,15 +366,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native byte getByte(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native byte getByte(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code char} or of another primitive type convertible to
* type {@code char} via a widening conversion.
*
- * @param object the object to extract the {@code char} value
+ * @param obj the object to extract the {@code char} value
* from
* @return the value of the field converted to type {@code char}
*
@@ -389,15 +393,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native char getChar(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native char getChar(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code short} or of another primitive type convertible to
* type {@code short} via a widening conversion.
*
- * @param object the object to extract the {@code short} value
+ * @param obj the object to extract the {@code short} value
* from
* @return the value of the field converted to type {@code short}
*
@@ -416,15 +420,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native short getShort(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native short getShort(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code int} or of another primitive type convertible to
* type {@code int} via a widening conversion.
*
- * @param object the object to extract the {@code int} value
+ * @param obj the object to extract the {@code int} value
* from
* @return the value of the field converted to type {@code int}
*
@@ -443,15 +447,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native int getInt(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native int getInt(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code long} or of another primitive type convertible to
* type {@code long} via a widening conversion.
*
- * @param object the object to extract the {@code long} value
+ * @param obj the object to extract the {@code long} value
* from
* @return the value of the field converted to type {@code long}
*
@@ -470,15 +474,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native long getLong(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native long getLong(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code float} or of another primitive type convertible to
* type {@code float} via a widening conversion.
*
- * @param object the object to extract the {@code float} value
+ * @param obj the object to extract the {@code float} value
* from
* @return the value of the field converted to type {@code float}
*
@@ -497,15 +501,15 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native float getFloat(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native float getFloat(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Gets the value of a static or instance field of type
* {@code double} or of another primitive type convertible to
* type {@code double} via a widening conversion.
*
- * @param object the object to extract the {@code double} value
+ * @param obj the object to extract the {@code double} value
* from
* @return the value of the field converted to type {@code double}
*
@@ -524,8 +528,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#get
*/
- public native double getDouble(Object object)
- throws IllegalAccessException, IllegalArgumentException;
+ public native double getDouble(Object obj)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the field represented by this {@code Field} object on the
@@ -577,7 +581,7 @@ class Field extends AccessibleObject implements Member {
* <p>If the field is hidden in the type of {@code obj},
* the field's value is set according to the preceding rules.
*
- * @param object the object whose field should be modified
+ * @param obj the object whose field should be modified
* @param value the new value for the field of {@code obj}
* being modified
*
@@ -593,8 +597,8 @@ class Field extends AccessibleObject implements Member {
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
*/
- public native void set(Object object, Object value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void set(Object obj, Object value)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code boolean} on the specified object.
@@ -603,8 +607,8 @@ class Field extends AccessibleObject implements Member {
* where {@code zObj} is a {@code Boolean} object and
* {@code zObj.booleanValue() == z}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param z the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -620,8 +624,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setBoolean(Object object, boolean value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setBoolean(Object obj, boolean z)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code byte} on the specified object.
@@ -630,8 +634,8 @@ class Field extends AccessibleObject implements Member {
* where {@code bObj} is a {@code Byte} object and
* {@code bObj.byteValue() == b}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param b the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -647,8 +651,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setByte(Object object, byte value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setByte(Object obj, byte b)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code char} on the specified object.
@@ -657,8 +661,8 @@ class Field extends AccessibleObject implements Member {
* where {@code cObj} is a {@code Character} object and
* {@code cObj.charValue() == c}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param c the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -674,8 +678,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setChar(Object object, char value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setChar(Object obj, char c)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code short} on the specified object.
@@ -684,8 +688,8 @@ class Field extends AccessibleObject implements Member {
* where {@code sObj} is a {@code Short} object and
* {@code sObj.shortValue() == s}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param s the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -701,8 +705,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setShort(Object object, short value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setShort(Object obj, short s)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as an {@code int} on the specified object.
@@ -711,8 +715,8 @@ class Field extends AccessibleObject implements Member {
* where {@code iObj} is a {@code Integer} object and
* {@code iObj.intValue() == i}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param i the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -728,8 +732,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setInt(Object object, int value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setInt(Object obj, int i)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code long} on the specified object.
@@ -738,8 +742,8 @@ class Field extends AccessibleObject implements Member {
* where {@code lObj} is a {@code Long} object and
* {@code lObj.longValue() == l}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param l the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -755,8 +759,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setLong(Object object, long value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setLong(Object obj, long l)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code float} on the specified object.
@@ -765,8 +769,8 @@ class Field extends AccessibleObject implements Member {
* where {@code fObj} is a {@code Float} object and
* {@code fObj.floatValue() == f}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param f the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -782,8 +786,8 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setFloat(Object object, float value)
- throws IllegalAccessException, IllegalArgumentException;
+ public native void setFloat(Object obj, float f)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* Sets the value of a field as a {@code double} on the specified object.
@@ -792,8 +796,8 @@ class Field extends AccessibleObject implements Member {
* where {@code dObj} is a {@code Double} object and
* {@code dObj.doubleValue() == d}.
*
- * @param object the object whose field should be modified
- * @param value the new value for the field of {@code obj}
+ * @param obj the object whose field should be modified
+ * @param d the new value for the field of {@code obj}
* being modified
*
* @exception IllegalAccessException if this {@code Field} object
@@ -809,41 +813,17 @@ class Field extends AccessibleObject implements Member {
* by this method fails.
* @see Field#set
*/
- public native void setDouble(Object object, double value)
- throws IllegalAccessException, IllegalArgumentException;
-
- /*
- * Utility routine to paper over array type names
- */
- static String getTypeName(Class<?> type) {
- if (type.isArray()) {
- try {
- Class<?> cl = type;
- int dimensions = 0;
- while (cl.isArray()) {
- dimensions++;
- cl = cl.getComponentType();
- }
- StringBuffer sb = new StringBuffer();
- sb.append(cl.getName());
- for (int i = 0; i < dimensions; i++) {
- sb.append("[]");
- }
- return sb.toString();
- } catch (Throwable e) { /*FALLTHRU*/ }
- }
- return type.getName();
- }
+ public native void setDouble(Object obj, double d)
+ throws IllegalArgumentException, IllegalAccessException;
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
- @Override public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
- if (annotationType == null) {
- throw new NullPointerException("annotationType == null");
- }
- return getAnnotationNative(annotationType);
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
+ Objects.requireNonNull(annotationClass);
+ return getAnnotationNative(annotationClass);
}
private native <A extends Annotation> A getAnnotationNative(Class<A> annotationType);
@@ -868,9 +848,10 @@ class Field extends AccessibleObject implements Member {
private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType);
/**
- * @since 1.5
+ * {@inheritDoc}
*/
- @Override public native Annotation[] getDeclaredAnnotations();
+ @Override
+ public native Annotation[] getDeclaredAnnotations();
/**
* Returns the index of this field's ID in its dex file.
diff --git a/ojluni/src/main/java/java/lang/reflect/GenericDeclaration.java b/ojluni/src/main/java/java/lang/reflect/GenericDeclaration.java
index fb319bfbea..042e359ad6 100644
--- a/ojluni/src/main/java/java/lang/reflect/GenericDeclaration.java
+++ b/ojluni/src/main/java/java/lang/reflect/GenericDeclaration.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,7 @@ package java.lang.reflect;
*
* @since 1.5
*/
-public interface GenericDeclaration {
+public interface GenericDeclaration extends AnnotatedElement {
/**
* Returns an array of {@code TypeVariable} objects that
* represent the type variables declared by the generic
diff --git a/ojluni/src/main/java/java/lang/reflect/Modifier.java b/ojluni/src/main/java/java/lang/reflect/Modifier.java
index f461be5690..760f1a363f 100644
--- a/ojluni/src/main/java/java/lang/reflect/Modifier.java
+++ b/ojluni/src/main/java/java/lang/reflect/Modifier.java
@@ -40,8 +40,7 @@ package java.lang.reflect;
* @author Nakul Saraiya
* @author Kenneth Russell
*/
-public
-class Modifier {
+public class Modifier {
/**
* Return {@code true} if the integer argument includes the
@@ -227,7 +226,7 @@ class Modifier {
* represented by {@code mod}
*/
public static String toString(int mod) {
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
int len;
if ((mod & PUBLIC) != 0) sb.append("public ");
@@ -337,12 +336,27 @@ class Modifier {
* @hide
*/
public static final int SYNTHETIC = 0x00001000;
- static final int ANNOTATION= 0x00002000;
+ static final int ANNOTATION = 0x00002000;
static final int ENUM = 0x00004000;
+ static final int MANDATED = 0x00008000;
static boolean isSynthetic(int mod) {
return (mod & SYNTHETIC) != 0;
}
+ static boolean isMandated(int mod) {
+ return (mod & MANDATED) != 0;
+ }
+
+ // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
+ // the sets of modifiers are not guaranteed to be constants
+ // across time and Java SE releases. Therefore, it would not be
+ // appropriate to expose an external interface to this information
+ // that would allow the values to be treated as Java-level
+ // constants since the values could be constant folded and updates
+ // to the sets of modifiers missed. Thus, the fooModifiers()
+ // methods return an unchanging values for a given release, but a
+ // value that can potentially change over time.
+
/**
* Dex addition to mark instance constructors and static class
* initializer methods.
@@ -360,7 +374,8 @@ class Modifier {
public static final int DEFAULT = 0x00400000;
/**
- * See JLSv3 section 8.1.1.
+ * The Java source modifiers that can be applied to a class.
+ * @jls 8.1.1 Class Modifiers
*/
private static final int CLASS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
@@ -368,7 +383,8 @@ class Modifier {
Modifier.STRICT;
/**
- * See JLSv3 section 9.1.1.
+ * The Java source modifiers that can be applied to an interface.
+ * @jls 9.1.1 Interface Modifiers
*/
private static final int INTERFACE_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
@@ -376,13 +392,15 @@ class Modifier {
/**
- * See JLSv3 section 8.8.3.
+ * The Java source modifiers that can be applied to a constructor.
+ * @jls 8.8.3 Constructor Modifiers
*/
private static final int CONSTRUCTOR_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/**
- * See JLSv3 section 8.4.3.
+ * The Java source modifiers that can be applied to a method.
+ * @jls8.4.3 Method Modifiers
*/
private static final int METHOD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
@@ -390,7 +408,8 @@ class Modifier {
Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
/**
- * See JLSv3 section 8.3.1.
+ * The Java source modifiers that can be applied to a field.
+ * @jls 8.3.1 Field Modifiers
*/
private static final int FIELD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
@@ -398,6 +417,13 @@ class Modifier {
Modifier.VOLATILE;
/**
+ * The Java source modifiers that can be applied to a method or constructor parameter.
+ * @jls 8.4.1 Formal Parameters
+ */
+ private static final int PARAMETER_MODIFIERS =
+ Modifier.FINAL;
+
+ /**
*
*/
static final int ACCESS_MODIFIERS =
@@ -420,7 +446,7 @@ class Modifier {
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to an interface.
* @return an {@code int} value OR-ing together the source language
- * modifiers that can be applied to an inteface.
+ * modifiers that can be applied to an interface.
*
* @jls 9.1.1 Interface Modifiers
* @since 1.7
@@ -455,7 +481,6 @@ class Modifier {
return METHOD_MODIFIERS;
}
-
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a field.
@@ -468,4 +493,18 @@ class Modifier {
public static int fieldModifiers() {
return FIELD_MODIFIERS;
}
+
+ /**
+ * Return an {@code int} value OR-ing together the source language
+ * modifiers that can be applied to a parameter.
+ * @return an {@code int} value OR-ing together the source language
+ * modifiers that can be applied to a parameter.
+ *
+ * @jls 8.4.1 Formal Parameters
+ * @since 1.8
+ * @hide Hidden pending tests
+ */
+ public static int parameterModifiers() {
+ return PARAMETER_MODIFIERS;
+ }
}