📄 constructor.java
字号:
/** * Get a String representation of the Constructor. A Constructor's String * representation is "<modifier> <classname>(<paramtypes>) * throws <exceptions>", where everything after ')' is omitted if * there are no exceptions.<br> Example: * <code>public java.io.FileInputStream(java.lang.Runnable) * throws java.io.FileNotFoundException</code> * * @return the String representation of the Constructor */ public String toString() { // 128 is a reasonable buffer initial size for constructor StringBuilder sb = new StringBuilder(128); Modifier.toString(getModifiers(), sb).append(' '); sb.append(getDeclaringClass().getName()).append('('); Class[] c = getParameterTypes(); if (c.length > 0) { sb.append(ClassHelper.getUserName(c[0])); for (int i = 1; i < c.length; i++) sb.append(',').append(ClassHelper.getUserName(c[i])); } sb.append(')'); c = getExceptionTypes(); if (c.length > 0) { sb.append(" throws ").append(c[0].getName()); for (int i = 1; i < c.length; i++) sb.append(',').append(c[i].getName()); } return sb.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> * * If this class is abstract, you will get an * <code>InstantiationException</code>. If the constructor takes 0 * arguments, you may use null or a 0-length array for <code>args</code>.<p> * * If this Constructor enforces access control, your runtime context is * evaluated, and you may have an <code>IllegalAccessException</code> if * you could not create this object in similar compiled code. If the class * is uninitialized, you trigger class initialization, which may end in a * <code>ExceptionInInitializerError</code>.<p> * * Then, the constructor is invoked. If it completes normally, the return * value will be the new object. If it completes abruptly, the exception is * wrapped in an <code>InvocationTargetException</code>. * * @param args the arguments to the constructor * @return the newly created object * @throws IllegalAccessException if the constructor could not normally be * called by the Java code (i.e. it is not public) * @throws IllegalArgumentException if the number of arguments is incorrect; * or if the arguments types are wrong even with a widening * conversion * @throws InstantiationException if the class is abstract * @throws InvocationTargetException if the constructor throws an exception * @throws ExceptionInInitializerError if construction triggered class * initialization, which then failed */ public Object newInstance(Object args[]) throws InstantiationException, IllegalAccessException, InvocationTargetException { return constructNative(args, declaringClass, parameterTypes, slot, flag); } /** * 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 constructor has no type * variables. * * @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]: Add <Constructor<T>> */ public TypeVariable[] getTypeParameters() { String sig = getSignature(declaringClass, slot); if (sig == null) return new TypeVariable[0]; MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getTypeParameters(); } /** * 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(declaringClass, slot); 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(declaringClass, slot); if (sig == null) return getParameterTypes(); MethodSignatureParser p = new MethodSignatureParser(this, sig); return p.getGenericParameterTypes(); } public Annotation getAnnotation(Class annoClass) { Annotation[] annos = getDeclaredAnnotations(); for (int i = 0; i < annos.length; i++) if (annos[i].annotationType() == annoClass) return annos[i]; return null; } public Annotation[] getDeclaredAnnotations() { return getDeclaredAnnotationsNative(declaringClass, slot); } public Annotation[][] getParameterAnnotations() { return getParameterAnnotationsNative(declaringClass, slot); } /* * NATIVE HELPERS */ /** * Return the String in the Signature attribute for this constructor. If there * is no Signature attribute, return null. */ private native String getSignature(Class declaringClass, int slot); /** * 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 getConstructorModifiers(Class declaringClass, int slot); private native Object constructNative(Object[] args, Class declaringClass, Class[] parameterTypes, int slot, boolean noAccessCheck) throws InstantiationException, IllegalAccessException, InvocationTargetException; private native Annotation[] getDeclaredAnnotationsNative(Class declaringClass, int slot); private native Annotation[][] getParameterAnnotationsNative(Class declaringClass, int slot);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -