Last active
August 29, 2015 14:13
-
-
Save tolitius/76eead05e684f2461b9f to your computer and use it in GitHub Desktop.
filters not null fields of a bean (e.g. has getters for all of the fields)
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
public class Bean { | |
private String a; | |
private Integer b; | |
private String c; | |
private Integer d; | |
public Bean( String a, Integer b, String c, Integer d ) { | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
this.d = d; | |
} | |
public String getC() { | |
return c; | |
} | |
public Integer getD() { | |
return d; | |
} | |
public String getA() { | |
return a; | |
} | |
public Integer getB() { | |
return b; | |
} | |
} |
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.Field; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class FieldFilter { | |
// should be somewhere in apache or just put it in other utils | |
public static String capitalizeFirstLetter( String s ) { | |
// if non empty / null | |
return Character.toUpperCase( s.charAt( 0 ) ) + s.substring( 1 ); | |
} | |
@SuppressWarnings( "unchecked" ) | |
public static boolean isExistingMethod( Class clazz, String name ) { | |
try { | |
clazz.getMethod( name ); | |
return true; | |
} | |
catch ( NoSuchMethodException nsme ) { | |
return false; | |
} | |
} | |
@SuppressWarnings( "unchecked" ) | |
public static List<String> notNullFields( Class clazz, Object obj ) { | |
// use guava to create the list | |
List<String> nullFields = new ArrayList<String>(); | |
for ( Field f: clazz.getDeclaredFields() ) { | |
String getterName = "get" + FieldFilter.capitalizeFirstLetter ( f.getName() ); | |
if ( isExistingMethod( clazz, getterName ) ) { | |
try { | |
Method method = clazz.getMethod( getterName ); | |
Object value = method.invoke( obj, ( Object[] ) null ); | |
if ( value == null ) | |
nullFields.add( f.getName() ); | |
} | |
catch ( Throwable t ) { | |
throw new RuntimeException( "could not read " + clazz + " field values", t ); | |
} | |
} | |
} | |
return nullFields; | |
} | |
public static void main( String[] args ) { | |
// should return [a, b] | |
System.out.println( | |
FieldFilter.notNullFields( Bean.class, | |
new Bean( null, null, "No", 42 ) ) ); | |
// should return [b, c] | |
System.out.println( | |
FieldFilter.notNullFields( Bean.class, | |
new Bean( "Y", null, null, 42 ) ) ); | |
// should return [a, b, c, d] | |
System.out.println( | |
FieldFilter.notNullFields( Bean.class, | |
new Bean( null, null, null, null ) ) ); | |
// should return [a, d] | |
System.out.println( | |
FieldFilter.notNullFields( Bean.class, | |
new Bean( null, 0, "", null ) ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment