简体   繁体   English

使用Java Reflection Bad Practice?

[英]Is using Java Reflection Bad Practice?

I am building an application for a client and I am in the situation where I need to have the ability to reference a field value via a string, ie the users uses a string to define which field they want to change the value of, this is part of an abstract framework so technically I don't know the name of the fields they desire to change. 我正在为客户端构建应用程序,我需要能够通过字符串引用字段值,即用户使用字符串来定义要更改其值的字段,这是抽象框架的一部分,从技术上讲,我不知道他们想要改变的字段的名称。 Of course I could do this using hash maps, but I am considering using java reflection as this allows the fields to stay as fields of the object rather than the values being coded into a hash map. 当然我可以使用哈希映射来做到这一点,但我正在考虑使用java反射,因为这允许字段保留为对象的字段而不是被编码为哈希映射的值。 I have used reflection for my own personal work, but I was wondering if using Java reflection is actually bad practice, and I should stick to the hashmap methodology. 我已经将反射用于我自己的个人工作,但我想知道使用Java反射实际上是不好的做法,我应该坚持使用hashmap方法。

(Any other suggestions for solving the design problem described are also appreciated) (对于解决所描述的设计问题的任何其他建议也表示赞赏)

Thanks 谢谢

The question itself is opinion based, although I believe most will agree that you can't just say "reflection is bad". 问题本身就是基于意见的,虽然我相信大多数人会同意你不能只说“反思是坏事”。 Sometimes it's the only way, which is why a lot of libraries use reflection. 有时这是唯一的方法,这就是许多图书馆使用反射的原因。 Sometimes it's not the only way, but a workaround would be even worse. 有时这不是唯一的方法,但解决方法会更糟。 Sometimes it's not the only way, and not the easiest way, but the developer is far too amazed at the power of reflection to think straight. 有时它并不是唯一的方式,也不是最简单的方法,但开发人员对反思思维直接的力量感到非常惊讶。

Except for that last one there are plenty of valid reasons to consider reflection as a solution. 除了最后一个,有充分的理由将反思视为一种解决方案。

Personally reflection makes me sad, and in my experience there is almost always a better way. 个人反思让我很伤心,根据我的经验,几乎有一种更好的方法。 In the problem you described, setting variables based on a string i'd consider going with your hashmap idea which would reference variables via a string key which seems like exactly what you are describing. 在你描述的问题中,基于一个字符串设置变量我会考虑使用你的hashmap想法,它会通过字符串键引用变量,这看起来就像你正在描述的那样。 If you need the ability to reference values that do not exist you could also include factory methods to create variables when no key exists and then add to the map, if you are wrapping the objects then they will be passed by reference to avoid the problem you describe but this depends on the implementation (eg using Integer class etc for auto boxing if you are referencing primitives) Together this would allow for a much tighter and well defined implementation rather than reflecting values here there and everywhere. 如果你需要能够引用不存在的值,你还可以包含工厂方法来创建变量,当没有键存在然后添加到地图,如果你包装对象然后它们将通过引用传递,以避免你的问题描述,但这取决于实现(例如,如果您正在引用基元,则使用Integer类等进行自动装箱)这将允许更严格和定义明确的实现,而不是在那里和任何地方反映值。 Apologies for the anti-reflection bias! 为反思偏见道歉! Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM