Last active
December 18, 2015 10:09
-
-
Save lsantos/5766303 to your computer and use it in GitHub Desktop.
Wrapper to filter what methods get printed for commons lang ToStringBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(value = ElementType.METHOD) | |
@Retention(value = RetentionPolicy.RUNTIME) | |
public @interface ToStringBuilderIgnore { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.beans.BeanInfo; | |
import java.beans.Introspector; | |
import java.beans.MethodDescriptor; | |
import java.lang.reflect.Method; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
public class ToStringBuilderUtil { | |
public static String reflectionToString(Object o) { | |
Class<? extends Object> clazz = o.getClass(); | |
ToStringBuilder builder = new ToStringBuilder(o); | |
try { | |
BeanInfo info = Introspector.getBeanInfo(clazz); | |
MethodDescriptor[] methods = info.getMethodDescriptors(); | |
Method property = null; | |
for (MethodDescriptor descriptor : methods) { | |
if (descriptor == null) | |
continue; | |
String name = descriptor.getName(); | |
if (isPlainGetterAndNotCollectionAccessor(name)) { | |
property = descriptor.getMethod(); | |
builder.append(property.invoke(o, (Object[]) null)); | |
} | |
} | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
return builder.toString(); | |
} | |
private static boolean isPlainGetterAndNotCollectionAccessor(String name) { | |
return name.startsWith("get") && !name.endsWith("s"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
import com.santos.leandro.annotations.ToStringBuilderIgnore; | |
public class ToStringBuilderUtil { | |
public static String reflectionToString(Object o) { | |
ToStringBuilder builder = new ToStringBuilder(o); | |
try { | |
Method[] methods = o.getClass().getMethods(); | |
for (Method method : methods) { | |
if (method.getName().startsWith("get") | |
&& !method | |
.isAnnotationPresent(ToStringBuilderIgnore.class)) { | |
builder.append(method.invoke(o, (Object[]) null)); | |
} | |
} | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment