Reputation: 2520
Colleagues, this is my method which describes all attributes (fields) of the any Object:
public static void describeObjectFields(Object o) {
System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");
for (Field field: o.getClass().getDeclaredFields()) {
field.setAccessible(true);
String name = field.getName();
Object value;
try {
value = field.get(o);
/*
if (value.toString().contains("com.comp."))
{
LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
System.out.println(value.getClass().getFields().toString() );
for (Field field1 : value.getClass().getDeclaredFields()) {
field1.setAccessible(true);
String name1 = field1.getName();
Object value1 = null;
value1 = field1.get(value1);
System.out.println(name1 + " -> " + value1);
}
} else {
*/
System.out.println(name + " -> " + value);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
It works fine with primitive types (string, data, int...) Could anybody improve this method to print not primitive types but complex (when one of object fields is a object too).
UPDATE
According to @Jon Skeet advise lets the program will be looks like
package App;
public class Kitchen {
double sqr;
int windows;
public Kitchen(float sqr, int windows) {
this.sqr = sqr;
this.windows = windows;
}
public double getSqr() {
return sqr;
}
public void setSqr(double sqr) {
this.sqr = sqr;
}
public int getWindows() {
return windows;
}
public void setWindows(int windows) {
this.windows = windows;
}
}
package App;
public class Flat {
Kitchen kitchen;
double sqr;
int windows;
int rooms;
public Flat(Kitchen kitchen, double sqr, int windows, int rooms) {
this.kitchen = kitchen;
this.sqr = sqr;
this.windows = windows;
this.rooms = rooms;
}
public Kitchen getKitchen() {
return kitchen;
}
public void setKitchen(Kitchen kitchen) {
this.kitchen = kitchen;
}
public double getSqr() {
return sqr;
}
public void setSqr(float sqr) {
this.sqr = sqr;
}
public int getWindows() {
return windows;
}
public void setWindows(int windows) {
this.windows = windows;
}
public int getRooms() {
return rooms;
}
public void setRooms(int rooms) {
this.rooms = rooms;
}
}
package App;
import java.lang.reflect.Field;
public class App {
public static void main (String [] args)
{
Kitchen kit = new Kitchen(12, 1 );
Flat flat = new Flat(kit, 32.32, 5, 4);
App.describeObjectFields(flat);
}
public static void describeObjectFields(Object o) {
System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");
for (Field field: o.getClass().getDeclaredFields()) {
field.setAccessible(true);
String name = field.getName();
Object value;
try {
value = field.get(o);
/*
if (value.toString().contains("com.comp."))
{
LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
System.out.println(value.getClass().getFields().toString() );
for (Field field1 : value.getClass().getDeclaredFields()) {
field1.setAccessible(true);
String name1 = field1.getName();
Object value1 = null;
value1 = field1.get(value1);
System.out.println(name1 + " -> " + value1);
}
} else {
*/
System.out.println(name + " -> " + value);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
As a result i receive
Object description: App.Flat:
kitchen -> App.Kitchen@140e19d
sqr -> 32.32
windows -> 5
rooms -> 4
but would like
Object description: App.Flat:
Object description: App.Kitchen:
sqr -> 12
windows -> 1
sqr -> 32.32
windows -> 5
rooms -> 4
How?
Upvotes: 0
Views: 69
Reputation: 1944
You should override the toString
method for each type you want to print.
For example, if you want to print an instance of YourObject in a readable way, you should write something like this:
public class YourObject
{
private int yourField1;
private int yourField2;
public YourObject(int f1, int f2)
{
yourField1 = f1;
yourField2 = f2;
}
@Override
public String toString()
{
return "YourObject[yourField1=" + yourField1 + ", yourField2=" + yourField2 + "]");
}
}
That given, you can do something like this:
AnotherObject obj = new AnotherObject();
obj.setYourObject(new YourObject(1,2));
And when you call:
describeObjectFields(obj);
You will get:
YourObject[yourField1=1, yourField2=2]
UPDATE
In your case, you should do something like this in your Kitchen class:
public class Kitchen {
// your instance variables, constructor, getters and setters, etc.
@Override
public String toString() {
return ("sqr: " + sqr + ", windows: " + windows);
}
}
Upvotes: 1