use of org.b3log.latke.ioc.point.ParameterInjectionPoint in project latke by b3log.
the class BeanImpl method resolveCurrentclassMethodDependencies.
/**
* Resolves current class method dependencies for the specified reference.
*
* @param reference the specified reference
*/
private void resolveCurrentclassMethodDependencies(final Object reference) {
for (final Map.Entry<AnnotatedMethod<?>, List<ParameterInjectionPoint>> methodParameterInjectionPoint : methodParameterInjectionPoints.entrySet()) {
final List<ParameterInjectionPoint> paraSet = methodParameterInjectionPoint.getValue();
final Object[] args = new Object[paraSet.size()];
int i = 0;
for (final ParameterInjectionPoint paraInjectionPoint : paraSet) {
Object arg = beanManager.getInjectableReference(paraInjectionPoint, null);
if (arg == null) {
for (final ParameterProvider<?> provider : methodParameterProviders.get(methodParameterInjectionPoint.getKey())) {
if (provider.getAnnotated().equals(paraInjectionPoint.getAnnotated())) {
arg = provider;
break;
}
}
}
args[i++] = arg;
}
final AnnotatedMethod<?> annotatedMethod = methodParameterInjectionPoint.getKey();
final Method method = annotatedMethod.getJavaMember();
try {
final Method declaredMethod = proxyClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
try {
declaredMethod.setAccessible(true);
declaredMethod.invoke(reference, args);
} catch (final Exception e) {
throw new RuntimeException(e);
}
} catch (final NoSuchMethodException ex) {
try {
method.setAccessible(true);
method.invoke(reference, args);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
}
use of org.b3log.latke.ioc.point.ParameterInjectionPoint in project latke by b3log.
the class BeanImpl method resolveSuperclassMethodDependencies.
/**
* Resolves super class method dependencies for the specified reference.
*
* @param reference the specified reference
* @param clazz the super class of the specified reference
* @throws Exception exception
*/
private void resolveSuperclassMethodDependencies(final Object reference, final Class<?> clazz) throws Exception {
if (clazz.equals(Object.class)) {
return;
}
final Class<?> superclass = clazz.getSuperclass();
resolveSuperclassMethodDependencies(reference, superclass);
if (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())) {
return;
}
final BeanImpl<?> superBean = (BeanImpl<?>) beanManager.getBean(clazz);
for (final Map.Entry<AnnotatedMethod<?>, List<ParameterInjectionPoint>> methodParameterInjectionPoint : superBean.methodParameterInjectionPoints.entrySet()) {
final List<ParameterInjectionPoint> paraSet = methodParameterInjectionPoint.getValue();
final Object[] args = new Object[paraSet.size()];
int i = 0;
for (final ParameterInjectionPoint paraInjectionPoint : paraSet) {
Object arg = beanManager.getInjectableReference(paraInjectionPoint, null);
if (arg == null) {
for (final ParameterProvider<?> provider : superBean.methodParameterProviders.get(methodParameterInjectionPoint.getKey())) {
if (provider.getAnnotated().equals(paraInjectionPoint.getAnnotated())) {
arg = provider;
break;
}
}
}
args[i++] = arg;
}
final AnnotatedMethod<?> superAnnotatedMethod = methodParameterInjectionPoint.getKey();
final Method superMethod = superAnnotatedMethod.getJavaMember();
final Method overrideMethod = Reflections.getOverrideMethod(superMethod, proxyClass);
if (superMethod.equals(overrideMethod)) {
try {
superMethod.invoke(reference, args);
} catch (final Exception e) {
throw new RuntimeException(e);
}
return;
}
}
}
use of org.b3log.latke.ioc.point.ParameterInjectionPoint in project latke by b3log.
the class BeanImpl method initMethodInjectionPoints.
/**
* Initializes method injection points.
*/
@SuppressWarnings("unchecked")
private void initMethodInjectionPoints() {
final Set<AnnotatedMethod<? super T>> annotatedMethods = annotatedType.getMethods();
for (final AnnotatedMethod annotatedMethod : annotatedMethods) {
final List<AnnotatedParameter<?>> parameters = annotatedMethod.getParameters();
final List<ParameterInjectionPoint> paraInjectionPointArrayList = new ArrayList<ParameterInjectionPoint>();
final List<ParameterProvider<?>> paraProviders = new ArrayList<ParameterProvider<?>>();
for (final AnnotatedParameter<?> annotatedParameter : parameters) {
Type type = annotatedParameter.getBaseType();
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getRawType();
}
if (type.equals(Provider.class)) {
final ParameterProvider<T> provider = new ParameterProvider<T>(beanManager, annotatedParameter);
paraProviders.add(provider);
}
final ParameterInjectionPoint parameterInjectionPoint = new ParameterInjectionPoint(this, annotatedParameter);
paraInjectionPointArrayList.add(parameterInjectionPoint);
}
methodParameterProviders.put(annotatedMethod, paraProviders);
methodParameterInjectionPoints.put(annotatedMethod, paraInjectionPointArrayList);
}
}
use of org.b3log.latke.ioc.point.ParameterInjectionPoint in project latke by b3log.
the class BeanImpl method instantiateReference.
/**
* Constructs the bean object with dependencies resolved.
*
* @return bean object
* @throws Exception exception
*/
private T instantiateReference() throws Exception {
T ret;
if (constructorParameterInjectionPoints.size() == 1) {
// only one constructor allow to be annotated with @Inject
// instantiate an instance by the constructor annotated with @Inject
final AnnotatedConstructor<T> annotatedConstructor = constructorParameterInjectionPoints.keySet().iterator().next();
final List<ParameterInjectionPoint> paraInjectionPoints = constructorParameterInjectionPoints.get(annotatedConstructor);
final Object[] args = new Object[paraInjectionPoints.size()];
int i = 0;
for (final ParameterInjectionPoint paraInjectionPoint : paraInjectionPoints) {
Object arg = beanManager.getInjectableReference(paraInjectionPoint, null);
if (arg == null) {
for (final ParameterProvider<?> provider : constructorParameterProviders) {
if (provider.getAnnotated().equals(paraInjectionPoint.getAnnotated())) {
arg = provider;
break;
}
}
}
args[i++] = arg;
}
final Constructor<T> oriBeanConstructor = annotatedConstructor.getJavaMember();
final Constructor<T> constructor = proxyClass.getConstructor(oriBeanConstructor.getParameterTypes());
ret = constructor.newInstance(args);
} else {
ret = proxyClass.newInstance();
}
((ProxyObject) ret).setHandler(javassistMethodHandler);
LOGGER.log(Level.TRACE, "Uses Javassist method handler for bean[class={0}]", beanClass.getName());
return ret;
}
use of org.b3log.latke.ioc.point.ParameterInjectionPoint in project latke by b3log.
the class BeanImpl method initConstructorInjectionPoints.
/**
* Initializes constructor injection points.
*/
private void initConstructorInjectionPoints() {
final Set<AnnotatedConstructor<T>> annotatedConstructors = annotatedType.getConstructors();
for (final AnnotatedConstructor annotatedConstructor : annotatedConstructors) {
final List<AnnotatedParameter<?>> parameters = annotatedConstructor.getParameters();
final List<ParameterInjectionPoint> paraInjectionPointArrayList = new ArrayList<ParameterInjectionPoint>();
for (final AnnotatedParameter<?> annotatedParameter : parameters) {
Type type = annotatedParameter.getBaseType();
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getRawType();
}
if (type.equals(Provider.class)) {
final ParameterProvider<T> provider = new ParameterProvider<T>(beanManager, annotatedParameter);
constructorParameterProviders.add(provider);
}
final ParameterInjectionPoint parameterInjectionPoint = new ParameterInjectionPoint(this, annotatedParameter);
paraInjectionPointArrayList.add(parameterInjectionPoint);
}
constructorParameterInjectionPoints.put(annotatedConstructor, paraInjectionPointArrayList);
}
}
Aggregations