Skip to content

Commit

Permalink
Rename InstantiationContext to Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
hjohn committed Jul 1, 2022
1 parent a4b0806 commit 95f978f
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import org.int4.dirk.api.instantiation.AmbiguousResolutionException;
import org.int4.dirk.api.instantiation.UnsatisfiedResolutionException;
import org.int4.dirk.spi.instantiation.InjectionTargetExtension;
import org.int4.dirk.spi.instantiation.InstantiationContext;
import org.int4.dirk.spi.instantiation.Instance;
import org.int4.dirk.spi.instantiation.Resolution;
import org.int4.dirk.util.TypeVariables;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

/**
Expand All @@ -19,51 +18,51 @@
*
* @param <T> the provided type
*/
public class InstanceInjectionTargetExtension<T> extends InjectionTargetExtension<Instance<T>, T> {
public class InstanceInjectionTargetExtension<T> extends InjectionTargetExtension<jakarta.enterprise.inject.Instance<T>, T> {

/**
* Constructs a new instance.
*/
public InstanceInjectionTargetExtension() {
super(TypeVariables.get(Instance.class, 0), Resolution.LAZY, DefaultInstance::new);
super(TypeVariables.get(jakarta.enterprise.inject.Instance.class, 0), Resolution.LAZY, DefaultInstance::new);
}

private static final class DefaultInstance<T> implements Instance<T> {
final InstantiationContext<T> context;
private static final class DefaultInstance<T> implements jakarta.enterprise.inject.Instance<T> {
final Instance<T> instance;

DefaultInstance(InstantiationContext<T> context) {
this.context = context;
DefaultInstance(Instance<T> instance) {
this.instance = instance;
}

@Override
public T get() {
return context.get();
return instance.get();
}

@Override
public Iterator<T> iterator() {
return context.getAll().iterator();
return instance.getAll().iterator();
}

@Override
public Instance<T> select(Annotation... qualifiers) {
return new DefaultInstance<>(context.select(qualifiers));
public jakarta.enterprise.inject.Instance<T> select(Annotation... qualifiers) {
return new DefaultInstance<>(instance.select(qualifiers));
}

@Override
public <U extends T> Instance<U> select(Class<U> subtype, Annotation... qualifiers) {
return new DefaultInstance<>(context.select(subtype, qualifiers));
public <U extends T> jakarta.enterprise.inject.Instance<U> select(Class<U> subtype, Annotation... qualifiers) {
return new DefaultInstance<>(instance.select(subtype, qualifiers));
}

@Override
public <U extends T> Instance<U> select(TypeLiteral<U> literal, Annotation... qualifiers) {
return new DefaultInstance<>(context.select(literal.getRawType(), qualifiers));
public <U extends T> jakarta.enterprise.inject.Instance<U> select(TypeLiteral<U> literal, Annotation... qualifiers) {
return new DefaultInstance<>(instance.select(literal.getRawType(), qualifiers));
}

@Override
public boolean isUnsatisfied() { // TODO this implementation is sub par; improve when extensions to InstantiationContext interface are finalized
public boolean isUnsatisfied() { // TODO this implementation is sub par; improve when extensions to Instance interface are finalized
try {
context.get();
instance.get();

return false;
}
Expand All @@ -76,9 +75,9 @@ public <U extends T> Instance<U> select(TypeLiteral<U> literal, Annotation... qu
}

@Override
public boolean isAmbiguous() { // TODO this implementation is sub par; improve when extensions to InstantiationContext interface are finalized
public boolean isAmbiguous() { // TODO this implementation is sub par; improve when extensions to Instance interface are finalized
try {
context.get();
instance.get();

return false;
}
Expand All @@ -91,9 +90,9 @@ public <U extends T> Instance<U> select(TypeLiteral<U> literal, Annotation... qu
}

@Override
public boolean isResolvable() { // TODO this implementation is sub par; improve when extensions to InstantiationContext interface are finalized
public boolean isResolvable() { // TODO this implementation is sub par; improve when extensions to Instance interface are finalized
try {
context.get();
instance.get();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
class DefaultInjectableFactory implements InjectableFactory {
private final ScopeResolverManager scopeResolverManager;
private final InstantiationContextFactory instantiationContextFactory;
private final InstanceFactory instanceFactory;
private final AnnotationStrategy annotationStrategy;
private final ScopeStrategy scopeStrategy;
private final Set<Class<?>> extendedTypes;
Expand All @@ -47,14 +47,14 @@ class DefaultInjectableFactory implements InjectableFactory {
* Constructs a new instance.
*
* @param scopeResolverManager a {@link ScopeResolverManager}, cannot be {@code null}
* @param instantiationContextFactory an {@link InstantiationContextFactory}, cannot be {@code null}
* @param instanceFactory an {@link InstanceFactory}, cannot be {@code null}
* @param annotationStrategy a {@link AnnotationStrategy}, cannot be {@code null}
* @param scopeStrategy a {@link ScopeStrategy}, cannot be {@code null}
* @param extendedTypes a set of {@link Class} for which injection target extensions are in use, cannot be {@code null} or contain {@code null} but can be empty
*/
DefaultInjectableFactory(ScopeResolverManager scopeResolverManager, InstantiationContextFactory instantiationContextFactory, AnnotationStrategy annotationStrategy, ScopeStrategy scopeStrategy, Set<Class<?>> extendedTypes) {
DefaultInjectableFactory(ScopeResolverManager scopeResolverManager, InstanceFactory instanceFactory, AnnotationStrategy annotationStrategy, ScopeStrategy scopeStrategy, Set<Class<?>> extendedTypes) {
this.scopeResolverManager = Objects.requireNonNull(scopeResolverManager, "scopeResolverManager");
this.instantiationContextFactory = Objects.requireNonNull(instantiationContextFactory, "instantiationContextFactory");
this.instanceFactory = Objects.requireNonNull(instanceFactory, "instanceFactory");
this.annotationStrategy = Objects.requireNonNull(annotationStrategy, "annotationStrategy");
this.scopeStrategy = Objects.requireNonNull(scopeStrategy, "scopeStrategy");
this.extendedTypes = Objects.requireNonNull(extendedTypes, "extendedTypes");
Expand Down Expand Up @@ -116,7 +116,7 @@ public <T> Injectable<T> create(Type ownerType, Member member, AnnotatedElement
}

private InjectionTarget toInjectionTarget(Binding binding, ScopeResolver scopeResolver) {
Instantiator<?> instantiator = instantiationContextFactory.createInstantiator(new Key(binding.getType(), binding.getQualifiers()), binding.isOptional(), scopeResolver.getAnnotation());
Instantiator<?> instantiator = instanceFactory.createInstantiator(new Key(binding.getType(), binding.getQualifiers()), binding.isOptional(), scopeResolver.getAnnotation());

return new InjectionTarget() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
*/
class DefaultInstanceResolver implements InstanceResolver {
private final Resolver<Injectable<?>> resolver;
private final InstantiationContextFactory instantiationContextFactory;
private final InstanceFactory instanceFactory;

/**
* Constructs a new instance.
*
* @param instantiationContextFactory an {@link InstantiationContextFactory}, cannot be {@code null}
* @param instanceFactory an {@link InstanceFactory}, cannot be {@code null}
*/
DefaultInstanceResolver(Resolver<Injectable<?>> resolver, InstantiationContextFactory instantiationContextFactory) {
DefaultInstanceResolver(Resolver<Injectable<?>> resolver, InstanceFactory instanceFactory) {
this.resolver = resolver;
this.instantiationContextFactory = instantiationContextFactory;
this.instanceFactory = instanceFactory;
}

@Override
Expand All @@ -50,10 +50,10 @@ public <T> List<T> getInstances(Class<T> cls, Object... qualifiers) throws Creat
}

private <T> T getInstance(Key key) throws UnsatisfiedResolutionException, AmbiguousResolutionException, CreationException, ScopeNotActiveException {
return instantiationContextFactory.<T>createContext(resolver, key, false).get();
return instanceFactory.<T>createInstance(resolver, key, false).get();
}

private <T> List<T> getInstances(Key key) throws CreationException {
return instantiationContextFactory.<T>createContext(resolver, key, false).getAll();
return instanceFactory.<T>createInstance(resolver, key, false).getAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.int4.dirk.api.instantiation.UnsatisfiedResolutionException;
import org.int4.dirk.api.scope.ScopeException;
import org.int4.dirk.api.scope.ScopeNotActiveException;
import org.int4.dirk.core.RootInstantiationContextFactory.RootInstantiationContext;
import org.int4.dirk.core.RootInstanceFactory.RootInstance;
import org.int4.dirk.core.definition.Binding;
import org.int4.dirk.core.definition.ExtendedScopeResolver;
import org.int4.dirk.core.definition.Injectable;
Expand All @@ -30,25 +30,25 @@
import org.int4.dirk.spi.config.AnnotationStrategy;
import org.int4.dirk.spi.config.ProxyStrategy;
import org.int4.dirk.spi.instantiation.InjectionTargetExtension;
import org.int4.dirk.spi.instantiation.Instance;
import org.int4.dirk.spi.instantiation.InstanceProvider;
import org.int4.dirk.spi.instantiation.InstantiationContext;
import org.int4.dirk.spi.instantiation.Resolution;
import org.int4.dirk.spi.scope.CreationalContext;
import org.int4.dirk.spi.scope.ScopeResolver;
import org.int4.dirk.util.Types;

/**
* Factory for {@link InstantiationContext}s.
* Factory for {@link Instance}s.
*/
class InstantiationContextFactory {
private static final Logger LOGGER = Logger.getLogger(InstantiationContextFactory.class.getName());
class InstanceFactory {
private static final Logger LOGGER = Logger.getLogger(InstanceFactory.class.getName());
private static final ExtendedCreationalContext<?> NULL_CONTEXT = new FixedCreationalContext<>(null);

private static boolean strictOrder;

private final ProxyStrategy proxyStrategy;
private final InjectionTargetExtensionStore injectionTargetExtensionStore;
private final RootInstantiationContextFactory rootInstantiationContextFactory;
private final RootInstanceFactory rootInstanceFactory;
private final ThreadLocal<Deque<ExtendedCreationalContext<?>>> stack = ThreadLocal.withInitial(ArrayDeque::new);

/**
Expand All @@ -58,14 +58,14 @@ class InstantiationContextFactory {
* @param proxyStrategy a {@link ProxyStrategy}, cannot be {@code null}
* @param injectionTargetExtensionStore an {@link InjectionTargetExtensionStore}, cannot be {@code null}
*/
InstantiationContextFactory(AnnotationStrategy annotationStrategy, ProxyStrategy proxyStrategy, InjectionTargetExtensionStore injectionTargetExtensionStore) {
InstanceFactory(AnnotationStrategy annotationStrategy, ProxyStrategy proxyStrategy, InjectionTargetExtensionStore injectionTargetExtensionStore) {
this.proxyStrategy = Objects.requireNonNull(proxyStrategy, "proxyStrategy");
this.injectionTargetExtensionStore = Objects.requireNonNull(injectionTargetExtensionStore, "injectionTargetExtensionStore");
this.rootInstantiationContextFactory = new RootInstantiationContextFactory(annotationStrategy);
this.rootInstanceFactory = new RootInstanceFactory(annotationStrategy);
}

<T> InstantiationContext<T> createContext(Resolver<Injectable<?>> resolver, Key key, boolean optional) {
return rootInstantiationContextFactory.create(resolver, createInstantiatorInternal(key, optional, null));
<T> Instance<T> createInstance(Resolver<Injectable<?>> resolver, Key key, boolean optional) {
return rootInstanceFactory.create(resolver, createInstantiatorInternal(key, optional, null));
}

<T> Instantiator<T> createInstantiator(Key key, boolean optional, Annotation parentScope) {
Expand Down Expand Up @@ -156,13 +156,13 @@ public ExtendedCreationalContext<T> create(Resolver<Injectable<?>> resolver) thr
return castContext;
}

RootInstantiationContext<E, ?> instantiationContext = rootInstantiationContextFactory.create(resolver, elementInstantiator);
InjectionTargetExtensionCreationalContext<T, E> creationalContext = new InjectionTargetExtensionCreationalContext<>(stack.get().isEmpty() ? null : stack.get().getLast(), resolution == Resolution.LAZY ? instantiationContext : null);
RootInstance<E, ?> instance = rootInstanceFactory.create(resolver, elementInstantiator);
InjectionTargetExtensionCreationalContext<T, E> creationalContext = new InjectionTargetExtensionCreationalContext<>(stack.get().isEmpty() ? null : stack.get().getLast(), resolution == Resolution.LAZY ? instance : null);

open(resolution == Resolution.LAZY ? NULL_CONTEXT : creationalContext);

try {
creationalContext.initialize(instanceProvider.getInstance(instantiationContext), resolution == Resolution.LAZY);
creationalContext.initialize(instanceProvider.getInstance(instance), resolution == Resolution.LAZY);

return creationalContext;
}
Expand Down Expand Up @@ -350,16 +350,16 @@ public void attach(ExtendedCreationalContext<?> creationalContext) {

private static final class InjectionTargetExtensionCreationalContext<T, E> implements ExtendedCreationalContext<T> {
private final ExtendedCreationalContext<?> parent;
private final RootInstantiationContext<E, ?> instantiationContext;
private final RootInstance<E, ?> rootInstance;

private T instance;
private boolean needsDestroy;
private boolean initialized;
private List<CreationalContext<?>> children;

InjectionTargetExtensionCreationalContext(ExtendedCreationalContext<?> parent, RootInstantiationContext<E, ?> context) {
InjectionTargetExtensionCreationalContext(ExtendedCreationalContext<?> parent, RootInstance<E, ?> rootInstance) {
this.parent = parent;
this.instantiationContext = context;
this.rootInstance = rootInstance;
}

void initialize(T instance, boolean needsDestroy) {
Expand Down Expand Up @@ -395,8 +395,8 @@ public final void attach(ExtendedCreationalContext<?> child) {

@Override
public void release() {
if(instantiationContext != null) {
instantiationContext.release();
if(rootInstance != null) {
rootInstance.release();
}

if(children != null) {
Expand Down Expand Up @@ -443,7 +443,7 @@ public boolean needsDestroy() {
*
* <p>A {@code CreationalContext} is created whenever an {@link Injectable} is obtained from
* a {@link ScopeResolver}. If the resolver decides to create a new instance, it will
* call recursively into a {@link InstantiationContext}.
* call recursively into a {@link Instance}.
*/
private class LazyCreationalContext<T> implements ExtendedCreationalContext<T> {
private final ExtendedCreationalContext<?> parent;
Expand Down
Loading

0 comments on commit 95f978f

Please sign in to comment.