I'm not sure what is going on with what I'm doing, so thought I'd ask for 
some help.  My environment is Sun JDK 1.3.

        I am trying to understand reflection.  When I run the command 'java 
com.bascom.TestClass3' from a terminal, I get this output (I'm only 
pasteing in the pertanent part):
[treaves@double treaves]$ java com.bascom.TestClass3
4
Fields for TestClass3
=====================
Field 0: private int com.bascom.TestClass3.attribute3
Field 1: private java.util.HashMap com.bascom.TestClass3.hasmMapAttribute
Field 2: private java.lang.String com.bascom.TestClass3.lastAttribute
Field 3: private static java.lang.Class 
com.bascom.TestClass3.class$Ljava$lang$Object


This is what I get from BeanShell:
bsh % Class testClass = Class.forName("com.bascom.TestClass3");
bsh % Field[] fields = testClass.getDeclaredFields();
bsh % print(fields.length);
3
bsh % print(fields);
Array: [Ljava.lang.reflect.Field;@6f247 {
    private int com.bascom.TestClass3.attribute3
    private java.util.HashMap com.bascom.TestClass3.hasmMapAttribute
    private java.lang.String com.bascom.TestClass3.lastAttribute
}


        So when I run from the commandline, it lists that there are four fields, 
and in BeanShell it lists three.  The command line shows the 
java.lang.Class as the fourth field.  Why?  What am I missing?

        Thanks.
package com.bascom;

public class TestClass1 {
        private int attribute1;
        protected String rootAttribute;

        public int getAttribute1(){
                        return attribute1;
                }

        public void setAttribute1(int attribute1){
                        this.attribute1 = attribute1;
                }

        public String getRootAttribute(){
                        return rootAttribute;
                }

        public void setRootAttribute(String rootAttribute){
                        this.rootAttribute = rootAttribute;
                }

        private void operation1() {
        }
}

package com.bascom;

public class TestClass2 extends TestClass1 {
        private int attribute2;
        private boolean boolAttribute;
        public float staticAttribute;

        public boolean isBoolAttribute(){
                        return boolAttribute;
                }

        public void setBoolAttribute(boolean boolAttribute){
                        this.boolAttribute = boolAttribute;
                }
}

package com.bascom;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

public class TestClass3 extends TestClass2 {
        private int attribute3;
        private HashMap hasmMapAttribute;
        private String lastAttribute;

        public static void main(String[] args){
                try{
                        Class testClass = Class.forName("com.bascom.TestClass3");
                        Field[] fields = testClass.getDeclaredFields();
                System.out.println(fields.length);
                        ArrayList list = new ArrayList();
                        list.addAll(Arrays.asList(testClass.getDeclaredFields()));
                        Class superClass = testClass.getSuperclass();
                        do {
                                
list.addAll(Arrays.asList(superClass.getDeclaredFields()));
                        } while ( (superClass = superClass.getSuperclass()) != null && 
superClass != Object.class );


                        int index = 0;

                        System.out.println("Fields for TestClass3");
                        System.out.println("=====================");
                        for (;index < fields.length; index++) {
                                System.out.println("Field " + index + ": " + 
fields[index]);
                        }

                        System.out.println("Fields for TestClass3 + superclasses");
                        System.out.println("====================================");
                        Iterator it = list.iterator();
                        index = 0;
                        while (it.hasNext()) {
                                System.out.println("Field " + index++ + ": " + 
it.next().toString());
                        }
                } catch(Exception e){
                        e.printStackTrace();
                }

        }
}

Reply via email to