Java Reflection Help

Discussion in 'Java' started by z0vb, Dec 10, 2010.

  1. z0vb

    z0vb New Member

    Joined:
    Dec 10, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    So I create the following code:

    Code:
    public class Person {
      public String name;
    
      public Person(String name) {
        this.name = name;
      }
    
      public String getName() {
        return this.name;
      }
    
      protected final static Person firstPerson = new Person("john") {
    };
    protected final static Person secondPerson = new Person("jack"[SIZE=2]) {
    };
    }
    
    I can load these files using the loadClass method.
    but the question is how would I return the name "firstPerson"
    and how would I call the method getName() to return "john"?

    Thanks in advance!
    [/SIZE]
     
    Last edited by a moderator: Dec 10, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    is this what you want?

    Code:
    public class Person {
      public String name;
    
      public Person(String name) {
        this.name = name;
      }
    
      public String getName() {
        return this.name;
      }
    
      protected final static Person firstPerson = new Person("john");
    protected final static Person secondPerson = new Person("jack");
    
    public static void main(String args[]){
        System.out.println("first person's name is "+firstPerson.getName());
        System.out.println("second person's name is "+secondPerson.getName());
    }
    }
    
     
  3. z0vb

    z0vb New Member

    Joined:
    Dec 10, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Lol, I wish it was that easy! I want to use java reflection by reading a class file and asking for those values if the class' superclass is Person. So thearatically, I will have no clue what is in the class, but I want java relfection to tell me.
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    then this is more suitable

    Code:
    import java.lang.reflect.*;
    
    
    public class PersonReflection {
    
        public PersonReflection() {
            try {
                Class c = Class.forName("Person");
                System.out.println("\n\nproperties(type --> name)");
                System.out.println("-----------------------------");
                Field[] flds = c.getDeclaredFields();
                for (int i = 0; i < flds.length; i++){
                    System.out.println(flds[i].getType().getName().toString()+"---->"+flds[i].getName().toString());
                }
                
                System.out.println("\n methods(return type --->name --->(parameters)");
                System.out.println("------------------------------------------------");
                Method m[] = c.getDeclaredMethods();
                for (int i = 0; i < m.length; i++){
                    System.out.print(m[i].getReturnType().getName().toString()+"--->"+m[i].getName().toString()+"--->(");
                Class[] parameters=m[i].getParameterTypes();
                for (int j=0;j<parameters.length;j++)
                    System.out.print(parameters[j].getName().toString()+" ");
                System.out.println(")");
                
                }
                
                System.out.println(" \n\nget data");
                System.out.println("-----------------");            
                Field person1 = c.getDeclaredField("firstPerson");
                System.out.println("name="+((Person) person1.get(c)).getName());
                Field person2 = c.getDeclaredField("secondPerson");
                System.out.println("name="+person2.getName());
             }
             catch (Throwable e) {
                System.err.println(e);
             }
    
        }
     
    
        
        public static void main(String [] args){
        new PersonReflection();    
        }
    }
    
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice