Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arc - add basic BeanManager support #59

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public class BeanGenerator extends AbstractGenerator {

private static final AtomicInteger PRODUCER_INDEX = new AtomicInteger();

private static final String FIELD_NAME_DECLARING_PROVIDER = "declaringProvider";
protected static final String FIELD_NAME_DECLARING_PROVIDER = "declaringProvider";
protected static final String FIELD_NAME_BEAN_TYPES = "types";
protected static final String FIELD_NAME_QUALIFIERS = "qualifiers";
protected static final String FIELD_NAME_STEREOTYPES = "stereotypes";
protected static final String FIELD_NAME_PROXY = "proxy";

protected final AnnotationLiteralProcessor annotationLiterals;

Expand Down Expand Up @@ -121,14 +125,18 @@ Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass, Refle
ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(InjectableBean.class).build();

// Fields
FieldCreator beanTypes = beanCreator.getFieldCreator("beanTypes", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator qualifiers = null;
if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
qualifiers = beanCreator.getFieldCreator("qualifiers", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
if (bean.getScope().isNormal()) {
// For normal scopes a client proxy is generated too
beanCreator.getFieldCreator("proxy", LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
beanCreator.getFieldCreator(FIELD_NAME_PROXY, LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
FieldCreator stereotypes = null;
if (!bean.getStereotypes().isEmpty()) {
stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}

Map<InjectionPointInfo, String> injectionPointToProviderField = new HashMap<>();
Expand All @@ -155,6 +163,10 @@ Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass, Refle
if (bean.isAlternative()) {
implementGetAlternativePriority(bean, beanCreator);
}
if (stereotypes != null) {
implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
}
implementGetBeanClass(bean, beanCreator);

beanCreator.close();
return classOutput.getResources();
Expand Down Expand Up @@ -183,14 +195,18 @@ Collection<Resource> generateProducerMethodBean(BeanInfo bean, MethodInfo produc
ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(InjectableBean.class).build();

// Fields
FieldCreator beanTypes = beanCreator.getFieldCreator("beanTypes", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator qualifiers = null;
if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
qualifiers = beanCreator.getFieldCreator("qualifiers", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
if (bean.getScope().isNormal()) {
// For normal scopes a client proxy is generated too
beanCreator.getFieldCreator("proxy", LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
beanCreator.getFieldCreator(FIELD_NAME_PROXY, LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
FieldCreator stereotypes = null;
if (!bean.getStereotypes().isEmpty()) {
stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}

Map<InjectionPointInfo, String> injectionPointToProviderField = new HashMap<>();
Expand All @@ -215,6 +231,10 @@ Collection<Resource> generateProducerMethodBean(BeanInfo bean, MethodInfo produc
implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
}
implementGetDeclaringBean(beanCreator);
if (stereotypes != null) {
implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
}
implementGetBeanClass(bean, beanCreator);

beanCreator.close();
return classOutput.getResources();
Expand Down Expand Up @@ -243,14 +263,18 @@ Collection<Resource> generateProducerFieldBean(BeanInfo bean, FieldInfo producer
ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(InjectableBean.class).build();

// Fields
FieldCreator beanTypes = beanCreator.getFieldCreator("beanTypes", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator qualifiers = null;
if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
qualifiers = beanCreator.getFieldCreator("qualifiers", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
if (bean.getScope().isNormal()) {
// For normal scopes a client proxy is generated too
beanCreator.getFieldCreator("proxy", LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
beanCreator.getFieldCreator(FIELD_NAME_PROXY, LazyValue.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
FieldCreator stereotypes = null;
if (!bean.getStereotypes().isEmpty()) {
stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}

createProviderFields(beanCreator, bean, Collections.emptyMap(), Collections.emptyMap());
Expand All @@ -271,6 +295,10 @@ Collection<Resource> generateProducerFieldBean(BeanInfo bean, FieldInfo producer
implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
}
implementGetDeclaringBean(beanCreator);
if (stereotypes != null) {
implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
}
implementGetBeanClass(bean, beanCreator);

beanCreator.close();
return classOutput.getResources();
Expand Down Expand Up @@ -410,9 +438,8 @@ protected MethodCreator initConstructor(ClassOutput classOutput, ClassCreator be
for (org.jboss.jandex.Type type : bean.getTypes()) {
constructor.invokeInterfaceMethod(MethodDescriptors.SET_ADD, typesHandle, Types.getTypeHandle(constructor, type));
}
ResultHandle unmodifiableTypesHandle = constructor
.invokeStaticMethod(MethodDescriptor.ofMethod(Collections.class, "unmodifiableSet", Set.class, Set.class), typesHandle);
constructor.writeInstanceField(FieldDescriptor.of(beanCreator.getClassName(), "beanTypes", Set.class.getName()), constructor.getThis(),
ResultHandle unmodifiableTypesHandle = constructor.invokeStaticMethod(MethodDescriptors.COLLECTIONS_UNMODIFIABLE_SET, typesHandle);
constructor.writeInstanceField(FieldDescriptor.of(beanCreator.getClassName(), FIELD_NAME_BEAN_TYPES, Set.class.getName()), constructor.getThis(),
unmodifiableTypesHandle);

// Qualifiers
Expand All @@ -433,12 +460,23 @@ protected MethodCreator initConstructor(ClassOutput classOutput, ClassCreator be
constructor.newInstance(MethodDescriptor.ofConstructor(annotationLiteralName)));
}
}
ResultHandle unmodifiableQualifiersHandle = constructor
.invokeStaticMethod(MethodDescriptor.ofMethod(Collections.class, "unmodifiableSet", Set.class, Set.class), qualifiersHandle);
constructor.writeInstanceField(FieldDescriptor.of(beanCreator.getClassName(), "qualifiers", Set.class.getName()), constructor.getThis(),
ResultHandle unmodifiableQualifiersHandle = constructor.invokeStaticMethod(MethodDescriptors.COLLECTIONS_UNMODIFIABLE_SET, qualifiersHandle);
constructor.writeInstanceField(FieldDescriptor.of(beanCreator.getClassName(), FIELD_NAME_QUALIFIERS, Set.class.getName()), constructor.getThis(),
unmodifiableQualifiersHandle);
}

// Stereotypes
if (!bean.getStereotypes().isEmpty()) {
ResultHandle stereotypesHandle = constructor.newInstance(MethodDescriptor.ofConstructor(HashSet.class));
for (StereotypeInfo stereotype : bean.getStereotypes()) {
constructor.invokeInterfaceMethod(MethodDescriptors.SET_ADD, stereotypesHandle,
constructor.loadClass(stereotype.getTarget().name().toString()));
}
ResultHandle unmodifiableStereotypesHandle = constructor.invokeStaticMethod(MethodDescriptors.COLLECTIONS_UNMODIFIABLE_SET, stereotypesHandle);
constructor.writeInstanceField(FieldDescriptor.of(beanCreator.getClassName(), FIELD_NAME_STEREOTYPES, Set.class.getName()), constructor.getThis(),
unmodifiableStereotypesHandle);
}

if (bean.getScope().isNormal()) {
// this.proxy = new LazyValue(() -> new Bar_ClientProxy(this))
String proxyTypeName = ClientProxyGenerator.getProxyPackageName(bean) + "." + baseName + ClientProxyGenerator.CLIENT_PROXY_SUFFIX;
Expand Down Expand Up @@ -1066,4 +1104,14 @@ protected void implementGetAlternativePriority(BeanInfo bean, ClassCreator beanC
getAlternativePriority.load(bean.getAlternativePriority())));
}

protected void implementGetStereotypes(BeanInfo bean, ClassCreator beanCreator, FieldDescriptor stereotypesField) {
MethodCreator getStereotypes = beanCreator.getMethodCreator("getStereotypes", Set.class).setModifiers(ACC_PUBLIC);
getStereotypes.returnValue(getStereotypes.readInstanceField(stereotypesField, getStereotypes.getThis()));
}

protected void implementGetBeanClass(BeanInfo bean, ClassCreator beanCreator) {
MethodCreator getBeanClass = beanCreator.getMethodCreator("getBeanClass", Class.class).setModifiers(ACC_PUBLIC);
getBeanClass.returnValue(getBeanClass.loadClass(bean.getBeanClass().toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.jboss.protean.arc.processor.Methods.MethodKey;
Expand Down Expand Up @@ -100,6 +101,13 @@ boolean isProducerField() {
return Kind.FIELD.equals(target.kind());
}

DotName getBeanClass() {
if (declaringBean != null) {
return declaringBean.target.asClass().name();
}
return target.asClass().name();
}

boolean isInterceptor() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static void resolveInjectionPoint(BeanDeployment deployment, BeanInfo bean, Inje
resolved.add(b);
}
}
BeanInfo selected;
BeanInfo selected = null;
if (resolved.isEmpty()) {
throw new UnsatisfiedResolutionException(injectionPoint + " on " + bean);
} else if (resolved.size() > 1) {
Expand All @@ -251,9 +251,19 @@ static void resolveInjectionPoint(BeanDeployment deployment, BeanInfo bean, Inje
if (resolvedAmbiguity.size() == 1) {
selected = resolvedAmbiguity.get(0);
} else if (resolvedAmbiguity.size() > 1) {
// Keep only the highest priorities
resolvedAmbiguity.sort(Beans::compareAlternativeBeans);
selected = resolvedAmbiguity.get(0);
} else {
Integer highest = getAlternativePriority(resolvedAmbiguity.get(0));
for (Iterator<BeanInfo> iterator = resolvedAmbiguity.iterator(); iterator.hasNext();) {
if (!highest.equals(getAlternativePriority(iterator.next()))) {
iterator.remove();
}
}
if (resolved.size() == 1) {
selected = resolvedAmbiguity.get(0);
}
}
if (selected == null) {
throw new AmbiguousResolutionException(
injectionPoint + " on " + bean + "\nBeans:\n" + resolved.stream().map(Object::toString).collect(Collectors.joining("\n")));
}
Expand All @@ -263,6 +273,10 @@ static void resolveInjectionPoint(BeanDeployment deployment, BeanInfo bean, Inje
injectionPoint.resolve(selected);
}

private static Integer getAlternativePriority(BeanInfo bean) {
return bean.getDeclaringBean() != null ? bean.getDeclaringBean().getAlternativePriority() : bean.getAlternativePriority();
}

private static int compareAlternativeBeans(BeanInfo bean1, BeanInfo bean2) {
// The highest priority wins
Integer priority2 = bean2.getDeclaringBean() != null ? bean2.getDeclaringBean().getAlternativePriority() : bean2.getAlternativePriority();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class InterceptorGenerator extends BeanGenerator {

private static final Logger LOGGER = Logger.getLogger(InterceptorGenerator.class);

protected static final String FIELD_NAME_BINDINGS = "bindings";

/**
*
* @param annotationLiterals
Expand Down Expand Up @@ -76,8 +78,8 @@ Collection<Resource> generate(InterceptorInfo interceptor, ReflectionRegistratio
.build();

// Fields
FieldCreator beanTypes = interceptorCreator.getFieldCreator("beanTypes", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator bindings = interceptorCreator.getFieldCreator("bindings", Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator beanTypes = interceptorCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator bindings = interceptorCreator.getFieldCreator(FIELD_NAME_BINDINGS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);

Map<InjectionPointInfo, String> injectionPointToProviderField = new HashMap<>();
Map<InterceptorInfo, String> interceptorToProviderField = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class InterceptorInfo extends BeanInfo implements Comparable<InterceptorInfo> {
*/
InterceptorInfo(AnnotationTarget target, BeanDeployment beanDeployment, Set<AnnotationInstance> bindings, List<Injection> injections, int priority) {
super(target, beanDeployment, ScopeInfo.DEPENDENT, Collections.singleton(Type.create(target.asClass().name(), Kind.CLASS)), new HashSet<>(), injections,
null, null, null, null);
null, null, null, Collections.emptyList());
this.bindings = bindings;
this.priority = priority;
this.aroundInvoke = target.asClass().methods().stream().filter(m -> m.hasAnnotation(DotNames.AROUND_INVOKE)).findAny().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -91,6 +92,8 @@ final class MethodDescriptors {
static final MethodDescriptor CREATIONAL_CTX_ADD_DEP_TO_PARENT = MethodDescriptor.ofMethod(CreationalContextImpl.class, "addDependencyToParent", void.class, InjectableBean.class,
Object.class, CreationalContext.class);

static final MethodDescriptor COLLECTIONS_UNMODIFIABLE_SET = MethodDescriptor.ofMethod(Collections.class, "unmodifiableSet", Set.class, Set.class);

private MethodDescriptors() {
}

Expand Down
6 changes: 0 additions & 6 deletions ext/arc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<exclusions>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Loading