com.davidbracewell.reflection.BeanUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mango Show documentation
Show all versions of mango Show documentation
A set of utilities and tools to speed up and ease programming in Java.
/*
* (c) 2005 David B. Bracewell
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.davidbracewell.reflection;
import com.davidbracewell.config.Config;
import com.davidbracewell.conversion.Cast;
import com.davidbracewell.conversion.Convert;
import com.google.common.collect.Lists;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* Methods for constructing beans and setting their parameters using value in the {@link Config}
*
* @author David B. Bracewell
*/
public class BeanUtils {
private static final ConcurrentSkipListMap SINGLETONS = new ConcurrentSkipListMap<>();
private static boolean doCollection(BeanMap beanMap, String configProperty, String propertyName, Class> valueType) {
// Check if we need a Collection
if (Collection.class.isAssignableFrom(valueType)) {
String value = Config.get(configProperty).asString();
Class> elementType = Config.get(configProperty + ".elementType").asClass(String.class);
beanMap.put(propertyName, Convert.convert(value, valueType, elementType));
return true;
}
return false;
}
private static boolean doMap(BeanMap beanMap, String configProperty, String propertyName, Class> valueType) {
// Check if we need a Map
if (Map.class.isAssignableFrom(valueType)) {
String value = Config.get(configProperty).asString();
Class> mapKeyType = Config.get(configProperty + ".keyType").asClass(String.class);
Class> mapValueType = Config.get(configProperty + ".valueType").asClass(String.class);
beanMap.put(propertyName, Convert.convert(value, valueType, mapKeyType, mapValueType));
return true;
}
return false;
}
private static void doParametrization(BeanMap beanMap, String match) {
for (String propertyName : beanMap.getSetters()) {
String configOption = match + propertyName;
String typeProperty = configOption + ".type";
if (Config.hasProperty(typeProperty)) {
Class> valueType = Config.get(typeProperty).asClass();
if (valueType == null) {
throw new IllegalArgumentException(Config.get(typeProperty).asString() + " is not a valid class");
}
if (doCollection(beanMap, configOption, propertyName, valueType) ||
doMap(beanMap, configOption, propertyName, valueType)) {
continue;
}
beanMap.put(propertyName, Config.get(configOption).as(valueType));
} else if (Config.hasProperty(configOption)) {
beanMap.put(propertyName, Config.get(configOption).as(Object.class));
}
}
}
/**
* Constructs a new instance of the given class and then sets it properties.
*
* @param clazz The class that we want to instantiate
* @return A new instance of the given class
*/
public static T getBean(Class clazz) throws ReflectionException {
return parameterizeObject(Reflect.onClass(clazz).create().get());
}
/**
* Instantiates a named bean (defined via the Config) which can describe the class (name.class) and properties or
* can instantiate a bean that is described as a script.
*
* @param name The name of the bean
* @param clazz The class type of the bean
* @return The named bean
*/
public static T getNamedBean(String name, Class clazz) throws ReflectionException {
if (SINGLETONS.containsKey(name)) {
return Cast.as(SINGLETONS.get(name));
}
if (Config.valueIsScript(name)) {
return Config.get(name).as(clazz);
}
Reflect reflect;
if (Config.hasProperty(name + ".class")) {
reflect = Reflect.onClass(Config.get(name + ".class").asClass());
} else {
reflect = Reflect.onClass(clazz);
}
boolean isSingleton = Config.get(name + ".singleton").asBoolean(false);
List> paramTypes = new ArrayList<>();
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy