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)

package co.finance.flow;

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;

while(result){
if(classSuper.getName().equals("java.lang.Object"))
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
source.addAll(Arrays.asList(fields));
target.addAll(Arrays.asList(targetFileds));
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;
while(itSource.hasNext()){
Field fldSourcs = (Field)itSource.next();
Field fldTarget = (Field)itTarget.next();
fldSourcs.setAccessible(true);
fldTarget.setAccessible(true);


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

if (!retType){
refObj = (Object)fldSourcs;
System.out.println( " Type of Object : " +fldSourcs.getType());
reflectObject(refObj);
}
val = fldSourcs.get(object);
fldTarget.set(flowObj,val);
System.out.println ( "Index no " + seq + " : [ " + fldSourcs.getName() + " ] the type :" + fldSourcs.getType());
seq++;
}
} 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;
if(source.getType().isPrimitive())
res = true;

return res;
}
} 
package co.finance.flow;

public class First {
     
     private int x;
     private int y;
    private Second sec;
}
package co.finance.flow;

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