net.sf.andromedaioc.util.BeanUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of andromeda-ioc Show documentation
Show all versions of andromeda-ioc Show documentation
Inversion of Control Framework for Android
The newest version!
package net.sf.andromedaioc.util;
import net.sf.andromedaioc.bean.Pair;
import net.sf.andromedaioc.bean.param.AccessorWrapper;
import net.sf.andromedaioc.bean.param.ParameterWrapper;
import net.sf.andromedaioc.exception.BeanNotFoundException;
import net.sf.andromedaioc.exception.DependencyInjectionException;
import net.sf.andromedaioc.exception.ResourceLoadingException;
import net.sf.andromedaioc.model.beans.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
public class BeanUtils {
private final static String SETTER_START = "set";
private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0];
private final static Map, Class>> types;
static {
types = new HashMap, Class>>();
types.put(boolean.class, Boolean.class);
types.put(byte.class, Byte.class);
types.put(short.class, Short.class);
types.put(int.class, Integer.class);
types.put(long.class, Long.class);
types.put(float.class, Float.class);
types.put(double.class, Double.class);
}
private BeanUtils(){
}
public static Object[] unwrapParameters(List wrappedParameters) {
if(wrappedParameters == null || wrappedParameters.isEmpty()) {
return EMPTY_OBJECT_ARRAY;
}
Object[] parameters = new Object[wrappedParameters.size()];
for(int i=0; i> beanProperties) throws Exception {
for(Pair entry : beanProperties) {
AccessorWrapper accessorWrapper = entry.getFirst();
ParameterWrapper parameterWrapper = entry.getSecond();
accessorWrapper.set(beanInstance, parameterWrapper.getValue());
}
}
public static void callInitMethod(Object beanInstance, String initMethod) throws Exception {
Method method = beanInstance.getClass().getMethod(initMethod);
method.invoke(beanInstance);
}
public static Class> getWrapperForPrimitiveType(Class> type) {
if(type.isPrimitive()) {
return types.get(type);
} else {
return type;
}
}
public static Map getResourceIdFields(String resourceClassName, String prefixForCopies) {
try {
Map fieldsMap = new HashMap();
Class> resourceClass = Class.forName(resourceClassName);
for(Class> subClass : resourceClass.getDeclaredClasses()) {
String subClassName = subClass.getSimpleName();
String subClassPath = resourceClassName + "." + subClassName + ".";
for(Field field : subClass.getDeclaredFields()) {
if(field.getType().equals(int.class) && Modifier.isStatic(field.getModifiers())) {
String fieldPath = subClassPath + field.getName();
Integer fieldValue = field.getInt(null);
fieldsMap.put(fieldPath, fieldValue);
if(fieldPath.startsWith(prefixForCopies)) {
fieldsMap.put(fieldPath.substring(prefixForCopies.length()), fieldValue);
}
}
}
}
return fieldsMap;
} catch(Exception e) {
throw new ResourceLoadingException("Error loading resources. Reason: " + e, e);
}
}
public static Map getSetters(Class> beanClass) {
Map setters = new HashMap();
for(Method method : beanClass.getMethods()) {
String methodName = method.getName();
if(methodName.matches("^set[A-Z].*") && method.getParameterTypes().length == 1) {
String propertyName = methodName.substring(SETTER_START.length());
boolean needToDecapitalize = true;
if(propertyName.length() > 2 && Character.isUpperCase(propertyName.charAt(0))
&& Character.isUpperCase(propertyName.charAt(1)) ) {
needToDecapitalize = false;
}
if(needToDecapitalize) {
propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);
}
setters.put(propertyName, method);
}
}
return setters;
}
public static T getBeansWithType(Class> requiredType, Collection input) {
List result = new ArrayList();
for(T bean : input) {
if(requiredType.isAssignableFrom(bean.getBeanClass())) {
result.add(bean);
}
}
if(result.isEmpty()) {
throw new BeanNotFoundException(String.format("No bean with type = %s found", requiredType));
} else if(result.size() > 1) {
throw new DependencyInjectionException(String.format("Found more than one bean that is assignable from %s type. Please use id to specify what bean should be use", requiredType));
} else {
return result.get(0);
}
}
public static Set getAllReferences(BeanModel beanModel) {
Set dependencies = getReferences(beanModel.getConstructorArguments());
dependencies.addAll(getReferences(beanModel.getProperties().values()));
if(beanModel.getFactoryBean() != null) {
dependencies.add(beanModel.getFactoryBean());
}
if(beanModel instanceof CollectionModel) {
dependencies.addAll(getItemsReferences((CollectionModel) beanModel));
}
if(beanModel instanceof MapModel) {
dependencies.addAll(getEntriesReferences((MapModel) beanModel));
}
return dependencies;
}
private static Set getItemsReferences(CollectionModel collectionModel) {
Set references = new LinkedHashSet();
for(ValueModel value : collectionModel.getItems()) {
if(value.getType().isReference()) {
references.add(value.getReference());
}
}
return references;
}
private static Set getEntriesReferences(MapModel mapModel) {
Set references = new LinkedHashSet();
for(EntryModel entry : mapModel.getEntries()) {
if(entry.getKey().getType().isReference()) {
references.add(entry.getKey().getReference());
}
if(entry.getValue().getType().isReference()) {
references.add(entry.getValue().getReference());
}
}
return references;
}
private static Set getReferences(Collection extends AbstractParameterModel> parameters) {
Set references = new LinkedHashSet();
for(AbstractParameterModel parameter : parameters) {
ValueModel value = parameter.getValue();
if(value.getType().isReference()) {
references.add(value.getReference());
}
}
return references;
}
public static List splitAndTrimCSV(String csv) {
if(csv == null) {
return Collections.emptyList();
}
String[] elements = csv.split(",");
List result = new LinkedList();
for(String packagePath : elements) {
String trimmedPackage = packagePath.trim();
if(trimmedPackage.length() > 0) {
result.add(packagePath.trim());
}
}
return result;
}
}