This discussion is archived
0 Replies Latest reply: May 14, 2007 5:45 AM by 807606 RSS

Not getting the class name when getting from the Field [ ]  of reflection

807606 Newbie
Currently Being Moderated
Hi , I am developing a code.

1. we pass an object to a method (reflectObject)
2. reflectObject return a new object which is a true copy . I am using the reflection

but in one condition when a class contain any object of other class that time i am unable to fetch the object from the Field Array which i have retrived from Class.getDeclareMethods();

problem is that i want to explore the preimitive data type of that object too.

My problem is

1 how to exit from recursive function reflectObject
2 how to get the Object from the Field [ ]
( I am getting the object form field but when i get the class of this
object the value is java.lang.reflect.Field , I required the that
class whose object is this)


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class ReflectionObject {
/* using the Refelection creating the object (kailash)*/
public static Object reflectObject(Object object){
Class[] paramTypes = new Class[1];
if(object.getClass().equals("java.lang.Object")) // first we have to make a proper exit from recursion and ensure that the reference 
return null; // of object via a Field can get easily 

Class classSuper = null;
Class classNewSuper = null;
paramTypes[0] = String.class;
ArrayList source;
ArrayList target;

Class obj = object.getClass();
String className = obj.getName();
Object flowObj = null;
Object refObj = null;
try {
flowObj = Class.forName(className).newInstance();
Class objNew = flowObj.getClass();
source = new ArrayList();
target = new ArrayList();
classSuper = obj;
classNewSuper = objNew;

boolean result = true;

result = false;
Field [] fields = classSuper.getDeclaredFields();
Field [] targetFileds = classNewSuper.getDeclaredFields();
// if any class has a object as a data member in that case we have to explore that class also
classSuper = classSuper.getSuperclass(); // to get the parent class
classNewSuper = classNewSuper.getSuperclass();
Iterator itSource = source.iterator();
Iterator itTarget = target.iterator();
Object val;
boolean retType;
int seq = 1;
Field fldSourcs = (Field);
Field fldTarget = (Field);

retType = checkForObject(fldSourcs, fldTarget); //checking that it is Object or Primitive

if (!retType){
refObj = (Object)fldSourcs;
System.out.println( " Type of Object : " +fldSourcs.getType());
val = fldSourcs.get(object);
System.out.println ( "Index no " + seq + " : [ " + fldSourcs.getName() + " ] the type :" + fldSourcs.getType());
} catch(Exception e ){
System.out.println(" Exception : [ " + e.getStackTrace()+ " ]");
return (Object)flowObj;

public static void main(String[] args) {
System.out.println("In the reflection main object");
First f = new First();
Object o = reflectObject(f);

public static boolean checkForObject(Field source, Field target){
boolean res = false;
res = true;

return res;

public class First {
     private int x;
     private int y;
    private Second sec;

public class Second {
 private int z;
 private int r;