反射Reflect

getMethod
public Method getMethod(String name,
Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
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 of the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array.
If the name is "<init>;"or "<clinit>" a NoSuchMethodException is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object:

C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C.
If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected.
To find a matching method in a class C: If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily.
Note that there may be more than one matching method in a class because while the Java language forbids a class to declare multiple methods with the same signature but different return types, the Java virtual machine does not. This increased flexibility in the virtual machine can be used to implement various language features. For example, covariant returns can be implemented with bridge methods; the bridge method and the method being overridden would have the same signature but different return types.

See The Java Language Specification, sections 8.2 and 8.4.


Parameters:
name - the name of the method
parameterTypes - the list of parameters
Returns:
the Method object that matches the specified name and parameterTypes
Throws:
NoSuchMethodException - if a matching method is not found or if the name is "<init>"or "<clinit>".
NullPointerException - if name is null
SecurityException - If a security manager, s, is present and any of the following conditions is met:
invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the method
the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
Since:
JDK1.1

--------------------------------------------------------------------------------- 

 getDeclaredMethods

public Method[] getDeclaredMethods()
throws SecurityException
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. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type, an array class, or void. The class initialization method <clinit> is not included in the returned array. If the class declares multiple public member methods with the same parameter types, they are all included in the returned array.
See The Java Language Specification, section 8.2.


Returns:
the array of Method objects representing all the declared methods of this class
Throws:
SecurityException - If a security manager, s, is present and any of the following conditions is met:
invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared methods within this class
the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
Since:
JDK1.1

 

 

package com.exmyth.reflect;

import java.lang.reflect.Method;

public class TestPrivate
{
public static void main(String[] args) throws Exception
{
Private p = new Private();

Class<?> classType = p.getClass();

Method method = classType.getDeclaredMethod("sayHello",
new Class[] { String.class });

method.setAccessible(true);//压制Java的访问控制检查

String str = (String)method.invoke(p, new Object[]{"zhangsan"});

System.out.println(str);
}
}

 

 

 

 

 

posted @ 2013-07-15 17:34  牧之丨  阅读(679)  评论(0编辑  收藏  举报