So how am I supposed to use Class isAssignableFrom() now?

  • Thread starter The Abrasive Sponge
  • Start date
T

The Abrasive Sponge

So I am creating a method called isStandardProperty and it is returning
true if the Class given is primitive, or a wrapper, or String. Before
I would be able to isAssignableFrom and all is good. In 1.5 API, the
signature it asks for is

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

public boolean isAssignableFrom(Class<?> cls)

The Class<?> throws me off. I guess this interprets itself as that the
class is representative of some sort of object. In my own words.
Anyways, here is my question, how do I use this method correctly now?
Many of my attempts result in Lint warnings.



/* Attempt 2: Using the .class property

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(Byte.class) return true;
if (clazz.isAssignableFrom(Short.class) return true;
if (clazz.isAssignableFrom(Integer.class) return true;
if (clazz.isAssignableFrom(Long.class) return true;
if (clazz.isAssignableFrom(Float.class) return true;
if (clazz.isAssignableFrom(Double.class) return true;
if (clazz.isAssignableFrom(Character.class) return true;
if (clazz.isAssignableFrom(String.class) return true;
return false;
}




/* Attempt 2: Using the types on wrappers

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(java.lang.Byte.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Short.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Integer.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Long.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Float.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Double.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Character.TYPE)) return true;
if (clazz.isAssignableFrom(new String().getClass())) return true;
return false;
}
 
T

The Abrasive Sponge

The said:
So I am creating a method called isStandardProperty and it is returning
true if the Class given is primitive, or a wrapper, or String. Before
I would be able to isAssignableFrom and all is good. In 1.5 API, the
signature it asks for is

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)


public boolean isAssignableFrom(Class<?> cls)

The Class<?> throws me off. I guess this interprets itself as that the
class is representative of some sort of object. In my own words.
Anyways, here is my question, how do I use this method correctly now?
Many of my attempts result in Lint warnings.



/* Attempt 2: Using the .class property

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(Byte.class) return true;
if (clazz.isAssignableFrom(Short.class) return true;
if (clazz.isAssignableFrom(Integer.class) return true;
if (clazz.isAssignableFrom(Long.class) return true;
if (clazz.isAssignableFrom(Float.class) return true;
if (clazz.isAssignableFrom(Double.class) return true;
if (clazz.isAssignableFrom(Character.class) return true;
if (clazz.isAssignableFrom(String.class) return true;
return false;
}




/* Attempt 2: Using the types on wrappers

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(java.lang.Byte.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Short.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Integer.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Long.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Float.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Double.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Character.TYPE)) return true;
if (clazz.isAssignableFrom(new String().getClass())) return true;
return false;
}


Well, here is the solution....crazy

private static boolean isStandardProperty(Class<?> clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(Byte.class)) return true;
if (clazz.isAssignableFrom(Short.class)) return true;
if (clazz.isAssignableFrom(Integer.class)) return true;
if (clazz.isAssignableFrom(Long.class)) return true;
if (clazz.isAssignableFrom(Float.class)) return true;
if (clazz.isAssignableFrom(Double.class)) return true;
if (clazz.isAssignableFrom(Character.class)) return true;
if (clazz.isAssignableFrom(String.class)) return true;
return false;
}


But now I am thinking that because of autoboxing my code should clear
up. Should wrapper classes be called primitive? Shouldn't but what a
surprise it would be.

Testing....

Nah, that's good.
 
J

John C. Bollinger

The said:
So I am creating a method called isStandardProperty and it is returning
true if the Class given is primitive, or a wrapper, or String. Before
I would be able to isAssignableFrom and all is good. In 1.5 API, the
signature it asks for is

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)


public boolean isAssignableFrom(Class<?> cls)

The Class<?> throws me off. I guess this interprets itself as that the
class is representative of some sort of object. In my own words.
Anyways, here is my question, how do I use this method correctly now?
Many of my attempts result in Lint warnings.

The argument to the method is a reference to an object of class Class.
It always was. In 1.5, class Class is generic (although the advantages
of this elude me at the moment; I'm sure I've heard some). The method
signature simply says that the parameter is a Class instance with
unspecified / unknown / any type parameter. In general, you will get
unchecked type warnings from the Java compiler if you attempt to pass an
argument of generic class to a method that wants a generic argument, but
the type parameter of the actual argument cannot be determined. The
/* Attempt 2: Using the .class property

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(Byte.class) return true;
if (clazz.isAssignableFrom(Short.class) return true;
if (clazz.isAssignableFrom(Integer.class) return true;
if (clazz.isAssignableFrom(Long.class) return true;
if (clazz.isAssignableFrom(Float.class) return true;
if (clazz.isAssignableFrom(Double.class) return true;
if (clazz.isAssignableFrom(Character.class) return true;
if (clazz.isAssignableFrom(String.class) return true;
return false;
}




/* Attempt 2: Using the types on wrappers

Result : Lint warnings */

private boolean isStandardProperty(Class clazz) {
if (clazz.isPrimitive()) return true;
if (clazz.isAssignableFrom(java.lang.Byte.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Short.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Integer.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Long.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Float.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Double.TYPE)) return true;
if (clazz.isAssignableFrom(java.lang.Character.TYPE)) return true;
if (clazz.isAssignableFrom(new String().getClass())) return true;
return false;
}

Do note that your second example does not mean the same thing as your
first. java.lang.Byte.TYPE != java.lang.Byte.class (etc.). The former
are used by the reflection API to represent the primitive types; the
latter are the wrapper classes.

Note also that you don't need to use isAssignable for any of these
particular classes; they are all final, so you can safely test with the
== operator.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top