Source Code : Reflection based toString() utilities
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Reflection based toString() utilities
import java.lang.reflect.Field;
public class Main {
String hello = "world";
int i = 42;
public static void main(String args[]) {
System.out.println(Util.toString(new MyClass()));
System.out.println(Util.toString(new MyAnotherClass()));
}
}
class Util {
public static String toString(Object o) {
StringBuilder sb = new StringBuilder();
toString(o, o.getClass(), sb);
return o.getClass().getName()+ "
"+sb.toString();
}
private static void toString(Object o, Class clazz, StringBuilder sb) {
Field f[] = clazz.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
try {
sb.append(f[i].getName() + "=" + f[i].get(o)+"
");
} catch (Exception e) {
e.printStackTrace();
}
}
if (clazz.getSuperclass() != null)
toString(o, clazz.getSuperclass(), sb);
}
}
class MyClass {
int i = 1;
private double d = 3.14;
}
class MyAnotherClass extends MyClass{
int f = 9;
}
Thank with us