Java: Instanceof and Generics - java

Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to.
But Eclipse complains when I do this:
#Override
public int indexOf(Object arg0) {
if (!(arg0 instanceof E)) {
return -1;
}
This is the error message:
Cannot perform instanceof check against type parameter E. Use instead its erasure Object since generic type information will be erased at runtime
What is the better way to do it?

The error message says it all. At runtime, the type is gone, there is no way to check for it.
You could catch it by making a factory for your object like this:
public static <T> MyObject<T> createMyObject(Class<T> type) {
return new MyObject<T>(type);
}
And then in the object's constructor store that type, so variable so that your method could look like this:
if (arg0 != null && !(this.type.isAssignableFrom(arg0.getClass())) {
return -1;
}

Two options for runtime type checking with generics:
Option 1 - Corrupt your constructor
Let's assume you are overriding indexOf(...), and you want to check the type just for performance, to save yourself iterating the entire collection.
Make a filthy constructor like this:
public MyCollection<T>(Class<T> t) {
this.t = t;
}
Then you can use isAssignableFrom to check the type.
public int indexOf(Object o) {
if (
o != null &&
!t.isAssignableFrom(o.getClass())
) return -1;
//...
Each time you instantiate your object you would have to repeat yourself:
new MyCollection<Apples>(Apples.class);
You might decide it isn't worth it. In the implementation of ArrayList.indexOf(...), they do not check that the type matches.
Option 2 - Let it fail
If you need to use an abstract method that requires your unknown type, then all you really want is for the compiler to stop crying about instanceof. If you have a method like this:
protected abstract void abstractMethod(T element);
You can use it like this:
public int indexOf(Object o) {
try {
abstractMethod((T) o);
} catch (ClassCastException e) {
//...
You are casting the object to T (your generic type), just to fool the compiler. Your cast does nothing at runtime, but you will still get a ClassCastException when you try to pass the wrong type of object into your abstract method.
NOTE 1: If you are doing additional unchecked casts in your abstract method, your ClassCastExceptions will get caught here. That could be good or bad, so think it through.
NOTE 2: You get a free null check when you use instanceof. Since you can't use it, you may need to check for null with your bare hands.

Old post, but a simple way to do generic instanceOf checking.
public static <T> boolean isInstanceOf(Class<T> clazz, Class<T> targetClass) {
return clazz.isInstance(targetClass);
}

Provided your class extends a class with a generic parameter, you can also get this at runtime via reflection, and then use that for comparison, i.e.
class YourClass extends SomeOtherClass<String>
{
private Class<?> clazz;
public Class<?> getParameterizedClass()
{
if(clazz == null)
{
ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();
clazz = (Class<?>)pt.getActualTypeArguments()[0];
}
return clazz;
}
}
In the case above, at runtime you will get String.class from getParameterizedClass(), and it caches so you don't get any reflection overhead upon multiple checks. Note that you can get the other parameterized types by index from the ParameterizedType.getActualTypeArguments() method.

I had the same problem and here is my solution (very humble, #george: this time compiling AND working ...).
My probem was inside an abstract class that implements Observer.
The Observable fires method update(...) with Object class that can be any kind of Object.
I only want to handler Objects of type T
The solution is to pass the class to the constructor in order to be able to compare types at runtime.
public abstract class AbstractOne<T> implements Observer {
private Class<T> tClass;
public AbstractOne(Class<T> clazz) {
tClass = clazz;
}
#Override
public void update(Observable o, Object arg) {
if (tClass.isInstance(arg)) {
// Here I am, arg has the type T
foo((T) arg);
}
}
public abstract foo(T t);
}
For the implementation we just have to pass the Class to the constructor
public class OneImpl extends AbstractOne<Rule> {
public OneImpl() {
super(Rule.class);
}
#Override
public void foo(Rule t){
}
}

Or you could catch a failed attempt to cast into E eg.
public int indexOf(Object arg0){
try{
E test=(E)arg0;
return doStuff(test);
}catch(ClassCastException e){
return -1;
}
}

Technically you shouldn't have to, that's the point of generics, so you can do compile-type checking:
public int indexOf(E arg0) {
...
}
but then the #Override may be a problem if you have a class hierarchy. Otherwise see Yishai's answer.

The runtime type of the object is a relatively arbitrary condition to filter on. I suggest keeping such muckiness away from your collection. This is simply achieved by having your collection delegate to a filter passed in a construction.
public interface FilterObject {
boolean isAllowed(Object obj);
}
public class FilterOptimizedList<E> implements List<E> {
private final FilterObject filter;
...
public FilterOptimizedList(FilterObject filter) {
if (filter == null) {
throw NullPointerException();
}
this.filter = filter;
}
...
public int indexOf(Object obj) {
if (!filter.isAllows(obj)) {
return -1;
}
...
}
...
}
final List<String> longStrs = new FilterOptimizedList<String>(
new FilterObject() { public boolean isAllowed(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof String) {
String str = (String)str;
return str.length() > = 4;
} else {
return false;
}
}}
);

Let Java determine it and catch the exception bottom line.
public class Behaviour<T> {
public void behave(Object object) {
T typedObject = null;
try { typedObject = (T) object; }
catch (ClassCastException ignored) {}
if (null != typedObject) {
// Do something type-safe with typedObject
}
}
}

Related

How to pass an object with an unknown type to a Class with an unknown type

I am currently working on a homework assignment for a Java programming course I am taking. I am not asking for an exact answer, but for some guidance.
The problem I'm working on is this:
I have a filter class that implements a Filter interface. This interface has just one method - matches(T element)
I have configured my filter method to check an Integer that is being passed in for prime-ness.
There is also a decorator class that decorates a collection class to only display objects that pass the filter.
I'm having problems getting the contains(Object o) method to work correctly.
Basically the contains(Obj o) method in the FilteredCollection class should first check to see if the object passes the filter, and then if it does, call the undecorated contains() method on that object.
Assuming I want to be able to use this FilteredCollection class with many different types of filters, How can I determine what type of object is being passed in, and then be able to pass that object to the current Filter that's being implemented.
Here is my PrimeNumberFilter Class:
public class PrimeNumberFilter implements Filter<Integer> {
public boolean matches(Integer e) {
int n = e.intValue();
if (n != 2 && n % 2 == 0) {
return false;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Then here is my shortened FilteredCollection Class:
class FilteredCollection<T> implements Collection<T> {
Collection<T> fc;
Filter<T> currentFilter;
private FilteredCollection(Collection<T> coll, Filter<T> filter) {
this.fc = coll;
this.currentFilter = filter;
}
public static <T> FilteredCollection<T> decorate(Collection<T> coll,
Filter<T> filter) {
return new FilteredCollection<T>(coll, filter);
}
public boolean contains(Object o) {
//What do I do here?
return fc.contains(o);
}
The object being passed in to the contains method has to pass the filter, in this case a PrimeNumberFilter.
The error I'm getting is it keeps wanting to cast the object to type T, and I know that this will never work because of erasure.
I've done a ton of research, and I've boiled it down to needing to use reflection.
The only hint my instructor will give me is that object only has a few methods I can use, and I should use one of those.
Thanks for your help!
EDIT: One of the requirements of the project is to NOT cast an object to T in any method. So while these answers are great, I am not able to use any of them.
The method to use is Object.equals(Object). You can iterate the collection fc and check if it contains an element wich equals(o). If so, continue on with said element (which is of type T).
for(T e : fc) {
if(o.equals(e)) {
// carry on with e
}
}
You might also want to cover o == null.
There is nothing wrong with your code.
The problem is due to the java.util.Collection.contains(Object o) interface method not being generically typed. This is outside of your control.
Option 1: Simple approach
In your implementation of that method you can cast:
public boolean contains(Object o) {
return o != null && currentFilter.matches((T)o) && fc.contains(o);
}
Option 2: Add a getParameterType() method to the Filter interface
This method would return the generic type of the filter as implemented in the various subclasses.
interface Filter<T> {
boolean matches(T parameter);
Class<T> getParameterType();
}
Then...
public boolean contains(Object o) {
return o != null && currentFilter.getParameterType().isAssignableFrom(o.getClass()) && currentFilter.matches((T)o) && fc.contains(o);
}
Option 3: Determine generic type via reflection
Technically the generic type of your filter will not actually be erased at runtime. Type erasure does not apply here because PrimeNumberFilter is an actual class which implements a generically typed interface.
#SuppressWarnings("unchecked")
public boolean contains(Object o) {
Class<?> genericTypeOfFilter = getGenericTypeOfFilter(currentFilter);
return o != null && genericTypeOfFilter.isAssignableFrom(o.getClass()) && currentFilter.matches((T)o) && fc.contains(o);
}
static <T> Class<T> getGenericTypeOfFilter(Filter<T> filter) {
try {
#SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
Class<T> type = (Class<T>) ((ParameterizedType)filter.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
return type;
}
catch (Exception e) {
throw new IllegalStateException("Unexpectedly failed to read generic type of filter: " + filter, e);
}
}
If it was my code I'd go with Option 2 in this case, it is more robust than relying on reflection.
Since the contains method makes use of .equals, and .equals is qn overridden version of the one in Object, I would think about making a new Collection with the Object parameter type, so for example ArrayList then use it's addAll method to add everything from your T type collection - this will work because all classes inherit from Object. Then when it uses the equals method, it will still be the one from class T (assuming T has overridden it). It's not perhaps the perfect approach, but I think it is simpler and neater than trying to check if the Object can be cast to T.
public boolean contains(Object o) {
Collection<Object> temp = new ArrayList<Object>();
temp.addAll(fc);
return temp.contains(o);
}
The type cast to T will work, you just need to be careful to catch ClassCastExceptions because you can't check the type prior to the cast due to erasure.
This will leave you with something like:
public boolean contains(Object other)
{
try
{
return currentFilter.matches((T)other) && fc.contains(other);
}
catch (ClassCastException e)
{
//Add some logging.
return false;
}
}
Using exceptions in this way is generally frowned upon, but in this instance, you don't have much choice.

How does one restrict number of allowed types in Java generic type

Say, i have a generic type as below
public class GenericType<T> {
private T someVar;
public void setVar(T var) { this.someVar = var; }
//Rest of the code
}
I want to allow it to take only specific types(String/Integer/Double). I know about bounded wildcards but they don't help me here. In setVar(), I can check the instanceof and throw an Exception if type is not Integer/String etc. Is this the best way to do it?
I have the same problem when doing operations on this type. Depending on the type, I want to do different operations. Inheritance and bounded wildcards seem like the way to go in general for this kind of problem but these are primitive wrappers.
Using Inheritance:
Parent.java
public abstract class Parent<T> {
public abstract void display(T t);
}
ChildString.java
public class ChildString extends Parent<String> {
#Override
public void display(String t) {
// Do something here...
}
}
ChildInteger.java
public class ChildInteger extends Parent<Integer> {
#Override
public void display(Integer t) {
// Do something here...
}
}
ChildDouble.java
public class ChildDouble extends Parent<Double> {
#Override
public void display(Double t) {
// Do something here...
}
}
And access the class child rather than you directly access the parent class.
Update
Here another example:
GenericType.java
public class GenericType {
public void display(Object t) {
String msg;
if(t instanceof String) {
msg = "String";
} else if (t instanceof Integer) {
msg = "Integer";
} else if (t instanceof Double) {
msg = "Double";
} else {
msg = "Another Object";
}
System.out.println(msg);
}
}
SpecificGeneric.java
public class SpecificGeneric {
public static void main(String[] args) {
GenericType basicType = new GenericType();
basicType.display(new String());
basicType.display(new Integer(1));
basicType.display(new Double(0.1));
}
}
You cannot (more than extends something, but in your case you want few unrelated types, so it does not help).
What you can, is check instance passed to method (you already know it). If you want one instace of generic class for eg. String another for Integers, but don't allow eg. Point2D, you can make constructor with parameter Class clazz and check when constructing whether its allowed.
If you are more paranoid, you can store that clazz and in all function compare whether parameter is actualy that class.
This way, you can still create MyClass, but cannot create instance with this type. (But you can cast it, co its not fool proof)
Inferring the desired type say GenericType<Double> and using instanceof when neccesary is the quickest and easy option. Alternatively overload setVar(..) to accept the restricted types in your Generic class.
public static class GenericType<T>
{
private T someVar;
public void setVar(String var)
{
this.someVar = (T) var;
}
public void setVar(Integer var)
{
this.someVar = (T) var;
}
public void setVar(Double var)
{
this.someVar = (T) var;
}
}

how to check the Class Type(instanceof) in java template method?

public class Test {
private static Object createInstance(String classPath) {
try {
Class<?> tClass = Class.forName(classPath);
if (tClass != null) {
return tClass.newInstance();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#SuppressWarnings("unchecked")
public final static <INPUT, OUTPUT> Filter<INPUT, OUTPUT> getFilter(String path) {
return (Filter<INPUT, OUTPUT>) createInstance(path);
}
public final static <INPUT, OUTPUT> OUTPUT filter(String path, INPUT mes) {
Filter<INPUT, OUTPUT> filter = getFilter(path);
//How to check the INPUT and OUTPUT type here?
//if(INPUT instanceof String){ ... } not work
return filter.filter(mes);
}
}
refer to my earlier question here
thanks for help :)
Other answer are certainly correct. Anyway i think you are doing something quite unusual.
I'll try to explain:
Generics are use for static polymorphism. An instance of a generic type is determined at compile time.
Constructs like instanceof are used to check dynamic type of an object at runtime, so if you are using them, you can simply avoid the use of generics.
(You can use a generic Object as parameter for your function and then use instanceof to check its type)
For example:
public void method(Object o){
if (o instanceof String){} //do what you want
else ....
}
Typically, if you use generics, you are just trying to avoid that constructs. Typically a generic type can implements a well know interface, in a way that any operation performed upon that object into your filter method, could be performed for different types of objects implementing that interface and without knowing the specific type of objects involved.
I don't know exactly what are the polymorphic feature that you need, anyway you could try also something like that:
public interface polymorphicObj{
public method();
}
public class Filter<GENERIC implements polymorphicObj>{
public filter(GENERIC obj){
obj.method(); //you don't need to know of which specific type is polymorphicObj
}
}
if mes instanceof String should work.
Instead of checking INPUT, how about checking the actual parameter?
if(mes instanceof String) {
You will need instance of INPUT:
class A<INPUT>{
void typeOf(INPUT input){
if(input.getClass() == "".getClass()){
System.out.println("Input is String");
}
}
}
all objects extends Object co they have getClass method. You could use it to check class.

Java generics (template) specialization possible (overriding template types with specific types)

I'm wondering what are the options to specialize generic types in Java, i.e. in a templated class to have specific overrides for certain types.
In my case I was a generic class (of type T) to return null usually, but return "" (the empty string), when T is the String type, or 0 (zero) when its the Integer type, etc.
Merely providing a type-specific overload of a method produces a "method is ambiguous" error:
e.g.:
public class Hacking {
public static void main(String[] args) {
Bar<Integer> barInt = new Bar<Integer>();
Bar<String> barString = new Bar<String>();
// OK, returns null
System.out.println(barInt.get(new Integer(4)));
// ERROR: The method get(String) is ambiguous for the type Bar<String>
System.out.println(barString.get(new String("foo")));
}
public static class Bar<T> {
public T get(T x) {
return null;
}
public String get(String x) {
return "";
}
}
}
Is the only option to subclass the generic class with a specific type (see StringBar in the following example?
public static void main(String[] args) {
Bar<Integer> barInt = new Bar<Integer>();
StringBar barString2 = new StringBar();
// OK, returns null
System.out.println(barInt.get());
// OK, returns ""
System.out.println(barString2.get());
}
public static class Bar<T> {
public T get() {
return null;
}
}
public static class StringBar extends Bar<String> {
public String get() {
return "";
}
}
}
Is this is the only way, it's a bit of a pain to have to create a subclass for every type I want to specialize instead of an overload of get() in the Bar class.
I'm guessing I could check the instanceof in the Bar.get() method, e.g.
T get(T t) {
if (t instanceof String) return "";
if (t instanceof Integer) return 0;
else return null;
}
However I've been taught to avoid instanceof and use polymorphism when possible.
All things considered, the concensus appears to be that the StringBar method mentioned in the question is the only way to go.
public static class StringBar extends Bar<String> {
public String get() {
return "";
}
}
Generics in Java are very different from templates in C++ in this respect. It is not possible to write a specific version of a generic class to do something different for a particular case, as C++ can do. It is also not possible to determine at run time what T is - this is because that information is not passed into the byte code (object code) and so doesn't even exist at runtime. This due to something called "type erasure".
BarString and BarInt would be the obvious way of doing this, but there are improvements you can make. For example you can write a generic Bar to cover the common cases, and then write specialized BarString and BarInt to implement special cases. Ensure that the instances can only be created through a factory, which takes the class of the object to be processed:
class Bar<T> {
class BarString extends Bar<String> {
// specialist code goes here
}
static Bar<T> createBar(Class<T> clazz) {
if (clazz==String.class) {
return new BarString();
} else {
return new Bar<T>;
}
That probably won't compile, but I don't have the time to work out the exact syntax. It does illustrate the principle.
The compiler is actually correct, because the following code is compile-time checked (Bar<String> barString = new Bar<String>();) when compiled, from
public static class Bar<T> {
public T get(T x) {
return null;
}
public String get(String x) {
return "";
}
}
to
public static class Bar<String> {
public String get(String x) {
return null;
}
public String get(String x) {
return "";
}
}
and is ambiguous as you can't have 2 identical methods with the same return types and the same parameter arguments.
See an explanation by Jon Skeet's:
What is the concept of erasure of generics in java?
Java Generics - Types erasures - when and what happens?
You can subclass Bar<T> and create StringBar (note I removed the static keyword) and override get() method.
public class BarString extends Bar<String> {
#Override
public String get(String x) {
return "";
}
}
Generics in Java aren't made for specialization. They're made for generalization! If you want to specialize for certain types, you should be specializing...through a subclass.
Often you don't need to do something in a specialized manner however. Your StringBar example is kind of contrived because you could have this:
public class Bar<T> {
private final T value;
public T get() {
return value;
}
}
I don't see why you need to specialize for a String here.

Java Generics: Comparing the class of Object o to <E>

Let's say I have the following class:
public class Test<E> {
public boolean sameClassAs(Object o) {
// TODO help!
}
}
How would I check that o is the same class as E?
Test<String> test = new Test<String>();
test.sameClassAs("a string"); // returns true;
test.sameClassAs(4); // returns false;
I can't change the method signature from (Object o) as I'm overridding a superclass and so don't get to choose my method signature.
I would also rather not go down the road of attempting a cast and then catching the resulting exception if it fails.
An instance of Test has no information as to what E is at runtime. So, you need to pass a Class<E> to the constructor of Test.
public class Test<E> {
private final Class<E> clazz;
public Test(Class<E> clazz) {
if (clazz == null) {
throw new NullPointerException();
}
this.clazz = clazz;
}
// To make things easier on clients:
public static <T> Test<T> create(Class<T> clazz) {
return new Test<T>(clazz);
}
public boolean sameClassAs(Object o) {
return o != null && o.getClass() == clazz;
}
}
If you want an "instanceof" relationship, use Class.isAssignableFrom instead of the Class comparison. Note, E will need to be a non-generic type, for the same reason Test needs the Class object.
For examples in the Java API, see java.util.Collections.checkedSet and similar.
The method I've always used is below. It is a pain and a bit ugly, but I haven't found a better one. You have to pass the class type through on construction, as when Generics are compiled class information is lost.
public class Test<E> {
private Class<E> clazz;
public Test(Class<E> clazz) {
this.clazz = clazz;
}
public boolean sameClassAs(Object o) {
return this.clazz.isInstance(o);
}
}
I could only make it working like this:
public class Test<E> {
private E e;
public void setE(E e) {
this.e = e;
}
public boolean sameClassAs(Object o) {
return (o.getClass().equals(e.getClass()));
}
public boolean sameClassAs2(Object o) {
return e.getClass().isInstance(o);
}
}
I was just trying to do the same thing, and one neat trick i just realized is that you can can try a cast, and if the cast fails, ClassCastException will be thrown. You can can catch that, and do whatever.
so your sameClassAs method should look like:
public boolean sameClassAs(Object o) {
boolean same = false;
try {
E t = (E)o;
same = true;
} catch (ClassCastException e) {
// same is false, nothing else to do
} finally {
return same;
}
}

Categories