jtulach@258: /* jtulach@258: * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. jtulach@258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@258: * jtulach@258: * This code is free software; you can redistribute it and/or modify it jtulach@258: * under the terms of the GNU General Public License version 2 only, as jtulach@258: * published by the Free Software Foundation. Oracle designates this jtulach@258: * particular file as subject to the "Classpath" exception as provided jtulach@258: * by Oracle in the LICENSE file that accompanied this code. jtulach@258: * jtulach@258: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@258: * version 2 for more details (a copy is included in the LICENSE file that jtulach@258: * accompanied this code). jtulach@258: * jtulach@258: * You should have received a copy of the GNU General Public License version jtulach@258: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@258: * jtulach@258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@258: * or visit www.oracle.com if you need additional information or have any jtulach@258: * questions. jtulach@258: */ jtulach@258: jtulach@258: package java.lang.reflect; jtulach@258: jtulach@258: import sun.reflect.MethodAccessor; jtulach@258: import sun.reflect.Reflection; jtulach@258: import sun.reflect.generics.repository.MethodRepository; jtulach@258: import sun.reflect.generics.factory.CoreReflectionFactory; jtulach@258: import sun.reflect.generics.factory.GenericsFactory; jtulach@258: import sun.reflect.generics.scope.MethodScope; jtulach@258: import sun.reflect.annotation.AnnotationType; jtulach@258: import sun.reflect.annotation.AnnotationParser; jtulach@258: import java.lang.annotation.Annotation; jtulach@258: import java.lang.annotation.AnnotationFormatError; jtulach@258: import java.nio.ByteBuffer; jtulach@258: import java.util.Map; jtulach@258: jtulach@258: /** jtulach@258: * A {@code Method} provides information about, and access to, a single method jtulach@258: * on a class or interface. The reflected method may be a class method jtulach@258: * or an instance method (including an abstract method). jtulach@258: * jtulach@258: *
A {@code Method} permits widening conversions to occur when matching the
jtulach@258: * actual parameters to invoke with the underlying method's formal
jtulach@258: * parameters, but it throws an {@code IllegalArgumentException} if a
jtulach@258: * narrowing conversion would occur.
jtulach@258: *
jtulach@258: * @see Member
jtulach@258: * @see java.lang.Class
jtulach@258: * @see java.lang.Class#getMethods()
jtulach@258: * @see java.lang.Class#getMethod(String, Class[])
jtulach@258: * @see java.lang.Class#getDeclaredMethods()
jtulach@258: * @see java.lang.Class#getDeclaredMethod(String, Class[])
jtulach@258: *
jtulach@258: * @author Kenneth Russell
jtulach@258: * @author Nakul Saraiya
jtulach@258: */
jtulach@258: public final
jtulach@258: class Method extends AccessibleObject implements GenericDeclaration,
jtulach@258: Member {
jtulach@258: private Class> clazz;
jtulach@258: private int slot;
jtulach@258: // This is guaranteed to be interned by the VM in the 1.4
jtulach@258: // reflection implementation
jtulach@258: private String name;
jtulach@258: private Class> returnType;
jtulach@258: private Class>[] parameterTypes;
jtulach@258: private Class>[] exceptionTypes;
jtulach@258: private int modifiers;
jtulach@258: // Generics and annotations support
jtulach@258: private transient String signature;
jtulach@258: // generic info repository; lazily initialized
jtulach@258: private transient MethodRepository genericInfo;
jtulach@258: private byte[] annotations;
jtulach@258: private byte[] parameterAnnotations;
jtulach@258: private byte[] annotationDefault;
jtulach@258: private volatile MethodAccessor methodAccessor;
jtulach@258: // For sharing of MethodAccessors. This branching structure is
jtulach@258: // currently only two levels deep (i.e., one root Method and
jtulach@258: // potentially many Method objects pointing to it.)
jtulach@258: private Method root;
jtulach@258:
jtulach@258: // Generics infrastructure
jtulach@258:
jtulach@258: private String getGenericSignature() {return signature;}
jtulach@258:
jtulach@258: // Accessor for factory
jtulach@258: private GenericsFactory getFactory() {
jtulach@258: // create scope and factory
jtulach@258: return CoreReflectionFactory.make(this, MethodScope.make(this));
jtulach@258: }
jtulach@258:
jtulach@258: // Accessor for generic info repository
jtulach@258: private MethodRepository getGenericInfo() {
jtulach@258: // lazily initialize repository if necessary
jtulach@258: if (genericInfo == null) {
jtulach@258: // create and cache generic info repository
jtulach@258: genericInfo = MethodRepository.make(getGenericSignature(),
jtulach@258: getFactory());
jtulach@258: }
jtulach@258: return genericInfo; //return cached repository
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Package-private constructor used by ReflectAccess to enable
jtulach@258: * instantiation of these objects in Java code from the java.lang
jtulach@258: * package via sun.reflect.LangReflectAccess.
jtulach@258: */
jtulach@258: Method(Class> declaringClass,
jtulach@258: String name,
jtulach@258: Class>[] parameterTypes,
jtulach@258: Class> returnType,
jtulach@258: Class>[] checkedExceptions,
jtulach@258: int modifiers,
jtulach@258: int slot,
jtulach@258: String signature,
jtulach@258: byte[] annotations,
jtulach@258: byte[] parameterAnnotations,
jtulach@258: byte[] annotationDefault)
jtulach@258: {
jtulach@258: this.clazz = declaringClass;
jtulach@258: this.name = name;
jtulach@258: this.parameterTypes = parameterTypes;
jtulach@258: this.returnType = returnType;
jtulach@258: this.exceptionTypes = checkedExceptions;
jtulach@258: this.modifiers = modifiers;
jtulach@258: this.slot = slot;
jtulach@258: this.signature = signature;
jtulach@258: this.annotations = annotations;
jtulach@258: this.parameterAnnotations = parameterAnnotations;
jtulach@258: this.annotationDefault = annotationDefault;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Package-private routine (exposed to java.lang.Class via
jtulach@258: * ReflectAccess) which returns a copy of this Method. The copy's
jtulach@258: * "root" field points to this Method.
jtulach@258: */
jtulach@258: Method copy() {
jtulach@258: // This routine enables sharing of MethodAccessor objects
jtulach@258: // among Method objects which refer to the same underlying
jtulach@258: // method in the VM. (All of this contortion is only necessary
jtulach@258: // because of the "accessibility" bit in AccessibleObject,
jtulach@258: // which implicitly requires that new java.lang.reflect
jtulach@258: // objects be fabricated for each reflective call on Class
jtulach@258: // objects.)
jtulach@258: Method res = new Method(clazz, name, parameterTypes, returnType,
jtulach@258: exceptionTypes, modifiers, slot, signature,
jtulach@258: annotations, parameterAnnotations, annotationDefault);
jtulach@258: res.root = this;
jtulach@258: // Might as well eagerly propagate this if already present
jtulach@258: res.methodAccessor = methodAccessor;
jtulach@258: return res;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns the {@code Class} object representing the class or interface
jtulach@258: * that declares the method represented by this {@code Method} object.
jtulach@258: */
jtulach@258: public Class> getDeclaringClass() {
jtulach@258: return clazz;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns the name of the method represented by this {@code Method}
jtulach@258: * object, as a {@code String}.
jtulach@258: */
jtulach@258: public String getName() {
jtulach@258: return name;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns the Java language modifiers for the method represented
jtulach@258: * by this {@code Method} object, as an integer. The {@code Modifier} class should
jtulach@258: * be used to decode the modifiers.
jtulach@258: *
jtulach@258: * @see Modifier
jtulach@258: */
jtulach@258: public int getModifiers() {
jtulach@258: return modifiers;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns an array of {@code TypeVariable} objects that represent the
jtulach@258: * type variables declared by the generic declaration represented by this
jtulach@258: * {@code GenericDeclaration} object, in declaration order. Returns an
jtulach@258: * array of length 0 if the underlying generic declaration declares no type
jtulach@258: * variables.
jtulach@258: *
jtulach@258: * @return an array of {@code TypeVariable} objects that represent
jtulach@258: * the type variables declared by this generic declaration
jtulach@258: * @throws GenericSignatureFormatError if the generic
jtulach@258: * signature of this generic declaration does not conform to
jtulach@258: * the format specified in
jtulach@258: * The Java™ Virtual Machine Specification
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public TypeVariable If the return type is a parameterized type,
jtulach@258: * the {@code Type} object returned must accurately reflect
jtulach@258: * the actual type parameters used in the source code.
jtulach@258: *
jtulach@258: * If the return type is a type variable or a parameterized type, it
jtulach@258: * is created. Otherwise, it is resolved.
jtulach@258: *
jtulach@258: * @return a {@code Type} object that represents the formal return
jtulach@258: * type of the underlying method
jtulach@258: * @throws GenericSignatureFormatError
jtulach@258: * if the generic method signature does not conform to the format
jtulach@258: * specified in
jtulach@258: * The Java™ Virtual Machine Specification
jtulach@258: * @throws TypeNotPresentException if the underlying method's
jtulach@258: * return type refers to a non-existent type declaration
jtulach@258: * @throws MalformedParameterizedTypeException if the
jtulach@258: * underlying method's return typed refers to a parameterized
jtulach@258: * type that cannot be instantiated for any reason
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public Type getGenericReturnType() {
jtulach@258: if (getGenericSignature() != null) {
jtulach@258: return getGenericInfo().getReturnType();
jtulach@258: } else { return getReturnType();}
jtulach@258: }
jtulach@258:
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns an array of {@code Class} objects that represent the formal
jtulach@258: * parameter types, in declaration order, of the method
jtulach@258: * represented by this {@code Method} object. Returns an array of length
jtulach@258: * 0 if the underlying method takes no parameters.
jtulach@258: *
jtulach@258: * @return the parameter types for the method this object
jtulach@258: * represents
jtulach@258: */
jtulach@258: public Class>[] getParameterTypes() {
jtulach@258: return (Class>[]) parameterTypes.clone();
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns an array of {@code Type} objects that represent the formal
jtulach@258: * parameter types, in declaration order, of the method represented by
jtulach@258: * this {@code Method} object. Returns an array of length 0 if the
jtulach@258: * underlying method takes no parameters.
jtulach@258: *
jtulach@258: * If a formal parameter type is a parameterized type,
jtulach@258: * the {@code Type} object returned for it must accurately reflect
jtulach@258: * the actual type parameters used in the source code.
jtulach@258: *
jtulach@258: * If a formal parameter type is a type variable or a parameterized
jtulach@258: * type, it is created. Otherwise, it is resolved.
jtulach@258: *
jtulach@258: * @return an array of Types that represent the formal
jtulach@258: * parameter types of the underlying method, in declaration order
jtulach@258: * @throws GenericSignatureFormatError
jtulach@258: * if the generic method signature does not conform to the format
jtulach@258: * specified in
jtulach@258: * The Java™ Virtual Machine Specification
jtulach@258: * @throws TypeNotPresentException if any of the parameter
jtulach@258: * types of the underlying method refers to a non-existent type
jtulach@258: * declaration
jtulach@258: * @throws MalformedParameterizedTypeException if any of
jtulach@258: * the underlying method's parameter types refer to a parameterized
jtulach@258: * type that cannot be instantiated for any reason
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public Type[] getGenericParameterTypes() {
jtulach@258: if (getGenericSignature() != null)
jtulach@258: return getGenericInfo().getParameterTypes();
jtulach@258: else
jtulach@258: return getParameterTypes();
jtulach@258: }
jtulach@258:
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns an array of {@code Class} objects that represent
jtulach@258: * the types of the exceptions declared to be thrown
jtulach@258: * by the underlying method
jtulach@258: * represented by this {@code Method} object. Returns an array of length
jtulach@258: * 0 if the method declares no exceptions in its {@code throws} clause.
jtulach@258: *
jtulach@258: * @return the exception types declared as being thrown by the
jtulach@258: * method this object represents
jtulach@258: */
jtulach@258: public Class>[] getExceptionTypes() {
jtulach@258: return (Class>[]) exceptionTypes.clone();
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns an array of {@code Type} objects that represent the
jtulach@258: * exceptions declared to be thrown by this {@code Method} object.
jtulach@258: * Returns an array of length 0 if the underlying method declares
jtulach@258: * no exceptions in its {@code throws} clause.
jtulach@258: *
jtulach@258: * If an exception type is a type variable or a parameterized
jtulach@258: * type, it is created. Otherwise, it is resolved.
jtulach@258: *
jtulach@258: * @return an array of Types that represent the exception types
jtulach@258: * thrown by the underlying method
jtulach@258: * @throws GenericSignatureFormatError
jtulach@258: * if the generic method signature does not conform to the format
jtulach@258: * specified in
jtulach@258: * The Java™ Virtual Machine Specification
jtulach@258: * @throws TypeNotPresentException if the underlying method's
jtulach@258: * {@code throws} clause refers to a non-existent type declaration
jtulach@258: * @throws MalformedParameterizedTypeException if
jtulach@258: * the underlying method's {@code throws} clause refers to a
jtulach@258: * parameterized type that cannot be instantiated for any reason
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public Type[] getGenericExceptionTypes() {
jtulach@258: Type[] result;
jtulach@258: if (getGenericSignature() != null &&
jtulach@258: ((result = getGenericInfo().getExceptionTypes()).length > 0))
jtulach@258: return result;
jtulach@258: else
jtulach@258: return getExceptionTypes();
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Compares this {@code Method} against the specified object. Returns
jtulach@258: * true if the objects are the same. Two {@code Methods} are the same if
jtulach@258: * they were declared by the same class and have the same name
jtulach@258: * and formal parameter types and return type.
jtulach@258: */
jtulach@258: public boolean equals(Object obj) {
jtulach@258: if (obj != null && obj instanceof Method) {
jtulach@258: Method other = (Method)obj;
jtulach@258: if ((getDeclaringClass() == other.getDeclaringClass())
jtulach@258: && (getName() == other.getName())) {
jtulach@258: if (!returnType.equals(other.getReturnType()))
jtulach@258: return false;
jtulach@258: /* Avoid unnecessary cloning */
jtulach@258: Class>[] params1 = parameterTypes;
jtulach@258: Class>[] params2 = other.parameterTypes;
jtulach@258: if (params1.length == params2.length) {
jtulach@258: for (int i = 0; i < params1.length; i++) {
jtulach@258: if (params1[i] != params2[i])
jtulach@258: return false;
jtulach@258: }
jtulach@258: return true;
jtulach@258: }
jtulach@258: }
jtulach@258: }
jtulach@258: return false;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns a hashcode for this {@code Method}. The hashcode is computed
jtulach@258: * as the exclusive-or of the hashcodes for the underlying
jtulach@258: * method's declaring class name and the method's name.
jtulach@258: */
jtulach@258: public int hashCode() {
jtulach@258: return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns a string describing this {@code Method}. The string is
jtulach@258: * formatted as the method access modifiers, if any, followed by
jtulach@258: * the method return type, followed by a space, followed by the
jtulach@258: * class declaring the method, followed by a period, followed by
jtulach@258: * the method name, followed by a parenthesized, comma-separated
jtulach@258: * list of the method's formal parameter types. If the method
jtulach@258: * throws checked exceptions, the parameter list is followed by a
jtulach@258: * space, followed by the word throws followed by a
jtulach@258: * comma-separated list of the thrown exception types.
jtulach@258: * For example:
jtulach@258: * The access modifiers are placed in canonical order as
jtulach@258: * specified by "The Java Language Specification". This is
jtulach@258: * {@code public}, {@code protected} or {@code private} first,
jtulach@258: * and then other modifiers in the following order:
jtulach@258: * {@code abstract}, {@code static}, {@code final},
jtulach@258: * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258: */
jtulach@258: public String toString() {
jtulach@258: try {
jtulach@258: StringBuilder sb = new StringBuilder();
jtulach@258: int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258: if (mod != 0) {
jtulach@258: sb.append(Modifier.toString(mod)).append(' ');
jtulach@258: }
jtulach@258: sb.append(Field.getTypeName(getReturnType())).append(' ');
jtulach@258: sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258: sb.append(getName()).append('(');
jtulach@258: Class>[] params = parameterTypes; // avoid clone
jtulach@258: for (int j = 0; j < params.length; j++) {
jtulach@258: sb.append(Field.getTypeName(params[j]));
jtulach@258: if (j < (params.length - 1))
jtulach@258: sb.append(',');
jtulach@258: }
jtulach@258: sb.append(')');
jtulach@258: Class>[] exceptions = exceptionTypes; // avoid clone
jtulach@258: if (exceptions.length > 0) {
jtulach@258: sb.append(" throws ");
jtulach@258: for (int k = 0; k < exceptions.length; k++) {
jtulach@258: sb.append(exceptions[k].getName());
jtulach@258: if (k < (exceptions.length - 1))
jtulach@258: sb.append(',');
jtulach@258: }
jtulach@258: }
jtulach@258: return sb.toString();
jtulach@258: } catch (Exception e) {
jtulach@258: return "<" + e + ">";
jtulach@258: }
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns a string describing this {@code Method}, including
jtulach@258: * type parameters. The string is formatted as the method access
jtulach@258: * modifiers, if any, followed by an angle-bracketed
jtulach@258: * comma-separated list of the method's type parameters, if any,
jtulach@258: * followed by the method's generic return type, followed by a
jtulach@258: * space, followed by the class declaring the method, followed by
jtulach@258: * a period, followed by the method name, followed by a
jtulach@258: * parenthesized, comma-separated list of the method's generic
jtulach@258: * formal parameter types.
jtulach@258: *
jtulach@258: * If this method was declared to take a variable number of
jtulach@258: * arguments, instead of denoting the last parameter as
jtulach@258: * "Type[]", it is denoted as
jtulach@258: * "Type...".
jtulach@258: *
jtulach@258: * A space is used to separate access modifiers from one another
jtulach@258: * and from the type parameters or return type. If there are no
jtulach@258: * type parameters, the type parameter list is elided; if the type
jtulach@258: * parameter list is present, a space separates the list from the
jtulach@258: * class name. If the method is declared to throw exceptions, the
jtulach@258: * parameter list is followed by a space, followed by the word
jtulach@258: * throws followed by a comma-separated list of the generic thrown
jtulach@258: * exception types. If there are no type parameters, the type
jtulach@258: * parameter list is elided.
jtulach@258: *
jtulach@258: * The access modifiers are placed in canonical order as
jtulach@258: * specified by "The Java Language Specification". This is
jtulach@258: * {@code public}, {@code protected} or {@code private} first,
jtulach@258: * and then other modifiers in the following order:
jtulach@258: * {@code abstract}, {@code static}, {@code final},
jtulach@258: * {@code synchronized}, {@code native}, {@code strictfp}.
jtulach@258: *
jtulach@258: * @return a string describing this {@code Method},
jtulach@258: * include type parameters
jtulach@258: *
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public String toGenericString() {
jtulach@258: try {
jtulach@258: StringBuilder sb = new StringBuilder();
jtulach@258: int mod = getModifiers() & Modifier.methodModifiers();
jtulach@258: if (mod != 0) {
jtulach@258: sb.append(Modifier.toString(mod)).append(' ');
jtulach@258: }
jtulach@258: TypeVariable>[] typeparms = getTypeParameters();
jtulach@258: if (typeparms.length > 0) {
jtulach@258: boolean first = true;
jtulach@258: sb.append('<');
jtulach@258: for(TypeVariable> typeparm: typeparms) {
jtulach@258: if (!first)
jtulach@258: sb.append(',');
jtulach@258: // Class objects can't occur here; no need to test
jtulach@258: // and call Class.getName().
jtulach@258: sb.append(typeparm.toString());
jtulach@258: first = false;
jtulach@258: }
jtulach@258: sb.append("> ");
jtulach@258: }
jtulach@258:
jtulach@258: Type genRetType = getGenericReturnType();
jtulach@258: sb.append( ((genRetType instanceof Class>)?
jtulach@258: Field.getTypeName((Class>)genRetType):genRetType.toString()))
jtulach@258: .append(' ');
jtulach@258:
jtulach@258: sb.append(Field.getTypeName(getDeclaringClass())).append('.');
jtulach@258: sb.append(getName()).append('(');
jtulach@258: Type[] params = getGenericParameterTypes();
jtulach@258: for (int j = 0; j < params.length; j++) {
jtulach@258: String param = (params[j] instanceof Class)?
jtulach@258: Field.getTypeName((Class)params[j]):
jtulach@258: (params[j].toString());
jtulach@258: if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
jtulach@258: param = param.replaceFirst("\\[\\]$", "...");
jtulach@258: sb.append(param);
jtulach@258: if (j < (params.length - 1))
jtulach@258: sb.append(',');
jtulach@258: }
jtulach@258: sb.append(')');
jtulach@258: Type[] exceptions = getGenericExceptionTypes();
jtulach@258: if (exceptions.length > 0) {
jtulach@258: sb.append(" throws ");
jtulach@258: for (int k = 0; k < exceptions.length; k++) {
jtulach@258: sb.append((exceptions[k] instanceof Class)?
jtulach@258: ((Class)exceptions[k]).getName():
jtulach@258: exceptions[k].toString());
jtulach@258: if (k < (exceptions.length - 1))
jtulach@258: sb.append(',');
jtulach@258: }
jtulach@258: }
jtulach@258: return sb.toString();
jtulach@258: } catch (Exception e) {
jtulach@258: return "<" + e + ">";
jtulach@258: }
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Invokes the underlying method represented by this {@code Method}
jtulach@258: * object, on the specified object with the specified parameters.
jtulach@258: * Individual parameters are automatically unwrapped to match
jtulach@258: * primitive formal parameters, and both primitive and reference
jtulach@258: * parameters are subject to method invocation conversions as
jtulach@258: * necessary.
jtulach@258: *
jtulach@258: * If the underlying method is static, then the specified {@code obj}
jtulach@258: * argument is ignored. It may be null.
jtulach@258: *
jtulach@258: * If the number of formal parameters required by the underlying method is
jtulach@258: * 0, the supplied {@code args} array may be of length 0 or null.
jtulach@258: *
jtulach@258: * If the underlying method is an instance method, it is invoked
jtulach@258: * using dynamic method lookup as documented in The Java Language
jtulach@258: * Specification, Second Edition, section 15.12.4.4; in particular,
jtulach@258: * overriding based on the runtime type of the target object will occur.
jtulach@258: *
jtulach@258: * If the underlying method is static, the class that declared
jtulach@258: * the method is initialized if it has not already been initialized.
jtulach@258: *
jtulach@258: * If the method completes normally, the value it returns is
jtulach@258: * returned to the caller of invoke; if the value has a primitive
jtulach@258: * type, it is first appropriately wrapped in an object. However,
jtulach@258: * if the value has the type of an array of a primitive type, the
jtulach@258: * elements of the array are not wrapped in objects; in
jtulach@258: * other words, an array of primitive type is returned. If the
jtulach@258: * underlying method return type is void, the invocation returns
jtulach@258: * null.
jtulach@258: *
jtulach@258: * @param obj the object the underlying method is invoked from
jtulach@258: * @param args the arguments used for the method call
jtulach@258: * @return the result of dispatching the method represented by
jtulach@258: * this object on {@code obj} with parameters
jtulach@258: * {@code args}
jtulach@258: *
jtulach@258: * @exception IllegalAccessException if this {@code Method} object
jtulach@258: * is enforcing Java language access control and the underlying
jtulach@258: * method is inaccessible.
jtulach@258: * @exception IllegalArgumentException if the method is an
jtulach@258: * instance method and the specified object argument
jtulach@258: * is not an instance of the class or interface
jtulach@258: * declaring the underlying method (or of a subclass
jtulach@258: * or implementor thereof); if the number of actual
jtulach@258: * and formal parameters differ; if an unwrapping
jtulach@258: * conversion for primitive arguments fails; or if,
jtulach@258: * after possible unwrapping, a parameter value
jtulach@258: * cannot be converted to the corresponding formal
jtulach@258: * parameter type by a method invocation conversion.
jtulach@258: * @exception InvocationTargetException if the underlying method
jtulach@258: * throws an exception.
jtulach@258: * @exception NullPointerException if the specified object is null
jtulach@258: * and the method is an instance method.
jtulach@258: * @exception ExceptionInInitializerError if the initialization
jtulach@258: * provoked by this method fails.
jtulach@258: */
jtulach@258: public Object invoke(Object obj, Object... args)
jtulach@258: throws IllegalAccessException, IllegalArgumentException,
jtulach@258: InvocationTargetException
jtulach@258: {
jtulach@258: if (!override) {
jtulach@258: if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
jtulach@258: Class> caller = Reflection.getCallerClass(1);
jtulach@258:
jtulach@258: checkAccess(caller, clazz, obj, modifiers);
jtulach@258: }
jtulach@258: }
jtulach@258: MethodAccessor ma = methodAccessor; // read volatile
jtulach@258: if (ma == null) {
jtulach@258: ma = acquireMethodAccessor();
jtulach@258: }
jtulach@258: return ma.invoke(obj, args);
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns {@code true} if this method is a bridge
jtulach@258: * method; returns {@code false} otherwise.
jtulach@258: *
jtulach@258: * @return true if and only if this method is a bridge
jtulach@258: * method as defined by the Java Language Specification.
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public boolean isBridge() {
jtulach@258: return (getModifiers() & Modifier.BRIDGE) != 0;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns {@code true} if this method was declared to take
jtulach@258: * a variable number of arguments; returns {@code false}
jtulach@258: * otherwise.
jtulach@258: *
jtulach@258: * @return {@code true} if an only if this method was declared to
jtulach@258: * take a variable number of arguments.
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public boolean isVarArgs() {
jtulach@258: return (getModifiers() & Modifier.VARARGS) != 0;
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * Returns {@code true} if this method is a synthetic
jtulach@258: * method; returns {@code false} otherwise.
jtulach@258: *
jtulach@258: * @return true if and only if this method is a synthetic
jtulach@258: * method as defined by the Java Language Specification.
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public boolean isSynthetic() {
jtulach@258: return Modifier.isSynthetic(getModifiers());
jtulach@258: }
jtulach@258:
jtulach@258: // NOTE that there is no synchronization used here. It is correct
jtulach@258: // (though not efficient) to generate more than one MethodAccessor
jtulach@258: // for a given Method. However, avoiding synchronization will
jtulach@258: // probably make the implementation more scalable.
jtulach@258: private MethodAccessor acquireMethodAccessor() {
jtulach@258: // First check to see if one has been created yet, and take it
jtulach@258: // if so
jtulach@258: MethodAccessor tmp = null;
jtulach@258: if (root != null) tmp = root.getMethodAccessor();
jtulach@258: if (tmp != null) {
jtulach@258: methodAccessor = tmp;
jtulach@258: } else {
jtulach@258: // Otherwise fabricate one and propagate it up to the root
jtulach@258: tmp = reflectionFactory.newMethodAccessor(this);
jtulach@258: setMethodAccessor(tmp);
jtulach@258: }
jtulach@258:
jtulach@258: return tmp;
jtulach@258: }
jtulach@258:
jtulach@258: // Returns MethodAccessor for this Method object, not looking up
jtulach@258: // the chain to the root
jtulach@258: MethodAccessor getMethodAccessor() {
jtulach@258: return methodAccessor;
jtulach@258: }
jtulach@258:
jtulach@258: // Sets the MethodAccessor for this Method object and
jtulach@258: // (recursively) its root
jtulach@258: void setMethodAccessor(MethodAccessor accessor) {
jtulach@258: methodAccessor = accessor;
jtulach@258: // Propagate up
jtulach@258: if (root != null) {
jtulach@258: root.setMethodAccessor(accessor);
jtulach@258: }
jtulach@258: }
jtulach@258:
jtulach@258: /**
jtulach@258: * @throws NullPointerException {@inheritDoc}
jtulach@258: * @since 1.5
jtulach@258: */
jtulach@258: public
jtulach@258: * public boolean java.lang.Object.equals(java.lang.Object)
jtulach@258: *
jtulach@258: *
jtulach@258: *