Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Reflection Help (http://www.go4expert.com/forums/java-reflection-help-t24135/)

z0vb 10Dec2010 08:11

Java Reflection Help
 
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!

virxen 10Dec2010 19:53

Re: Java Reflection Help
 
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());
}
}


z0vb 10Dec2010 20:43

Re: Java Reflection Help
 
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.

virxen 10Dec2010 22:44

Re: Java Reflection Help
 
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();   
    }
}



All times are GMT +5.5. The time now is 03:40.