I am trying to obtain a field’s value via reflection. The problem is I don’t know the field’s type and have to decide it while getting the value. This code results with this exception: Can not set java.lang.String field com….fieldName to java.lang.String I tried to cast, but I get comp…
Tag: reflection
Is it possible to cast a retrieved Class object (via reflection) to an interface?
I have 3 projects in my workspace – A, B, and C which is an Android lib project. A contains an Activity named Atest B contains a class named Btest which implements interface I. C contains the I interface. Both A and B are installed on my Android device. During Atest runtime, i’m executing the next…
Java seek a method with specific annotation and its annotation element
Suppose I have this annotation class So is there a way to look into an object, “seek” out the method with the @MethodXY annotation, where its element x = 3, y = 2, and invoke it? Thanks Answer Here is a method, which returns methods with specific annotations: It can be easily modified to your spec…
calling numbered function name in loop using java reflection
i have a problem as : MyFirstClass.java MySecondClass.java TestClass.java I want to use java reflection and call the pObj.getP**Param() methods in for loop by providing the parameters to g How it can be made possible. I don’t want to use array of p*Params. Thanks in advance. Answer Are you sure this is …
Change private static final field using Java reflection
I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the value? Answer Assuming no SecurityManager is preventing you from doi…
How do I programmatically compile and instantiate a Java class?
I have the class name stored in a property file. I know that the classes store will implement IDynamicLoad. How do I instantiate the class dynamically? Right now I have Does the newInstance only load compiled .class files? How do I load a Java Class that is not compiled? Answer How do I load a Java Class that…
Java – Get a list of all Classes loaded in the JVM
I would like to get a list of all the classes belonging to a certain package as well as all of their children. The classes may or may not be already loaded in the JVM. Answer Well, what I did was simply listing all the files in the classpath. It may not be a glorious solution, but it works reliably
Get type of a generic parameter in Java with reflection
Is it possible to get the type of a generic parameter? An example: Answer One construct, I once stumbled upon looked like So there seems to be some reflection-magic around that I unfortunetly don’t fully understand… Sorry.
Simple way to get wrapper class type in Java
I have a piece of code where I need to pass the class of a field in a method. Because of the mechanics of my code I can only handle reference objects and not primitives. I want an easy way of determining if a Field’s type is primitive and swap it with the appropriate wrapper class. So in code what
Is there a general “back-end” library for Java reflection
I’m currently working with a specialized, interpreted, programming language implemented in Java. As a very small part of the language, I’d like to add the ability to make calls into Java. Before I dive into all of the nitty-gritty of reflection, I was wondering if anyone knew of a general library …