<code></code><pre name="code" class="java"><code style="background-color: rgb(255, 255, 255);"></code><pre name="code" class="java">package com.tracy.proxy.reflect;
public class reflect {
public static void main(String[] args) throws Exception {
Boolean var1 = true;
Class<?> classType2 = var1.getClass();
//class java.lang.Boolean
System.out.println(classType2);//class java.lang.Boolean
Class<?> classType4 = Boolean.class;
System.out.println(classType4);//class java.lang.Boolean
Class<?> classType5 = Class.forName("java.lang.Boolean");
System.out.println(classType5);//class java.lang.Boolean
Class<?> classType3 = Boolean.TYPE;
System.out.println(classType3);//boolean
System.out.println(Integer.TYPE);//int
impTestReflect timpTestReflect = (impTestReflect) Class.forName("com.tracy.proxy.reflect.impTestReflect").newInstance();
timpTestReflect.sayme();
// TODO Auto-generated catch block
// TODO Auto-generated catch block
}
}
</pre><pre name="code" class="java"><span style="font-size:18px;">反射根据class 类路径 来new一个实例对象
Constructor提供关于类的单个构造方法的信息以及对它的访问权限。
Constructor 允许在将实参与带有基础构造方法的形参的 newInstance() 匹配时进行扩展转换,但是如果发生收缩转换,则抛出IllegalArgumentException。</span>
外部调用java类的私有方法: 利用java.lang.reflect包里面提供的方法
package com.tracy.proxy.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class UsePrivate {
public static void main(String[] args) {
//返回一个constructor对象的一个数组,这些对象反映此 Class 对象表示的类声明的所有构造方法。
//Constructor 提供关于类的单个构造方法的信息以及对它的访问权限 (Class类对象的方法)
//返回 Constructor 对象的一个数组,这些对象反映此 Class 对象表示的类声明的所有构造方法。
//Class 类的实例表示正在运行的 Java 应用程序中的类和接口
Constructor<?> constructor = privateDTO.class.getDeclaredConstructors()[0];
//设置不需要检查此对象的java语言性是否可以访问
constructor.setAccessible(true);
try {
privateDTO privateDTO = (com.tracy.proxy.reflect.privateDTO) constructor.newInstance();
privateDTO.sayhello();
//privateDTO.sayhi();//私有方法不能访问
//返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法。
for(Method method:privateDTO.class.getDeclaredMethods())
{
//得到对对象之后,再得到方法
method.setAccessible(true);
if(method.getName().equals("sayhi"))
{
method.invoke(privateDTO);
}
else if(method.getName().equals("add"))
{
method.invoke(privateDTO, 1,7);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
代理模式:动态代理
package com.tracy.proxy.reflect;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class MethodSelector implements InvocationHandler {
private Object proxied;
//构造方法
public MethodSelector(Object proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("interesting"))
System.out.println("Proxy detected the interesting method");
//在代理实例上处理方法调用并返回结果
return method.invoke(proxied, args);
}
}
interface SomeMethods {
void boring1();
void boring2();
void interesting(String arg);
void boring3();
}
class Implementation implements SomeMethods {
public void boring1() {
System.out.println("boring1");
}
public void boring2() {
System.out.println("boring2");
}
public void interesting(String arg) {
System.out.println("interesting " + arg);
}
public void boring3() {
System.out.println("boring3");
}
}
public class SelectingMethods {
public static void main(String[] args) {
传进去的参数,类加载器是负责加载类的对象 ,代理类要实现的接口接口列表,指派方法方法调用的调用处理程序
SomeMethods proxy = (SomeMethods) Proxy.newProxyInstance(
SomeMethods.class.getClassLoader(),
new Class[] { SomeMethods.class }, new MethodSelector(
new Implementation()));
proxy.boring1();
proxy.boring2();
proxy.interesting("bonobo");
proxy.boring3();
}
}