How can I create a generic array in Java?

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 explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of … Read more

What Method.isBridge used for?

A bridge method may be created by the compiler when extending a parameterized type whose methods have parameterized arguments. You can find in this class BridgeMethodResolver a way to get the actual Method referred by a ‘bridge method’. See Create Frame, Synchronize, Transfer Control: As an example of such a situation, consider the declarations: class … Read more

How to get actual type arguments of a reified generic parameter in Kotlin?

Due to type erasure, actual generic arguments cannot be obtained through T::class token of a generic class. Different objects of a class must have the same class token, that’s why it cannot contain actual generic arguments. Edit: Since Kotlin 1.3.50, following the technique described below to get type information for a reified type parameter is … Read more

C# “is” operator – is that reflection?

Referencing ECMA-335, the is operator generates the isinst object model IL instruction (Partition III §4.6), which is part of the base instruction set as opposed to being part of the Reflection library (Partition IV §5.5). Edit: The is operator is extremely efficient compared to the reflection library. You could perform basically the same test much … Read more

How do I use getConstructor(params).newInstance(args)?

In Java this is called Reflection. Assuming the class has this constructor, otherwise you will get a NoSuchMethod exception I believe. clazz.asSubclass(asSubclassOfClass) .getConstructor(String.class,XYZ.class) .newInstance(“howdy”,XyzObj); Since you are new to Java, let me give you an easier so that you can understand what’s going on under the hood when you do this. Assume you have the … Read more

Assembly.GetTypes() throwing an exception

Getting a type from a assembly may require additional assemblies to be loaded so that is most probably the reason for the error; a failure to load a dependent assembly. However a .NET assembly may be constructed from several modules in different files so I believe you may also face this problem if you have … Read more

Loading a Class from a String

No, there is no better way (by design). You are not supposed to do this, Java is designed as a type-safe language. However, I can understand that you sometimes need to do things like this, and for that purposes you can create a library function like this: public <T> T instantiate(final String className, final Class<T> … Read more