- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的应用程序中有以下代码:
for(PropertyDescriptor property : myObjectProperties){
if(property.getName().equals("myPropertyName")){
// logic goes here
}
}
这当然在多个层面上都是危险的,最糟糕的可能是如果我在“MyObject”上重命名属性“myPropertyName”,代码将会中断。
就是说,我可以在不显式键入属性名称的情况下引用属性名称的最简单方法是什么(因为这会使我收到编译器警告)?我看起来像:
for(PropertyDescriptor property : myObjectProperties){
if(property.getName().equals(MyObject.myPropertyName.getPropertyName())){
// logic goes here
}
}
或者这甚至可以用 Java 实现吗?
最佳答案
您可以通过向其添加一些注释来定义目标属性。然后在具有所需注释的循环搜索字段中。
首先定义一个注解,可以在运行时访问
@Retention(RetentionPolicy.RUNTIME)
public @interface Target {
}
简单又好用现在创建使用它的类
public class PropertySearcher {
int awesome;
int cool;
@Target
int foo;
int bar;
String something;
}
现在让我们搜索它
public static void main(String[] args) {
PropertySearcher ps = new PropertySearcher();
for (Field f : ps.getClass().getDeclaredFields()) {
for (Annotation a : f.getDeclaredAnnotations()) {
if (a.annotationType().getName().equals(Target.class.getName())) {
System.out.println("Fname= " + f.toGenericString());
//do magic here
}
}
}
}
输出Fname= int reflection.PropertySearcher.foo
找到属性(property)。
这样您就可以毫无顾虑地重构您的代码。
关于java - 如何避免 Java 反射中的魔术字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23757561/
有没有办法在 Java 中找到所有硬编码的数字和字符串?我的意思是尚未放入专用变量的值。 例如: while (i < 6) { System.out.println("6 is a numb
在Scala _ [underscore] magic中,Anantha Kumaran使用_+_作为(a,b)=>a+b的缩写。 在Kotlin Koans -- SAM conversions中,
更新:字典解决方案很棒,除非要查找的事物的数量与列表中的事物数量相比显得苍白无力。我应该事先说明这一点。 假设你有数组: var arr = { Tuple.Create("1", "won"),
有没有一种魔术方法,当从一个对象调用某个方法时,首先调用一个魔术方法。有点像 __call 方法,但这只会在找不到该方法时触发。 所以就我而言,我想要这样的东西: class MyClass {
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我试图在 WPF 中制作一些动画,并在动画结束时运行一些其他操作。 另外,想避免动画完成回调机制,所以,我想出了一个解决方案,如下面的代码所示: // Start one second of anim
我正在测试用于 Web 窗体的新 ASP.NET 4.5 模型绑定(bind),使用一个简单的存储库公开 IQueryable。该存储库使用 EF 5,数据库优先方法。我正在投影 EF 自动生成的实体
我一直在阅读OpenGL ES 2.0,SpriteKit和GLKit,但是我仍然不知道如何解决以下问题。 问题: 1)创建一个圆柱形的360度空间,该空间容纳一个AVCaptureVideoPrev
我将 iPython 更新为: Python 3.6.7 (default, Mar 29 2019, 10:38:28) Type 'copyright', 'credits' or 'licens
我正在为C#项目构建一个日志记录控件,并希望能够使用当前源代码文件,行,类,函数等的名称来调用它。PHP使用具有所有此信息的“魔术常数”: http://php.net/manual/en/langu
我想为我拥有的MTG卡创建一个数据库。设计将是什么? 我想存储有关每张卡的以下信息: 1. Name of card. 2. Set the card belongs to. 3. Condition
我正在使用 __get()使我的一些属性“动态”(仅在请求时初始化它们)。这些“假”属性存储在私有(private)数组属性中,我正在 __get 中检查它。 无论如何,您认为为每个属性创建方法比在
我已经使用 magento 有一段时间了,总是无法在使用魔法 getter 和 getData() 之间做出决定 除了轻微的性能开销(而且必须非常轻微)之外,有人可以解释主要区别吗? 我在思考: fu
多年前,在我失传已久的 Charles Petzold 的 Windows 3.0 编程书籍中,记录了一个神奇的 COLORREF 或 RGB 值,您可以使用它来检查应该用浅色还是深色绘制文本。例如。
我想编写一个包装类,它接受一个值并且除了添加一个“原因”属性外,其行为与它一样。我有这样的想法: class ExplainedValue(object): def __init__(self
IPython %timeit 魔术命令在测量运行某些 Python 代码所需的时间方面做得很好。现在,我想在 Python 脚本中使用类似的东西。我知道 timeit 模块,但是它有几个缺点,例如,
以下是 python 2.7 和 MySQLdb 1.2.3。 我需要一个类包装器来为不支持它的对象添加一些属性(带有 __slots__ 的类和/或一些用 C 编写的类)所以我想出了这样的东西: c
我是 Ruby 的新手,正在学习一些教程/截屏视频。我已经到达他们讨论 before_filter 回调的部分,它使用了一些对我来说有点奇怪的语法。我不知道它是否是 ruby 的一个特性,或者它是
新年快乐! 我是 Python 新手 multiprocessing模块。为了更好地理解 apply_async有效,我在下面写下了简短的脚本。除非我将第二行注释掉( get_ipython().ma
我正在尝试遵循以下示例:http://bl.ocks.org/3887051 但是,我有一个 JSON 对象,而不是 CSV。 现在,我看到我可以按照以下说明将 JSON 转换为 CSV:How to
我是一名优秀的程序员,十分优秀!