Skip to content

Commit

Permalink
Merge pull request #48 from mkouba/resource-producer-field
Browse files Browse the repository at this point in the history
Arc - resource provider SPI
  • Loading branch information
stuartwdouglas authored Oct 9, 2018
2 parents 42891ba + 36bb218 commit ebdf33a
Show file tree
Hide file tree
Showing 34 changed files with 1,282 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
builder.setIndex(index);
builder.setAdditionalBeanDefiningAnnotations(additionalBeanDefiningAnnotations);
builder.setSharedAnnotationLiterals(false);
builder.addResourceAnnotations(beanDeployment.getResourceAnnotations());
builder.setReflectionRegistration(new ReflectionRegistration() {
@Override
public void registerMethod(MethodInfo methodInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,64 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;

public class BeanDeployment {

private final List<String> additionalBeans = new ArrayList<>();

private final Map<String, byte[]> generatedBeans = new HashMap<>();

// Lite profile
private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers = new ArrayList<>();

private final List<DotName> resourceAnnotations = new ArrayList<>();

// Full profile
private final List<String> extensions = new ArrayList<>();

public void addAdditionalBean(Class<?>... beanClass) {
additionalBeans.addAll(Arrays.stream(beanClass).map(Class::getName).collect(Collectors.toList()));
}

public void addAdditionalBean(String... beanClass) {
additionalBeans.addAll(Arrays.stream(beanClass).collect(Collectors.toList()));
}

public void addGeneratedBean(String name, byte[] bean) {
generatedBeans.put(name, bean);
}

public void addAnnotationTransformer(BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>> transformer) {
annotationTransformers.add(transformer);
}

public void addExtension(String extensionClass) {
extensions.add(extensionClass);
}


public void addResourceAnnotation(DotName resourceAnnotation) {
resourceAnnotations.add(resourceAnnotation);
}

public List<String> getAdditionalBeans() {
return additionalBeans;
}

public Map<String, byte[]> getGeneratedBeans() {
return generatedBeans;
}

public List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> getAnnotationTransformers() {
return annotationTransformers;
}

public List<String> getExtensions() {
return extensions;
}


public List<DotName> getResourceAnnotations() {
return resourceAnnotations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public final class Capabilities {

public static final String CDI_WELD = "org.jboss.shamrock.cdi.weld";
public static final String CDI_ARC = "org.jboss.shamrock.cdi.arc";
public static final String TRANSACTIONS = "org.jboss.shamrock.transactions";

private Capabilities() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.function.Function;

import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.protean.gizmo.MethodCreator;
import org.jboss.shamrock.deployment.codegen.BytecodeRecorder;
import org.objectweb.asm.ClassVisitor;

Expand Down Expand Up @@ -120,7 +118,8 @@ public interface ProcessorContext {
/**
*
* @param capability
* @return
* @return if the given capability is present
* @see Capabilities
*/
boolean isCapabilityPresent(String capability);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
Expand All @@ -24,28 +24,19 @@
@WebServlet(name = "JPATestBootstrapEndpoint", urlPatterns = "/jpa/testfunctionality")
public class JPAFunctionalityTestEndpoint extends HttpServlet {

@PersistenceUnit(unitName = "templatePU")
EntityManagerFactory entityManagerFactory;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
bootAndDoStuff();
doStuffWithHibernate(entityManagerFactory);
} catch (Exception e) {
reportException("Oops, shit happened, No boot for you!", e, resp);
}
resp.getWriter().write("OK");
}

public void bootAndDoStuff() throws Exception {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("templatePU");
try {
System.out.println("Hibernate EntityManagerFactory: booted");
doStuffWithHibernate(entityManagerFactory);
}
finally {
entityManagerFactory.close();
System.out.println("Hibernate EntityManagerFactory: shut down");
}
}

/**
* Lists the various operations we want to test for:
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.jboss.shamrock.example.jpa;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

@Dependent
public class JpaProducer {

@Produces
Expand Down
8 changes: 8 additions & 0 deletions ext/arc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<version.jboss-logging>3.3.2.Final</version.jboss-logging>
<version.javax-annotation>1.3</version.javax-annotation>
<version.gizmo>1.0.0.Alpha1-SNAPSHOT</version.gizmo>
<version.jpa>2.2</version.jpa>
</properties>

<modules>
Expand Down Expand Up @@ -118,6 +119,13 @@
<!-- scope>provided</scope -->
</dependency>

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${version.jpa}</version>
<scope>test</scope>
</dependency>

</dependencies>

</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -55,9 +56,18 @@ public class BeanDeployment {

private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers;

private final Set<DotName> resourceAnnotations;

BeanDeployment(IndexView index, Collection<DotName> additionalBeanDefiningAnnotations,
List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers) {
this(index, additionalBeanDefiningAnnotations, annotationTransformers, Collections.emptyList());
}

BeanDeployment(IndexView index, Collection<DotName> additionalBeanDefiningAnnotations,
List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers,
Collection<DotName> resourceAnnotations) {
long start = System.currentTimeMillis();
this.resourceAnnotations = new HashSet<>(resourceAnnotations);
this.index = index;
this.qualifiers = findQualifiers(index);
// TODO interceptor bindings are transitive!!!
Expand Down Expand Up @@ -109,6 +119,10 @@ StereotypeInfo getStereotype(DotName name) {
return stereotypes.get(name);
}

Set<DotName> getResourceAnnotations() {
return resourceAnnotations;
}

Collection<AnnotationInstance> getAnnotations(AnnotationTarget target) {
Collection<AnnotationInstance> annotations = null;
switch (target.kind()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ public static Builder builder() {

private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers;

private final Collection<DotName> resourceAnnotations;

private BeanProcessor(String name, IndexView index, Collection<DotName> additionalBeanDefiningAnnotations, ResourceOutput output,
boolean sharedAnnotationLiterals, ReflectionRegistration reflectionRegistration,
List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers) {
List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers,
Collection<DotName> resourceAnnotations) {
this.reflectionRegistration = reflectionRegistration;
Objects.requireNonNull(output);
this.name = name;
Expand All @@ -69,11 +72,13 @@ private BeanProcessor(String name, IndexView index, Collection<DotName> addition
this.output = output;
this.sharedAnnotationLiterals = sharedAnnotationLiterals;
this.annotationTransformers = annotationTransformers;
this.resourceAnnotations = resourceAnnotations;
}

public BeanDeployment process() throws IOException {

BeanDeployment beanDeployment = new BeanDeployment(new IndexWrapper(index), additionalBeanDefiningAnnotations, annotationTransformers);
BeanDeployment beanDeployment = new BeanDeployment(new IndexWrapper(index), additionalBeanDefiningAnnotations, annotationTransformers,
resourceAnnotations);
beanDeployment.init();

AnnotationLiteralProcessor annotationLiterals = new AnnotationLiteralProcessor(name, sharedAnnotationLiterals);
Expand Down Expand Up @@ -182,6 +187,8 @@ public static class Builder {

private final List<BiFunction<AnnotationTarget, Collection<AnnotationInstance>, Collection<AnnotationInstance>>> annotationTransformers = new ArrayList<>();

private final List<DotName> resourceAnnotations = new ArrayList<>();

public Builder setName(String name) {
this.name = name;
return this;
Expand Down Expand Up @@ -217,9 +224,14 @@ public Builder addAnnotationTransformer(BiFunction<AnnotationTarget, Collection<
return this;
}

public Builder addResourceAnnotations(Collection<DotName> resourceAnnotations) {
this.resourceAnnotations.addAll(resourceAnnotations);
return this;
}

public BeanProcessor build() {
return new BeanProcessor(name, addBuiltinClasses(index), additionalBeanDefiningAnnotations, output, sharedAnnotationLiterals,
reflectionRegistration, annotationTransformers);
reflectionRegistration, annotationTransformers, resourceAnnotations);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
Expand All @@ -12,6 +13,8 @@
import org.jboss.protean.arc.InjectableReferenceProvider;
import org.jboss.protean.arc.InjectionPointProvider;
import org.jboss.protean.arc.InstanceProvider;
import org.jboss.protean.arc.ResourceProvider;
import org.jboss.protean.arc.processor.InjectionPointInfo.Kind;
import org.jboss.protean.gizmo.ClassCreator;
import org.jboss.protean.gizmo.ClassOutput;
import org.jboss.protean.gizmo.FieldDescriptor;
Expand Down Expand Up @@ -101,27 +104,61 @@ void generate(ClassOutput classOutput, BeanDeployment beanDeployment, InjectionP
}
}
ResultHandle parameterizedType = Types.getTypeHandle(constructor, injectionPoint.requiredType);
ResultHandle eventProvider = constructor.newInstance(
MethodDescriptor.ofConstructor(EventProvider.class, java.lang.reflect.Type.class, Set.class), parameterizedType, qualifiers);
ResultHandle eventProvider = constructor.newInstance(MethodDescriptor.ofConstructor(EventProvider.class, java.lang.reflect.Type.class, Set.class),
parameterizedType, qualifiers);
constructor.writeInstanceField(FieldDescriptor.of(clazzCreator.getClassName(), providerName, InjectableReferenceProvider.class.getName()),
constructor.getThis(), eventProvider);
}
});
}), RESOURCE(DotNames.OBJECT, new Generator() {
@Override
void generate(ClassOutput classOutput, BeanDeployment beanDeployment, InjectionPointInfo injectionPoint, ClassCreator clazzCreator,
MethodCreator constructor, String providerName, AnnotationLiteralProcessor annotationLiterals) {

ResultHandle annotations = constructor.newInstance(MethodDescriptor.ofConstructor(HashSet.class));
// For a resource field the requiredQualifiers field contains all annotations declared on the field
if (!injectionPoint.requiredQualifiers.isEmpty()) {
for (AnnotationInstance annotation : injectionPoint.requiredQualifiers) {
// Create annotation literal first
ClassInfo annotationClass = beanDeployment.getIndex().getClassByName(annotation.name());
String annotationLiteralName = annotationLiterals.process(classOutput, annotationClass, annotation,
Types.getPackageName(clazzCreator.getClassName()));
constructor.invokeInterfaceMethod(MethodDescriptors.SET_ADD, annotations,
constructor.newInstance(MethodDescriptor.ofConstructor(annotationLiteralName)));
}
}
ResultHandle parameterizedType = Types.getTypeHandle(constructor, injectionPoint.requiredType);
ResultHandle resourceProvider = constructor.newInstance(
MethodDescriptor.ofConstructor(ResourceProvider.class, java.lang.reflect.Type.class, Set.class), parameterizedType, annotations);
constructor.writeInstanceField(FieldDescriptor.of(clazzCreator.getClassName(), providerName, InjectableReferenceProvider.class.getName()),
constructor.getThis(), resourceProvider);
}
}, ip -> ip.kind == Kind.RESOURCE);

private final DotName rawTypeDotName;

private final Generator generator;

private final Predicate<InjectionPointInfo> matcher;

BuiltinBean(DotName rawTypeDotName, Generator generator) {
this(rawTypeDotName, generator, ip -> ip.kind == Kind.CDI && rawTypeDotName.equals(ip.requiredType.name()));
}

BuiltinBean(DotName rawTypeDotName, Generator generator, Predicate<InjectionPointInfo> matcher) {
this.rawTypeDotName = rawTypeDotName;
this.generator = generator;
this.matcher = matcher;
}

boolean matches(InjectionPointInfo injectionPoint) {
return matcher.test(injectionPoint);
}

public DotName getRawTypeDotName() {
DotName getRawTypeDotName() {
return rawTypeDotName;
}

public Generator getGenerator() {
Generator getGenerator() {
return generator;
}

Expand All @@ -131,7 +168,7 @@ static boolean resolvesTo(InjectionPointInfo injectionPoint) {

static BuiltinBean resolve(InjectionPointInfo injectionPoint) {
for (BuiltinBean bean : values()) {
if (bean.getRawTypeDotName().equals(injectionPoint.requiredType.name())) {
if (bean.matches(injectionPoint)) {
return bean;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static String simpleName(DotName dotName) {
static String packageName(DotName dotName) {
String name = dotName.toString();
int index = name.lastIndexOf('.');
if(index == -1) {
if (index == -1) {
return "";
}
return name.substring(0, index);
Expand Down
Loading

0 comments on commit ebdf33a

Please sign in to comment.