Skip to content

Tag: reflection

java – Set final fields with reflection in Constructor

I’m trying to make a multi-language app with messages inside multiple *.properties files. I’ve started working on something like this: Now, there’s a lot of messages, and I don’t feel like typing the same thing each time (plus there could me typos which could be an issue…). The first solution I thought of was to loop through all of the

Check if Annotation is inherited

I have an annotation and three classes like this: Is there any way I can determine if a present annotation is inherited or declared directly at the class? Something like a method public boolean isInherited(Class<?> clazz, MyAnnotation annotation) that should only be called if the annotation is present. Exptected output: Answer You can use getDeclaredAnnotation instead of getAnnotation:

Updating field using reflection from parent class

I have a Parent class that has a method that uses reflection to retrieve and update fields. This method will be called by the child class When the child class (in this case, an anonymous class) calls the method, which is inherited form the parent, the value of this is the child class. Because of that, I thought there would

In Kotlin, how does GSON.fromJson(…) manage to mutate a read-only field of an existing object?

In Kotlin, how can an instance’s read-only val field be mutated? In the following Kotlin code, gson.fromJson(…) gets a Thing(0) from the InstanceCreator and somehow manages to mutate value from 0 to 1, then return that object. How? I verified that the object returned by gson.fromJson(…) is the same object provided by the InstanceCreator, so it’s not creating a new

Invoke nested deepobject method via reflection

Is it possible to invoke nested method inside nested object from real object using reflection? Something like this Real object i have is chatClient and i want to invoke method of class3, which i cannot obtain directly but via class1 path. Answer You can have a logic of fetching nested fields in a function inside a for loop , so