Addition of Parameter / MalformedParametersException
Addition of the java.lang.reflect Parameter and
MalformedParametersException classes from OpenJDK 8u60.
New public methods have been added but are hidden and
will be unhidden when the associated tests are
submitted.
The Parameter support is currently "synthesized" only:
the Parameter object is not created with any modifier or
name information. Adding more will require further
changes and will be added as a separate activity.
Bug: 28666126
Test: Ran CTS CtsLibcoreTestCases and test-art-host
Change-Id: I66de1472db1fbda093360bad20d6d24a855070aa
diff --git a/libart/src/main/java/java/lang/reflect/AbstractMethod.java b/libart/src/main/java/java/lang/reflect/AbstractMethod.java
index 6e3e181..73cc60b 100644
--- a/libart/src/main/java/java/lang/reflect/AbstractMethod.java
+++ b/libart/src/main/java/java/lang/reflect/AbstractMethod.java
@@ -216,6 +216,10 @@
}
}
+ boolean hasGenericInformationInternal() {
+ return getSignatureAnnotation() != null;
+ }
+
/**
* Returns generic information associated with this method/constructor member.
*/
diff --git a/ojluni/src/main/java/java/lang/reflect/Constructor.java b/ojluni/src/main/java/java/lang/reflect/Constructor.java
index 3e26cbb..a85d427 100644
--- a/ojluni/src/main/java/java/lang/reflect/Constructor.java
+++ b/ojluni/src/main/java/java/lang/reflect/Constructor.java
@@ -74,6 +74,12 @@
return new Constructor<T>(ctor, cl);
}
+ @Override
+ boolean hasGenericInformation() {
+ // Android-changed: Signature retrieval is handled in AbstractMethod.
+ return super.hasGenericInformationInternal();
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/ojluni/src/main/java/java/lang/reflect/Executable.java b/ojluni/src/main/java/java/lang/reflect/Executable.java
index 1840566..6c7f775 100644
--- a/ojluni/src/main/java/java/lang/reflect/Executable.java
+++ b/ojluni/src/main/java/java/lang/reflect/Executable.java
@@ -41,6 +41,11 @@
*/
Executable() {}
+ /**
+ * Does the Executable have generic information.
+ */
+ abstract boolean hasGenericInformation();
+
boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
/* Avoid unnecessary cloning */
if (params1.length == params2.length) {
@@ -256,6 +261,160 @@
}
/**
+ * Behaves like {@code getGenericParameterTypes}, but returns type
+ * information for all parameters, including synthetic parameters.
+ */
+ Type[] getAllGenericParameterTypes() {
+ final boolean genericInfo = hasGenericInformation();
+
+ // Easy case: we don't have generic parameter information. In
+ // this case, we just return the result of
+ // getParameterTypes().
+ if (!genericInfo) {
+ return getParameterTypes();
+ } else {
+ final boolean realParamData = hasRealParameterData();
+ final Type[] genericParamTypes = getGenericParameterTypes();
+ final Type[] nonGenericParamTypes = getParameterTypes();
+ final Type[] out = new Type[nonGenericParamTypes.length];
+ final Parameter[] params = getParameters();
+ int fromidx = 0;
+ // If we have real parameter data, then we use the
+ // synthetic and mandate flags to our advantage.
+ if (realParamData) {
+ for (int i = 0; i < out.length; i++) {
+ final Parameter param = params[i];
+ if (param.isSynthetic() || param.isImplicit()) {
+ // If we hit a synthetic or mandated parameter,
+ // use the non generic parameter info.
+ out[i] = nonGenericParamTypes[i];
+ } else {
+ // Otherwise, use the generic parameter info.
+ out[i] = genericParamTypes[fromidx];
+ fromidx++;
+ }
+ }
+ } else {
+ // Otherwise, use the non-generic parameter data.
+ // Without method parameter reflection data, we have
+ // no way to figure out which parameters are
+ // synthetic/mandated, thus, no way to match up the
+ // indexes.
+ return genericParamTypes.length == nonGenericParamTypes.length ?
+ genericParamTypes : nonGenericParamTypes;
+ }
+ return out;
+ }
+ }
+
+ /**
+ * Returns an array of {@code Parameter} objects that represent
+ * all the parameters to the underlying executable represented by
+ * this object. Returns an array of length 0 if the executable
+ * has no parameters.
+ *
+ * <p>The parameters of the underlying executable do not necessarily
+ * have unique names, or names that are legal identifiers in the
+ * Java programming language (JLS 3.8).
+ *
+ * @since 1.8
+ * @throws MalformedParametersException if the class file contains
+ * a MethodParameters attribute that is improperly formatted.
+ * @return an array of {@code Parameter} objects representing all
+ * the parameters to the executable this object represents.
+ * @hide Hidden pending tests
+ */
+ public Parameter[] getParameters() {
+ // TODO: This may eventually need to be guarded by security
+ // mechanisms similar to those in Field, Method, etc.
+ //
+ // Need to copy the cached array to prevent users from messing
+ // with it. Since parameters are immutable, we can
+ // shallow-copy.
+ return privateGetParameters().clone();
+ }
+
+ private Parameter[] synthesizeAllParams() {
+ final int realparams = getParameterCount();
+ final Parameter[] out = new Parameter[realparams];
+ for (int i = 0; i < realparams; i++)
+ // TODO: is there a way to synthetically derive the
+ // modifiers? Probably not in the general case, since
+ // we'd have no way of knowing about them, but there
+ // may be specific cases.
+ out[i] = new Parameter("arg" + i, 0, this, i);
+ return out;
+ }
+
+ private void verifyParameters(final Parameter[] parameters) {
+ final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED;
+
+ if (getParameterTypes().length != parameters.length)
+ throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute");
+
+ for (Parameter parameter : parameters) {
+ final String name = parameter.getRealName();
+ final int mods = parameter.getModifiers();
+
+ if (name != null) {
+ if (name.isEmpty() || name.indexOf('.') != -1 ||
+ name.indexOf(';') != -1 || name.indexOf('[') != -1 ||
+ name.indexOf('/') != -1) {
+ throw new MalformedParametersException("Invalid parameter name \"" + name + "\"");
+ }
+ }
+
+ if (mods != (mods & mask)) {
+ throw new MalformedParametersException("Invalid parameter modifiers");
+ }
+ }
+ }
+
+ private Parameter[] privateGetParameters() {
+ // Use tmp to avoid multiple writes to a volatile.
+ Parameter[] tmp = parameters;
+
+ if (tmp == null) {
+ // Android-changed: Commented for now.
+ /*
+ // Otherwise, go to the JVM to get them
+ try {
+ tmp = getParameters0();
+ } catch(IllegalArgumentException e) {
+ // Rethrow ClassFormatErrors
+ throw new MalformedParametersException("Invalid constant pool index");
+ }
+ */
+ // Android-changed: End of changes.
+
+ // If we get back nothing, then synthesize parameters
+ if (tmp == null) {
+ hasRealParameterData = false;
+ tmp = synthesizeAllParams();
+ } else {
+ hasRealParameterData = true;
+ verifyParameters(tmp);
+ }
+
+ parameters = tmp;
+ }
+
+ return tmp;
+ }
+
+ boolean hasRealParameterData() {
+ // If this somehow gets called before parameters gets
+ // initialized, force it into existence.
+ if (parameters == null) {
+ privateGetParameters();
+ }
+ return hasRealParameterData;
+ }
+
+ private transient volatile boolean hasRealParameterData;
+ private transient volatile Parameter[] parameters;
+
+ /**
* Returns an array of {@code Class} objects that represent the
* types of exceptions declared to be thrown by the underlying
* executable represented by this object. Returns an array of
diff --git a/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java b/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java
new file mode 100644
index 0000000..6b14b15
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.reflect;
+
+/**
+ * Thrown when {@link java.lang.reflect.Executable#getParameters the
+ * java.lang.reflect package} attempts to read method parameters from
+ * a class file and determines that one or more parameters are
+ * malformed.
+ *
+ * <p>The following is a list of conditions under which this exception
+ * can be thrown:
+ * <ul>
+ * <li> The number of parameters (parameter_count) is wrong for the method
+ * <li> A constant pool index is out of bounds.
+ * <li> A constant pool index does not refer to a UTF-8 entry
+ * <li> A parameter's name is "", or contains an illegal character
+ * <li> The flags field contains an illegal flag (something other than
+ * FINAL, SYNTHETIC, or MANDATED)
+ * </ul>
+ *
+ * See {@link java.lang.reflect.Executable#getParameters} for more
+ * information.
+ *
+ * @see java.lang.reflect.Executable#getParameters
+ * @since 1.8
+ * @hide Hidden pending tests
+ */
+public class MalformedParametersException extends RuntimeException {
+
+ /**
+ * Version for serialization.
+ */
+ private static final long serialVersionUID = 20130919L;
+
+ /**
+ * Create a {@code MalformedParametersException} with an empty
+ * reason.
+ */
+ public MalformedParametersException() {}
+
+ /**
+ * Create a {@code MalformedParametersException}.
+ *
+ * @param reason The reason for the exception.
+ */
+ public MalformedParametersException(String reason) {
+ super(reason);
+ }
+}
diff --git a/ojluni/src/main/java/java/lang/reflect/Method.java b/ojluni/src/main/java/java/lang/reflect/Method.java
index a9f2eb7..fe41c5e 100644
--- a/ojluni/src/main/java/java/lang/reflect/Method.java
+++ b/ojluni/src/main/java/java/lang/reflect/Method.java
@@ -82,9 +82,15 @@
private Method() {
}
+ @Override
+ boolean hasGenericInformation() {
+ // Android-changed: Signature retrieval is handled in AbstractMethod.
+ return super.hasGenericInformationInternal();
+ }
+
/**
* {@inheritDoc}
- */
+ */
@Override
public Class<?> getDeclaringClass() {
// Android-changed: This is handled by AbstractMethod.
@@ -159,6 +165,7 @@
* @since 1.5
*/
public Type getGenericReturnType() {
+ // Android-changed: Modified implementation to use AbstractMethod.
return Types.getType(getMethodOrConstructorGenericInfoInternal().genericReturnType);
}
@@ -238,7 +245,6 @@
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
}
-
/**
* Returns a string describing this {@code Method}. The string is
* formatted as the method access modifiers, if any, followed by
@@ -332,7 +338,6 @@
sb.append(getName());
}
-
/**
* Invokes the underlying method represented by this {@code Method}
* object, on the specified object with the specified parameters.
@@ -425,7 +430,6 @@
return super.isSynthetic();
}
-
/**
* Returns {@code true} if this method is a default
* method; returns {@code false} otherwise.
diff --git a/ojluni/src/main/java/java/lang/reflect/Parameter.java b/ojluni/src/main/java/java/lang/reflect/Parameter.java
new file mode 100644
index 0000000..89dd606
--- /dev/null
+++ b/ojluni/src/main/java/java/lang/reflect/Parameter.java
@@ -0,0 +1,324 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.lang.reflect;
+
+import java.lang.annotation.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import libcore.reflect.AnnotatedElements;
+
+/**
+ * Information about method parameters.
+ *
+ * A {@code Parameter} provides information about method parameters,
+ * including its name and modifiers. It also provides an alternate
+ * means of obtaining attributes for the parameter.
+ *
+ * @since 1.8
+ * @hide Hidden pending tests
+ */
+public final class Parameter implements AnnotatedElement {
+
+ private final String name;
+ private final int modifiers;
+ private final Executable executable;
+ private final int index;
+
+ /**
+ * Package-private constructor for {@code Parameter}.
+ *
+ * If method parameter data is present in the classfile, then the
+ * JVM creates {@code Parameter} objects directly. If it is
+ * absent, however, then {@code Executable} uses this constructor
+ * to synthesize them.
+ *
+ * @param name The name of the parameter.
+ * @param modifiers The modifier flags for the parameter.
+ * @param executable The executable which defines this parameter.
+ * @param index The index of the parameter.
+ */
+ Parameter(String name,
+ int modifiers,
+ Executable executable,
+ int index) {
+ this.name = name;
+ this.modifiers = modifiers;
+ this.executable = executable;
+ this.index = index;
+ }
+
+ /**
+ * Compares based on the executable and the index.
+ *
+ * @param obj The object to compare.
+ * @return Whether or not this is equal to the argument.
+ */
+ public boolean equals(Object obj) {
+ if(obj instanceof Parameter) {
+ Parameter other = (Parameter)obj;
+ return (other.executable.equals(executable) &&
+ other.index == index);
+ }
+ return false;
+ }
+
+ /**
+ * Returns a hash code based on the executable's hash code and the
+ * index.
+ *
+ * @return A hash code based on the executable's hash code.
+ */
+ public int hashCode() {
+ return executable.hashCode() ^ index;
+ }
+
+ // Android-changed: Removed references to the class file format.
+ /**
+ * Returns true if the parameter has a name; returns false otherwise.
+ * Whether a parameter has a name is determined by compiler options
+ * and whether the parameter is synthesized.
+ *
+ * @return true if and only if the parameter has a name
+ */
+ public boolean isNamePresent() {
+ return executable.hasRealParameterData() && name != null;
+ }
+
+ /**
+ * Returns a string describing this parameter. The format is the
+ * modifiers for the parameter, if any, in canonical order as
+ * recommended by <cite>The Java™ Language
+ * Specification</cite>, followed by the fully- qualified type of
+ * the parameter (excluding the last [] if the parameter is
+ * variable arity), followed by "..." if the parameter is variable
+ * arity, followed by a space, followed by the name of the
+ * parameter.
+ *
+ * @return A string representation of the parameter and associated
+ * information.
+ */
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ final Type type = getParameterizedType();
+ final String typename = type.getTypeName();
+
+ sb.append(Modifier.toString(getModifiers()));
+
+ if(0 != modifiers)
+ sb.append(' ');
+
+ if(isVarArgs())
+ sb.append(typename.replaceFirst("\\[\\]$", "..."));
+ else
+ sb.append(typename);
+
+ sb.append(' ');
+ sb.append(getName());
+
+ return sb.toString();
+ }
+
+ /**
+ * Return the {@code Executable} which declares this parameter.
+ *
+ * @return The {@code Executable} declaring this parameter.
+ */
+ public Executable getDeclaringExecutable() {
+ return executable;
+ }
+
+ /**
+ * Get the modifier flags for this the parameter represented by
+ * this {@code Parameter} object.
+ *
+ * @return The modifier flags for this parameter.
+ */
+ public int getModifiers() {
+ return modifiers;
+ }
+
+ /**
+ * Returns the name of the parameter. If the parameter's name is
+ * {@linkplain #isNamePresent() present}, then this method returns
+ * the name provided by the class file. Otherwise, this method
+ * synthesizes a name of the form argN, where N is the index of
+ * the parameter in the descriptor of the method which declares
+ * the parameter.
+ *
+ * @return The name of the parameter, either provided by the class
+ * file or synthesized if the class file does not provide
+ * a name.
+ */
+ public String getName() {
+ // Note: empty strings as paramete names are now outlawed.
+ // The .equals("") is for compatibility with current JVM
+ // behavior. It may be removed at some point.
+ if(name == null || name.equals(""))
+ return "arg" + index;
+ else
+ return name;
+ }
+
+ // Package-private accessor to the real name field.
+ String getRealName() {
+ return name;
+ }
+
+ /**
+ * Returns a {@code Type} object that identifies the parameterized
+ * type for the parameter represented by this {@code Parameter}
+ * object.
+ *
+ * @return a {@code Type} object identifying the parameterized
+ * type of the parameter represented by this object
+ */
+ public Type getParameterizedType() {
+ Type tmp = parameterTypeCache;
+ if (null == tmp) {
+ tmp = executable.getAllGenericParameterTypes()[index];
+ parameterTypeCache = tmp;
+ }
+
+ return tmp;
+ }
+
+ private transient volatile Type parameterTypeCache = null;
+
+ /**
+ * Returns a {@code Class} object that identifies the
+ * declared type for the parameter represented by this
+ * {@code Parameter} object.
+ *
+ * @return a {@code Class} object identifying the declared
+ * type of the parameter represented by this object
+ */
+ public Class<?> getType() {
+ Class<?> tmp = parameterClassCache;
+ if (null == tmp) {
+ tmp = executable.getParameterTypes()[index];
+ parameterClassCache = tmp;
+ }
+ return tmp;
+ }
+
+ private transient volatile Class<?> parameterClassCache = null;
+
+ /**
+ * Returns {@code true} if this parameter is implicitly declared
+ * in source code; returns {@code false} otherwise.
+ *
+ * @return true if and only if this parameter is implicitly
+ * declared as defined by <cite>The Java™ Language
+ * Specification</cite>.
+ */
+ public boolean isImplicit() {
+ return Modifier.isMandated(getModifiers());
+ }
+
+ /**
+ * Returns {@code true} if this parameter is neither implicitly
+ * nor explicitly declared in source code; returns {@code false}
+ * otherwise.
+ *
+ * @jls 13.1 The Form of a Binary
+ * @return true if and only if this parameter is a synthetic
+ * construct as defined by
+ * <cite>The Java™ Language Specification</cite>.
+ */
+ public boolean isSynthetic() {
+ return Modifier.isSynthetic(getModifiers());
+ }
+
+ /**
+ * Returns {@code true} if this parameter represents a variable
+ * argument list; returns {@code false} otherwise.
+ *
+ * @return {@code true} if an only if this parameter represents a
+ * variable argument list.
+ */
+ public boolean isVarArgs() {
+ return executable.isVarArgs() &&
+ index == executable.getParameterCount() - 1;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
+ Objects.requireNonNull(annotationClass);
+ // Android-changed: Uses native code to obtain annotation information.
+ return getAnnotationNative(executable, index, annotationClass);
+ }
+ private static native <A extends Annotation> A getAnnotationNative(
+ Executable executable, int parameterIndex, Class<A> annotationType);
+
+ /**
+ * {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ @Override
+ public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
+ // Android-changed: Uses AnnotatedElements instead.
+ return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Annotation[] getDeclaredAnnotations() {
+ return executable.getParameterAnnotations()[index];
+ }
+
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
+ // Only annotations on classes are inherited, for all other
+ // objects getDeclaredAnnotation is the same as
+ // getAnnotation.
+ return getAnnotation(annotationClass);
+ }
+
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
+ @Override
+ public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
+ // Only annotations on classes are inherited, for all other
+ // objects getDeclaredAnnotations is the same as
+ // getAnnotations.
+ return getAnnotationsByType(annotationClass);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Annotation[] getAnnotations() {
+ return getDeclaredAnnotations();
+ }
+}
diff --git a/openjdk_java_files.mk b/openjdk_java_files.mk
index 1238d5a..b7ab504 100644
--- a/openjdk_java_files.mk
+++ b/openjdk_java_files.mk
@@ -186,9 +186,11 @@
ojluni/src/main/java/java/lang/reflect/InvocationTargetException.java \
ojluni/src/main/java/java/lang/ReflectiveOperationException.java \
ojluni/src/main/java/java/lang/reflect/MalformedParameterizedTypeException.java \
+ ojluni/src/main/java/java/lang/reflect/MalformedParametersException.java \
ojluni/src/main/java/java/lang/reflect/Member.java \
ojluni/src/main/java/java/lang/reflect/Method.java \
ojluni/src/main/java/java/lang/reflect/Modifier.java \
+ ojluni/src/main/java/java/lang/reflect/Parameter.java \
ojluni/src/main/java/java/lang/reflect/ParameterizedType.java \
ojluni/src/main/java/java/lang/reflect/Proxy.java \
ojluni/src/main/java/java/lang/reflect/ReflectPermission.java \