Skip to content

Instantly share code, notes, and snippets.

@aembleton
Created July 26, 2011 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aembleton/1106493 to your computer and use it in GitHub Desktop.
Save aembleton/1106493 to your computer and use it in GitHub Desktop.
Auto-generated toString useful for all beans.
/*
Copyright (c) 2007 Arthur Embleton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Provides functionality common to all beans.
*
* @author Arthur Embleton
*
*/
public abstract class Bean {
private static final String NEW_LINE = "\n";
private static final String[] PROPERTY_NAMES = { "get", "is" };
/**
* Produces a toString that caters for the needs of a bean. It shows the
* value of all of the properties, by calling any methods that begin with
* "get" or "is". This class is intended to be extended, so these properties
* would be those of child classes.
*/
public String toString() {
StringBuffer string = new StringBuffer();
Class<?> c = this.getClass();
Method[] methods = null;
String fieldName = "";
if (c != null) {
// write out the field name
string.append(c.getName()).append(':').append(NEW_LINE);
methods = c.getMethods();
if (methods != null) {
// got the methods, now iterate over them
for (Method method : methods) {
if (method != null
&& method.getTypeParameters().length == 0) {
// got a method, now try and get the field name
fieldName = getFieldName(method.getName());
if (fieldName != null) {
// fieldName is not a null, so it has been found.
try {
string.append(NEW_LINE).append(fieldName)
.append(" = ").append(
method.invoke(this,
new Object[0]));
} catch (IllegalArgumentException e) {
// do nothing. This code is only executed during
// debugging.
} catch (IllegalAccessException e) {
// do nothing. This code is only executed during
// debugging.
e.printStackTrace();
} catch (InvocationTargetException e) {
// do nothing. This code is only executed during
// debugging.
e.printStackTrace();
}
}
}
}
}
}
return string.toString();
}
/**
* Checks if the supplied name starts with a property name that is held in
* the PROPERTY_NAMES field.
*
* @param name
* Name to check the begining of
* @return true if name starts with a string listed in PROPERTY_NAMES,
* otherwise false.
*/
private String getFieldName(String methodName) {
String fieldName = null;
if (methodName != null) {
// got the name, iterate through the property names to check if it
// starts with any of them
for (String propertyName : PROPERTY_NAMES) {
if ((propertyName != null)
&& (methodName.startsWith(propertyName))) {
// found a matching property name, strip off the property
// name to leave the field name
fieldName = methodName.substring(propertyName.length());
break;
}
}
}
return fieldName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment