0N/A/*
3261N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.reflect;
0N/A
6338N/Aimport sun.reflect.CallerSensitive;
0N/Aimport sun.reflect.ConstructorAccessor;
0N/Aimport sun.reflect.Reflection;
0N/Aimport sun.reflect.generics.repository.ConstructorRepository;
0N/Aimport sun.reflect.generics.factory.CoreReflectionFactory;
0N/Aimport sun.reflect.generics.factory.GenericsFactory;
0N/Aimport sun.reflect.generics.scope.ConstructorScope;
0N/Aimport java.lang.annotation.Annotation;
0N/Aimport java.util.Map;
0N/Aimport sun.reflect.annotation.AnnotationParser;
0N/Aimport java.lang.annotation.AnnotationFormatError;
0N/Aimport java.lang.reflect.Modifier;
0N/A
0N/A/**
0N/A * {@code Constructor} provides information about, and access to, a single
0N/A * constructor for a class.
0N/A *
0N/A * <p>{@code Constructor} permits widening conversions to occur when matching the
0N/A * actual parameters to newInstance() with the underlying
0N/A * constructor's formal parameters, but throws an
0N/A * {@code IllegalArgumentException} if a narrowing conversion would occur.
0N/A *
0N/A * @param <T> the class in which the constructor is declared
0N/A *
0N/A * @see Member
0N/A * @see java.lang.Class
0N/A * @see java.lang.Class#getConstructors()
0N/A * @see java.lang.Class#getConstructor(Class[])
0N/A * @see java.lang.Class#getDeclaredConstructors()
0N/A *
0N/A * @author Kenneth Russell
0N/A * @author Nakul Saraiya
0N/A */
0N/Apublic final
0N/A class Constructor<T> extends AccessibleObject implements
0N/A GenericDeclaration,
0N/A Member {
0N/A
0N/A private Class<T> clazz;
0N/A private int slot;
1717N/A private Class<?>[] parameterTypes;
1717N/A private Class<?>[] exceptionTypes;
0N/A private int modifiers;
0N/A // Generics and annotations support
0N/A private transient String signature;
0N/A // generic info repository; lazily initialized
0N/A private transient ConstructorRepository genericInfo;
0N/A private byte[] annotations;
0N/A private byte[] parameterAnnotations;
0N/A
0N/A // Generics infrastructure
0N/A // Accessor for factory
0N/A private GenericsFactory getFactory() {
0N/A // create scope and factory
0N/A return CoreReflectionFactory.make(this, ConstructorScope.make(this));
0N/A }
0N/A
0N/A // Accessor for generic info repository
0N/A private ConstructorRepository getGenericInfo() {
0N/A // lazily initialize repository if necessary
0N/A if (genericInfo == null) {
0N/A // create and cache generic info repository
0N/A genericInfo =
0N/A ConstructorRepository.make(getSignature(),
0N/A getFactory());
0N/A }
0N/A return genericInfo; //return cached repository
0N/A }
0N/A
0N/A private volatile ConstructorAccessor constructorAccessor;
0N/A // For sharing of ConstructorAccessors. This branching structure
0N/A // is currently only two levels deep (i.e., one root Constructor
0N/A // and potentially many Constructor objects pointing to it.)
0N/A private Constructor<T> root;
0N/A
0N/A /**
0N/A * Package-private constructor used by ReflectAccess to enable
0N/A * instantiation of these objects in Java code from the java.lang
0N/A * package via sun.reflect.LangReflectAccess.
0N/A */
0N/A Constructor(Class<T> declaringClass,
1717N/A Class<?>[] parameterTypes,
1717N/A Class<?>[] checkedExceptions,
0N/A int modifiers,
0N/A int slot,
0N/A String signature,
0N/A byte[] annotations,
0N/A byte[] parameterAnnotations)
0N/A {
0N/A this.clazz = declaringClass;
0N/A this.parameterTypes = parameterTypes;
0N/A this.exceptionTypes = checkedExceptions;
0N/A this.modifiers = modifiers;
0N/A this.slot = slot;
0N/A this.signature = signature;
0N/A this.annotations = annotations;
0N/A this.parameterAnnotations = parameterAnnotations;
0N/A }
0N/A
0N/A /**
0N/A * Package-private routine (exposed to java.lang.Class via
0N/A * ReflectAccess) which returns a copy of this Constructor. The copy's
0N/A * "root" field points to this Constructor.
0N/A */
0N/A Constructor<T> copy() {
0N/A // This routine enables sharing of ConstructorAccessor objects
0N/A // among Constructor objects which refer to the same underlying
0N/A // method in the VM. (All of this contortion is only necessary
0N/A // because of the "accessibility" bit in AccessibleObject,
0N/A // which implicitly requires that new java.lang.reflect
0N/A // objects be fabricated for each reflective call on Class
0N/A // objects.)
3323N/A Constructor<T> res = new Constructor<>(clazz,
0N/A parameterTypes,
0N/A exceptionTypes, modifiers, slot,
0N/A signature,
0N/A annotations,
0N/A parameterAnnotations);
0N/A res.root = this;
0N/A // Might as well eagerly propagate this if already present
0N/A res.constructorAccessor = constructorAccessor;
0N/A return res;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code Class} object representing the class that declares
0N/A * the constructor represented by this {@code Constructor} object.
0N/A */
0N/A public Class<T> getDeclaringClass() {
0N/A return clazz;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of this constructor, as a string. This is
2745N/A * the binary name of the constructor's declaring class.
0N/A */
0N/A public String getName() {
0N/A return getDeclaringClass().getName();
0N/A }
0N/A
0N/A /**
0N/A * Returns the Java language modifiers for the constructor
0N/A * represented by this {@code Constructor} object, as an integer. The
0N/A * {@code Modifier} class should be used to decode the modifiers.
0N/A *
0N/A * @see Modifier
0N/A */
0N/A public int getModifiers() {
0N/A return modifiers;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of {@code TypeVariable} objects that represent the
0N/A * type variables declared by the generic declaration represented by this
0N/A * {@code GenericDeclaration} object, in declaration order. Returns an
0N/A * array of length 0 if the underlying generic declaration declares no type
0N/A * variables.
0N/A *
0N/A * @return an array of {@code TypeVariable} objects that represent
0N/A * the type variables declared by this generic declaration
0N/A * @throws GenericSignatureFormatError if the generic
0N/A * signature of this generic declaration does not conform to
4008N/A * the format specified in
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>
0N/A * @since 1.5
0N/A */
0N/A public TypeVariable<Constructor<T>>[] getTypeParameters() {
0N/A if (getSignature() != null) {
0N/A return (TypeVariable<Constructor<T>>[])getGenericInfo().getTypeParameters();
0N/A } else
0N/A return (TypeVariable<Constructor<T>>[])new TypeVariable[0];
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of {@code Class} objects that represent the formal
0N/A * parameter types, in declaration order, of the constructor
0N/A * represented by this {@code Constructor} object. Returns an array of
0N/A * length 0 if the underlying constructor takes no parameters.
0N/A *
0N/A * @return the parameter types for the constructor this object
0N/A * represents
0N/A */
0N/A public Class<?>[] getParameterTypes() {
0N/A return (Class<?>[]) parameterTypes.clone();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of {@code Type} objects that represent the formal
0N/A * parameter types, in declaration order, of the method represented by
0N/A * this {@code Constructor} object. Returns an array of length 0 if the
0N/A * underlying method takes no parameters.
0N/A *
0N/A * <p>If a formal parameter type is a parameterized type,
0N/A * the {@code Type} object returned for it must accurately reflect
0N/A * the actual type parameters used in the source code.
0N/A *
0N/A * <p>If a formal parameter type is a type variable or a parameterized
0N/A * type, it is created. Otherwise, it is resolved.
0N/A *
0N/A * @return an array of {@code Type}s that represent the formal
0N/A * parameter types of the underlying method, in declaration order
0N/A * @throws GenericSignatureFormatError
0N/A * if the generic method signature does not conform to the format
4008N/A * specified in
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>
0N/A * @throws TypeNotPresentException if any of the parameter
0N/A * types of the underlying method refers to a non-existent type
0N/A * declaration
0N/A * @throws MalformedParameterizedTypeException if any of
0N/A * the underlying method's parameter types refer to a parameterized
0N/A * type that cannot be instantiated for any reason
0N/A * @since 1.5
0N/A */
0N/A public Type[] getGenericParameterTypes() {
0N/A if (getSignature() != null)
0N/A return getGenericInfo().getParameterTypes();
0N/A else
0N/A return getParameterTypes();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of {@code Class} objects that represent the types
0N/A * of exceptions declared to be thrown by the underlying constructor
0N/A * represented by this {@code Constructor} object. Returns an array of
0N/A * length 0 if the constructor declares no exceptions in its {@code throws} clause.
0N/A *
0N/A * @return the exception types declared as being thrown by the
0N/A * constructor this object represents
0N/A */
0N/A public Class<?>[] getExceptionTypes() {
0N/A return (Class<?>[])exceptionTypes.clone();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of {@code Type} objects that represent the
0N/A * exceptions declared to be thrown by this {@code Constructor} object.
0N/A * Returns an array of length 0 if the underlying method declares
0N/A * no exceptions in its {@code throws} clause.
0N/A *
0N/A * <p>If an exception type is a type variable or a parameterized
0N/A * type, it is created. Otherwise, it is resolved.
0N/A *
0N/A * @return an array of Types that represent the exception types
0N/A * thrown by the underlying method
0N/A * @throws GenericSignatureFormatError
0N/A * if the generic method signature does not conform to the format
4008N/A * specified in
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>
0N/A * @throws TypeNotPresentException if the underlying method's
0N/A * {@code throws} clause refers to a non-existent type declaration
0N/A * @throws MalformedParameterizedTypeException if
0N/A * the underlying method's {@code throws} clause refers to a
0N/A * parameterized type that cannot be instantiated for any reason
0N/A * @since 1.5
0N/A */
0N/A public Type[] getGenericExceptionTypes() {
0N/A Type[] result;
0N/A if (getSignature() != null &&
0N/A ( (result = getGenericInfo().getExceptionTypes()).length > 0 ))
0N/A return result;
0N/A else
0N/A return getExceptionTypes();
0N/A }
0N/A
0N/A /**
0N/A * Compares this {@code Constructor} against the specified object.
0N/A * Returns true if the objects are the same. Two {@code Constructor} objects are
0N/A * the same if they were declared by the same class and have the
0N/A * same formal parameter types.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj != null && obj instanceof Constructor) {
1717N/A Constructor<?> other = (Constructor<?>)obj;
0N/A if (getDeclaringClass() == other.getDeclaringClass()) {
0N/A /* Avoid unnecessary cloning */
1717N/A Class<?>[] params1 = parameterTypes;
1717N/A Class<?>[] params2 = other.parameterTypes;
0N/A if (params1.length == params2.length) {
0N/A for (int i = 0; i < params1.length; i++) {
0N/A if (params1[i] != params2[i])
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this {@code Constructor}. The hashcode is
0N/A * the same as the hashcode for the underlying constructor's
0N/A * declaring class name.
0N/A */
0N/A public int hashCode() {
0N/A return getDeclaringClass().getName().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing this {@code Constructor}. The string is
0N/A * formatted as the constructor access modifiers, if any,
0N/A * followed by the fully-qualified name of the declaring class,
0N/A * followed by a parenthesized, comma-separated list of the
0N/A * constructor's formal parameter types. For example:
0N/A * <pre>
0N/A * public java.util.Hashtable(int,float)
0N/A * </pre>
0N/A *
0N/A * <p>The only possible modifiers for constructors are the access
0N/A * modifiers {@code public}, {@code protected} or
0N/A * {@code private}. Only one of these may appear, or none if the
0N/A * constructor has default (package) access.
0N/A */
0N/A public String toString() {
0N/A try {
0N/A StringBuffer sb = new StringBuffer();
1600N/A int mod = getModifiers() & Modifier.constructorModifiers();
0N/A if (mod != 0) {
0N/A sb.append(Modifier.toString(mod) + " ");
0N/A }
0N/A sb.append(Field.getTypeName(getDeclaringClass()));
0N/A sb.append("(");
1717N/A Class<?>[] params = parameterTypes; // avoid clone
0N/A for (int j = 0; j < params.length; j++) {
0N/A sb.append(Field.getTypeName(params[j]));
0N/A if (j < (params.length - 1))
0N/A sb.append(",");
0N/A }
0N/A sb.append(")");
1717N/A Class<?>[] exceptions = exceptionTypes; // avoid clone
0N/A if (exceptions.length > 0) {
0N/A sb.append(" throws ");
0N/A for (int k = 0; k < exceptions.length; k++) {
0N/A sb.append(exceptions[k].getName());
0N/A if (k < (exceptions.length - 1))
0N/A sb.append(",");
0N/A }
0N/A }
0N/A return sb.toString();
0N/A } catch (Exception e) {
0N/A return "<" + e + ">";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing this {@code Constructor},
0N/A * including type parameters. The string is formatted as the
0N/A * constructor access modifiers, if any, followed by an
0N/A * angle-bracketed comma separated list of the constructor's type
0N/A * parameters, if any, followed by the fully-qualified name of the
0N/A * declaring class, followed by a parenthesized, comma-separated
0N/A * list of the constructor's generic formal parameter types.
0N/A *
0N/A * If this constructor was declared to take a variable number of
0N/A * arguments, instead of denoting the last parameter as
0N/A * "<tt><i>Type</i>[]</tt>", it is denoted as
0N/A * "<tt><i>Type</i>...</tt>".
0N/A *
0N/A * A space is used to separate access modifiers from one another
0N/A * and from the type parameters or return type. If there are no
0N/A * type parameters, the type parameter list is elided; if the type
0N/A * parameter list is present, a space separates the list from the
0N/A * class name. If the constructor is declared to throw
0N/A * exceptions, the parameter list is followed by a space, followed
0N/A * by the word "{@code throws}" followed by a
0N/A * comma-separated list of the thrown exception types.
0N/A *
0N/A * <p>The only possible modifiers for constructors are the access
0N/A * modifiers {@code public}, {@code protected} or
0N/A * {@code private}. Only one of these may appear, or none if the
0N/A * constructor has default (package) access.
0N/A *
0N/A * @return a string describing this {@code Constructor},
0N/A * include type parameters
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public String toGenericString() {
0N/A try {
0N/A StringBuilder sb = new StringBuilder();
1600N/A int mod = getModifiers() & Modifier.constructorModifiers();
0N/A if (mod != 0) {
0N/A sb.append(Modifier.toString(mod) + " ");
0N/A }
0N/A TypeVariable<?>[] typeparms = getTypeParameters();
0N/A if (typeparms.length > 0) {
0N/A boolean first = true;
0N/A sb.append("<");
0N/A for(TypeVariable<?> typeparm: typeparms) {
0N/A if (!first)
0N/A sb.append(",");
0N/A // Class objects can't occur here; no need to test
0N/A // and call Class.getName().
0N/A sb.append(typeparm.toString());
0N/A first = false;
0N/A }
0N/A sb.append("> ");
0N/A }
0N/A sb.append(Field.getTypeName(getDeclaringClass()));
0N/A sb.append("(");
0N/A Type[] params = getGenericParameterTypes();
0N/A for (int j = 0; j < params.length; j++) {
0N/A String param = (params[j] instanceof Class<?>)?
0N/A Field.getTypeName((Class<?>)params[j]):
0N/A (params[j].toString());
0N/A if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
0N/A param = param.replaceFirst("\\[\\]$", "...");
0N/A sb.append(param);
0N/A if (j < (params.length - 1))
0N/A sb.append(",");
0N/A }
0N/A sb.append(")");
0N/A Type[] exceptions = getGenericExceptionTypes();
0N/A if (exceptions.length > 0) {
0N/A sb.append(" throws ");
0N/A for (int k = 0; k < exceptions.length; k++) {
0N/A sb.append((exceptions[k] instanceof Class)?
1717N/A ((Class<?>)exceptions[k]).getName():
0N/A exceptions[k].toString());
0N/A if (k < (exceptions.length - 1))
0N/A sb.append(",");
0N/A }
0N/A }
0N/A return sb.toString();
0N/A } catch (Exception e) {
0N/A return "<" + e + ">";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Uses the constructor represented by this {@code Constructor} object to
0N/A * create and initialize a new instance of the constructor's
0N/A * declaring class, with the specified initialization parameters.
0N/A * Individual parameters are automatically unwrapped to match
0N/A * primitive formal parameters, and both primitive and reference
0N/A * parameters are subject to method invocation conversions as necessary.
0N/A *
0N/A * <p>If the number of formal parameters required by the underlying constructor
0N/A * is 0, the supplied {@code initargs} array may be of length 0 or null.
0N/A *
0N/A * <p>If the constructor's declaring class is an inner class in a
0N/A * non-static context, the first argument to the constructor needs
4008N/A * to be the enclosing instance; see section 15.9.3 of
4008N/A * <cite>The Java&trade; Language Specification</cite>.
0N/A *
0N/A * <p>If the required access and argument checks succeed and the
0N/A * instantiation will proceed, the constructor's declaring class
0N/A * is initialized if it has not already been initialized.
0N/A *
0N/A * <p>If the constructor completes normally, returns the newly
0N/A * created and initialized instance.
0N/A *
0N/A * @param initargs array of objects to be passed as arguments to
0N/A * the constructor call; values of primitive types are wrapped in
0N/A * a wrapper object of the appropriate type (e.g. a {@code float}
0N/A * in a {@link java.lang.Float Float})
0N/A *
0N/A * @return a new object created by calling the constructor
0N/A * this object represents
0N/A *
0N/A * @exception IllegalAccessException if this {@code Constructor} object
3896N/A * is enforcing Java language access control and the underlying
0N/A * constructor is inaccessible.
0N/A * @exception IllegalArgumentException if the number of actual
0N/A * and formal parameters differ; if an unwrapping
0N/A * conversion for primitive arguments fails; or if,
0N/A * after possible unwrapping, a parameter value
0N/A * cannot be converted to the corresponding formal
0N/A * parameter type by a method invocation conversion; if
0N/A * this constructor pertains to an enum type.
0N/A * @exception InstantiationException if the class that declares the
0N/A * underlying constructor represents an abstract class.
0N/A * @exception InvocationTargetException if the underlying constructor
0N/A * throws an exception.
0N/A * @exception ExceptionInInitializerError if the initialization provoked
0N/A * by this method fails.
0N/A */
6338N/A @CallerSensitive
0N/A public T newInstance(Object ... initargs)
0N/A throws InstantiationException, IllegalAccessException,
0N/A IllegalArgumentException, InvocationTargetException
0N/A {
0N/A if (!override) {
0N/A if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
6338N/A Class<?> caller = Reflection.getCallerClass();
3903N/A checkAccess(caller, clazz, null, modifiers);
0N/A }
0N/A }
0N/A if ((clazz.getModifiers() & Modifier.ENUM) != 0)
0N/A throw new IllegalArgumentException("Cannot reflectively create enum objects");
3903N/A ConstructorAccessor ca = constructorAccessor; // read volatile
3903N/A if (ca == null) {
3903N/A ca = acquireConstructorAccessor();
3903N/A }
3903N/A return (T) ca.newInstance(initargs);
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this constructor was declared to take
0N/A * a variable number of arguments; returns {@code false}
0N/A * otherwise.
0N/A *
0N/A * @return {@code true} if an only if this constructor was declared to
0N/A * take a variable number of arguments.
0N/A * @since 1.5
0N/A */
0N/A public boolean isVarArgs() {
0N/A return (getModifiers() & Modifier.VARARGS) != 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this constructor is a synthetic
0N/A * constructor; returns {@code false} otherwise.
0N/A *
0N/A * @return true if and only if this constructor is a synthetic
4008N/A * constructor as defined by
4008N/A * <cite>The Java&trade; Language Specification</cite>.
0N/A * @since 1.5
0N/A */
0N/A public boolean isSynthetic() {
0N/A return Modifier.isSynthetic(getModifiers());
0N/A }
0N/A
0N/A // NOTE that there is no synchronization used here. It is correct
0N/A // (though not efficient) to generate more than one
0N/A // ConstructorAccessor for a given Constructor. However, avoiding
0N/A // synchronization will probably make the implementation more
0N/A // scalable.
3903N/A private ConstructorAccessor acquireConstructorAccessor() {
0N/A // First check to see if one has been created yet, and take it
0N/A // if so.
0N/A ConstructorAccessor tmp = null;
0N/A if (root != null) tmp = root.getConstructorAccessor();
0N/A if (tmp != null) {
0N/A constructorAccessor = tmp;
3903N/A } else {
3903N/A // Otherwise fabricate one and propagate it up to the root
3903N/A tmp = reflectionFactory.newConstructorAccessor(this);
3903N/A setConstructorAccessor(tmp);
0N/A }
3903N/A
3903N/A return tmp;
0N/A }
0N/A
0N/A // Returns ConstructorAccessor for this Constructor object, not
0N/A // looking up the chain to the root
0N/A ConstructorAccessor getConstructorAccessor() {
0N/A return constructorAccessor;
0N/A }
0N/A
0N/A // Sets the ConstructorAccessor for this Constructor object and
0N/A // (recursively) its root
0N/A void setConstructorAccessor(ConstructorAccessor accessor) {
0N/A constructorAccessor = accessor;
0N/A // Propagate up
0N/A if (root != null) {
0N/A root.setConstructorAccessor(accessor);
0N/A }
0N/A }
0N/A
0N/A int getSlot() {
0N/A return slot;
0N/A }
0N/A
0N/A String getSignature() {
0N/A return signature;
0N/A }
0N/A
0N/A byte[] getRawAnnotations() {
0N/A return annotations;
0N/A }
0N/A
0N/A byte[] getRawParameterAnnotations() {
0N/A return parameterAnnotations;
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
0N/A if (annotationClass == null)
0N/A throw new NullPointerException();
0N/A
0N/A return (T) declaredAnnotations().get(annotationClass);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public Annotation[] getDeclaredAnnotations() {
930N/A return AnnotationParser.toArray(declaredAnnotations());
0N/A }
0N/A
1717N/A private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
0N/A
1717N/A private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
0N/A if (declaredAnnotations == null) {
0N/A declaredAnnotations = AnnotationParser.parseAnnotations(
0N/A annotations, sun.misc.SharedSecrets.getJavaLangAccess().
0N/A getConstantPool(getDeclaringClass()),
0N/A getDeclaringClass());
0N/A }
0N/A return declaredAnnotations;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of arrays that represent the annotations on the formal
0N/A * parameters, in declaration order, of the method represented by
0N/A * this {@code Constructor} object. (Returns an array of length zero if the
0N/A * underlying method is parameterless. If the method has one or more
0N/A * parameters, a nested array of length zero is returned for each parameter
0N/A * with no annotations.) The annotation objects contained in the returned
0N/A * arrays are serializable. The caller of this method is free to modify
0N/A * the returned arrays; it will have no effect on the arrays returned to
0N/A * other callers.
0N/A *
0N/A * @return an array of arrays that represent the annotations on the formal
0N/A * parameters, in declaration order, of the method represented by this
0N/A * Constructor object
0N/A * @since 1.5
0N/A */
0N/A public Annotation[][] getParameterAnnotations() {
0N/A int numParameters = parameterTypes.length;
0N/A if (parameterAnnotations == null)
0N/A return new Annotation[numParameters][0];
0N/A
0N/A Annotation[][] result = AnnotationParser.parseParameterAnnotations(
0N/A parameterAnnotations,
0N/A sun.misc.SharedSecrets.getJavaLangAccess().
0N/A getConstantPool(getDeclaringClass()),
0N/A getDeclaringClass());
0N/A if (result.length != numParameters) {
0N/A Class<?> declaringClass = getDeclaringClass();
0N/A if (declaringClass.isEnum() ||
0N/A declaringClass.isAnonymousClass() ||
0N/A declaringClass.isLocalClass() )
0N/A ; // Can't do reliable parameter counting
0N/A else {
0N/A if (!declaringClass.isMemberClass() || // top-level
0N/A // Check for the enclosing instance parameter for
0N/A // non-static member classes
0N/A (declaringClass.isMemberClass() &&
0N/A ((declaringClass.getModifiers() & Modifier.STATIC) == 0) &&
0N/A result.length + 1 != numParameters) ) {
0N/A throw new AnnotationFormatError(
0N/A "Parameter annotations don't match number of parameters");
0N/A }
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A}