class.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 718 行 · 第 1/2 页

JAVA
718
字号
    }

    /**
     * Returns an array containing Method objects reflecting all the
     * public <em>member</em> methods of the class or interface
     * represented by this Class object, including those declared by
     * the class or interface and and those inherited from
     * superclasses and superinterfaces. Returns an array of length 0
     * if the class or interface has no public member methods.
     *
     * <p>See <em>The Java Language Specification</em>, sections 8.2
     * and 8.4.
     *
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Method
     * @since     JDK1.1
     */
    public Method[] getMethods() throws SecurityException {
	checkMemberAccess(Member.PUBLIC);
	return getMethods0(Member.PUBLIC);
    }

    /**
     * Returns an array containing Constructor objects reflecting
     * all the public constructors of the class represented by this
     * Class object.  An array of length 0 is returned if the class
     * has no public constructors.
     *
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Constructor
     * @since     JDK1.1
     */
    public Constructor[] getConstructors() throws SecurityException {
	checkMemberAccess(Member.PUBLIC);
	return getConstructors0(Member.PUBLIC);
    }

    /**
     * Returns a Field object that reflects the specified public
     * member field of the class or interface represented by
     * this Class object. The name parameter is a String specifying
     * the simple name of the desired field.
     *
     * <p>The field to be reflected is located by searching all the
     * member fields of the class or interface represented by this
     * Class object for a public field with the specified name.
     *
     * <p>See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
     *
     * @exception NoSuchFieldException if a field with the specified name is
     *              not found.
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Field
     * @since     JDK1.1
     */
    public Field getField(String name)
	throws NoSuchFieldException, SecurityException {
	checkMemberAccess(Member.PUBLIC);
	return getField0(name, Member.PUBLIC);
    }

    /**
     * Returns a Method object that reflects the specified public
     * member method of the class or interface represented by this
     * Class object. The name parameter is a String specifying the
     * simple name the desired method, and the parameterTypes
     * parameter is an array of Class objects that identify the
     * method's formal parameter types, in declared order.
     *
     * <p>The method to reflect is located by searching all the member
     * methods of the class or interface represented by this Class
     * object for a public method with the specified name and exactly
     * the same formal parameter types.
     *
     * <p>See <em>The Java Language Specification</em>, sections 8.2
     * and 8.4.
     *
     * @exception NoSuchMethodException if a matching method is not found.
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Method
     * @since     JDK1.1
     */
    public Method getMethod(String name, Class[] parameterTypes)
	throws NoSuchMethodException, SecurityException {
	checkMemberAccess(Member.PUBLIC);
	return getMethod0(name, parameterTypes, Member.PUBLIC);
    }

    /**
     * Returns a Constructor object that reflects the specified public
     * constructor of the class represented by this Class object. The
     * parameterTypes parameter is an array of Class objects that
     * identify the constructor's formal parameter types, in declared
     * order.
     *
     * <p>The constructor to reflect is located by searching all the
     * constructors of the class represented by this Class object for
     * a public constructor with the exactly the same formal parameter
     * types.
     *
     * @exception NoSuchMethodException if a matching method is not found.
     * @exception SecurityException     if access to the information is denied.
     * @see       java.lang.reflect.Constructor
     * @since     JDK1.1
     */
    public Constructor getConstructor(Class[] parameterTypes)
	throws NoSuchMethodException, SecurityException {
	checkMemberAccess(Member.PUBLIC);
	return getConstructor0(parameterTypes, Member.PUBLIC);
    }

    /**
     * Not implemented in this version of the 
     * Java<font size="-2"><sup>TM</sup></font> Development Kit. 
     * <p>
     * Returns an array of Class objects reflecting all the classes
     * and interfaces declared as members of the class represented by
     * this Class object. This includes public, protected, default
     * (package) access, and private classes and interfaces declared
     * by the class, but excludes inherited classes and interfaces.
     * Returns an array of length 0 if the class declares no classes
     * or interfaces as members, or if this Class object represents a
     * primitive type.
     *
     * @exception SecurityException    if access to the information is denied.
     * @since     JDK1.1
     */
    public Class[] getDeclaredClasses() throws SecurityException {
	checkMemberAccess(Member.DECLARED);
	return new Class[0];			/* not implemented */
    }

    /**
     * Returns an array of Field objects reflecting all the fields
     * declared by the class or interface represented by this Class
     * object. This includes public, protected, default (package)
     * access, and private fields, but excludes inherited
     * fields. Returns an array of length 0 if the class or interface
     * declares no fields, or if this Class object represents a
     * primitive type.
     *
     * See <em>The Java Language Specification</em>, sections 8.2 and
     * 8.3.
     *
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Field
     * @since     JDK1.1
     */
    public Field[] getDeclaredFields() throws SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getFields0(Member.DECLARED);
    }

    /**
     * Returns an array of Method objects reflecting all the methods
     * declared by the class or interface represented by this Class
     * object. This includes public, protected, default (package)
     * access, and private methods, but excludes inherited
     * methods. Returns an array of length 0 if the class or interface
     * declares no methods, or if this Class object represents a
     * primitive type.
     *
     * <p>See <em>The Java Language Specification</em>, section 8.2.
     *
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Method
     * @since     JDK1.1
     */
    public Method[] getDeclaredMethods() throws SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getMethods0(Member.DECLARED);
    }

    /**
     * Returns an array of Constructor objects reflecting all the
     * constructors declared by the class represented by this Class
     * object. These are public, protected, default (package) access,
     * and private constructors.  Returns an array of length 0 if this
     * Class object represents an interface or a primitive type.
     *
     * <p>See <em>The Java Language Specification</em>, section 8.2.
     *
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Constructor
     * @since     JDK1.1
     */
    public Constructor[] getDeclaredConstructors() throws SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getConstructors0(Member.DECLARED);
    }

    /**
     * Returns a Field object that reflects the specified declared
     * field of the class or interface represented by this Class
     * object. The name parameter is a String that specifies the
     * simple name of the desired field.
     *
     * @exception NoSuchFieldException if a field with the specified name is
     *              not found.
     * @exception SecurityException    if access to the information is denied.
     * @see       java.lang.reflect.Field
     * @since     JDK1.1
     */
    public Field getDeclaredField(String name)
	throws NoSuchFieldException, SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getField0(name, Member.DECLARED);
    }

    /**
     * Returns a Method object that reflects the specified declared
     * method of the class or interface represented by this Class
     * object. The name parameter is a String that specifies the
     * simple name of the desired method, and the parameterTypes
     * parameter is an array of Class objects that identify the
     * method's formal parameter types, in declared order.
     *
     * @exception NoSuchMethodException if a matching method is not found.
     * @exception SecurityException     if access to the information is denied.
     * @see       java.lang.reflect.Method
     * @since     JDK1.1
     */
    public Method getDeclaredMethod(String name, Class[] parameterTypes)
	throws NoSuchMethodException, SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getMethod0(name, parameterTypes, Member.DECLARED);
    }

    /**
     * Returns a Constructor object that reflects the specified declared
     * constructor of the class or interface represented by this Class
     * object.  The parameterTypes parameter is an array of Class
     * objects that identify the constructor's formal parameter types,
     * in declared order.
     *
     * @exception NoSuchMethodException if a matching method is not found.
     * @exception SecurityException     if access to the information is denied.
     * @see       java.lang.reflect.Constructor
     * @since     JDK1.1
     */
    public Constructor getDeclaredConstructor(Class[] parameterTypes)
	throws NoSuchMethodException, SecurityException {
	checkMemberAccess(Member.DECLARED);
	return getConstructor0(parameterTypes, Member.DECLARED);
    }

    /**
     * Finds a resource with a given name.  Will return null if no
     * resource with this name is found.  The rules for searching a
     * resources associated with a given class are implemented by the
     * ClassLoader of the class.<p>
     *
     * The Class methods delegate to ClassLoader methods, after applying
     * a naming convention: if the resource name starts with "/", it is used
     * as is.  Otherwise, the name of the package is prepended, after
     * converting "." to "/".
     *
     * @param   name the string representing the resource to be found
     * @return  the <code>InputStream</code> object having the 
     *             specified name, or <code>null</code> if no 
     *             resource with the specified name is found.
     * @see     java.lang.ClassLoader
     * @see     java.lang.Class#getResource
     * @since   JDK1.1
     */
    public InputStream getResourceAsStream(String name) {
	name = resolveName(name);
	ClassLoader cl = getClassLoader();
	if (cl==null) {
	    // A system class.
	    return ClassLoader.getSystemResourceAsStream(name);
	}
	return cl.getResourceAsStream(name);
    }

    /**
     * Finds a resource with the specified name. The rules for searching 
     * for resources associated with a given class are implemented by 
     * the class loader of the class.
     * <p>
     * The Class methods delegate to ClassLoader methods, after applying
     * a naming convention: if the resource name starts with "/", it is used
     * as is.  Otherwise, the name of the package is prepended, after
     * converting "." to "/".
     *
     * @param   name the string representing the resource to be found.
     * @return  the <code>URL</code> object having the specified name,  
     *             or <code>null</code> if no resource with the specified 
     *             name is found.
     * @see     java.lang.ClassLoader 
     * @see     java.lang.Class#getResourceAsStream
     * @since   JDK1.1
     */
    public java.net.URL getResource(String name) {
	name = resolveName(name);
	ClassLoader cl = getClassLoader();
	if (cl==null) {
	    // A system class.
	    return ClassLoader.getSystemResource(name);
	}
	return cl.getResource(name);
    }

    /*
     * Return the Virtual Machine's Class object for the named
     * primitive type.
     */
    static native Class getPrimitiveClass(String name);

    /*
     * Check if client is allowed to access members.  If access is
     * denied, throw a SecurityException.
     *
     * <p>Default policy: allow all clients access with normal Java
     * access control.
     */
    private void checkMemberAccess(int which) {
	SecurityManager s = System.getSecurityManager();
	if (s != null) {
	    s.checkMemberAccess(this, which);
	}
    }

    /**
     * Add a package name prefix if the name is not absolute
     * Remove leading "/" if name is absolute
     */
    private String resolveName(String name) {
	if (name == null) {
	    return name;
	}
	if (!name.startsWith("/")) {
	    Class c = this;
	    while (c.isArray()) {
		c = c.getComponentType();
	    }
	    String baseName = c.getName();
	    int index = baseName.lastIndexOf('.');
	    if (index != -1) {
		name = baseName.substring(0, index).replace('.', '/')
		    +"/"+name;
	    }
	} else {
	    name = name.substring(1);
	}
	return name;
    }

    private native Field[] getFields0(int which);
    private native Method[] getMethods0(int which);
    private native Constructor[] getConstructors0(int which);
    private native Field getField0(String name, int which);
    private native Method getMethod0(String name, Class[] parameterTypes,
	int which);
    private native Constructor getConstructor0(Class[] parameterTypes,
	int which);

}

⌨️ 快捷键说明

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