How to compare type of an object's instance to a generic type?

Basically you can't do that due to type erasure. The normal workaround is to pass a Class object as a parameter; e. G public U GetComponent(Class clazz) { for (IComponent component : list) { if (clazz.

IsInstance(component)) { return clazz. Cast(component); } } } You could also use if (clazz. Equals(component.getClass())) { but that does an exact type match ... which is not what the instanceof operator does.

The instanceof operator and the Class. InstanceOf method both test to see if the value's type is assignment compatible.

Basically you can't do that due to type erasure. The normal workaround is to pass a Class object as a parameter; e.g. Public U GetComponent(Class clazz) { for (IComponent component : list) { if (clazz. IsInstance(component)) { return clazz.

Cast(component); } } } You could also use if (clazz. Equals(component.getClass())) { ... but that does an exact type match ... which is not what the instanceof operator does. The instanceof operator and the Class.

InstanceOf method both test to see if the value's type is assignment compatible.

Everyone says 'can't' yet my little demo works.. – sje397 May 17 at 14:59.

General rule with java - if a generic class needs to know its generic type(s), then you have to pass a hint. A common approach for the problem is using the constructor: public class ComponentsManager { private Class genericType = null; public ComponentsManager( Class genericType) { this. GenericType = genericType; } } Now the class knows it's generic type class and you can uses the generic type class instance to verify, if a component in your collection matches the classes generic type.

You could try adding a couple of 'test' methods: private static boolean testComponent(U u) { return true; } private static boolean testComponent(Object o) { return false; } Then, use testComponent(component) instead of component instanceof U. Example code: import java.util. *; class IComponent { } class T1 extends IComponent { } public class Test { public static boolean testComponent(U u) { return true; } public static boolean testComponent(Object o) { return false; } public static void main(String args) { T1 t = new T1(); System.out.

Println("hm? " + (testComponent(t)? "true" : "false")); } } Output: hm?True.

(Since @sje397 asked me to comment ...): This will "work", but you have to do an unsafe type conversion inside the GetComponent method ... or change it to a non-generic method. In addition, you are duplicating Java's type checking by adding your own ersatz type checking. This is cumbersome, and the end solution is more fragile than a solution that uses real Java type checking.

– Stephen C May 17 at 23:11 Thanks @Stephen C – sje397 May 18 at 1:08.

As far as I know, you can't. You'll have to take a Class object as a parameter: public U getComponent(Class clazz) { // ... if (component.getClass() == clazz) { return (U) component; } } And call it like this: getComponent(MyComponentImpl. Class).

This won't compile. – Stephen C May 17 at 12:14 You're right @Stephen. I forgot to cast the result.

Thanks for pointing it out. – Hosam Aly May 17 at 13:36 that won't compile either. Try it and see.

– Stephen C May 17 at 23:04 @Stephen: I tried it and it works (at least in Eclipse using JDK 6). It compiles with an "Unchecked cast" warning, but it works fine. What error do you get?

– Hosam Aly May 17 at 9:33 that's what I mean. Most people consider those to be anathema. The problem is that while it is safe in practice, a small change will result in it being unsafe.

For this reason, it is a bad idea to ignore or suppress it. Instead you should treat it as a hard compilation error and fix it. – Stephen C May 17 at 3:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions