Last active
August 29, 2015 14:07
-
-
Save wakim/15bcebd7771ef337add5 to your computer and use it in GitHub Desktop.
Request Parameter and Attribute Injection
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
package br.com.wakim.teste_jsf.util; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Modifier; | |
public abstract class AbstractEntityBuilder<T, ValueAccessor> { | |
public T build(Class<T> klass, ValueAccessor v) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { | |
Constructor<T> constructor = klass.getDeclaredConstructor(); | |
if(constructor == null) { | |
throw new RuntimeException("No empty Constructor"); | |
} | |
T t = constructor.newInstance(); | |
return build(t, v); | |
} | |
public T build(T t, ValueAccessor v) throws IllegalArgumentException, IllegalAccessException, SecurityException, InvocationTargetException { | |
iterateOverFields(t, v); | |
iterateOverMethods(t, v); | |
return t; | |
} | |
void iterateOverFields(T t, ValueAccessor v) throws IllegalArgumentException, IllegalAccessException, SecurityException, InvocationTargetException { | |
Field[] fields = t.getClass().getDeclaredFields(); | |
for(Field field : fields) { | |
RequestInject an = null; | |
if(field.isAnnotationPresent(RequestInject.class)) { | |
an = field.getAnnotation(RequestInject.class); | |
if(! isValidAnnotation(an, field, v)) { | |
continue; | |
} | |
// Atributo publico | |
if(Modifier.isPublic(field.getModifiers())) { | |
field.set(t, getValue(field, an, v)); | |
} else { // Buscamos o setter | |
try { | |
Method setter = t.getClass().getMethod("set".concat(capitalize(field.getName())), field.getType()); | |
setter.invoke(t, getValue(setter, an, v)); | |
} catch(NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
void iterateOverMethods(T t, ValueAccessor v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { | |
Method[] methods = t.getClass().getDeclaredMethods(); | |
for(Method method : methods) { | |
RequestInject an = null; | |
if(method.isAnnotationPresent(RequestInject.class)) { | |
an = method.getAnnotation(RequestInject.class); | |
if(! isValidAnnotation(an, method, v)) { | |
continue; | |
} | |
if(Modifier.isPublic(method.getModifiers())) { | |
method.invoke(t, getValue(method, an, v)); | |
} | |
} | |
} | |
} | |
String capitalize(String str) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(Character.toUpperCase(str.charAt(0))); | |
if(str.length() > 1) { | |
sb.append(str.substring(1)); | |
} | |
return sb.toString(); | |
} | |
public abstract boolean isValidAnnotation(RequestInject an, Field field, ValueAccessor v); | |
public abstract boolean isValidAnnotation(RequestInject an, Method method, ValueAccessor v); | |
public abstract Object getValue(Field field, RequestInject an, ValueAccessor v); | |
public abstract Object getValue(Method method, RequestInject an, ValueAccessor v); | |
} |
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
package br.com.wakim.teste_jsf.util; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import javax.faces.context.ExternalContext; | |
public class RequestAttributeEntityBuilder<T> extends AbstractEntityBuilder<T, ExternalContext> { | |
<P> P cast(Object obj, Class<P> targetClass) { | |
return targetClass.cast(obj); | |
} | |
@Override | |
public boolean isValidAnnotation(RequestInject an, Field field, ExternalContext v) { | |
String requestAttributeName = an.name().isEmpty() ? field.getName() : an.name(); | |
return v.getRequestMap().containsKey(requestAttributeName); | |
} | |
@Override | |
public boolean isValidAnnotation(RequestInject an, Method method, ExternalContext v) { | |
String requestAttributeName = an.name().isEmpty() ? method.getName() : an.name(); | |
return v.getRequestMap().containsKey(requestAttributeName); | |
} | |
@Override | |
public Object getValue(Field field, RequestInject an, ExternalContext v) { | |
String requestAttributeName = an.name().isEmpty() ? field.getName() : an.name(); | |
return cast(v.getRequestMap().get(requestAttributeName), field.getType()); | |
} | |
@Override | |
public Object getValue(Method method, RequestInject an, ExternalContext v) { | |
String requestAttributeName = an.name().isEmpty() ? method.getName() : an.name(); | |
return cast(v.getRequestMap().get(requestAttributeName), method.getParameterTypes()[0]); | |
} | |
} |
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
package br.com.wakim.teste_jsf.util; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import javax.faces.context.ExternalContext; | |
public class RequestParameterEntityBuilder<T> extends AbstractEntityBuilder<T, ExternalContext> { | |
<P> P parse(String str, Class<P> targetClass) { | |
if(targetClass == String.class) return targetClass.cast(str); | |
try { | |
Method method = targetClass.getMethod("valueOf", String.class); | |
return targetClass.cast(method.invoke(null, str)); | |
} catch (Exception e) { | |
throw new RuntimeException("No available Class"); | |
} | |
} | |
@Override | |
public boolean isValidAnnotation(RequestInject an, Field field, ExternalContext v) { | |
String requestParameterName = an.name().isEmpty() ? field.getName() : an.name(); | |
return v.getRequestParameterMap().containsKey(requestParameterName); | |
} | |
@Override | |
public boolean isValidAnnotation(RequestInject an, Method method, ExternalContext v) { | |
String requestParameterName = an.name().isEmpty() ? method.getName() : an.name(); | |
return v.getRequestParameterMap().containsKey(requestParameterName); | |
} | |
@Override | |
public Object getValue(Field field, RequestInject an, ExternalContext v) { | |
String requestParameterName = an.name().isEmpty() ? field.getName() : an.name(); | |
return parse(v.getRequestParameterMap().get(requestParameterName), field.getType()); | |
} | |
@Override | |
public Object getValue(Method method, RequestInject an, ExternalContext v) { | |
String requestParameterName = an.name().isEmpty() ? method.getName() : an.name(); | |
return parse(v.getRequestParameterMap().get(requestParameterName), method.getParameterTypes()[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment