Reputation: 6086
Given an Object o
and a String className = "org.foo.Foo"
, I want to check if o
is instance of List<className>
I tried this but won't compile:
Class<?> cls = Class.forName(className);
if (o instanceof List<cls>){ // this gives error: cls cannot be resolved to a type
doSomething();
}
Please note that my inputs are Object o
and String className
(please mind types).
Upvotes: 17
Views: 71571
Reputation: 41
if what you want to do is like this:
class XXX implements InterfaceX {
@Override
public void methedX() { }
}
interface InterfaceX {
void methedX();
}
List<XXX> x = new ArrayList<>();
if(x instanceOf List<InterfaceX>) {
data.get(0).methedX();
}
you may try to use template like:
class TypeHelper <T extends InterfaceX> {
List<T> data;
TypeHelper (List<T> data) {
this.data = data;
}
public void func() {
data.get(0).methedX();
}
}
TypeHelper helper = new TypeHelper<XXX>(x);
helper.func();
Upvotes: 0
Reputation: 6385
Solution i used in my project.
/**
* Used to get List of String From Object.
*
* 1. used to convert object to list of String.
*
* @param objectList Object List.
* @return List Of String.
*/
private List<String> getListOfStringFromObject(final Object objectList) {
List<String> stringList = new ArrayList<>();
if (objectList instanceof List<?>) {
for (Object object : (List<?>) objectList) {
if (object instanceof String) {
stringList.add((String) object);
}
}
}
return stringList;
}
Upvotes: 6
Reputation: 6086
Ok, I found a method... it's a little dirty code but it works:
if (o instanceof List<?>){
ParameterizedType pt = (ParameterizedType)o.getClass().getGenericSuperclass();
String innerClass = pt.getActualTypeArguments()[0].toString().replace("class ", "");
System.out.println(innerClass.equals(className)); // true
}
Upvotes: 6
Reputation: 7034
public class Main{
public static void main(String[] args){
String str = "";
int oi = 0;
char c = 'a';
Main af = new Main();
checkType(str);
checkType(oi);
checkType(c);
checkType(af);
}
public static void checkType(Object o){
String str = o.getClass().getName();
switch(str){
case "java.lang.String":
System.out.println("String");
break;
case "java.lang.Integer":
System.out.println("Integer");
break;
case "java.lang.Character":
System.out.println("Char");
break;
case "Main":
System.out.println("Main");
break;
default :
System.out.println("Something else:" + str);
break;
}
}
}
Note> just change to public static boolean checkType(Object o)
and then return true
in each case
that isn't default.
Upvotes: 2
Reputation: 693
I think you can do it in two steps: First, you check it's a List.
if (o instanceof List)
Then, you check that one (each?) member of the list has the given type.
for (Object obj : (List) o) {
if (obj instanceof cls) {
doSomething();
}
}
Upvotes: 11
Reputation: 62884
It's because of Type Erasure. The statement
if (o instanceof List<cls>) {
doSomething();
}
will be executed at runtime, when the generic type of the list will be erased. Therefore, there's no point of checking for instanceof
generic-type.
Upvotes: 11