use of jodd.introspector.CtorDescriptor in project jodd by oblac.
the class JsonParserBase method newObjectInstance.
/**
* Creates new object or a <code>HashMap</code> if type is not specified.
*/
protected Object newObjectInstance(Class targetType) {
if (targetType == null || targetType == Map.class) {
return new HashMap();
}
ClassDescriptor cd = ClassIntrospector.lookup(targetType);
CtorDescriptor ctorDescriptor = cd.getDefaultCtorDescriptor(true);
if (ctorDescriptor == null) {
throw new JsonException("Default ctor not found for: " + targetType.getName());
}
try {
return ctorDescriptor.getConstructor().newInstance();
} catch (Exception e) {
throw new JsonException(e);
}
}
use of jodd.introspector.CtorDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetiteCtorInjectionPoint.
// ---------------------------------------------------------------- injection points
/**
* Registers constructor injection point.
*
* @param beanName bean name
* @param paramTypes constructor parameter types, may be <code>null</code>
* @param references references for arguments
*/
public void registerPetiteCtorInjectionPoint(String beanName, Class[] paramTypes, String[] references) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
String[][] ref = PetiteUtil.convertRefToReferences(references);
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
Constructor constructor = null;
if (paramTypes == null) {
CtorDescriptor[] ctors = cd.getAllCtorDescriptors();
if (ctors != null && ctors.length > 0) {
if (ctors.length > 1) {
throw new PetiteException(ctors.length + " suitable constructor found as injection point for: " + beanDefinition.type.getName());
}
constructor = ctors[0].getConstructor();
}
} else {
CtorDescriptor ctorDescriptor = cd.getCtorDescriptor(paramTypes, true);
if (ctorDescriptor != null) {
constructor = ctorDescriptor.getConstructor();
}
}
if (constructor == null) {
throw new PetiteException("Constructor not found: " + beanDefinition.type.getName());
}
beanDefinition.ctor = injectionPointFactory.createCtorInjectionPoint(constructor, ref);
}
use of jodd.introspector.CtorDescriptor in project jodd by oblac.
the class CtorResolver method resolve.
/**
* Resolves constructor injection point from type. Looks for single annotated constructor.
* If no annotated constructors found, the total number of constructors will be checked.
* If there is only one constructor, that one will be used as injection point. If more
* constructors exist, the default one will be used as injection point. Otherwise, exception
* is thrown.
*/
public CtorInjectionPoint resolve(Class type, boolean useAnnotation) {
ClassDescriptor cd = ClassIntrospector.lookup(type);
CtorDescriptor[] allCtors = cd.getAllCtorDescriptors();
Constructor foundedCtor = null;
Constructor defaultCtor = null;
String refValues = null;
for (CtorDescriptor ctorDescriptor : allCtors) {
Constructor<?> ctor = ctorDescriptor.getConstructor();
Class<?>[] paramTypes = ctor.getParameterTypes();
if (paramTypes.length == 0) {
// detects default ctors
defaultCtor = ctor;
}
if (!useAnnotation) {
continue;
}
PetiteInject ref = ctor.getAnnotation(PetiteInject.class);
if (ref == null) {
continue;
}
if (foundedCtor != null) {
throw new PetiteException("Two or more constructors are annotated as injection points in bean: " + type.getName());
}
foundedCtor = ctor;
refValues = ref.value().trim();
}
if (foundedCtor == null) {
if (allCtors.length == 1) {
foundedCtor = allCtors[0].getConstructor();
} else {
foundedCtor = defaultCtor;
}
}
if (foundedCtor == null) {
throw new PetiteException("No constructor (annotated, single or default) founded as injection point for: " + type.getName());
}
String[][] references = PetiteUtil.convertAnnValueToReferences(refValues);
return injectionPointFactory.createCtorInjectionPoint(foundedCtor, references);
}
Aggregations