Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Reflection question: int ->Integer->??

Thread view: 
timjowers@gmail.com - 20 Oct 2005 22:32 GMT
Using reflection I cannot get to the data value of an int member
attribute. Anyone solved this?

Here's the sample code. The issue is reflection works fine if you know
beforehand the type and can cast to an Integer but what if you do not
know? I cannot get the actual int to print! What am I missing?

public class EqualsUtil {

    static void printField(Object r, String fieldName) {
      Field heightField;
      //Integer heightValue;
      Object oValue;
      Class c = r.getClass();
      try {
        heightField = c.getField(fieldName);
        //heightValue = (Integer) heightField.get(r);
        oValue = heightField.get(r);
        //System.out.println(fieldName + ": " + heightValue.toString());
        System.out.println(fieldName + ": " + oValue);
        if( fieldName == "TYPE" && oValue.toString().equals("int")) //
special case in Java
        {    ;; // where's the real value? The debugger shows the value "123"
but oValue prints as "int"
        }
      } catch (NoSuchFieldException e) {
          System.out.println(e);
      } catch (SecurityException e) {
          System.out.println(e);
      } catch (IllegalAccessException e) {
          System.out.println(e);
      }
    }
    public static void printObject(Object o )
    {
        Class c = o.getClass();
        Field[] publicFields = c.getFields();
        for (int i = 0; i < publicFields.length; i++) {
          String fieldName = publicFields[i].getName();
          Class typeClass = publicFields[i].getType();
          String fieldType = typeClass.getName();
          Object val=null;
          System.out.println("Name: " + fieldName + ", Type: " + fieldType);
          printField( o, fieldName );
          }
    }

    public static void main(String[] args) {
        Integer i1 = new Integer(123);
        printObject(i1);
}
}

Note that this code is almost a duplicate of Sun's tutorial. See:
http://java.sun.com/docs/books/tutorial/reflect/object/get.html
I've also tried with getDeclaredFields but no luck. What does work is
creating an isntance of an innerclass with a memeber attribute of
Integer (or else even of int) as these both print the value fine using
reflection.

Thanks,
TimJowers
Oliver Wong - 20 Oct 2005 23:12 GMT
> Using reflection I cannot get to the data value of an int member
> attribute. Anyone solved this?
>
> Here's the sample code. The issue is reflection works fine if you know
> beforehand the type and can cast to an Integer but what if you do not
> know? I cannot get the actual int to print! What am I missing?

   I'm not sure I understand the question: Do you want a general way to
print fields which may be of any primitive type or what? Because the code
you're working with seems to half of the time not making any assumption
about the type of the field, and the other half of the time, assumes the
type of the field is an int. Do you want to print ALL objects as if they
were primitive ints? Do you want to print all objects, regardless of whether
they are ints or not? What exactly are you trying to do?

   - Oliver
John Currier - 20 Oct 2005 23:19 GMT
Try using getDeclaredFields() and getDeclaredField().  After you get
the field you'll need to call setAccessible(true) on it or you'll get
some form of security exception.

John
http://schemaspy.sourceforge.net
timjowers@gmail.com - 21 Oct 2005 14:51 GMT
Fine. That was it. Thanks a bunch John!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.