I am trying implement the Data transformation using Reflection1 example in my code. The GetSourceValue function has a switch comparing various types, but I want to remove these types...
  • April 15, 2022
  • 0 Comments
Due to the implementation of Java generics, you can’t have code like this: public class GenSet<E> { private E a; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error:...
  • April 14, 2022
  • 0 Comments
What’s the best way to call a generic method when the type parameter isn’t known at compile time, but instead is obtained dynamically at runtime? Consider the following sample...
  • April 14, 2022
  • 0 Comments
What is reflection, and why is it useful? I’m particularly interested in Java, but I assume the principles are the same in any language. 2 23 The name reflection...
  • April 10, 2022
  • 0 Comments
The name reflection is used to describe code which is able to inspect other code in the same system (or itself). For example, say you have an object of...
  • April 8, 2022
  • 0 Comments
I have to ask a question in return: is your GenSet “checked” or “unchecked”? What does that mean? Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was...
  • April 5, 2022
  • 0 Comments
After type erasure, all that is known about T is that it is some subclass of Object. You need to specify some factory to create instances of T. One approach could use a Supplier<T>:...
  • April 5, 2022
  • 0 Comments
Like answered before, you should use: Object value = field.get(objectInstance); Another way, which is sometimes prefered, is calling the getter dynamically. example code: public static Object runGetter(Field field, BaseValidationObject...
  • April 4, 2022
  • 0 Comments