Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > check if a variable is of a custom type

Reply
Thread Tools

check if a variable is of a custom type

 
 
stephenfu1@gmail.com
Guest
Posts: n/a
 
      09-09-2005
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

 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      09-09-2005
> 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.


 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      09-09-2005
jan V wrote:
>
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      09-09-2005
jan V wrote:

> 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


 
Reply With Quote
 
jan V
Guest
Posts: n/a
 
      09-09-2005

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:4321b827$0$38043$(E-Mail Removed).. .
> jan V wrote:
>
> > 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.

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...


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-09-2005
On Fri, 09 Sep 2005 17:17:56 +0100, Thomas Hawtin
<(E-Mail Removed)> wrote or quoted :

>
>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom allocation and custom type castironpi Python 0 08-04-2008 07:15 PM
PyTuple_Check and other type check functions didn't check the NULLpointer NotGuru Python 3 03-24-2008 04:24 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
How do I check the type of the value represented in a system.type? Don ASP .Net 5 09-13-2005 03:57 PM
Question: Invoking custom file type (file associated with custom app) VB Programmer ASP .Net 1 11-03-2003 11:49 PM



Advertisments