Redian新闻
>
谁深入解答一下Java的Reflection机制
avatar
谁深入解答一下Java的Reflection机制# Java - 爪哇娇娃
s*n
1
功能和用处在哪里?
有点糊涂
avatar
p*2
2
感觉挺有用处的呀。虽然我还没怎么学这东西。
比如可以用函数的名字来调用这个函数。
avatar
z*3
3
很重要的一点是你不需要import类就写出调用这个类方法的代码
这个其实你用得很少,spring等框架用得多
写spring的人不可能去import你写的类
那么要调用你写的类里面的方法,就只有通过reflection
avatar
s*n
4

就说core java里面
提供了这个机制
具体有啥新的性能?

【在 z*******3 的大作中提到】
: 很重要的一点是你不需要import类就写出调用这个类方法的代码
: 这个其实你用得很少,spring等框架用得多
: 写spring的人不可能去import你写的类
: 那么要调用你写的类里面的方法,就只有通过reflection

avatar
r*s
5
computer science的一个重要的思路就是
add (yet) a(nother) layer of indirection,
Java的长处就在于JVM这层indirection,
而Reflection以及AOP存在的原因
就在于发挥这层indirection,
Reflection就可以让code自己去分析发现class/method/parameters etc.
并且invoke methods when necessary.

【在 s*******n 的大作中提到】
:
: 就说core java里面
: 提供了这个机制
: 具体有啥新的性能?

avatar
s*n
6

高屋建瓴
就是我比较浅
貌似知道一点
能给个situation说明下
这个机制好处在哪里?
特别是相对于C++而言?

【在 r*****s 的大作中提到】
: computer science的一个重要的思路就是
: add (yet) a(nother) layer of indirection,
: Java的长处就在于JVM这层indirection,
: 而Reflection以及AOP存在的原因
: 就在于发挥这层indirection,
: Reflection就可以让code自己去分析发现class/method/parameters etc.
: 并且invoke methods when necessary.

avatar
p*2
7

具体记不清是什么case了。以前用C的时候经常想通过一个函数名来调用这个函数。
比如
void foo()
{
}
我希望能这样调用call("foo")

【在 s*******n 的大作中提到】
:
: 高屋建瓴
: 就是我比较浅
: 貌似知道一点
: 能给个situation说明下
: 这个机制好处在哪里?
: 特别是相对于C++而言?

avatar
b*i
8
类似C动态库的调用,用库的名字和函数名字来执行函数。java甚至可以生成类的实例(
new)。C++要通过extern "C"化的函数来动态初始化类,或者用微软那一套,就是DLL的
东西,后来变成DCOM, ActiveX。

【在 s*******n 的大作中提到】
: 功能和用处在哪里?
: 有点糊涂

avatar
g*g
9
举个例子,就是JDBC,不同数据库有不同JDBC driver。你可以把这样一个property写
成一个configuration.
driver=com.mysql.jdbc.Driver
到时候DriverManager就可以动态Load。里面调用的就是reflection API
Class.forName(getProperty("driver"))
这就是Factory Pattern。C++里面Factory Pattern,只能有几个固定的选项hardcoded
,而Java里面,可以直接把选项做成configuration。干净而灵活,增加一个新的选项
,无需修改代码。这就是多一层indirection的好处。
avatar
r*s
10
基于这个例子,
比如现在定义好了这个JDBC的API,
有了Reflection,
无论底下library是MySQL的也好,
Oracle的也好,
version不同也没关系,
只要用Reflection找到那个API的method,
直接invoke就好了,
别说不用像C/C++那样重新build,
你写code的时候底下用和runtime时不同的library都没关系。

hardcoded

【在 g*****g 的大作中提到】
: 举个例子,就是JDBC,不同数据库有不同JDBC driver。你可以把这样一个property写
: 成一个configuration.
: driver=com.mysql.jdbc.Driver
: 到时候DriverManager就可以动态Load。里面调用的就是reflection API
: Class.forName(getProperty("driver"))
: 这就是Factory Pattern。C++里面Factory Pattern,只能有几个固定的选项hardcoded
: ,而Java里面,可以直接把选项做成configuration。干净而灵活,增加一个新的选项
: ,无需修改代码。这就是多一层indirection的好处。

avatar
w*z
11
jdbc 本身就是API. Reflection 用的最多的就像古德霸说的load class from
property file, cast to the interface. 如果用reflection find method,就没必要
了。又慢,还有run time exception. Java is a statically typed language after
all.

【在 r*****s 的大作中提到】
: 基于这个例子,
: 比如现在定义好了这个JDBC的API,
: 有了Reflection,
: 无论底下library是MySQL的也好,
: Oracle的也好,
: version不同也没关系,
: 只要用Reflection找到那个API的method,
: 直接invoke就好了,
: 别说不用像C/C++那样重新build,
: 你写code的时候底下用和runtime时不同的library都没关系。

avatar
z*e
12
初衷就是当我要写一个工具给别人用
需要调用到别人写的方法
但是又没有办法在我写的时候,拿到别人写的这个类
那就只能通过反射来调用这个方法
因为反射可以做到不需要import类的前提下
调用一个类的方法
avatar
w*z
13
能不用就别用。looks ugly to me.

【在 z****e 的大作中提到】
: 初衷就是当我要写一个工具给别人用
: 需要调用到别人写的方法
: 但是又没有办法在我写的时候,拿到别人写的这个类
: 那就只能通过反射来调用这个方法
: 因为反射可以做到不需要import类的前提下
: 调用一个类的方法

avatar
c*e
14
这个不就是static吗?

【在 p*****2 的大作中提到】
: 感觉挺有用处的呀。虽然我还没怎么学这东西。
: 比如可以用函数的名字来调用这个函数。

avatar
c*e
15
函数名字本身就是一个指针,难道就是找到这个指针,来调用这个函数?

【在 p*****2 的大作中提到】
: 感觉挺有用处的呀。虽然我还没怎么学这东西。
: 比如可以用函数的名字来调用这个函数。

avatar
c*e
16
vb,c#也有reflection,threading阿。现在一个语言有了某个东西,其它语言都要跟进。

【在 s*******n 的大作中提到】
: 功能和用处在哪里?
: 有点糊涂

avatar
p*2
17

什么意思?ZKSS

【在 c*********e 的大作中提到】
: 这个不就是static吗?
avatar
p*2
18

函数名字不能通过一个字符串来调用吧?除非用LoadLibrary

【在 c*********e 的大作中提到】
: 函数名字本身就是一个指针,难道就是找到这个指针,来调用这个函数?
avatar
b*i
19
假设你发布一个软件,然后别人可以用你的jar,增加自己的模块,怎么让你的jar调用
他的呢?
假如你的程序读取一个xml的控制文件,你的程序从里面读入class, function的名字,
然后调用。那么别人就可以不用改你的程序。你用reflection就可以完成这个任务。别
人改写xml就行了。就是说,这个函数的调用动态发生在运行期,编译的时候不知道该
调谁。。

【在 c*********e 的大作中提到】
: 函数名字本身就是一个指针,难道就是找到这个指针,来调用这个函数?
avatar
z*e
20
是用string的方式告诉jvm,这个函数叫什么名字,在哪一个类里面
然后jvm根据你给的string对象,自动搜索并调用这个函数
你只需要告诉jvm,这个函数叫什么名字,在哪一个类里面就行了
都是通过string对象,这样你写的代码就没有必要import这个类
没有必要import就没有必要在这个类改变之后重新编译
这样就剥离了耦合,就是别人怎么改都无所谓
只要函数名和类名不变,你的代码就不用变
当然对于仅在公司内部这个范围而言
你也可以通过发布接口并要求写代码的人用虚拟工厂模式搞定
但是如果你写的是spring这种面向无数用户的框架来说
那这个就太有用了,spring如果去发布接口,那谁受得了
ejb一开始就傻乎乎地去发布接口,结果群众拒绝接受
这是比较高端的东西,一般开发人员不用也罢

【在 c*********e 的大作中提到】
: 函数名字本身就是一个指针,难道就是找到这个指针,来调用这个函数?
avatar
g*g
21
Actually iterating over methods and find the desired ones is being used too.
e.g.
MBean. In JMX client, getXXX are treated as attributes and all other
functions
are treated as operations.

after

【在 w**z 的大作中提到】
: jdbc 本身就是API. Reflection 用的最多的就像古德霸说的load class from
: property file, cast to the interface. 如果用reflection find method,就没必要
: 了。又慢,还有run time exception. Java is a statically typed language after
: all.

avatar
w*z
22
yes, I just read the source code for jmx , it is a bit hard to read if the
code is using reflection, don't you think. has to follow the convention.
getXxx, never liked Java bean.

too.

【在 g*****g 的大作中提到】
: Actually iterating over methods and find the desired ones is being used too.
: e.g.
: MBean. In JMX client, getXXX are treated as attributes and all other
: functions
: are treated as operations.
:
: after

avatar
g*g
23
It aims framework development.

【在 w**z 的大作中提到】
: yes, I just read the source code for jmx , it is a bit hard to read if the
: code is using reflection, don't you think. has to follow the convention.
: getXxx, never liked Java bean.
:
: too.

avatar
r*s
24
to be more specific, for integration,
loosely coupled integration,
when we are not sure and not necessarily to be sure about
the code/libraries to be integrated.
regarding perf issues in reflection,
searching for methods has been optimized
(do not try to optimize it yourself like a cache,
it has caches inside!)
as well as the invocations,
i don't think we need to worry about it
in 99 out 100 cases.

