Java Reflection Help

z0vb's Avatar, Join Date: Dec 2010
Newbie Member
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") {
};
}
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!

Last edited by shabbir; 10Dec2010 at 08:29.. Reason: Code blocks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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());
}
}
0
z0vb's Avatar, Join Date: Dec 2010
Newbie Member
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.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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();    
    }
}