0

What i am trying to do: get the value of the a reflected field named "f3" with the type "K". I would like to be able to use the value of f3 to reflect fields from within it. (edit)f3 is inside class b which is from a .jar file. (edit)

All methods of getting the value that i try give the error: can not set K field f3 to java.lang.Class.

Question: How can i get the value of the field f3 (which has the type K) on the class b (which is a class loaded from an external jar) ?

The following code throws an IllegalArgumentException :

Class instance = loadClass("b"); // simplified for readability
Field fieldf3 = instance.getDeclaredField("f3");
fieldf3.setAccessible(true);
Object value = fieldf3.get(instance); //exception on this line.

StackTrace :

java.lang.IllegalArgumentException: Can not set K field b.f3 to java.lang.Class
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:387)

1 Answer 1

3

You are trying to retrieve the value of the field on a Class<B> instance instead of doing it on an instance of the class representend by what you (quite clumsily) called instance in your code. To be clear, you are doing like if f3 was a static field of the class B (a field of Class<B> is indeed a static field of B) whereas it is actually a non-static field. Thus, you need to get an instance of B before calling the get method :

Class<?> clazz  = loadClass("b");
Object instance = clazz.newInstance();
Fied fieldf3    = B.class.getDeclaredField("f3");
fieldf3.setAccessible(true);
fieldf3.get(instance);

Let's see with an example (this time let's assume that B is a class defined in the main file) :

static class B {
    private int f3;
    public B(int f) {
        f3 = f;
    }
}

public static void main(String[] args) {
    B b = new b(17);
    Field field = B.class.getDeclaredField("f3");
    field.setAccessible(true);
    System.out.println(field.get(b));
}

This code will output 17 as expected. Now, imagine that f3 is a static field, then it becomes a field on any Class<B> instance (actually, I think it is a singleton so there is only a single such instance), then you could add System.out.println(field.get(B.class)); and it would still work. The value of the field will also be accessible from any instance of B so System.out.println(field.get(b)); will also work.

Sign up to request clarification or add additional context in comments.

11 Comments

instance is b.class, b has the field f3 of type K inside it. b is from a .jar file
instance is not K class
Yes, in my code currently, your K k is my b instance, and your int f3 is the K f3 value i want but field.get(instance) is what gives problem
Sorry i forgot to add how i got b, my b instance is loaded via URLClassLoader.loadClass("b") and calling the main method of b
<code>Method main = instance.getMethod("main", new Class[] {args.getClass()}); main.setAccessible(true); try { main.invoke(instance, new Object[] {args}); } catch(IllegalAccessException exc) { }</code>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.