check if a variable is of a custom type

S

stephenfu1

Hi

I wish to examine a class and return its fields using Reflection.

However if one of the fields is a custom type, i wish to examine that
type itself and see what fields it contains (and possibly custom
types).

How can i check whether the field is a standard Java type? e.g. int,
double, String.

Cheers
 
J

jan V

How can i check whether the field is a standard Java type? e.g. int,
double, String.

The reflection API gives you an isPrimitive() call, so that lets you
categorise the 8 primitive types. Secondly, for all reference types you can
look at the types' package names. Anything starting with java.* and javax.*
could be classed as "standard", unless you've got slightly different
criteria. It's really up to you.

Note that the latter scheme would not be foolproof. Anyone can declare a new
type in package java.sabotage, or even java.util... the compiler won't stop
it.
 
T

Thomas Hawtin

jan said:
The reflection API gives you an isPrimitive() call, so that lets you
categorise the 8 primitive types. Secondly, for all reference types you can
look at the types' package names. Anything starting with java.* and javax.*
could be classed as "standard", unless you've got slightly different
criteria. It's really up to you.

Note that the latter scheme would not be foolproof. Anyone can declare a new
type in package java.sabotage, or even java.util... the compiler won't stop
it.

Some of the broken code currently is Mustang (b50) assumes that, for
instance, every package starting with "com.sun." is standard. A better
test is whether the class' class loader is the bootstrap (usually null)
or extension class loader.

Tom Hawtin
 
C

Chris Uppal

jan said:
Anything starting with java.* and
javax.* could be classed as "standard", unless you've got slightly
different criteria. It's really up to you.

Note that the latter scheme would not be foolproof. Anyone can declare a
new type in package java.sabotage, or even java.util... the compiler
won't stop it.

You can check the classloader too, if the class's classloader is == that of,
say, java.lang.Object, then the class is as "standard" as it is possible to be.

-- chris
 
J

jan V

Chris Uppal said:
You can check the classloader too, if the class's classloader is == that of,
say, java.lang.Object, then the class is as "standard" as it is possible
to be.

But wouldn't that also categorise as standard classes loaded by the boot
loader which aren't public, i.e. private helper classes. Maybe such results
wouldn't be helpful for the OP ? Like I said earlier, it's really up to the
OP's detailed requirements, which he hasn't disclosed...
 
R

Roedy Green

Some of the broken code currently is Mustang (b50) assumes that, for
instance, every package starting with "com.sun." is standard. A better
test is whether the class' class loader is the bootstrap (usually null)
or extension class loader.

Even if the compiler blocked ordinary mortals from compiling com.sun
code, they could always patch the class files.

It is a bit like drugs. You can go only so far to stop people from
killing themselves with them. If they are determined, you are not
going to stop them.

Sun really is only obligated to work to prevent you from screwing up
OTHER people's machines, not your own.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top