⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.java

📁 一个开源的JAVA虚拟机
💻 JAVA
字号:
/*    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*//* *  java.lang.Class * *  (c) 1997 George David Morrison * *  API version: 1.0.2 * *  History: *  01JAN1997  George David Morrison *    Initial version */package java.lang;import aegis.*;import java.lang.reflect.*;import java.io.*;import java.net.*;import java.security.*;import java.util.Vector;public final class Class implements Serializable {    private Class() {}    static final int T_VOID = 0;    static final int T_BOOLEAN = 4;    static final int T_CHAR = 5;    static final int T_FLOAT = 6;    static final int T_DOUBLE = 7;    static final int T_BYTE = 8;    static final int T_SHORT = 9;    static final int T_INT = 10;    static final int T_LONG = 11;    static native Class getPrimitiveType(int atype);    public String toString() {	String name = getName();	if (isPrimitive())	    return name;	else if (isInterface())	    return "interface " + name;	else	    return "class " + name;    }    /**     * \todo Works only for declared classes.     */    public static native Class forName(String className)	throws ClassNotFoundException;    /**     * \todo Works only for declared classes.     */    public static native Class forName(String name,				       boolean initialize,				       ClassLoader loader)	throws ClassNotFoundException;    private static native Object newInstance(Class C)	throws InstantiationException, IllegalAccessException;    /**     * \todo Security checks.     */    public Object newInstance()	throws InstantiationException, IllegalAccessException {	// fix me: security checks go here	if (isPrimitive() || isInterface() || isArray() ||	    Modifier.isAbstract(getModifiers()))	    throw new InstantiationException();	return newInstance(this);    }    public native boolean isInstance(Object obj);    public native boolean isAssignableFrom(Class cls);    public native boolean isInterface();    public native boolean isArray();    public native boolean isPrimitive();    public String getName() {	if (isPrimitive()) {	    if (this == Void.TYPE)		return "void";	    else if (this == Boolean.TYPE)		return "boolean";	    else if (this == Byte.TYPE)		return "byte";	    else if (this == Character.TYPE)		return "char";	    else if (this == Short.TYPE)		return "short";	    else if (this == Integer.TYPE)		return "int";	    else if (this == Long.TYPE)		return "long";	    else if (this == Float.TYPE)		return "float";	    else if (this == Double.TYPE)		return "double";	    else		throw new InternalError();	} else {	    return getInternalName().replace('/', '.');	}    }       public native ClassLoader getClassLoader();    public Class getSuperclass() {	if (isInterface() || isPrimitive())	    return null;	else	    return getInternalSuperclass();    }    /**     * \todo Not implemented yet.     */    public Package getPackage() {	throw new FeatureNotYetImplementedError();    }    public Class[] getInterfaces() {	int count = getInterfacesCount();	Class[] interfaces = new Class[count];	for (int i = 0; i < count; i++)	    interfaces[i] = getInterface(i);	return interfaces;    }    public native Class getComponentType();    public native int getModifiers();    /**     * \todo Not implemented yet.     */    public Object[] getSigners() {	throw new FeatureNotYetImplementedError();    }    /**     * \todo Not implemented yet.     */    public Class getDeclaringClass() {	throw new FeatureNotYetImplementedError();    }    /**     * \todo Not implemented yet.     */    public Class[] getClasses() {	throw new FeatureNotYetImplementedError();    }    private static void getFields(Class clazz, Vector v) {	if (clazz.isPrimitive() || clazz.isArray())	    return;	Class superclass = clazz.getSuperclass();	if (superclass != null)	    getFields(superclass, v);	Class[] interfaces = clazz.getInterfaces();	for (int i = 0; i < interfaces.length; i++)	    clazz.getFields(interfaces[i], v);	Field[] fields = clazz.getDeclaredFields();	for (int i = 0; i < fields.length; i++) {	    if (Modifier.isPublic(fields[i].getModifiers()))		v.add(fields[i]);	}    }    /**     * \todo Security checks.     */    public Field[] getFields() throws SecurityException {	// security check goes here.	Vector v = new Vector();	getFields(this, v);	int length = v.size();	Field[] fields = new Field[length];	for (int i = 0; i < length; i++)	    fields[i] = (Field) v.get(i);	return fields;    }    private static void getMethods(Class clazz, Vector v) {	if (clazz.isPrimitive() || clazz.isArray())	    return;	Class[] interfaces = clazz.getInterfaces();	for (int i = 0; i < interfaces.length; i++)	    clazz.getMethods(interfaces[i], v);	Class superclass = clazz.getSuperclass();	if (superclass != null)	    getMethods(superclass, v);	Method[] methods = clazz.getDeclaredMethods();	for (int i = 0; i < methods.length; i++) {	    if (Modifier.isPublic(methods[i].getModifiers())) {		int size = v.size();		int j;		for (j = 0; j < size; j++) {		    Method method = (Method) v.get(j);		    if (compareParameterTypes(methods[i].getParameterTypes(),					      method.getParameterTypes()) &&			methods[i].getReturnType() == method.getReturnType())			break;		}		if (j < size) {		    v.set(j, methods[i]);		} else {		    v.add(methods[i]);		}	    }	}    }    /**     * \todo Security checks.     */    public Method[] getMethods() throws SecurityException {	// security check goes here.	Vector v = new Vector();	getMethods(this, v);	int length = v.size();	Method[] methods = new Method[length];	for (int i = 0; i < length; i++)	    methods[i] = (Method) v.get(i);	return methods;    }    /**     * \todo Security check goes here     */    public Constructor[] getConstructors() throws SecurityException {	// Security check goes here	Constructor[] tmp = getDeclaredConstructors();	int length = tmp.length;	int[] id = new int[length];	int n = 0;	for (int i = 0; i < length; i++) {	    if (Modifier.isPublic(tmp[i].getModifiers())) {		id[n] = i;		n++;	    }	}	Constructor[] constructors = new Constructor[n];	for (int i = 0; i < n; i++)	    constructors[i] = tmp[id[i]];	return constructors;    }    private static Field getField(Class clazz, String name) {	Field[] fields = clazz.getDeclaredFields();	int length = fields.length;	for (int i = 0; i < length; i++) {	    Field field = fields[i];	    if (Modifier.isPublic(field.getModifiers())) {		String n = field.getName();		if (n.equals(name))		    return field;	    }	}	Class[] interfaces = clazz.getInterfaces();	length = interfaces.length;	for (int i = 0; i < length; i++) {	    Field field = getField(interfaces[i], name);	    if (field != null)		return field;	}	Class superclass = clazz.getSuperclass();	if (superclass != null) {	    Field field = getField(superclass, name);	    if (field != null)		return field;	}	return null;    }    /**     * \todo Security checks.     */    public Field getField(String name)	throws NoSuchFieldException, SecurityException {	// Security check goes here	Field field = getField(this, name);	if (field != null)	    return field;	throw new NoSuchFieldException();    }    private static boolean compareParameterTypes(Class[] params1,						 Class[] params2) {	int length = ((params1 != null) ? params1.length : 0);	if (length != params2.length)	    return false;	for (int j = 0; j < length; j++)	    if (params1[j] != params2[j])		return false;	return true;    }    private static Method getMethod(Class clazz,				    String name,				    Class[] parameterTypes) {	Method M = null;	Method[] methods = clazz.getDeclaredMethods();	int length = methods.length;	for (int i = 0; i < length; i++) {	    Method method = methods[i];	    if (Modifier.isPublic(method.getModifiers()) &&		methods[i].getName().equals(name) &&		compareParameterTypes(methods[i].getParameterTypes(),				      parameterTypes)) {		if (M == null ||		    M.getReturnType().isAssignableFrom(methods[i].getReturnType()))		    M = methods[i];	    }	}	if (M != null)	    return M;	Class superclass = clazz.getInternalSuperclass();	if (superclass != null) {	    Method method = getMethod(superclass, name, parameterTypes);	    if (method != null)		return method;	}	Class[] interfaces = clazz.getInterfaces();	length = interfaces.length;	for (int i = 0; i < length; i++) {	    Method method = getMethod(interfaces[i], name, parameterTypes);	    if (method != null)		return method;	}	return null;    }    /**     * \todo Security checks.     */    public Method getMethod(String name, Class[] parameterTypes)	throws NoSuchMethodException, SecurityException {	// Security check goes here	Method method = getMethod(this, name, parameterTypes);	if (method != null)	    return method;	throw new NoSuchMethodException();    }    /**     * \todo Security checks.     */    public Constructor getConstructor(Class[] parameterTypes)	throws NoSuchMethodException, SecurityException {	// fix me: security check goes here	int count = getMethodsCount();	for (int i = 0; i < count; i++) {	    if (getMethodCategory(i) != 1)		continue;	    Constructor C = getConstructor(i);	    if (! Modifier.isPublic(C.getModifiers()))		continue;	    if (! compareParameterTypes(C.getParameterTypes(), parameterTypes))		continue;	    return getConstructor(i);	}	throw new NoSuchMethodException();    }    /**     * \todo Not implemented yet.     */    public Class[] getDeclaredClasses() throws SecurityException {	throw new FeatureNotYetImplementedError();    }    /**     * \todo No security checks.     */    public Field[] getDeclaredFields() throws SecurityException {	int count = getFieldsCount();	Field[] fields = new Field[count];	for (int i = 0; i < count; i++)	    fields[i] = getField(i);	return fields;    }    /**     * \todo Not implemented yet.     */    public Method[] getDeclaredMethods() throws SecurityException {	int count = getMethodsCount();	int n = 0;	int[] id = new int[count];	for (int i = 0; i < count; i++) {	    if (getMethodCategory(i) == 0) {		id[n] = i;		n++;	    }	}	Method[] methods = new Method[n];	for (int i = 0; i < n; i++)	    methods[i] = getMethod(id[i]);	return methods;    }    /**     * \todo Not implemented yet.     */    public Constructor[] getDeclaredConstructors() throws SecurityException {	int count = getMethodsCount();	int n = 0;	int[] id = new int[count];	for (int i = 0; i < count; i++) {	    if (getMethodCategory(i) == 1) {		id[n] = i;		n++;	    }	}	Constructor[] constructors = new Constructor[n];	for (int i = 0; i < n; i++)	    constructors[i] = getConstructor(id[i]);	return constructors;    }    public Field getDeclaredField(String name)	throws NoSuchFieldException, SecurityException {	Field[] fields = getDeclaredFields();	for (int i = 0; i < fields.length; i++)	    if (fields[i].getName().equals(name))		return fields[i];	throw new NoSuchFieldException();    }    /**     * \todo Security checks.     */    public Method getDeclaredMethod(String name, Class[] parameterTypes)	throws NoSuchMethodException, SecurityException {	// fix me: security check goes here	int count = getMethodsCount();	Method method = null;	Class returnType = null;	for (int i = 0; i < count; i++) {	    if (getMethodCategory(i) != 0)		continue;	    Method M = getMethod(i);	    if (! compareParameterTypes(M.getParameterTypes(), parameterTypes))		continue;	    Class T = M.getReturnType();	    if (returnType == null || returnType.isAssignableFrom(T)) {		method = M;		returnType = T;	    }	}	if (method == null)	    throw new NoSuchMethodException();	else	    return method;    }    /**     * \todo Security checks.     */    public Constructor getDeclaredConstructor(Class[] parameterTypes)	throws NoSuchMethodException, SecurityException {	// fix me: security check goes here	int count = getMethodsCount();	for (int i = 0; i < count; i++) {	    if (getMethodCategory(i) != 1)		continue;	    Constructor C = getConstructor(i);	    if (! compareParameterTypes(C.getParameterTypes(), parameterTypes))		continue;	    return getConstructor(i);	}	throw new NoSuchMethodException();    }    /**     * \todo Not implemented yet.     */    public InputStream getResourceAsStream(String name) {	throw new FeatureNotYetImplementedError();    }    /**     * \todo Not implemented yet.     */    public URL getResource(String name) {	throw new FeatureNotYetImplementedError();    }    /**     * \todo Not implemented yet.     */    public ProtectionDomain getProtectionDomain() {	throw new FeatureNotYetImplementedError();    }    private native Class getInternalSuperclass();    private native String getInternalName();    private native int getMethodsCount();    private native int getMethodCategory(int id);    private native Method getMethod(int id);    private native Constructor getConstructor(int id);    private native int getFieldsCount();    private native Field getField(int id);    private native int getInterfacesCount();    private native Class getInterface(int id);}

⌨️ 快捷键说明

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