diff options
Diffstat (limited to 'libjava/java/lang/reflect/Constructor.java')
-rw-r--r-- | libjava/java/lang/reflect/Constructor.java | 204 |
1 files changed, 180 insertions, 24 deletions
diff --git a/libjava/java/lang/reflect/Constructor.java b/libjava/java/lang/reflect/Constructor.java index b789fb13031..68b2d3985cf 100644 --- a/libjava/java/lang/reflect/Constructor.java +++ b/libjava/java/lang/reflect/Constructor.java @@ -1,15 +1,46 @@ -// Constructor.java - Represents a constructor for a class. +/* java.lang.reflect.Constructor - reflection of Java constructors + Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006 + Free Software Foundation, Inc. -/* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation +This file is part of GNU Classpath. - This file is part of libgcj. - -This software is copyrighted work licensed under the terms of the -Libgcj License. Please consult the file "LIBGCJ_LICENSE" for -details. */ +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + package java.lang.reflect; +import gnu.java.lang.reflect.MethodSignatureParser; + /** * The Constructor class represents a constructor of a class. It also allows * dynamic creation of an object, via reflection. Invocation on Constructor @@ -37,8 +68,8 @@ package java.lang.reflect; * @author Tom Tromey <tromey@redhat.com> * @see Member * @see Class - * @see java.lang.Class#getConstructor(Object[]) - * @see java.lang.Class#getDeclaredConstructor(Object[]) + * @see java.lang.Class#getConstructor(Class[]) + * @see java.lang.Class#getDeclaredConstructor(Class[]) * @see java.lang.Class#getConstructors() * @see java.lang.Class#getDeclaredConstructors() * @since 1.1 @@ -47,6 +78,9 @@ package java.lang.reflect; public final class Constructor extends AccessibleObject implements Member, GenericDeclaration { + private static final int CONSTRUCTOR_MODIFIERS + = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC; + /** * This class is uninstantiable except from native code. */ @@ -58,7 +92,7 @@ public final class Constructor extends AccessibleObject * Gets the class that declared this constructor. * @return the class that declared this member */ - public Class getDeclaringClass () + public Class getDeclaringClass() { return declaringClass; } @@ -68,12 +102,19 @@ public final class Constructor extends AccessibleObject * it was declared in). * @return the name of this constructor */ - public String getName () + public String getName() { return declaringClass.getName(); } /** + * Return the raw modifiers for this constructor. In particular + * this will include the synthetic and varargs bits. + * @return the constructor's modifiers + */ + private native int getModifiersInternal(); + + /** * Gets the modifiers this constructor uses. Use the <code>Modifier</code> * class to interpret the values. A constructor can only have a subset of the * following modifiers: public, private, protected. @@ -81,7 +122,31 @@ public final class Constructor extends AccessibleObject * @return an integer representing the modifiers to this Member * @see Modifier */ - public native int getModifiers (); + public int getModifiers () + { + return getModifiersInternal() & CONSTRUCTOR_MODIFIERS; + } + + /** + * Return true if this constructor is synthetic, false otherwise. + * A synthetic member is one which is created by the compiler, + * and which does not appear in the user's source code. + * @since 1.5 + */ + public boolean isSynthetic() + { + return (getModifiersInternal() & Modifier.SYNTHETIC) != 0; + } + + /** + * Return true if this is a varargs constructor, that is if + * the constructor takes a variable number of arguments. + * @since 1.5 + */ + public boolean isVarArgs() + { + return (getModifiersInternal() & Modifier.VARARGS) != 0; + } /** * Get the parameter list for this constructor, in declaration order. If the @@ -127,14 +192,14 @@ public final class Constructor extends AccessibleObject } /** - * Get the hash code for the Constructor. + * Get the hash code for the Constructor. The Constructor hash code is the + * hash code of the declaring class's name. * * @return the hash code for the object */ public int hashCode () { - // FIXME. - return getName().hashCode() + declaringClass.getName().hashCode(); + return declaringClass.getName().hashCode(); } /** @@ -147,7 +212,7 @@ public final class Constructor extends AccessibleObject * * @return the String representation of the Constructor */ - public String toString () + public String toString() { if (parameter_types == null) getType (); @@ -170,6 +235,45 @@ public final class Constructor extends AccessibleObject return b.toString(); } + /* FIXME[GENERICS]: Add X extends GenericDeclaration and TypeVariable<X> */ + static void addTypeParameters(StringBuilder sb, TypeVariable[] typeArgs) + { + if (typeArgs.length == 0) + return; + sb.append('<'); + for (int i = 0; i < typeArgs.length; ++i) + { + if (i > 0) + sb.append(','); + sb.append(typeArgs[i]); + } + sb.append("> "); + } + + public String toGenericString() + { + StringBuilder sb = new StringBuilder(128); + Modifier.toString(getModifiers(), sb).append(' '); + addTypeParameters(sb, getTypeParameters()); + sb.append(getDeclaringClass().getName()).append('('); + Type[] types = getGenericParameterTypes(); + if (types.length > 0) + { + sb.append(types[0]); + for (int i = 1; i < types.length; ++i) + sb.append(',').append(types[i]); + } + sb.append(')'); + types = getGenericExceptionTypes(); + if (types.length > 0) + { + sb.append(" throws ").append(types[0]); + for (int i = 1; i < types.length; i++) + sb.append(',').append(types[i]); + } + return sb.toString(); + } + /** * Create a new instance by invoking the constructor. Arguments are * automatically unwrapped and widened, if needed.<p> @@ -204,26 +308,78 @@ public final class Constructor extends AccessibleObject throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; - // FIXME - Write a real implementation - public boolean isSynthetic() { return false; } - /** * Returns an array of <code>TypeVariable</code> objects that represents * the type variables declared by this constructor, in declaration order. - * An array of size zero is returned if this class has no type + * An array of size zero is returned if this constructor has no type * variables. * - * @return the type variables associated with this class. + * @return the type variables associated with this constructor. * @throws GenericSignatureFormatError if the generic signature does * not conform to the format specified in the Virtual Machine * specification, version 3. * @since 1.5 */ - /* FIXME[GENERICS]: Should be TypeVariable<Method>[] */ + /* FIXME[GENERICS]: Add <Constructor<T>> */ public TypeVariable[] getTypeParameters() { - // FIXME - write a real implementation - return new TypeVariable[0]; + String sig = getSignature(); + if (sig == null) + return new TypeVariable[0]; + MethodSignatureParser p = new MethodSignatureParser(this, sig); + return p.getTypeParameters(); + } + + /** + * Return the String in the Signature attribute for this constructor. If there + * is no Signature attribute, return null. + */ + private String getSignature() + { + // FIXME: libgcj doesn't record this information yet. + return null; + } + + /** + * Returns an array of <code>Type</code> objects that represents + * the exception types declared by this constructor, in declaration order. + * An array of size zero is returned if this constructor declares no + * exceptions. + * + * @return the exception types declared by this constructor. + * @throws GenericSignatureFormatError if the generic signature does + * not conform to the format specified in the Virtual Machine + * specification, version 3. + * @since 1.5 + */ + public Type[] getGenericExceptionTypes() + { + String sig = getSignature(); + if (sig == null) + return getExceptionTypes(); + MethodSignatureParser p = new MethodSignatureParser(this, sig); + return p.getGenericExceptionTypes(); + } + + /** + * Returns an array of <code>Type</code> objects that represents + * the parameter list for this constructor, in declaration order. + * An array of size zero is returned if this constructor takes no + * parameters. + * + * @return a list of the types of the constructor's parameters + * @throws GenericSignatureFormatError if the generic signature does + * not conform to the format specified in the Virtual Machine + * specification, version 3. + * @since 1.5 + */ + public Type[] getGenericParameterTypes() + { + String sig = getSignature(); + if (sig == null) + return getParameterTypes(); + MethodSignatureParser p = new MethodSignatureParser(this, sig); + return p.getGenericParameterTypes(); } // Update cached values from method descriptor in class. |