GNU Classpath (0.20) | |
Frames | No Frames |
1: /* java.lang.reflect.Constructor - reflection of Java constructors 2: Copyright (C) 1998, 2001 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.lang.reflect; 40: 41: import java.util.Arrays; 42: 43: /** 44: * The Constructor class represents a constructor of a class. It also allows 45: * dynamic creation of an object, via reflection. Invocation on Constructor 46: * objects knows how to do widening conversions, but throws 47: * {@link IllegalArgumentException} if a narrowing conversion would be 48: * necessary. You can query for information on this Constructor regardless 49: * of location, but construction access may be limited by Java language 50: * access controls. If you can't do it in the compiler, you can't normally 51: * do it here either.<p> 52: * 53: * <B>Note:</B> This class returns and accepts types as Classes, even 54: * primitive types; there are Class types defined that represent each 55: * different primitive type. They are <code>java.lang.Boolean.TYPE, 56: * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class, 57: * byte.class</code>, etc. These are not to be confused with the 58: * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are 59: * real classes.<p> 60: * 61: * Also note that this is not a serializable class. It is entirely feasible 62: * to make it serializable using the Externalizable interface, but this is 63: * on Sun, not me. 64: * 65: * @author John Keiser 66: * @author Eric Blake <ebb9@email.byu.edu> 67: * @see Member 68: * @see Class 69: * @see java.lang.Class#getConstructor(Object[]) 70: * @see java.lang.Class#getDeclaredConstructor(Object[]) 71: * @see java.lang.Class#getConstructors() 72: * @see java.lang.Class#getDeclaredConstructors() 73: * @since 1.1 74: * @status updated to 1.4 75: */ 76: public final class Constructor 77: extends AccessibleObject implements Member 78: { 79: private Class clazz; 80: private int slot; 81: 82: /** 83: * This class is uninstantiable except from native code. 84: */ 85: private Constructor(Class declaringClass,int slot) 86: { 87: this.clazz = declaringClass; 88: this.slot = slot; 89: } 90: 91: private Constructor() 92: { 93: } 94: 95: /** 96: * Gets the class that declared this constructor. 97: * @return the class that declared this member 98: */ 99: public Class getDeclaringClass() 100: { 101: return clazz; 102: } 103: 104: /** 105: * Gets the name of this constructor (the non-qualified name of the class 106: * it was declared in). 107: * @return the name of this constructor 108: */ 109: public String getName() 110: { 111: return getDeclaringClass().getName(); 112: } 113: 114: /** 115: * Gets the modifiers this constructor uses. Use the <code>Modifier</code> 116: * class to interpret the values. A constructor can only have a subset of the 117: * following modifiers: public, private, protected. 118: * 119: * @return an integer representing the modifiers to this Member 120: * @see Modifier 121: */ 122: public native int getModifiers(); 123: 124: /** 125: * Get the parameter list for this constructor, in declaration order. If the 126: * constructor takes no parameters, returns a 0-length array (not null). 127: * 128: * @return a list of the types of the constructor's parameters 129: */ 130: public native Class[] getParameterTypes(); 131: 132: /** 133: * Get the exception types this constructor says it throws, in no particular 134: * order. If the constructor has no throws clause, returns a 0-length array 135: * (not null). 136: * 137: * @return a list of the types in the constructor's throws clause 138: */ 139: public native Class[] getExceptionTypes(); 140: 141: /** 142: * Compare two objects to see if they are semantically equivalent. 143: * Two Constructors are semantically equivalent if they have the same 144: * declaring class and the same parameter list. This ignores different 145: * exception clauses, but since you can't create a Method except through the 146: * VM, this is just the == relation. 147: * 148: * @param o the object to compare to 149: * @return <code>true</code> if they are equal; <code>false</code> if not. 150: */ 151: public boolean equals(Object o) 152: { 153: if (!(o instanceof Constructor)) 154: return false; 155: Constructor that = (Constructor)o; 156: if (this.getDeclaringClass() != that.getDeclaringClass()) 157: return false; 158: if (!Arrays.equals(this.getParameterTypes(), that.getParameterTypes())) 159: return false; 160: return true; 161: } 162: 163: /** 164: * Get the hash code for the Constructor. The Constructor hash code is the 165: * hash code of the declaring class's name. 166: * 167: * @return the hash code for the object 168: */ 169: public int hashCode() 170: { 171: return getDeclaringClass().getName().hashCode(); 172: } 173: 174: /** 175: * Get a String representation of the Constructor. A Constructor's String 176: * representation is "<modifier> <classname>(<paramtypes>) 177: * throws <exceptions>", where everything after ')' is omitted if 178: * there are no exceptions.<br> Example: 179: * <code>public java.io.FileInputStream(java.lang.Runnable) 180: * throws java.io.FileNotFoundException</code> 181: * 182: * @return the String representation of the Constructor 183: */ 184: public String toString() 185: { 186: // 128 is a reasonable buffer initial size for constructor 187: StringBuffer sb = new StringBuffer(128); 188: Modifier.toString(getModifiers(), sb).append(' '); 189: sb.append(getDeclaringClass().getName()).append('('); 190: Class[] c = getParameterTypes(); 191: if (c.length > 0) 192: { 193: sb.append(c[0].getName()); 194: for (int i = 1; i < c.length; i++) 195: sb.append(',').append(c[i].getName()); 196: } 197: sb.append(')'); 198: c = getExceptionTypes(); 199: if (c.length > 0) 200: { 201: sb.append(" throws ").append(c[0].getName()); 202: for (int i = 1; i < c.length; i++) 203: sb.append(',').append(c[i].getName()); 204: } 205: return sb.toString(); 206: } 207: 208: /** 209: * Create a new instance by invoking the constructor. Arguments are 210: * automatically unwrapped and widened, if needed.<p> 211: * 212: * If this class is abstract, you will get an 213: * <code>InstantiationException</code>. If the constructor takes 0 214: * arguments, you may use null or a 0-length array for <code>args</code>.<p> 215: * 216: * If this Constructor enforces access control, your runtime context is 217: * evaluated, and you may have an <code>IllegalAccessException</code> if 218: * you could not create this object in similar compiled code. If the class 219: * is uninitialized, you trigger class initialization, which may end in a 220: * <code>ExceptionInInitializerError</code>.<p> 221: * 222: * Then, the constructor is invoked. If it completes normally, the return 223: * value will be the new object. If it completes abruptly, the exception is 224: * wrapped in an <code>InvocationTargetException</code>. 225: * 226: * @param args the arguments to the constructor 227: * @return the newly created object 228: * @throws IllegalAccessException if the constructor could not normally be 229: * called by the Java code (i.e. it is not public) 230: * @throws IllegalArgumentException if the number of arguments is incorrect; 231: * or if the arguments types are wrong even with a widening 232: * conversion 233: * @throws InstantiationException if the class is abstract 234: * @throws InvocationTargetException if the constructor throws an exception 235: * @throws ExceptionInInitializerError if construction triggered class 236: * initialization, which then failed 237: */ 238: public Object newInstance(Object args[]) 239: throws InstantiationException, IllegalAccessException, 240: InvocationTargetException 241: { 242: return constructNative(args, clazz, slot); 243: } 244: 245: private native Object constructNative(Object[] args, Class declaringClass, 246: int slot) 247: throws InstantiationException, IllegalAccessException, 248: InvocationTargetException; 249: }
GNU Classpath (0.20) |