简体   繁体   English

Java反射:从超类继承的类中调用GetDeclaredFields()方法

[英]Java Reflection: GetDeclaredFields() method call in inherited class from super class

I have a bunch of sensors, which should be inherited from a superclass. 我有一堆传感器,应该从超类继承。

In my superclass, as well as in the sensor itself, there are static final attributes and possible value ranges of my attributes, which defines my sensor. 在我的超类以及传感器本身中,有静态的最终属性以及属性的可能值范围,这些属性定义了传感器。

Now I want to source out a method, which returns a HashMap of attributes and its values ranges in the superclass. 现在,我想提供一个方法,该方法返回属性的HashMap及其在超类中的值范围。

Because of the fact, that the sensor never changes its parameters, I want to use this getAttributes() method as a class method for optional calling it in my main[]. 由于这一事实,即传感器永远不会更改其参数,因此我想将此getAttributes()方法用作类方法,以便在main []中进行可选调用。

My problem is: How can I get the attributes in a static way (I used reflections at the moment) So well. 我的问题是:如何以静态方式获取属性(目前我使用了反射)很好。

A UML diagram of what I said: 我说的UML图:
这里

my main method looks like: 我的主要方法如下:

public static void main(String[] args){
    HashMap<String, List<String>> a = SensorA.getConfigurationAttributes();
}

and my abstract component class: 和我的抽象组件类:

public static HashMap<String, List<String>> getConfigurationAttributes(boolean getMandatoryOnly){
    Field[] classAttributes = this.getClass().getDeclaredFields();  // error: cannot use this in static context
    // ... other code, working with the attributes.
}   

Of course, now, Java says: "cannot use this in a static context." 当然,Java现在说:“不能在静态上下文中使用它。” But how can I correct this error? 但是,如何纠正此错误?

On the one hand, remove the static tag would solve the problem, but on the other hand I have to instantiate every sensor before getting my attribute information. 一方面,删除静态标签可以解决问题,但是另一方面,我必须实例化每个传感器,然后才能获取属性信息。

You're using the keyword "this" which references an instance of that object inside a static method. 您正在使用关键字“ this”,它引用静态方法中该对象的实例。 That's incorrect. 不对

In order to achieve the same task, use this code instead : 为了完成相同的任务,请改用以下代码:

Field[] fields = Class.forName("my.package.MyClass").getDeclaredFields();

You can do that statically if you pass along the class for which you want to retrieve those fields as an argument to your method. 如果传递要检索其字段的类作为方法的参数,则可以静态执行此操作。

Else you can only do it for the class you are in, which kind of defeats the purpose since you already know all the declared fields. 否则,您只能为您所在的班级做这件事,因为您已经知道所有声明的字段,所以这样做达不到目的。

Static methods are not inherited, so you cannot invoke it magically on the sub class (for this you indeed need to remove the static modifier). 静态方法不会被继承,因此您不能在子类上神奇地调用它(为此,您确实需要删除static修饰符)。

Thus, do something like this to maybe achieve what you want: 因此,执行以下操作可能会达到您想要的目标:

public static HashMap<String, List<String>> getConfigurationAttributes(boolean getMandatoryOnly, Class c) 
{
     Field[] classAttributes = c.getDeclaredFields();
     // ... other code, working with the attributes.
} 

Edit: And now the obligatory warning about reflection: Using reflection like this is not a very good or elegant solution. 编辑:现在关于反射的强制性警告:像这样使用反射不是一个很好的解决方案。 Reflection is costly, works around a lot of language features and not a very clean way of implementing things. 反射是昂贵的,它围绕许多语言功能工作,而不是一种非常干净的实现方式。 Please avoid reflection where ever you can, in most cases there is simple, elegant and easier solutions. 请尽可能避免反射,在大多数情况下,这里提供简单,优雅且更容易的解决方案。

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

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