Java:什么场景需要使用反射?
因此,通过阅读一些文章,我从中得到的信息是能够实时修改字段并向类设置值,而无需重新编译。
那么是否可以对第三方java库创建的没有可用源代码的类执行此操作/是否可以使用反射在运行时修改类实例?
反射还常用在哪些场景中?
我试图了解反射如何适用。
So from reading some of the articles, the message i got out of it was being able to modify fields and set values to classes in real time without recompiling.
so is it possible to do this to 3rd party java library created classes which no source code is available / is it possible to use reflection to modify class instances on run time?
in what other scenarios is reflection commonly used?
I am trying to understand how reflection can be applicable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
每当您在运行时处理字符串并希望将该字符串的一部分视为该语言中的标识符时。
它还可用于允许在语言中模拟语言功能。
考虑命令行 java com.example.MyClass 将字符串转换为类名。这不需要反射,因为
java
可执行文件可以将.class
文件转换为代码,但如果没有反射,它就无法编写java com. example.Wrapper com.example.MyClass
其中Wrapper
委托给其参数,如下所示:Any time you're dealing with a string at runtime and want to treat part of that string as an identifier in the language.
It can also be used to allow language features to be emulated in the language.
Consider the command line
java com.example.MyClass
which turns a string into a class name. This doesn't require reflection, because thejava
executable can turn a.class
file into code, but without reflection it would not be able to writejava com.example.Wrapper com.example.MyClass
whereWrapper
delegates to its argument as in:另一种情况是开发 IDE,如 eclipse/netbeans 等,以确定抽象类中的哪些方法需要由子类实现,并自动为您编写缺少的方法调用(一个示例)。
One other case developing IDEs like eclipse/netbeans etc., to determine which methods in an abstract class need to be implemented by a child class, and automatically write the missing method calls for you (one example).
Guice 或 Spring 等注入框架使用反射来帮助您在运行时构建对象的实例。
Injection frameworks like Guice or Spring use reflection to help you build instances of objects at runtime.
在需要配置将事物串在一起的情况下,反射也很有用。例如,在我编写的应用程序中,我有一个 @Report("debits") 注释,它只是添加到生成报告的方法中。然后,在 XML 配置中,用户可以简单地添加:
这可以最大限度地减少将 XML 代码映射到实际方法的样板代码,因为反射可以发现报告方法并使其直接可用。
Reflection is also useful in cases where configuration is required to string things together. For example, in an application I wrote I have a @Report("debits") annotation that is simply added to methods that generate reports. Then, in the XML configuration a user can simply add:
This minimizes boiler plate code from mapping the XML code to the actual method, since reflection can discover the report methods and make it available directly.
我被要求为以下语句创建一个解决方案。
“1)差异服务:
• 可以计算两个对象之间的差异并返回结果
“差异”
• 可以对原始对象应用先前创建的“差异”,以便
返回的对象与用于计算的修改后的对象相匹配
差异。 “
如果不使用反射,这将非常困难。使用反射,我可以列出所有未知对象的类元素、属性和方法。我可以使用它们来获取对象中包含的值。我可以比较原始对象值和修改后的对象值,创建反映两个对象之间变化的“diff”对象,
然后我可以读取“diff”对象中的指令并将其应用到原始对象上,Java 反射为我提供了更改未知属性值所需的工具。我可以调用原始对象的 setter 方法和如果原始属性为 null,则在需要时实例化类型以在原始对象上设置修改后的值。
“diff”应用程序适用于同一类型的任何两个对象,但它们可以是任何类型,两个对象都必须是同一类型。反射
非常强大,允许我们创建真正的通用多态方法、函数、库和系统,其中传递的对象类型不需要在编译时知道。这适用于同时使用 Java 反射和泛型(这是一个非常强大的组合)。
最后,我还使用 Java Reflection 创建了一个通用排序函数,它可以使用该类的任何属性作为排序键对任何类类型的任何列表进行排序。只要调用方法传递列表和属性要使用的名称,该方法将返回一个排序列表。
I was asked to create a solution for the below statement.
"1) A diff service that:
• can calculate the differences between two objects and return the resulting
"diff"
• can apply a previously created "diff" against an original object, so that
the returned object matches the modified object that was used to calculate
the diff. "
This would have been very difficult without using reflection. Using reflection I could list all the unknown object's Class elements, properties and methods. I could use these to get the values contained in the object. I could compare the original and modified objects values, create "diff" object reflecting changes between the two objects.
Using Java reflection I could then read the instructions in the "diff" object and apply these to the original object. Java reflection gave me the tools needed to change the values on the unknown properties of the original object. I could invoke setter methods and instantiate types where needed if the original property was null to set the modified value on the original object.
The "diff" app works on any two objects of the same type, but they could be any type, both objects just have to be of the same type.
Reflection is very powerful and allow us to create true generic polymorphic methods, functions, libraries and system, where the passed object type does not need to be known at compile time. This applies when using Java Reflection and Generics together, a very powerful combination.
To end of, I have also used Java Reflection to create a generic sort function, that could sort any list of any Class Type, using any property of the Class as the sort key.As long as the calling method passed the list and the property name to use, the method would return a sorted list.
以下是使用反射的一些情况 在
String 类的另一种情况
下 使用反射从 XML 加载
Here are some cases to use reflection in
Another case with the String class
Loading from XML with reflection