1.1 --- a/emul/mini/src/main/java/java/lang/reflect/Constructor.java Tue Feb 26 14:55:55 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,567 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.lang.reflect;
1.30 -
1.31 -import java.lang.annotation.Annotation;
1.32 -import org.apidesign.bck2brwsr.emul.reflect.TypeProvider;
1.33 -
1.34 -/**
1.35 - * {@code Constructor} provides information about, and access to, a single
1.36 - * constructor for a class.
1.37 - *
1.38 - * <p>{@code Constructor} permits widening conversions to occur when matching the
1.39 - * actual parameters to newInstance() with the underlying
1.40 - * constructor's formal parameters, but throws an
1.41 - * {@code IllegalArgumentException} if a narrowing conversion would occur.
1.42 - *
1.43 - * @param <T> the class in which the constructor is declared
1.44 - *
1.45 - * @see Member
1.46 - * @see java.lang.Class
1.47 - * @see java.lang.Class#getConstructors()
1.48 - * @see java.lang.Class#getConstructor(Class[])
1.49 - * @see java.lang.Class#getDeclaredConstructors()
1.50 - *
1.51 - * @author Kenneth Russell
1.52 - * @author Nakul Saraiya
1.53 - */
1.54 -public final
1.55 - class Constructor<T> extends AccessibleObject implements
1.56 - GenericDeclaration,
1.57 - Member {
1.58 -
1.59 - private Class<T> clazz;
1.60 - private int slot;
1.61 - private Class<?>[] parameterTypes;
1.62 - private Class<?>[] exceptionTypes;
1.63 - private int modifiers;
1.64 - // Generics and annotations support
1.65 - private transient String signature;
1.66 - private byte[] annotations;
1.67 - private byte[] parameterAnnotations;
1.68 -
1.69 -
1.70 - // For sharing of ConstructorAccessors. This branching structure
1.71 - // is currently only two levels deep (i.e., one root Constructor
1.72 - // and potentially many Constructor objects pointing to it.)
1.73 - private Constructor<T> root;
1.74 -
1.75 - /**
1.76 - * Package-private constructor used by ReflectAccess to enable
1.77 - * instantiation of these objects in Java code from the java.lang
1.78 - * package via sun.reflect.LangReflectAccess.
1.79 - */
1.80 - Constructor(Class<T> declaringClass,
1.81 - Class<?>[] parameterTypes,
1.82 - Class<?>[] checkedExceptions,
1.83 - int modifiers,
1.84 - int slot,
1.85 - String signature,
1.86 - byte[] annotations,
1.87 - byte[] parameterAnnotations)
1.88 - {
1.89 - this.clazz = declaringClass;
1.90 - this.parameterTypes = parameterTypes;
1.91 - this.exceptionTypes = checkedExceptions;
1.92 - this.modifiers = modifiers;
1.93 - this.slot = slot;
1.94 - this.signature = signature;
1.95 - this.annotations = annotations;
1.96 - this.parameterAnnotations = parameterAnnotations;
1.97 - }
1.98 -
1.99 - /**
1.100 - * Package-private routine (exposed to java.lang.Class via
1.101 - * ReflectAccess) which returns a copy of this Constructor. The copy's
1.102 - * "root" field points to this Constructor.
1.103 - */
1.104 - Constructor<T> copy() {
1.105 - return this;
1.106 - }
1.107 -
1.108 - /**
1.109 - * Returns the {@code Class} object representing the class that declares
1.110 - * the constructor represented by this {@code Constructor} object.
1.111 - */
1.112 - public Class<T> getDeclaringClass() {
1.113 - return clazz;
1.114 - }
1.115 -
1.116 - /**
1.117 - * Returns the name of this constructor, as a string. This is
1.118 - * the binary name of the constructor's declaring class.
1.119 - */
1.120 - public String getName() {
1.121 - return getDeclaringClass().getName();
1.122 - }
1.123 -
1.124 - /**
1.125 - * Returns the Java language modifiers for the constructor
1.126 - * represented by this {@code Constructor} object, as an integer. The
1.127 - * {@code Modifier} class should be used to decode the modifiers.
1.128 - *
1.129 - * @see Modifier
1.130 - */
1.131 - public int getModifiers() {
1.132 - return modifiers;
1.133 - }
1.134 -
1.135 - /**
1.136 - * Returns an array of {@code TypeVariable} objects that represent the
1.137 - * type variables declared by the generic declaration represented by this
1.138 - * {@code GenericDeclaration} object, in declaration order. Returns an
1.139 - * array of length 0 if the underlying generic declaration declares no type
1.140 - * variables.
1.141 - *
1.142 - * @return an array of {@code TypeVariable} objects that represent
1.143 - * the type variables declared by this generic declaration
1.144 - * @throws GenericSignatureFormatError if the generic
1.145 - * signature of this generic declaration does not conform to
1.146 - * the format specified in
1.147 - * <cite>The Java™ Virtual Machine Specification</cite>
1.148 - * @since 1.5
1.149 - */
1.150 - public TypeVariable<Constructor<T>>[] getTypeParameters() {
1.151 - return TypeProvider.getDefault().getTypeParameters(this);
1.152 - }
1.153 -
1.154 -
1.155 - /**
1.156 - * Returns an array of {@code Class} objects that represent the formal
1.157 - * parameter types, in declaration order, of the constructor
1.158 - * represented by this {@code Constructor} object. Returns an array of
1.159 - * length 0 if the underlying constructor takes no parameters.
1.160 - *
1.161 - * @return the parameter types for the constructor this object
1.162 - * represents
1.163 - */
1.164 - public Class<?>[] getParameterTypes() {
1.165 - return (Class<?>[]) parameterTypes.clone();
1.166 - }
1.167 -
1.168 -
1.169 - /**
1.170 - * Returns an array of {@code Type} objects that represent the formal
1.171 - * parameter types, in declaration order, of the method represented by
1.172 - * this {@code Constructor} object. Returns an array of length 0 if the
1.173 - * underlying method takes no parameters.
1.174 - *
1.175 - * <p>If a formal parameter type is a parameterized type,
1.176 - * the {@code Type} object returned for it must accurately reflect
1.177 - * the actual type parameters used in the source code.
1.178 - *
1.179 - * <p>If a formal parameter type is a type variable or a parameterized
1.180 - * type, it is created. Otherwise, it is resolved.
1.181 - *
1.182 - * @return an array of {@code Type}s that represent the formal
1.183 - * parameter types of the underlying method, in declaration order
1.184 - * @throws GenericSignatureFormatError
1.185 - * if the generic method signature does not conform to the format
1.186 - * specified in
1.187 - * <cite>The Java™ Virtual Machine Specification</cite>
1.188 - * @throws TypeNotPresentException if any of the parameter
1.189 - * types of the underlying method refers to a non-existent type
1.190 - * declaration
1.191 - * @throws MalformedParameterizedTypeException if any of
1.192 - * the underlying method's parameter types refer to a parameterized
1.193 - * type that cannot be instantiated for any reason
1.194 - * @since 1.5
1.195 - */
1.196 - public Type[] getGenericParameterTypes() {
1.197 - return TypeProvider.getDefault().getGenericParameterTypes(this);
1.198 - }
1.199 -
1.200 -
1.201 - /**
1.202 - * Returns an array of {@code Class} objects that represent the types
1.203 - * of exceptions declared to be thrown by the underlying constructor
1.204 - * represented by this {@code Constructor} object. Returns an array of
1.205 - * length 0 if the constructor declares no exceptions in its {@code throws} clause.
1.206 - *
1.207 - * @return the exception types declared as being thrown by the
1.208 - * constructor this object represents
1.209 - */
1.210 - public Class<?>[] getExceptionTypes() {
1.211 - return (Class<?>[])exceptionTypes.clone();
1.212 - }
1.213 -
1.214 -
1.215 - /**
1.216 - * Returns an array of {@code Type} objects that represent the
1.217 - * exceptions declared to be thrown by this {@code Constructor} object.
1.218 - * Returns an array of length 0 if the underlying method declares
1.219 - * no exceptions in its {@code throws} clause.
1.220 - *
1.221 - * <p>If an exception type is a type variable or a parameterized
1.222 - * type, it is created. Otherwise, it is resolved.
1.223 - *
1.224 - * @return an array of Types that represent the exception types
1.225 - * thrown by the underlying method
1.226 - * @throws GenericSignatureFormatError
1.227 - * if the generic method signature does not conform to the format
1.228 - * specified in
1.229 - * <cite>The Java™ Virtual Machine Specification</cite>
1.230 - * @throws TypeNotPresentException if the underlying method's
1.231 - * {@code throws} clause refers to a non-existent type declaration
1.232 - * @throws MalformedParameterizedTypeException if
1.233 - * the underlying method's {@code throws} clause refers to a
1.234 - * parameterized type that cannot be instantiated for any reason
1.235 - * @since 1.5
1.236 - */
1.237 - public Type[] getGenericExceptionTypes() {
1.238 - return TypeProvider.getDefault().getGenericExceptionTypes(this);
1.239 - }
1.240 -
1.241 - /**
1.242 - * Compares this {@code Constructor} against the specified object.
1.243 - * Returns true if the objects are the same. Two {@code Constructor} objects are
1.244 - * the same if they were declared by the same class and have the
1.245 - * same formal parameter types.
1.246 - */
1.247 - public boolean equals(Object obj) {
1.248 - if (obj != null && obj instanceof Constructor) {
1.249 - Constructor<?> other = (Constructor<?>)obj;
1.250 - if (getDeclaringClass() == other.getDeclaringClass()) {
1.251 - /* Avoid unnecessary cloning */
1.252 - Class<?>[] params1 = parameterTypes;
1.253 - Class<?>[] params2 = other.parameterTypes;
1.254 - if (params1.length == params2.length) {
1.255 - for (int i = 0; i < params1.length; i++) {
1.256 - if (params1[i] != params2[i])
1.257 - return false;
1.258 - }
1.259 - return true;
1.260 - }
1.261 - }
1.262 - }
1.263 - return false;
1.264 - }
1.265 -
1.266 - /**
1.267 - * Returns a hashcode for this {@code Constructor}. The hashcode is
1.268 - * the same as the hashcode for the underlying constructor's
1.269 - * declaring class name.
1.270 - */
1.271 - public int hashCode() {
1.272 - return getDeclaringClass().getName().hashCode();
1.273 - }
1.274 -
1.275 - /**
1.276 - * Returns a string describing this {@code Constructor}. The string is
1.277 - * formatted as the constructor access modifiers, if any,
1.278 - * followed by the fully-qualified name of the declaring class,
1.279 - * followed by a parenthesized, comma-separated list of the
1.280 - * constructor's formal parameter types. For example:
1.281 - * <pre>
1.282 - * public java.util.Hashtable(int,float)
1.283 - * </pre>
1.284 - *
1.285 - * <p>The only possible modifiers for constructors are the access
1.286 - * modifiers {@code public}, {@code protected} or
1.287 - * {@code private}. Only one of these may appear, or none if the
1.288 - * constructor has default (package) access.
1.289 - */
1.290 - public String toString() {
1.291 - try {
1.292 - StringBuffer sb = new StringBuffer();
1.293 - int mod = getModifiers() & Modifier.constructorModifiers();
1.294 - if (mod != 0) {
1.295 - sb.append(Modifier.toString(mod) + " ");
1.296 - }
1.297 - sb.append(Field.getTypeName(getDeclaringClass()));
1.298 - sb.append("(");
1.299 - Class<?>[] params = parameterTypes; // avoid clone
1.300 - for (int j = 0; j < params.length; j++) {
1.301 - sb.append(Field.getTypeName(params[j]));
1.302 - if (j < (params.length - 1))
1.303 - sb.append(",");
1.304 - }
1.305 - sb.append(")");
1.306 - Class<?>[] exceptions = exceptionTypes; // avoid clone
1.307 - if (exceptions.length > 0) {
1.308 - sb.append(" throws ");
1.309 - for (int k = 0; k < exceptions.length; k++) {
1.310 - sb.append(exceptions[k].getName());
1.311 - if (k < (exceptions.length - 1))
1.312 - sb.append(",");
1.313 - }
1.314 - }
1.315 - return sb.toString();
1.316 - } catch (Exception e) {
1.317 - return "<" + e + ">";
1.318 - }
1.319 - }
1.320 -
1.321 - /**
1.322 - * Returns a string describing this {@code Constructor},
1.323 - * including type parameters. The string is formatted as the
1.324 - * constructor access modifiers, if any, followed by an
1.325 - * angle-bracketed comma separated list of the constructor's type
1.326 - * parameters, if any, followed by the fully-qualified name of the
1.327 - * declaring class, followed by a parenthesized, comma-separated
1.328 - * list of the constructor's generic formal parameter types.
1.329 - *
1.330 - * If this constructor was declared to take a variable number of
1.331 - * arguments, instead of denoting the last parameter as
1.332 - * "<tt><i>Type</i>[]</tt>", it is denoted as
1.333 - * "<tt><i>Type</i>...</tt>".
1.334 - *
1.335 - * A space is used to separate access modifiers from one another
1.336 - * and from the type parameters or return type. If there are no
1.337 - * type parameters, the type parameter list is elided; if the type
1.338 - * parameter list is present, a space separates the list from the
1.339 - * class name. If the constructor is declared to throw
1.340 - * exceptions, the parameter list is followed by a space, followed
1.341 - * by the word "{@code throws}" followed by a
1.342 - * comma-separated list of the thrown exception types.
1.343 - *
1.344 - * <p>The only possible modifiers for constructors are the access
1.345 - * modifiers {@code public}, {@code protected} or
1.346 - * {@code private}. Only one of these may appear, or none if the
1.347 - * constructor has default (package) access.
1.348 - *
1.349 - * @return a string describing this {@code Constructor},
1.350 - * include type parameters
1.351 - *
1.352 - * @since 1.5
1.353 - */
1.354 - public String toGenericString() {
1.355 - try {
1.356 - StringBuilder sb = new StringBuilder();
1.357 - int mod = getModifiers() & Modifier.constructorModifiers();
1.358 - if (mod != 0) {
1.359 - sb.append(Modifier.toString(mod) + " ");
1.360 - }
1.361 - TypeVariable<?>[] typeparms = getTypeParameters();
1.362 - if (typeparms.length > 0) {
1.363 - boolean first = true;
1.364 - sb.append("<");
1.365 - for(TypeVariable<?> typeparm: typeparms) {
1.366 - if (!first)
1.367 - sb.append(",");
1.368 - // Class objects can't occur here; no need to test
1.369 - // and call Class.getName().
1.370 - sb.append(typeparm.toString());
1.371 - first = false;
1.372 - }
1.373 - sb.append("> ");
1.374 - }
1.375 - sb.append(Field.getTypeName(getDeclaringClass()));
1.376 - sb.append("(");
1.377 - Type[] params = getGenericParameterTypes();
1.378 - for (int j = 0; j < params.length; j++) {
1.379 - String param = (params[j] instanceof Class<?>)?
1.380 - Field.getTypeName((Class<?>)params[j]):
1.381 - (params[j].toString());
1.382 - if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
1.383 - param = param.replaceFirst("\\[\\]$", "...");
1.384 - sb.append(param);
1.385 - if (j < (params.length - 1))
1.386 - sb.append(",");
1.387 - }
1.388 - sb.append(")");
1.389 - Type[] exceptions = getGenericExceptionTypes();
1.390 - if (exceptions.length > 0) {
1.391 - sb.append(" throws ");
1.392 - for (int k = 0; k < exceptions.length; k++) {
1.393 - sb.append((exceptions[k] instanceof Class)?
1.394 - ((Class<?>)exceptions[k]).getName():
1.395 - exceptions[k].toString());
1.396 - if (k < (exceptions.length - 1))
1.397 - sb.append(",");
1.398 - }
1.399 - }
1.400 - return sb.toString();
1.401 - } catch (Exception e) {
1.402 - return "<" + e + ">";
1.403 - }
1.404 - }
1.405 -
1.406 - /**
1.407 - * Uses the constructor represented by this {@code Constructor} object to
1.408 - * create and initialize a new instance of the constructor's
1.409 - * declaring class, with the specified initialization parameters.
1.410 - * Individual parameters are automatically unwrapped to match
1.411 - * primitive formal parameters, and both primitive and reference
1.412 - * parameters are subject to method invocation conversions as necessary.
1.413 - *
1.414 - * <p>If the number of formal parameters required by the underlying constructor
1.415 - * is 0, the supplied {@code initargs} array may be of length 0 or null.
1.416 - *
1.417 - * <p>If the constructor's declaring class is an inner class in a
1.418 - * non-static context, the first argument to the constructor needs
1.419 - * to be the enclosing instance; see section 15.9.3 of
1.420 - * <cite>The Java™ Language Specification</cite>.
1.421 - *
1.422 - * <p>If the required access and argument checks succeed and the
1.423 - * instantiation will proceed, the constructor's declaring class
1.424 - * is initialized if it has not already been initialized.
1.425 - *
1.426 - * <p>If the constructor completes normally, returns the newly
1.427 - * created and initialized instance.
1.428 - *
1.429 - * @param initargs array of objects to be passed as arguments to
1.430 - * the constructor call; values of primitive types are wrapped in
1.431 - * a wrapper object of the appropriate type (e.g. a {@code float}
1.432 - * in a {@link java.lang.Float Float})
1.433 - *
1.434 - * @return a new object created by calling the constructor
1.435 - * this object represents
1.436 - *
1.437 - * @exception IllegalAccessException if this {@code Constructor} object
1.438 - * is enforcing Java language access control and the underlying
1.439 - * constructor is inaccessible.
1.440 - * @exception IllegalArgumentException if the number of actual
1.441 - * and formal parameters differ; if an unwrapping
1.442 - * conversion for primitive arguments fails; or if,
1.443 - * after possible unwrapping, a parameter value
1.444 - * cannot be converted to the corresponding formal
1.445 - * parameter type by a method invocation conversion; if
1.446 - * this constructor pertains to an enum type.
1.447 - * @exception InstantiationException if the class that declares the
1.448 - * underlying constructor represents an abstract class.
1.449 - * @exception InvocationTargetException if the underlying constructor
1.450 - * throws an exception.
1.451 - * @exception ExceptionInInitializerError if the initialization provoked
1.452 - * by this method fails.
1.453 - */
1.454 - public T newInstance(Object ... initargs)
1.455 - throws InstantiationException, IllegalAccessException,
1.456 - IllegalArgumentException, InvocationTargetException
1.457 - {
1.458 - throw new SecurityException();
1.459 - }
1.460 -
1.461 - /**
1.462 - * Returns {@code true} if this constructor was declared to take
1.463 - * a variable number of arguments; returns {@code false}
1.464 - * otherwise.
1.465 - *
1.466 - * @return {@code true} if an only if this constructor was declared to
1.467 - * take a variable number of arguments.
1.468 - * @since 1.5
1.469 - */
1.470 - public boolean isVarArgs() {
1.471 - return (getModifiers() & Modifier.VARARGS) != 0;
1.472 - }
1.473 -
1.474 - /**
1.475 - * Returns {@code true} if this constructor is a synthetic
1.476 - * constructor; returns {@code false} otherwise.
1.477 - *
1.478 - * @return true if and only if this constructor is a synthetic
1.479 - * constructor as defined by
1.480 - * <cite>The Java™ Language Specification</cite>.
1.481 - * @since 1.5
1.482 - */
1.483 - public boolean isSynthetic() {
1.484 - return Modifier.isSynthetic(getModifiers());
1.485 - }
1.486 -
1.487 - int getSlot() {
1.488 - return slot;
1.489 - }
1.490 -
1.491 - String getSignature() {
1.492 - return signature;
1.493 - }
1.494 -
1.495 - byte[] getRawAnnotations() {
1.496 - return annotations;
1.497 - }
1.498 -
1.499 - byte[] getRawParameterAnnotations() {
1.500 - return parameterAnnotations;
1.501 - }
1.502 -
1.503 - /**
1.504 - * @throws NullPointerException {@inheritDoc}
1.505 - * @since 1.5
1.506 - */
1.507 - public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1.508 - if (annotationClass == null)
1.509 - throw new NullPointerException();
1.510 -
1.511 - return null; // XXX (T) declaredAnnotations().get(annotationClass);
1.512 - }
1.513 -
1.514 - /**
1.515 - * @since 1.5
1.516 - */
1.517 - public Annotation[] getDeclaredAnnotations() {
1.518 - return new Annotation[0]; // XXX AnnotationParser.toArray(declaredAnnotations());
1.519 - }
1.520 -
1.521 - /**
1.522 - * Returns an array of arrays that represent the annotations on the formal
1.523 - * parameters, in declaration order, of the method represented by
1.524 - * this {@code Constructor} object. (Returns an array of length zero if the
1.525 - * underlying method is parameterless. If the method has one or more
1.526 - * parameters, a nested array of length zero is returned for each parameter
1.527 - * with no annotations.) The annotation objects contained in the returned
1.528 - * arrays are serializable. The caller of this method is free to modify
1.529 - * the returned arrays; it will have no effect on the arrays returned to
1.530 - * other callers.
1.531 - *
1.532 - * @return an array of arrays that represent the annotations on the formal
1.533 - * parameters, in declaration order, of the method represented by this
1.534 - * Constructor object
1.535 - * @since 1.5
1.536 - */
1.537 - public Annotation[][] getParameterAnnotations() {
1.538 - int numParameters = parameterTypes.length;
1.539 - if (parameterAnnotations == null)
1.540 - return new Annotation[numParameters][0];
1.541 -
1.542 - return new Annotation[numParameters][0]; // XXX
1.543 -/*
1.544 - Annotation[][] result = AnnotationParser.parseParameterAnnotations(
1.545 - parameterAnnotations,
1.546 - sun.misc.SharedSecrets.getJavaLangAccess().
1.547 - getConstantPool(getDeclaringClass()),
1.548 - getDeclaringClass());
1.549 - if (result.length != numParameters) {
1.550 - Class<?> declaringClass = getDeclaringClass();
1.551 - if (declaringClass.isEnum() ||
1.552 - declaringClass.isAnonymousClass() ||
1.553 - declaringClass.isLocalClass() )
1.554 - ; // Can't do reliable parameter counting
1.555 - else {
1.556 - if (!declaringClass.isMemberClass() || // top-level
1.557 - // Check for the enclosing instance parameter for
1.558 - // non-static member classes
1.559 - (declaringClass.isMemberClass() &&
1.560 - ((declaringClass.getModifiers() & Modifier.STATIC) == 0) &&
1.561 - result.length + 1 != numParameters) ) {
1.562 - throw new AnnotationFormatError(
1.563 - "Parameter annotations don't match number of parameters");
1.564 - }
1.565 - }
1.566 - }
1.567 - return result;
1.568 - */
1.569 - }
1.570 -}