Tag: reflection

Java:使用reflection实例化枚举

假设你有一个文本文件,如: my_setting = ON some_method = METHOD_A verbosity = DEBUG … 您希望相应地更新相应的对象: Setting my_setting = ON; Method some_method = METHOD_A; Verbosity verbosity = DEBUG; … 在哪里都是不同种类的枚举。 我想有一个通用的方式来实例化枚举值。 也就是说,在运行时使用reflection,而不用事先知道对象的枚举types。 我会想像这样的事情: for (ConfigLine line : lines) { String[] tokens = line.string.split("=", 2); String name = tokens[0].trim(); String value = tokens[1].trim(); try { Field field = this.getClass().getDeclaredField(name); if(field.getType().isEnum()) […]

检查原始字段的types

我试图确定一个对象的字段的types。 当它传递给我的时候,我不知道这个对象的types,但是我需要find很long的字段。 很容易区分盒装Long但原始long似乎更困难。 我可以确定传给我的物品只有Longs ,而不是原始物品,但我宁愿不要。 所以我拥有的是: for (Field f : o.getClass().getDeclaredFields()) { Class<?> clazz = f.getType(); if (clazz.equals(Long.class)) { // found one — I don't get here for primitive longs } } 这似乎工作的一个怪异的方式是这样的: for (Field f : o.getClass().getDeclaredFields()) { Class<?> clazz = f.getType(); if (clazz.equals(Long.class) || clazz.getName().equals("long")) { // found one } } 如果有的话,我真的想要一个更清洁的方法来做到这一点。 如果没有更好的方法,那么我认为要求我收到的对象只使用Long […]

运行包含在一个string中的一段代码

我在String中有一段Java代码。 String javaCode = "if(polishScreenHeight >= 200 && " + "polishScreenHeight <= 235 && polishScreenWidth >= 220) { }"; 是否有可能将此Javastring转换为Java语句并运行它? 可能使用Javareflection?

如何通过reflection获取由Ruby的Module类定义的常量?

我试图让Matz和Flanagan的“Ruby编程语言”元编程章节进入我的脑海,但是我无法理解我梦寐以求的代码片段的输出: p Module.constants.length # => 88 $snapshot1 = Module.constants class A NAME=:abc $snapshot2 = Module.constants p $snapshot2.length # => 90 p $snapshot2 – $snapshot1 # => ["A", "NAME"] end p Module.constants.length # => 89 p Module.constants – $snapshot1 # => ["A"] p A.constants # => ["NAME"] 本书指出类方法constants返回类的constants列表(正如你可以在A.constants的输出中看到的A.constants )。 当我遇到上述奇怪的行为时,我试图获取为Module类定义的常量列表。 A的常量显示在Module.constants中。 如何获得Module类定义的常量列表? 文档状态 Module.constants返回系统中定义的所有常量。 包括所有类和方法的名称 由于A从Module.constantsinheritance它的实现,它在基类和派生types中的行为有什么不同? […]

GetEntryAssembly为Web应用程序

Assembly.GetEntryAssembly()不适用于Web应用程序。 但是…我真的需要这样的东西。 我使用一些在web和非web应用程序中使用的深度嵌套代码。 我目前的解决scheme是浏览StackTracefind第一个被调用的程序集。 /// <summary> /// Version of 'GetEntryAssembly' that works with web applications /// </summary> /// <returns>The entry assembly, or the first called assembly in a web application</returns> public static Assembly GetEntyAssembly() { // get the entry assembly var result = Assembly.GetEntryAssembly(); // if none (ex: web application) if (result == null) { […]

如何使用reflection来确定数组的嵌套types(元素types)?

我有一个System.Type的实例,为此“IsArray”返回true。 我怎样才能确定数组types的“嵌套types”? 即 Type GetArrayType(Type t) { if(t.IsArray) { // What to put here? } throw new Exception("Type is not an array"); } Assert.That(GetArrayType(typeof(string[])), Iz.EqualTo(typeof(string)); Assert.That(GetArrayType(typeof(Foo[])), Iz.EqualTo(typeof(Foo));

如何通过reflection获取当前的属性名称?

我想通过reflection机制获取属性名称。 可能吗? 更新:我有这样的代码: public CarType Car { get { return (Wheel) this["Wheel"];} set { this["Wheel"] = value; } } 而且因为我需要更多这样的属性,我想要做这样的事情: public CarType Car { get { return (Wheel) this[GetThisPropertyName()];} set { this[GetThisPropertyName()] = value; } }

如何从基类中find子类名?

在run-time ,在base class内部,如何find当前的子类名?

如何创build一个注释的实例

我正在尝试做一些Java注释魔术。 我必须说,我仍然赶上诠释技巧,而某些事情对我来说还不是很清楚。 所以…我有一些注释类,方法和领域。 我有一个方法,它使用reflection在类上运行一些检查,并将一些值注入到类中。 这一切工作正常。 不过,我现在正面临着一个需要一个注释的实例(这么说)的情况。 所以…注释不像常规的接口,你不能做一个类的匿名实现。 我知道了。 我在这里看过有关类似问题的一些post,但我似乎无法find我正在寻找的答案。 我基本上喜欢得到一个注解的实例,并能够使用reflection设置它的一些领域(我想)。 有没有办法做到这一点?

通过使用reflection来获取带有注释的字段列表

我创build我的注释 public @interface MyAnnotation { } 我把它放在我的testing对象的字段中 public class TestObject { @MyAnnotation final private Outlook outlook; @MyAnnotation final private Temperature temperature; … } 现在我想用MyAnnotation获取所有字段的列表。 for(Field field : TestObject.class.getDeclaredFields()) { if (field.isAnnotationPresent(MyAnnotation.class)) { //do action } } 但似乎像我的块行动永远不会执行,并且字段没有注释,因为下面的代码返回0。 TestObject.class.getDeclaredField("outlook").getAnnotations().length; 有没有人可以帮助我,告诉我我做错了什么?