-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: add support for custom AlterableContext implementations
Custom contexts in ArC must implement the `InjectableContext` interface, which is ArC-specific. To implement CDI Lite properly, ArC must also support custom implementations of `AlterableContext`. Fortunately, the `InjectableContext` interface adds just a few methods on top of `AlterableContext`, and none of them are critical for custom contexts to function. Therefore, this commit simply takes the user-supplied implementation of `AlterableContext` and generates a subclass that implements `InjectableContext`, where all the additional methods throw `UnsupportedOperationException`. This subclass is then registered as the custom context, instead of the original user-supplied class.
- Loading branch information
Showing
6 changed files
with
734 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...rojects/arc/processor/src/main/java/io/quarkus/arc/processor/CustomAlterableContexts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
import jakarta.enterprise.context.spi.AlterableContext; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
public class CustomAlterableContexts { | ||
private final List<CustomAlterableContextInfo> registered = new ArrayList<>(); | ||
private final Predicate<DotName> applicationClassPredicate; | ||
|
||
CustomAlterableContexts(Predicate<DotName> applicationClassPredicate) { | ||
this.applicationClassPredicate = applicationClassPredicate; | ||
} | ||
|
||
public CustomAlterableContextInfo add(Class<? extends AlterableContext> contextClass, Boolean isNormal) { | ||
String generatedName = contextClass.getName() + "_InjectableContext"; | ||
boolean isApplicationClass = applicationClassPredicate.test(DotName.createSimple(contextClass)); | ||
CustomAlterableContextInfo result = new CustomAlterableContextInfo(contextClass, isNormal, generatedName, | ||
isApplicationClass); | ||
registered.add(result); | ||
return result; | ||
} | ||
|
||
void validate(BeanDeploymentValidator.ValidationContext validationContext, boolean transformUnproxyableClasses, | ||
Consumer<BytecodeTransformer> bytecodeTransformerConsumer) { | ||
for (CustomAlterableContextInfo info : registered) { | ||
if (Modifier.isFinal(info.contextClass.getModifiers())) { | ||
if (transformUnproxyableClasses) { | ||
bytecodeTransformerConsumer.accept(new BytecodeTransformer(info.contextClass.getName(), | ||
new Beans.FinalClassTransformFunction())); | ||
} else { | ||
validationContext.addDeploymentProblem( | ||
new DeploymentException("Custom context class may not be final: " + info.contextClass)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
List<CustomAlterableContextInfo> getRegistered() { | ||
return registered; | ||
} | ||
|
||
public static class CustomAlterableContextInfo { | ||
public final Class<? extends AlterableContext> contextClass; | ||
public final Boolean isNormal; | ||
public final String generatedName; | ||
public final boolean isApplicationClass; | ||
|
||
CustomAlterableContextInfo(Class<? extends AlterableContext> contextClass, Boolean isNormal, | ||
String generatedName, boolean isApplicationClass) { | ||
this.contextClass = contextClass; | ||
this.isNormal = isNormal; | ||
this.generatedName = generatedName; | ||
this.isApplicationClass = isApplicationClass; | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...rc/processor/src/main/java/io/quarkus/arc/processor/CustomAlterableContextsGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.arc.InjectableContext; | ||
import io.quarkus.arc.processor.CustomAlterableContexts.CustomAlterableContextInfo; | ||
import io.quarkus.arc.processor.ResourceOutput.Resource; | ||
import io.quarkus.gizmo.ClassCreator; | ||
import io.quarkus.gizmo.ClassOutput; | ||
import io.quarkus.gizmo.MethodCreator; | ||
import io.quarkus.gizmo.MethodDescriptor; | ||
|
||
/** | ||
* This is an internal companion of {@link CustomAlterableContexts} that handles generating | ||
* subclasses of given context classes that implement {@code InjectableContext}. | ||
*/ | ||
class CustomAlterableContextsGenerator extends AbstractGenerator { | ||
private static final Logger LOGGER = Logger.getLogger(CustomAlterableContextsGenerator.class); | ||
|
||
CustomAlterableContextsGenerator(boolean generateSources) { | ||
super(generateSources); | ||
} | ||
|
||
/** | ||
* Creator of an {@link CustomAlterableContexts} must call this method at an appropriate point | ||
* in time and write the result to an appropriate output. If not, the bytecode sequences generated | ||
* using the result of {@code CustomAlterableContexts.add()} will refer to non-existing classes. | ||
* | ||
* @param existingClasses names of classes that already exist and should not be generated again | ||
* @return the generated classes, never {@code null} | ||
*/ | ||
Collection<Resource> generate(CustomAlterableContexts customAlterableContexts, Set<String> existingClasses) { | ||
List<Resource> resources = new ArrayList<>(); | ||
for (CustomAlterableContextInfo info : customAlterableContexts.getRegistered()) { | ||
ResourceClassOutput classOutput = new ResourceClassOutput(info.isApplicationClass, generateSources); | ||
createInjectableContextSubclass(classOutput, info, existingClasses); | ||
resources.addAll(classOutput.getResources()); | ||
} | ||
return resources; | ||
} | ||
|
||
/** | ||
* Creator of an {@link AnnotationLiteralProcessor} must call this method at an appropriate point | ||
* in time and write the result to an appropriate output. If not, the bytecode sequences generated | ||
* using the result of {@code CustomAlterableContexts.add()} will refer to non-existing classes. | ||
* | ||
* @param existingClasses names of classes that already exist and should not be generated again | ||
* @return the generated classes, never {@code null} | ||
*/ | ||
Collection<Future<Collection<Resource>>> generate(CustomAlterableContexts customAlterableContexts, | ||
Set<String> existingClasses, ExecutorService executor) { | ||
List<Future<Collection<Resource>>> futures = new ArrayList<>(); | ||
for (CustomAlterableContextInfo info : customAlterableContexts.getRegistered()) { | ||
futures.add(executor.submit(new Callable<Collection<Resource>>() { | ||
@Override | ||
public Collection<Resource> call() throws Exception { | ||
ResourceClassOutput classOutput = new ResourceClassOutput(info.isApplicationClass, generateSources); | ||
createInjectableContextSubclass(classOutput, info, existingClasses); | ||
return classOutput.getResources(); | ||
} | ||
})); | ||
} | ||
return futures; | ||
} | ||
|
||
private void createInjectableContextSubclass(ClassOutput classOutput, CustomAlterableContextInfo info, | ||
Set<String> existingClasses) { | ||
|
||
String dottedGeneratedName = info.generatedName; | ||
String generatedName = dottedGeneratedName.replace('.', '/'); | ||
if (existingClasses.contains(generatedName)) { | ||
return; | ||
} | ||
|
||
ClassCreator injectableContextSubclass = ClassCreator.builder() | ||
.classOutput(classOutput) | ||
.className(generatedName) | ||
.superClass(info.contextClass) | ||
.interfaces(InjectableContext.class) | ||
.build(); | ||
|
||
// constructor | ||
MethodCreator constructor = injectableContextSubclass.getMethodCreator(Methods.INIT, void.class); | ||
constructor.invokeSpecialMethod(MethodDescriptor.ofConstructor(info.contextClass), constructor.getThis()); | ||
constructor.returnVoid(); | ||
|
||
// implement `isNormal()` if needed | ||
if (info.isNormal != null) { | ||
MethodCreator isNormal = injectableContextSubclass.getMethodCreator("isNormal", boolean.class); | ||
isNormal.returnBoolean(info.isNormal); | ||
} | ||
|
||
// implement `destroy()` | ||
MethodCreator destroy = injectableContextSubclass.getMethodCreator("destroy", void.class); | ||
destroy.throwException(UnsupportedOperationException.class, "Custom AlterableContext cannot destroy all instances"); | ||
destroy.returnVoid(); | ||
|
||
// implement `getState()` | ||
MethodCreator getState = injectableContextSubclass.getMethodCreator("getState", InjectableContext.ContextState.class); | ||
getState.throwException(UnsupportedOperationException.class, "Custom AlterableContext has no state"); | ||
getState.returnNull(); | ||
|
||
// implement `destroy(ContextState)` | ||
MethodCreator destroyState = injectableContextSubclass.getMethodCreator("destroy", void.class, | ||
InjectableContext.ContextState.class); | ||
destroyState.throwException(UnsupportedOperationException.class, "Custom AlterableContext has no state"); | ||
destroyState.returnVoid(); | ||
|
||
injectableContextSubclass.close(); | ||
LOGGER.debugf("InjectableContext subclass generated: %s", dottedGeneratedName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.