【在 g*****g 的大作中提到】
: It aims framework development.
avatar
c*e
25
听起来其实一点都不高深阿,就象函数里面有一个输入的参数是string一样。这个
string参数就是别人的class,function的名字。

【在 b***i 的大作中提到】
: 假设你发布一个软件,然后别人可以用你的jar,增加自己的模块,怎么让你的jar调用
: 他的呢?
: 假如你的程序读取一个xml的控制文件,你的程序从里面读入class, function的名字,
: 然后调用。那么别人就可以不用改你的程序。你用reflection就可以完成这个任务。别
: 人改写xml就行了。就是说,这个函数的调用动态发生在运行期,编译的时候不知道该
: 调谁。。

avatar
z*e
26
你遇到过有什么功能是你理解之后觉得很难的?
用起来要是很难,那还了得

【在 c*********e 的大作中提到】
: 听起来其实一点都不高深阿,就象函数里面有一个输入的参数是string一样。这个
: string参数就是别人的class,function的名字。

avatar
A*r
27
Java Reflection看起来容易,但是用起来不容易,功力不到去用,那肯定是用错了
1 首先这玩意肯定是业务无关的,纯粹用来写框架用,谁要是写业务逻辑用了反射立
马打回去重写
2 Java的Object Oriented世界讲究面向接口编程,力图实现各种“高聚合低耦合”的
类,尽可能提高类的可重用性。
3 组件与组件之间只通过接口通讯,这样就和具体实现解耦,解耦的方式正是通过文
件配置
4 xml文件配置里,无论是class还是property还是任何别的东西,都是String来表达
的,JVM通过什么方式将String转化成Class和Object,就是Java Reflection了
avatar
N*7
28
Are you sure this is reflection?

hardcoded

【在 g*****g 的大作中提到】
: 举个例子,就是JDBC,不同数据库有不同JDBC driver。你可以把这样一个property写
: 成一个configuration.
: driver=com.mysql.jdbc.Driver
: 到时候DriverManager就可以动态Load。里面调用的就是reflection API
: Class.forName(getProperty("driver"))
: 这就是Factory Pattern。C++里面Factory Pattern,只能有几个固定的选项hardcoded
: ,而Java里面,可以直接把选项做成configuration。干净而灵活,增加一个新的选项
: ,无需修改代码。这就是多一层indirection的好处。

avatar
g*g
29
Are you sure this is a question?

【在 N******7 的大作中提到】
: Are you sure this is reflection?
:
: hardcoded

avatar
N*7
30
Yes, I'm pretty sure this is a question.
Factory pattern is not based on reflection, it's more like you defined
interface and some basic/abstract class or data model. The implementation
implements those interfaces. You don't really need to use reflection to
invoke methods and so on. The only part that may relate to reflection is
Class.forName. But after that, every thing is just interface API call
directly, no dynamic.
Reflection more like dynamic programming, you invoke method not really from
interface or definition, but lookup method from name/signature, and invoke
them dynamically.
Factory pattern could use reflection, but doesn't mean it had to be
reflection, and in case of JDBC, I don't think it's reflection.

【在 g*****g 的大作中提到】
: Are you sure this is a question?
avatar
g*g
31
I don't know what you are smoking. But I don't think you can convince anyone
Class.forName().newInstance() is not a reflection call.
Factory Pattern doesn't have to use reflection. True, but only reflection
can achieve the decoupling that separates the factory from knowing what it's
gonna create. This is a huge advantage over those languages that do not
have reflection.
It's not about statistics. This one reflection call is critical in framework
design.

from

【在 N******7 的大作中提到】
: Yes, I'm pretty sure this is a question.
: Factory pattern is not based on reflection, it's more like you defined
: interface and some basic/abstract class or data model. The implementation
: implements those interfaces. You don't really need to use reflection to
: invoke methods and so on. The only part that may relate to reflection is
: Class.forName. But after that, every thing is just interface API call
: directly, no dynamic.
: Reflection more like dynamic programming, you invoke method not really from
: interface or definition, but lookup method from name/signature, and invoke
: them dynamically.

avatar
n*3
32
Just like function pointer in c language
but way more powerfully because of using jvm

【在 c*********e 的大作中提到】
: 听起来其实一点都不高深阿,就象函数里面有一个输入的参数是string一样。这个
: string参数就是别人的class,function的名字。

avatar
f*r
33
这个feature我用过的,很好用。具体的方法是用java的annotation
interface, 请用户标出我需要的constructors/methods/fields:
@()
...constructor/method/field definiton...
在程序运行的时候,我可以reflect用户的class, 找到其中的annotation,
以及annotate对象的精确定义, 然后自由调用。非常方便,现在很多
library code都在用。比如google的guice用于injection, jackson用于
json serialization/deserialization等等。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。