lundi 4 septembre 2023

Field exists but NoSuchFieldException

I have simple code where I try to get the signature of a field:

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {

    static class Test {
        int hi =  1;
    }

    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException {
        Field hi = Test.class.getDeclaredField("hi");

        Field field = Field.class.getDeclaredField("signature");
        field.setAccessible(true);
        String signature = (String) field.get(hi);

        System.out.println(signature);
    }
}

however, I am getting this exception:

Exception in thread "main" java.lang.NoSuchFieldException: signature
    at java.base/java.lang.Class.getDeclaredField(Class.java:2663)
    at Main.main(Main.java:13)




Aucun commentaire:

Enregistrer un commentaire