Created
September 21, 2011 15:40
-
-
Save thiagotn/1232415 to your computer and use it in GitHub Desktop.
Bean to CVS
This file contains 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 java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class TestReflection { | |
public static void main(String args[]) { | |
List<Usuario> usuarios = new ArrayList<Usuario>(); | |
for ( int i=0 ; i<10 ; i++) { | |
Usuario usuario = new Usuario(); | |
usuario.setNome("Teste"); | |
usuario.setEmail("teste@gmail.com"); | |
usuario.setCpf("111.111.111-11"); | |
usuarios.add(usuario); | |
} | |
List<String> header = Arrays.asList("Nome","Email","CPF"); | |
String csv = writeCsvContent(usuarios, header); | |
System.out.println(csv); | |
} | |
public static String writeCsvContent(List<?> data, List<String> header) { | |
StringBuilder csv = new StringBuilder(); | |
String delimiter = ","; | |
String breakLine = "\n"; | |
for (int i=0 ; i<header.size(); i++){ | |
csv.append(header.get(i)); | |
if (i < header.size()-1) csv.append(delimiter); | |
} | |
if (header.size() > 0) csv.append(breakLine); | |
for (Object obj : data) { | |
Object currentInstance = obj; | |
List<Method> getterMethods = filterGetterMethods(obj.getClass().getDeclaredMethods()); | |
for (int i = 0; i < getterMethods.size(); i++) { | |
if (isGetter(getterMethods.get(i))) { | |
Object returned = null; | |
try { | |
returned = getterMethods.get(i).invoke(currentInstance); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
String value = (returned == null) ? "\"\"" : "\"" + returned.toString() + "\""; | |
csv.append(value); | |
if (i < getterMethods.size()-1) csv.append(delimiter); | |
} | |
} | |
csv.append(breakLine); | |
} | |
return String.valueOf(csv); | |
} | |
public static List<Method> filterGetterMethods(Method [] methods) { | |
List<Method> list = new ArrayList<Method>(); | |
for (int i = 0; i < methods.length; i++) { | |
if (isGetter(methods[i])) list.add(methods[i]); | |
} | |
return list; | |
} | |
public static void printDeclaredGettersSetters(Class aClass){ | |
Method[] methods = aClass.getDeclaredMethods(); | |
for(Method method : methods){ | |
if(isGetter(method)) System.out.println("getter: " + method); | |
if(isSetter(method)) System.out.println("setter: " + method); | |
} | |
} | |
public static void printGettersSetters(Class aClass){ | |
Method[] methods = aClass.getMethods(); | |
for(Method method : methods){ | |
if(isGetter(method)) System.out.println("getter: " + method); | |
if(isSetter(method)) System.out.println("setter: " + method); | |
} | |
} | |
public static boolean isGetter(Method method){ | |
if(!method.getName().startsWith("get")) return false; | |
if(method.getParameterTypes().length != 0) return false; | |
if(void.class.equals(method.getReturnType())) return false; | |
return true; | |
} | |
public static boolean isSetter(Method method){ | |
if(!method.getName().startsWith("set")) return false; | |
if(method.getParameterTypes().length != 1) return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment