六、反射:运行时的类信息

  我们已经知道了,在编译时,编译器必须知道所有要通过RTTI来处理的类。而反射提供了一种机制——用来检查可用的方法,并返回方法名。区别就在于RTTI是处理已知类的,而反射用于处理未知类。Class类与java.lang.reflect类库一起对反射概念进行支持,该类库包含Field、Method以及Constructor(每个类都实现了Member接口)。这些类型是由JVM运行时创建的,用来表示未知类种对应的成员。使用Constructor(构造函数)创建新的对象,用get(),set()方法读取和修改与Field对象(字段)关联的字段,用invoke()方法调用与Method对象(方法)关联的方法。这样,匿名对象的类信息就能在运行时被完全确定下来,而在编译时不需要知道任何事情。

  其实,当反射与一个未知类型的对象打交道时,JVM只是简单地检查这个对象,在做其他事情之前必须先加载这个类的Class对象。因此,那个类的.class文件对于JVM来说必须时可获取的(在本地或网络上)所以反射与RTTI的区别只在于:对于RTTI来说,编译器在编译时打开和检查.class文件,而对于反射来说,.class文件在编译时是不可获得的,所以是运行时打开和检查.class文件。反射在需要创建更动态的代码时很有用。

七、动态代理

  代理是基本的设计模式:为其他对象提供一种代理,以便控制对象,而在对象前或后加上自己想加的东西。

interface Interface {
void doSomething(); void doSomeOtherThing(String args);
} class RealObject implements Interface { @Override
public void doSomething() {
System.out.println("doSomething");
} @Override
public void doSomeOtherThing(String args) {
System.out.println("doSomeOtherThing" + args);
}
} class SimpleProxy implements Interface { private Interface proxyId; public SimpleProxy(Interface proxyId) {
this.proxyId = proxyId;
} @Override
public void doSomething() {
//将原有的doSomething 方法添加上了一个输出 这就是代理之后新增的东西
//就好比某公司代理游戏后加的内购
System.out.println("SimpleProxy doSomething");
proxyId.doSomething();
} @Override
public void doSomeOtherThing(String args) {
proxyId.doSomeOtherThing(args);
//新增的东西可以在原有之前或之后都行
System.out.println("SimpleProxy doSomeOtherThing" + args);
}
} public class SimpleProxyDemo {
static void consumer(Interface i) {
i.doSomething();
i.doSomeOtherThing(" yi gi woli giao");
} public static void main(String[] args) {
consumer(new RealObject());
System.out.println("----- ----- -----");
consumer(new SimpleProxy(new RealObject()));
}
}

结果:

doSomething
doSomeOtherThing yi gi woli giao
----- ----- -----
SimpleProxy doSomething
doSomething
doSomeOtherThing yi gi woli giao
SimpleProxy doSomeOtherThing yi gi woli giao

  因为consumer()接受的Interface,所以无论是RealObject还是SimpleProxy,都可以作为参数,而SimpleProxy插了一脚 代理了RealObject加了不少自己的东西。

  java的动态代理更前进一步,因为它可以动态创建代理并动态地处理对所代理方法的调用。在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定相应的对策。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; interface Interface {
void doSomething(); void doSomeOtherThing(String args);
} class RealObject implements Interface { @Override
public void doSomething() {
System.out.println("doSomething");
} @Override
public void doSomeOtherThing(String args) {
System.out.println("doSomeOtherThing" + args);
}
} class DynamicProxyHandler implements InvocationHandler {
private Object proxyId; public DynamicProxyHandler(Object proxyId) {
this.proxyId = proxyId;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("**** proxy:" + proxy.getClass() + ", method" + method + ", args:" + args);
if (args != null) {
for (Object arg : args) {
System.out.println(" " + arg);
}
}
return method.invoke(proxyId, args);
}
} public class SimpleProxyDemo {
static void consumer(Interface i) {
i.doSomething();
i.doSomeOtherThing(" yi gi woli giao");
} public static void main(String[] args) {
RealObject realObject = new RealObject();
consumer(realObject);
System.out.println("----- ----- -----");
     //动态代理 可以代理任何东西
Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(), new Class[]{Interface.class}, new DynamicProxyHandler(realObject));
consumer(proxy);
}
}

结果:

doSomething
doSomeOtherThing yi gi woli giao
----- ----- -----
**** proxy:class $Proxy0, methodpublic abstract void Interface.doSomething(), args:null
doSomething
**** proxy:class $Proxy0, methodpublic abstract void Interface.doSomeOtherThing(java.lang.String),
args:[Ljava.lang.Object;@7ea987ac yi gi woli giao
doSomeOtherThing yi gi woli giao

通过Proxy.newProxyInstance()可以创建动态代理,这个方法需要三个参数:

1. 类加载器:可以从已经被加载的对象中获取其类加载器;

2. 你希望该代理实现的接口列表(不可以是类或抽象类,只能是接口);

3. InvocationHandler接口的一个实现;

在 invoke 实现中还可以根据方法名处对不同的方法进行处理,比如:

    @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("**** proxy:" + proxy.getClass() + ", method" + method + ", args:" + args);
if (args != null) {
for (Object arg : args) {
System.out.println(" " + arg);
}
}
if (method.getName().equals("doSomething")) {
System.out.println("this is the proxy for doSomething");
}
return method.invoke(proxyId, args);
}

还可以对参数或方法进行更多的操作因为 你已经得到了他们 尽情的使用你的代理权吧 ~~ 先加它十个内购。

九、接口与类型信息

  interface关键字的一种重要目标就是允许程序员隔离构件,进而降低耦合。反射,可以调用所有方法,甚至是private。唯独final是无法被修改的,运行时系统会在不抛任何异常的情况接受任何修改尝试,但是实际上不会发生任何修改。

    void callMethod(Object a, String methodName) throws Exception {
Method method = a.getClass().getDeclaredMethod(methodName);
method.setAccessible(true);
method.invoke(a);
}

  

Java编程思想——第14章 类型信息(二)反射的更多相关文章

  1. Java编程思想——第14章 类型信息(一)

    运行时类型信息使得你可以在程序运行时发现和使用类型信息.Java是如何让我们在运行时识别对象和类的信息得呢? 主要有两种方式:1.传统RTTI,他假定我们在编译期间已经知道了所有类型:2.反射,它允许 ...

  2. Java编程思想之十四 类型信息

    第十四章 类型信息 运行时类型信息使得你可以在程序运行时发现和使用类型信息 14.1 为什么需要RTTI 面向对象编程中基本的目的是:让代码只操作对基类的引用. 多态: import java.uti ...

  3. thinking in java学习笔记:14章 类型信息

    14.2 Class 对象 https://github.com/zhaojiatao/javase 1.什么是Class对象,Class对象是用来做什么的? Class对象是java程序用来创建类的 ...

  4. Java编程思想——第17章 容器深入研究(two)

    六.队列 排队,先进先出.除并发应用外Queue只有两个实现:LinkedList,PriorityQueue.他们的差异在于排序而非性能. 一些常用方法: 继承自Collection的方法: add ...

  5. Java编程思想 第21章 并发

    这是在2013年的笔记整理.现在重新拿出来,放在网上,重新总结下. 两种基本的线程实现方式 以及中断 package thread; /** * * @author zjf * @create_tim ...

  6. Java编程思想——第17章 容器深入研究 读书笔记(三)

    七.队列 排队,先进先出. 除并发应用外Queue只有两个实现:LinkedList,PriorityQueue.他们的差异在于排序而非性能. 一些常用方法: 继承自Collection的方法: ad ...

  7. Java编程思想读书笔记--第14章类型信息

    7.动态代理 代理是基本的设计模式之一,它是你为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. 什么是代理模式? ...

  8. Java编程思想(第一章 对象入门)总结

    面向对象编程(oop) 1.1抽象的进步 所有编程语言的最终目的都是提供一种“抽象”方法.   难点是 在机器模型(位于“方案空间”)和实际解决问题模型(位于“问题空间”)之间,程序员必须建立起一种联 ...

  9. Java编程思想-第四章练习题

    练习1:写一个程序,打印从1到100的值 public class Print1To100{ public static void main(String args[]){ for(int i = 1 ...

随机推荐

  1. [Luogu3659][USACO17FEB]Why Did the Cow Cross the Road I G

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  2. Api版本管理

    关于SpringMVC中如何添加,这一篇说的很详细了. http://www.cnblogs.com/jcli/p/springmvc_restful_version.html 版本管理可以通过路径进 ...

  3. python中的随机函数

    python--随机函数(random,uniform,randint,randrange,shuffle,sample) 本文转载自:[chamie] random() random()方法:返回随 ...

  4. Python编程系列---Python中装饰器的几种形式及万能装饰器

    根据函数是否传参  是否有返回值 ,可以分析出装饰器的四种形式: 形式一:无参无返回值 def outer(func): def wrapper(): print("装饰器功能1" ...

  5. Bugku SQL注入2的思考

    网络安全初学者,欢迎评论交流学习,若内容中有错误欢迎各位指正. 题目地址:http://123.206.87.240:8007/web2/ 题目提示:都过滤了绝望吗?,提示 !,!=,=,+,-,^, ...

  6. Webpack打包css后z-index被重新计算的解决方法

    发现问题 最近在使用 Webpack 打包 css 文件时,发现了一个问题,发现打包后的 z-index 值跟源文件 z-index 不一致. 如下图,左侧是源文件,右侧是打包后的文件: 即使加上 ! ...

  7. 轻量级CNN模型mobilenet v1

    mobilenet v1 论文解读 论文地址:https://arxiv.org/abs/1704.04861 核心思想就是通过depthwise conv替代普通conv. 有关depthwise ...

  8. scrollWidth、clientWidth 和 offsetWidth

    scrollWidth:对象的实际内容宽度,不包括边线宽度,会随对象中内容超过可视区而变大. clientWidth:对象内容的可视区的宽度,不包括边线宽度,会随对象显示大小的变化而变化. offse ...

  9. Prometheus(二):Prometheus 监控Windows机器

    一.安装wmi-exporter 首先在需要监控的Windows机器上安装wmi_exporter.wmi_exporter下载地址:https://github.com/martinlindhe/w ...

  10. MongoDB分页查询优化方法

    在网上看到很多关于MongoDB分页查询优化的文章,如出一辙.笔者自己实际生产中也遇到此问题,所以看了很多篇文章,这里分享一篇简明扼要的文章分享给大家,希望对大家在使用MongoDB时有所帮助. 凡事 ...