check if a variable is of a custom type

Discussion in 'Java' started by stephenfu1@gmail.com, Sep 9, 2005.

  1. Guest

    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
     
    , Sep 9, 2005
    #1
    1. Advertising

  2. jan V Guest

    > 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.
     
    jan V, Sep 9, 2005
    #2
    1. Advertising

  3. 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/
     
    Thomas Hawtin, Sep 9, 2005
    #3
  4. Chris Uppal Guest

    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
     
    Chris Uppal, Sep 9, 2005
    #4
  5. jan V Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:4321b827$0$38043$...
    > 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...
     
    jan V, Sep 9, 2005
    #5
  6. Roedy Green Guest

    On Fri, 09 Sep 2005 17:17:56 +0100, Thomas Hawtin
    <> 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.
     
    Roedy Green, Sep 9, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    1
    Views:
    403
    Axel Dahmen
    Nov 3, 2003
  2. Don
    Replies:
    5
    Views:
    625
    S. Justin Gengo
    Sep 13, 2005
  3. heyo
    Replies:
    3
    Views:
    939
    Dan Pop
    Apr 1, 2004
  4. NotGuru
    Replies:
    3
    Views:
    471
    NotGuru
    Mar 24, 2008
  5. castironpi

    custom allocation and custom type

    castironpi, Aug 4, 2008, in forum: Python
    Replies:
    0
    Views:
    282
    castironpi
    Aug 4, 2008
Loading...

Share This Page