jaroslav@601: /* jaroslav@601: * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.lang.reflect; jaroslav@601: jaroslav@601: import java.lang.annotation.Annotation; jaroslav@604: import org.apidesign.bck2brwsr.emul.reflect.TypeProvider; jaroslav@601: jaroslav@601: /** jaroslav@601: * {@code Constructor} provides information about, and access to, a single jaroslav@601: * constructor for a class. jaroslav@601: * jaroslav@601: *
{@code Constructor} permits widening conversions to occur when matching the
jaroslav@601: * actual parameters to newInstance() with the underlying
jaroslav@601: * constructor's formal parameters, but throws an
jaroslav@601: * {@code IllegalArgumentException} if a narrowing conversion would occur.
jaroslav@601: *
jaroslav@601: * @param If a formal parameter type is a parameterized type,
jaroslav@601: * the {@code Type} object returned for it must accurately reflect
jaroslav@601: * the actual type parameters used in the source code.
jaroslav@601: *
jaroslav@601: * If a formal parameter type is a type variable or a parameterized
jaroslav@601: * type, it is created. Otherwise, it is resolved.
jaroslav@601: *
jaroslav@601: * @return an array of {@code Type}s that represent the formal
jaroslav@601: * parameter types of the underlying method, in declaration order
jaroslav@601: * @throws GenericSignatureFormatError
jaroslav@601: * if the generic method signature does not conform to the format
jaroslav@601: * specified in
jaroslav@601: * The Java™ Virtual Machine Specification
jaroslav@601: * @throws TypeNotPresentException if any of the parameter
jaroslav@601: * types of the underlying method refers to a non-existent type
jaroslav@601: * declaration
jaroslav@601: * @throws MalformedParameterizedTypeException if any of
jaroslav@601: * the underlying method's parameter types refer to a parameterized
jaroslav@601: * type that cannot be instantiated for any reason
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public Type[] getGenericParameterTypes() {
jaroslav@604: return TypeProvider.getDefault().getGenericParameterTypes(this);
jaroslav@601: }
jaroslav@601:
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns an array of {@code Class} objects that represent the types
jaroslav@601: * of exceptions declared to be thrown by the underlying constructor
jaroslav@601: * represented by this {@code Constructor} object. Returns an array of
jaroslav@601: * length 0 if the constructor declares no exceptions in its {@code throws} clause.
jaroslav@601: *
jaroslav@601: * @return the exception types declared as being thrown by the
jaroslav@601: * constructor this object represents
jaroslav@601: */
jaroslav@601: public Class>[] getExceptionTypes() {
jaroslav@601: return (Class>[])exceptionTypes.clone();
jaroslav@601: }
jaroslav@601:
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns an array of {@code Type} objects that represent the
jaroslav@601: * exceptions declared to be thrown by this {@code Constructor} object.
jaroslav@601: * Returns an array of length 0 if the underlying method declares
jaroslav@601: * no exceptions in its {@code throws} clause.
jaroslav@601: *
jaroslav@601: * If an exception type is a type variable or a parameterized
jaroslav@601: * type, it is created. Otherwise, it is resolved.
jaroslav@601: *
jaroslav@601: * @return an array of Types that represent the exception types
jaroslav@601: * thrown by the underlying method
jaroslav@601: * @throws GenericSignatureFormatError
jaroslav@601: * if the generic method signature does not conform to the format
jaroslav@601: * specified in
jaroslav@601: * The Java™ Virtual Machine Specification
jaroslav@601: * @throws TypeNotPresentException if the underlying method's
jaroslav@601: * {@code throws} clause refers to a non-existent type declaration
jaroslav@601: * @throws MalformedParameterizedTypeException if
jaroslav@601: * the underlying method's {@code throws} clause refers to a
jaroslav@601: * parameterized type that cannot be instantiated for any reason
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public Type[] getGenericExceptionTypes() {
jaroslav@604: return TypeProvider.getDefault().getGenericExceptionTypes(this);
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Compares this {@code Constructor} against the specified object.
jaroslav@601: * Returns true if the objects are the same. Two {@code Constructor} objects are
jaroslav@601: * the same if they were declared by the same class and have the
jaroslav@601: * same formal parameter types.
jaroslav@601: */
jaroslav@601: public boolean equals(Object obj) {
jaroslav@601: if (obj != null && obj instanceof Constructor) {
jaroslav@601: Constructor> other = (Constructor>)obj;
jaroslav@601: if (getDeclaringClass() == other.getDeclaringClass()) {
jaroslav@601: /* Avoid unnecessary cloning */
jaroslav@601: Class>[] params1 = parameterTypes;
jaroslav@601: Class>[] params2 = other.parameterTypes;
jaroslav@601: if (params1.length == params2.length) {
jaroslav@601: for (int i = 0; i < params1.length; i++) {
jaroslav@601: if (params1[i] != params2[i])
jaroslav@601: return false;
jaroslav@601: }
jaroslav@601: return true;
jaroslav@601: }
jaroslav@601: }
jaroslav@601: }
jaroslav@601: return false;
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns a hashcode for this {@code Constructor}. The hashcode is
jaroslav@601: * the same as the hashcode for the underlying constructor's
jaroslav@601: * declaring class name.
jaroslav@601: */
jaroslav@601: public int hashCode() {
jaroslav@601: return getDeclaringClass().getName().hashCode();
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns a string describing this {@code Constructor}. The string is
jaroslav@601: * formatted as the constructor access modifiers, if any,
jaroslav@601: * followed by the fully-qualified name of the declaring class,
jaroslav@601: * followed by a parenthesized, comma-separated list of the
jaroslav@601: * constructor's formal parameter types. For example:
jaroslav@601: * The only possible modifiers for constructors are the access
jaroslav@601: * modifiers {@code public}, {@code protected} or
jaroslav@601: * {@code private}. Only one of these may appear, or none if the
jaroslav@601: * constructor has default (package) access.
jaroslav@601: */
jaroslav@601: public String toString() {
jaroslav@601: try {
jaroslav@601: StringBuffer sb = new StringBuffer();
jaroslav@601: int mod = getModifiers() & Modifier.constructorModifiers();
jaroslav@601: if (mod != 0) {
jaroslav@601: sb.append(Modifier.toString(mod) + " ");
jaroslav@601: }
jaroslav@601: sb.append(Field.getTypeName(getDeclaringClass()));
jaroslav@601: sb.append("(");
jaroslav@601: Class>[] params = parameterTypes; // avoid clone
jaroslav@601: for (int j = 0; j < params.length; j++) {
jaroslav@601: sb.append(Field.getTypeName(params[j]));
jaroslav@601: if (j < (params.length - 1))
jaroslav@601: sb.append(",");
jaroslav@601: }
jaroslav@601: sb.append(")");
jaroslav@601: Class>[] exceptions = exceptionTypes; // avoid clone
jaroslav@601: if (exceptions.length > 0) {
jaroslav@601: sb.append(" throws ");
jaroslav@601: for (int k = 0; k < exceptions.length; k++) {
jaroslav@601: sb.append(exceptions[k].getName());
jaroslav@601: if (k < (exceptions.length - 1))
jaroslav@601: sb.append(",");
jaroslav@601: }
jaroslav@601: }
jaroslav@601: return sb.toString();
jaroslav@601: } catch (Exception e) {
jaroslav@601: return "<" + e + ">";
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns a string describing this {@code Constructor},
jaroslav@601: * including type parameters. The string is formatted as the
jaroslav@601: * constructor access modifiers, if any, followed by an
jaroslav@601: * angle-bracketed comma separated list of the constructor's type
jaroslav@601: * parameters, if any, followed by the fully-qualified name of the
jaroslav@601: * declaring class, followed by a parenthesized, comma-separated
jaroslav@601: * list of the constructor's generic formal parameter types.
jaroslav@601: *
jaroslav@601: * If this constructor was declared to take a variable number of
jaroslav@601: * arguments, instead of denoting the last parameter as
jaroslav@601: * "Type[]", it is denoted as
jaroslav@601: * "Type...".
jaroslav@601: *
jaroslav@601: * A space is used to separate access modifiers from one another
jaroslav@601: * and from the type parameters or return type. If there are no
jaroslav@601: * type parameters, the type parameter list is elided; if the type
jaroslav@601: * parameter list is present, a space separates the list from the
jaroslav@601: * class name. If the constructor is declared to throw
jaroslav@601: * exceptions, the parameter list is followed by a space, followed
jaroslav@601: * by the word "{@code throws}" followed by a
jaroslav@601: * comma-separated list of the thrown exception types.
jaroslav@601: *
jaroslav@601: * The only possible modifiers for constructors are the access
jaroslav@601: * modifiers {@code public}, {@code protected} or
jaroslav@601: * {@code private}. Only one of these may appear, or none if the
jaroslav@601: * constructor has default (package) access.
jaroslav@601: *
jaroslav@601: * @return a string describing this {@code Constructor},
jaroslav@601: * include type parameters
jaroslav@601: *
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public String toGenericString() {
jaroslav@601: try {
jaroslav@601: StringBuilder sb = new StringBuilder();
jaroslav@601: int mod = getModifiers() & Modifier.constructorModifiers();
jaroslav@601: if (mod != 0) {
jaroslav@601: sb.append(Modifier.toString(mod) + " ");
jaroslav@601: }
jaroslav@601: TypeVariable>[] typeparms = getTypeParameters();
jaroslav@601: if (typeparms.length > 0) {
jaroslav@601: boolean first = true;
jaroslav@601: sb.append("<");
jaroslav@601: for(TypeVariable> typeparm: typeparms) {
jaroslav@601: if (!first)
jaroslav@601: sb.append(",");
jaroslav@601: // Class objects can't occur here; no need to test
jaroslav@601: // and call Class.getName().
jaroslav@601: sb.append(typeparm.toString());
jaroslav@601: first = false;
jaroslav@601: }
jaroslav@601: sb.append("> ");
jaroslav@601: }
jaroslav@601: sb.append(Field.getTypeName(getDeclaringClass()));
jaroslav@601: sb.append("(");
jaroslav@601: Type[] params = getGenericParameterTypes();
jaroslav@601: for (int j = 0; j < params.length; j++) {
jaroslav@601: String param = (params[j] instanceof Class>)?
jaroslav@601: Field.getTypeName((Class>)params[j]):
jaroslav@601: (params[j].toString());
jaroslav@601: if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
jaroslav@601: param = param.replaceFirst("\\[\\]$", "...");
jaroslav@601: sb.append(param);
jaroslav@601: if (j < (params.length - 1))
jaroslav@601: sb.append(",");
jaroslav@601: }
jaroslav@601: sb.append(")");
jaroslav@601: Type[] exceptions = getGenericExceptionTypes();
jaroslav@601: if (exceptions.length > 0) {
jaroslav@601: sb.append(" throws ");
jaroslav@601: for (int k = 0; k < exceptions.length; k++) {
jaroslav@601: sb.append((exceptions[k] instanceof Class)?
jaroslav@601: ((Class>)exceptions[k]).getName():
jaroslav@601: exceptions[k].toString());
jaroslav@601: if (k < (exceptions.length - 1))
jaroslav@601: sb.append(",");
jaroslav@601: }
jaroslav@601: }
jaroslav@601: return sb.toString();
jaroslav@601: } catch (Exception e) {
jaroslav@601: return "<" + e + ">";
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Uses the constructor represented by this {@code Constructor} object to
jaroslav@601: * create and initialize a new instance of the constructor's
jaroslav@601: * declaring class, with the specified initialization parameters.
jaroslav@601: * Individual parameters are automatically unwrapped to match
jaroslav@601: * primitive formal parameters, and both primitive and reference
jaroslav@601: * parameters are subject to method invocation conversions as necessary.
jaroslav@601: *
jaroslav@601: * If the number of formal parameters required by the underlying constructor
jaroslav@601: * is 0, the supplied {@code initargs} array may be of length 0 or null.
jaroslav@601: *
jaroslav@601: * If the constructor's declaring class is an inner class in a
jaroslav@601: * non-static context, the first argument to the constructor needs
jaroslav@601: * to be the enclosing instance; see section 15.9.3 of
jaroslav@601: * The Java™ Language Specification.
jaroslav@601: *
jaroslav@601: * If the required access and argument checks succeed and the
jaroslav@601: * instantiation will proceed, the constructor's declaring class
jaroslav@601: * is initialized if it has not already been initialized.
jaroslav@601: *
jaroslav@601: * If the constructor completes normally, returns the newly
jaroslav@601: * created and initialized instance.
jaroslav@601: *
jaroslav@601: * @param initargs array of objects to be passed as arguments to
jaroslav@601: * the constructor call; values of primitive types are wrapped in
jaroslav@601: * a wrapper object of the appropriate type (e.g. a {@code float}
jaroslav@601: * in a {@link java.lang.Float Float})
jaroslav@601: *
jaroslav@601: * @return a new object created by calling the constructor
jaroslav@601: * this object represents
jaroslav@601: *
jaroslav@601: * @exception IllegalAccessException if this {@code Constructor} object
jaroslav@601: * is enforcing Java language access control and the underlying
jaroslav@601: * constructor is inaccessible.
jaroslav@601: * @exception IllegalArgumentException if the number of actual
jaroslav@601: * and formal parameters differ; if an unwrapping
jaroslav@601: * conversion for primitive arguments fails; or if,
jaroslav@601: * after possible unwrapping, a parameter value
jaroslav@601: * cannot be converted to the corresponding formal
jaroslav@601: * parameter type by a method invocation conversion; if
jaroslav@601: * this constructor pertains to an enum type.
jaroslav@601: * @exception InstantiationException if the class that declares the
jaroslav@601: * underlying constructor represents an abstract class.
jaroslav@601: * @exception InvocationTargetException if the underlying constructor
jaroslav@601: * throws an exception.
jaroslav@601: * @exception ExceptionInInitializerError if the initialization provoked
jaroslav@601: * by this method fails.
jaroslav@601: */
jaroslav@601: public T newInstance(Object ... initargs)
jaroslav@601: throws InstantiationException, IllegalAccessException,
jaroslav@601: IllegalArgumentException, InvocationTargetException
jaroslav@601: {
jaroslav@604: throw new SecurityException();
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns {@code true} if this constructor was declared to take
jaroslav@601: * a variable number of arguments; returns {@code false}
jaroslav@601: * otherwise.
jaroslav@601: *
jaroslav@601: * @return {@code true} if an only if this constructor was declared to
jaroslav@601: * take a variable number of arguments.
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public boolean isVarArgs() {
jaroslav@601: return (getModifiers() & Modifier.VARARGS) != 0;
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns {@code true} if this constructor is a synthetic
jaroslav@601: * constructor; returns {@code false} otherwise.
jaroslav@601: *
jaroslav@601: * @return true if and only if this constructor is a synthetic
jaroslav@601: * constructor as defined by
jaroslav@601: * The Java™ Language Specification.
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public boolean isSynthetic() {
jaroslav@601: return Modifier.isSynthetic(getModifiers());
jaroslav@601: }
jaroslav@601:
jaroslav@601: int getSlot() {
jaroslav@601: return slot;
jaroslav@601: }
jaroslav@601:
jaroslav@601: String getSignature() {
jaroslav@601: return signature;
jaroslav@601: }
jaroslav@601:
jaroslav@601: byte[] getRawAnnotations() {
jaroslav@601: return annotations;
jaroslav@601: }
jaroslav@601:
jaroslav@601: byte[] getRawParameterAnnotations() {
jaroslav@601: return parameterAnnotations;
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * @throws NullPointerException {@inheritDoc}
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public
jaroslav@601: * public java.util.Hashtable(int,float)
jaroslav@601: *
jaroslav@601: *
jaroslav@601: *