use of jodd.introspector.ClassDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetiteInitMethods.
/**
* Registers init method.
*
* @param beanName bean name
* @param invocationStrategy moment of invocation
* @param initMethodNames init method names
*/
public void registerPetiteInitMethods(String beanName, InitMethodInvocationStrategy invocationStrategy, String... initMethodNames) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
if (initMethodNames == null) {
initMethodNames = StringPool.EMPTY_ARRAY;
}
int total = initMethodNames.length;
InitMethodPoint[] initMethodPoints = new InitMethodPoint[total];
int i;
for (i = 0; i < initMethodNames.length; i++) {
MethodDescriptor md = cd.getMethodDescriptor(initMethodNames[i], ReflectUtil.NO_PARAMETERS, true);
if (md == null) {
throw new PetiteException("Init method not found: " + beanDefinition.type.getName() + '#' + initMethodNames[i]);
}
initMethodPoints[i] = new InitMethodPoint(md.getMethod(), i, invocationStrategy);
}
beanDefinition.addInitMethodPoints(initMethodPoints);
}
use of jodd.introspector.ClassDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetiteMethodInjectionPoint.
/**
* Registers method injection point.
*
* @param beanName bean name
* @param methodName method name
* @param arguments method arguments, may be <code>null</code>
* @param references injection references
*/
public void registerPetiteMethodInjectionPoint(String beanName, String methodName, Class[] arguments, String[] references) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
String[][] ref = PetiteUtil.convertRefToReferences(references);
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
Method method = null;
if (arguments == null) {
MethodDescriptor[] methods = cd.getAllMethodDescriptors(methodName);
if (methods != null && methods.length > 0) {
if (methods.length > 1) {
throw new PetiteException(methods.length + " suitable methods found as injection points for: " + beanDefinition.type.getName() + '#' + methodName);
}
method = methods[0].getMethod();
}
} else {
MethodDescriptor md = cd.getMethodDescriptor(methodName, arguments, true);
if (md != null) {
method = md.getMethod();
}
}
if (method == null) {
throw new PetiteException("Method not found: " + beanDefinition.type.getName() + '#' + methodName);
}
MethodInjectionPoint mip = injectionPointFactory.createMethodInjectionPoint(method, ref);
beanDefinition.addMethodInjectionPoint(mip);
}
use of jodd.introspector.ClassDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetitePropertyInjectionPoint.
/**
* Registers property injection point.
*
* @param beanName bean name
* @param property property name
* @param reference explicit injection reference, may be <code>null</code>
*/
public void registerPetitePropertyInjectionPoint(String beanName, String property, String reference) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
String[] references = reference == null ? null : new String[] { reference };
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
PropertyDescriptor propertyDescriptor = cd.getPropertyDescriptor(property, true);
if (propertyDescriptor == null) {
throw new PetiteException("Property not found: " + beanDefinition.type.getName() + '#' + property);
}
PropertyInjectionPoint pip = injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, references);
beanDefinition.addPropertyInjectionPoint(pip);
}
use of jodd.introspector.ClassDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetiteProvider.
/**
* Registers static method provider.
*
* @param providerName provider name
* @param type class type
* @param staticMethodName static method name
* @param arguments method argument types
*/
public void registerPetiteProvider(String providerName, Class type, String staticMethodName, Class[] arguments) {
ClassDescriptor cd = ClassIntrospector.lookup(type);
MethodDescriptor md = cd.getMethodDescriptor(staticMethodName, arguments, true);
if (md == null) {
throw new PetiteException("Provider method not found: " + staticMethodName);
}
ProviderDefinition providerDefinition = new ProviderDefinition(providerName, md.getMethod());
providers.put(providerName, providerDefinition);
}
use of jodd.introspector.ClassDescriptor 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