Convert a class object to a human-readable string

Is there a way in which I can automatically convert a custom class object to a human-readable string?

eg. consider the following class:

class Person { String Name; int Salary; ... } Person p = new Person(); p.setName("Tony"); p.setSalary(1000); 

I need to get something like:

 Person: Name="Tony", Salary=1000 
+6
source share
7 answers

Importing Commons Lang you can use ToStringBuilder

Check the reflectionToString(java.lang.Object) method reflectionToString(java.lang.Object) , this will automatically create the view you expect.

This code:

 Person p = new Person(); p.setName("Tony"); p.setSalary(1000); System.out.println(ToStringBuilder.reflectionToString(p)); 

outputs this line:

 Person@64578ceb [Name=Tony,Salary=1000] 
+7
source

sure you can override the toString class method.

in the following way:

 class Person { String name; int salary; ... @Override public String toString() { return "Person: Name='" + name + "', Salary=" + salary; } } 

contact for more information https://blogs.oracle.com/CoreJavaTechTips/entry/writing_tostring_methods_tech_days

+2
source

This is mainly toString . But if you want this to be done automatically, you can create some general service that can do this. Use reflection to iterate over all the fields, and then print each name and value. The easiest way to print their values โ€‹โ€‹is to use them toString , but you can also pass them to this print service recursively in some cases (you, of course, have to find a stop condition).

For example, some PrintUtils classes have:

 public static void printFields(Object o) { System.out.print(o.getClass.getSimpleName() + ": "); for (Field field : o.getClass().getDeclaredFields()) { field.setAccessible(true); // you also get non-public fields System.out.print(field.getName() + " = " + field.get(o) + ", "); } } 

You will have to handle exceptions, etc. and it might be better to format the output, of course. In addition, these are only print fields declared in the current class. If you want the fields declared above in the inheritance hierarchy, you have to work a little more. Finally, using reflection is much slower than just regular toString . If using toString possible, this is preferable.

+2
source

I think you could use ToStringBuilder , which is part of commons-lang .

+1
source
 class Person { String Name; int Salary; ... @Override public String toString() { return "Person: Name = " + Name + "," + "Salary="+Salary; } } Person p = new Person(); p.setName("Tony"); p.setSalary(1000); System.out.println(p.toString()); 
0
source

One way to do this is to rely on Apache Commons BeanUtils.describe . This will create the Map properties of the bean properties, which are converted to a string using Map.toString . If you need something more ordinary, you need to delve into the reflection API.

0
source

You can use the message format from java: https://docs.oracle.com/javase/tutorial/i18n//message.html a separate variable - and there you have a class suitable for reading your class!

0
source

Source: https://habr.com/ru/post/919492/


All Articles