G
getsanjay.sharma
Hello to all Java programmers out there.
Recently I have been trying to do a multiple instanceof and thought it
would be better to write a function than do those ugly || with
repeated instanceofs'.
public class MyInstance
{
public static boolean check(Object obj, String...classes) throws
Exception
{
boolean result = false;
for(String str : classes)
{
Class c = Class.forName(str);
result = result | (obj instanceof c);
if(result) break;
}
return(result);
}
public static void main(String args[]) throws Exception
{
String str = "hello";
String classes[] = {"String", "StringBuffer"};
System.out.println(MyInstance.check(str, classes));
}
}
I do very well realize that the instanceof operator requires a TYPE on
its RHS, but I can't seem to find out how to convert the class name
string to a type?
Is such kind of thing even possible here or doing multiple || with
instanceofs' is the only way to go?
Thanks and regards,
STS
Recently I have been trying to do a multiple instanceof and thought it
would be better to write a function than do those ugly || with
repeated instanceofs'.
public class MyInstance
{
public static boolean check(Object obj, String...classes) throws
Exception
{
boolean result = false;
for(String str : classes)
{
Class c = Class.forName(str);
result = result | (obj instanceof c);
if(result) break;
}
return(result);
}
public static void main(String args[]) throws Exception
{
String str = "hello";
String classes[] = {"String", "StringBuffer"};
System.out.println(MyInstance.check(str, classes));
}
}
I do very well realize that the instanceof operator requires a TYPE on
its RHS, but I can't seem to find out how to convert the class name
string to a type?
Is such kind of thing even possible here or doing multiple || with
instanceofs' is the only way to go?
Thanks and regards,
STS