java reflect
java 反射得到方法
为了直接得到org.apache.http.impl.client.RequestWrapper{public boolean containsHeader(java.lang.String);}
public RequestWrapper(HttpRequest request) throws ProtocolException 构造函数带参数 (目标)
要用getDeclaredConstructor
newinstance中必须传入实例 如第8行
而httprequest是个interface 没有实例,必须得到其实例类BasicHttpEntityEnclosingRequest的class (见http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpRequest.html?is-external=true)
1 public static void main( String[] args ) throws IOException, ClassNotFoundException 2 { 3 try { 4 Class baseRequestClazz = Class.forName("org.apache.http.message.BasicHttpEntityEnclosingRequest");//得到class 5 Constructor baseRequestCon = baseRequestClazz.getDeclaredConstructor(String.class, String.class);//得到构造函数 6 baseRequestCon.setAccessible(true); 7 8 Object[] baseRequestParams = {new String("A"), new String("B")}; 9 Object baseRequestInstance = baseRequestCon.newInstance(baseRequestParams);//根据构造函数得到一个实例 10 11 Class httpRequestClazz = Class.forName("org.apache.http.HttpRequest");//得到httprequst的class,为了得到构造函数的参数 12 Class httpWrapperClazz = Class.forName("org.apache.http.impl.client.RequestWrapper");//构造函数的class 13 Constructor httpWrapperCon = httpWrapperClazz.getDeclaredConstructor(httpRequestClazz); 14 Object httpWrapperInstance = httpWrapperCon.newInstance(baseRequestInstance);//根据构造函数得到一个实例 15 16 Method method = httpWrapperClazz.getMethod("containsHeader", String.class);//申明class的中的方法 17 Object obj = method.invoke(httpWrapperInstance,"day");//invoke 根据实例和方法得到方法的返回值(必为object) 18 //http://javaweb.cc/JavaAPI1.6/ 19 System.out.println(obj.toString()); 20 System.out.println(obj.getClass()); 21 if ((Boolean)obj) //其实就是boolean直接强转 22 System.out.println("yes"); 23 else 24 System.out.println("no"); 25 }catch(Exception e) { 26 e.printStackTrace(); 27 } 28 29 }
下面再来一个复杂的例子
各种查api才能知道要哪些实例
Class : org.apache.http.impl.client.AbstractAuthenticationHandler
为了得到这个方法Method::public abstract boolean isAuthenticationRequested(org.apache.http.HttpResponse, org.apache.http.protocol.HttpContext);
interface HttpResponse --- BasicHttpResponse(ProtocolVersion ver, int code, String reason)
--- ProtocolVersion(String protocol, int major, int minor)
org.apache.http.protocol.HttpContext BasicHttpContext()
1 Class ProtocolVersionClass = Class.forName("org.apache.http.ProtocolVersion"); 2 Constructor ProtocolVersionCon = ProtocolVersionClass.getDeclaredConstructor(String.class,int.class,int.class); 3 ProtocolVersionCon.setAccessible(true); 4 Object[] ProtocolVersionParams = {new String("A"), new Integer(6), new Integer(6)}; 5 Object ProtocolVersionEx = ProtocolVersionCon.newInstance(ProtocolVersionParams); 6 7 Class BasicHttpResponseClass = Class.forName("org.apache.http.message.BasicHttpResponse"); 8 Constructor BasicHttpResponseCon = BasicHttpResponseClass.getDeclaredConstructor(ProtocolVersionClass,int.class,String.class); 9 Object BasicHttpResponseEx = BasicHttpResponseCon.newInstance(ProtocolVersionEx,new Integer(6),new String("A")); 10 Class AbstractAuthenticationHandlerClass = Class.forName("org.apache.http.client.AuthenticationHandler"); 11 //Object HttpResponsEx = AbstractAuthenticationHandlerCon.newInstance(BasicHttpResponseEx); 12 13 Class HttpResponseClass = Class.forName("org.apache.http.HttpResponse"); 14 15 Class HttpContextClass = Class.forName("org.apache.http.protocol.HttpContext"); 16 Class BasicHttpContextClass = Class.forName("org.apache.http.protocol.BasicHttpContext"); 17 Object BasicHttpContextEx = BasicHttpContextClass.newInstance(); 18 //Object HttpContextEx = HttpContextClass.newInstance(); 19 20 Class AuthenticationHandlerClass = Class.forName("org.apache.http.impl.client.DefaultProxyAuthenticationHandler"); 21 Object AuthenticationHandlerEx = AuthenticationHandlerClass.newInstance(); 22 Method method = AuthenticationHandlerClass.getMethod("isAuthenticationRequested",HttpResponseClass,HttpContextClass); 23 Object obj = method.invoke(AuthenticationHandlerEx,BasicHttpResponseEx,BasicHttpContextEx); 24 System.out.println(obj.toString());