Tag: reflection

Java Reflection:通过输入名称来获取给定类的实例?

是否可以通过将此类的名称作为字符串输入来获取类的所有实例? 像这样的东西? var instances = Reflection.findClass(“com.someone.MyClass”).getInstances(); 任何反馈都表示赞赏。 谢谢。

如何使用reflection在java中调用方法

如何使用reflection调用带参数的方法? 我想指定这些参数的值。

动态修改字段注释值

是否可以在运行时更改字段注释值? 我可以访问这些值,但无法找到更改它们的方法。 可以访问: Article.class.declaredFields.find {it.name=”annotatedField”}.declaredAnnotations

使用reflection创建新对象?

给定类值: public class Value { private int xVal1; private int xVal2; private double pVal; // constructor of the Value class public Value(int _xVal1 ,int _xVal2 , double _pVal) { this.xVal1 = _xVal1; this.xVal2 = _xVal2; this.pVal = _pVal; } public int getX1val() { return this.xVal1; } … } 我正在尝试使用reflection创建该类的新实例: 来自Main: …. // some code …. […]

反思安全

如何通过不允许Method , Field , Constructor对象调用setAccessible(true)来强制执行reflection安全性? SecurityPolicy文件还是其他什么? 通常,对于独立Java应用程序,没有注册SecurityManager 。 我使用这个System.setSecurityManager(new SecurityManager()); 这种方法适用于调用方法。 我想强制执行使用jar的整个jar或客户端代码不允许调用setAccessible(true); 有更好的方法吗? 谢谢。

如何使用Javareflection检查给定的类是实现Iterable

我有一个特定的目标类型(在运行时决定),以及一个可迭代的类,我正在与它进行比较。 我正在尝试编写一个检查类的generics参数的方法,以查看它是否是可以迭代我的目标类型的子类。 例子: Class X = SomeObject.class; matches(X, new ArrayList()) -> true matches(X, new ArrayList()) -> true matches(X, new ArrayList()) -> false matches(X, new ArrayList()) -> true (I think?) matches(X, new Iterable() { … }) -> true matches(X, new ListOfSomeObjects()) -> true (where ListOfSomeObjects extends Iterable)

从包中获取所有类

假设我有一个java包commands ,其中包含所有inheritance自ICommand类,我能以某种方式获得所有这些类吗? 我正在锁定以下内容: Package p = Package.getPackage(“commands”); Class[] c = p.getAllPackagedClasses(); //not real 有可能吗?

获取调用方法(java.lang.reflect.Method)

我想得到调用方法java.lang.reflect.Method 。 不是方法的名称。 以下是如何获取调用者类的示例。 // find the callers class Thread t = Thread.getCurrentThread(); Class klass = Class.forName(t.getStackTrace()[2].getClassName()); // do something with the class (like processing its annotations) … 它仅用于测试目的!

通过reflection获取给定类的可访问方法列表

有没有办法获得一个给定类可以访问(不一定是公共)的方法列表? 有问题的代码将在一个完全不同的类中。 例: public class A { public void methodA1(); protected void methodA2(); void methodA3(); private void methodA4(); } public class B extends A { public void methodB1(); protected void methodB2(); private void methodB3(); } 对于B级,我想得到: 所有自己的方法 A类A methodA1和methodA2 methodA3当且仅当B类与A在同一个包中时 methodA4永远不应该包含在结果中,因为B类无法访问它。 为了再次澄清,需要查找并返回上述方法的代码将在完全不同的类/包中。 现在, Class.getMethods()只返回公共方法,因此不会做我想要的事情; Class.getDeclaredMethods()仅返回当前类的方法。 虽然我当然可以使用后者并且在类层次结构中手动检查可见性规则,但我不愿意,如果有更好的解决方案。 我在这里错过了一些明显的东西吗?

你能在GWT客户端使用Java Reflection api吗?

是否可以在GWT客户端使用javareflectionAPI? 我想使用reflection来查找Javabean上的属性值。 这可能吗?