constructor.java

来自「一个开源的JAVA虚拟机」· Java 代码 · 共 102 行

JAVA
102
字号
/*    libaegisvm - The Aegis Virtual Machine for executing Java bytecode    Copyright (C) 2001-2002  Philip W. L. Fong    This library is free software; you can redistribute it and/or    modify it under the terms of the GNU Lesser General Public    License as published by the Free Software Foundation; either    version 2.1 of the License, or (at your option) any later version.    This library 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    Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public    License along with this library; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/package java.lang.reflect;public final class Constructor extends AccessibleObject implements Member {    private Class declaringClass;    private int id;    private Constructor(Class declaringClass, int id) {	this.declaringClass = declaringClass;	this.id = id;    }    public Class getDeclaringClass() {	return declaringClass;    }    public native String getName();    private native String getDescriptor();    public native int getModifiers();    public Class[] getParameterTypes() {	return aegis.Reflect.getParameterTypes(declaringClass,getDescriptor());    }    private native int getExceptionsCount();    private native String getException(int id);    public Class[] getExceptionTypes() {	int count = getExceptionsCount();	Class[] exceptions = new Class[count];	for (int i = 0; i < count; i++) {	    try {		String exception = getException(i);		exceptions[i] =		    Class.forName(exception.replace('/', '.'), false,				  declaringClass.getClassLoader());	    } catch (ClassNotFoundException ex) {		throw new NoClassDefFoundError();	    }	}	return exceptions;    }    public boolean equals(Object obj) {	if (! (obj instanceof Constructor))	    return false;	Constructor C = (Constructor) obj;	return declaringClass == C.declaringClass && id == C.id;    }    public int hashCode() {	return declaringClass.getName().hashCode();    }    public String toString() {	String S =	    Modifier.toString(getModifiers()) +	    declaringClass.getName() + '(';	Class[] params = getParameterTypes();	if (params.length > 0) {	    S += params[0].getName();	    for (int i = 1; i < params.length; i++) {		S += ',' + params[i].getName();	    }	}	S += ')';	Class[] exceptions = getExceptionTypes();	if (exceptions.length > 0) {	    S += " throws " + exceptions[0].getName();	    for (int i = 1; i < exceptions.length; i++)		S += ',' + exceptions[i].getName();	}	return S;    }    /**     * \todo Accessibility check.     */    public Object newInstance(Object[] args)	throws InstantiationException, IllegalAccessException,	       IllegalArgumentException, InvocationTargetException {	if (Modifier.isAbstract(getModifiers()))	    throw new InstantiationException();	// fix me: accessibility check goes here	args = aegis.Reflect.widen(args, getParameterTypes());	return createInstance(declaringClass, id, args);    }    private static native Object createInstance(Class clazz,						int i,						Object[] args)	throws InvocationTargetException;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?