Skip to content

Commit

Permalink
Merge pull request apache#392 from JCgH4164838Gh792C124B5/WW-5050_Fol…
Browse files Browse the repository at this point in the history
…lowup

Minor follow-up to WW-5050 changes to ContainerImpl
  • Loading branch information
lukaszlenart authored Jan 11, 2020
2 parents 59e7caf + ee583c2 commit 642b945
Showing 1 changed file with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ContainerImpl implements Container {

ContainerImpl(Map<Key<?>, InternalFactory<?>> factories) {
this.factories = factories;
Map<Class<?>, Set<String>> map = new HashMap<>();
final Map<Class<?>, Set<String>> map = new HashMap<>();
for (Key<?> key : factories.keySet()) {
Set<String> names = map.get(key.getType());
if (names == null) {
Expand Down Expand Up @@ -151,13 +151,25 @@ Injector create(ContainerImpl container, M member, String name)
throws MissingDependencyException;
}

private boolean isStatic(Member member) {
/**
* Determines if a given {@link Member} is static or not.
*
* @param member checked for the static modifier.
* @return true if member is static, false otherwise.
*/
private static boolean isStatic(Member member) {
return Modifier.isStatic(member.getModifiers());
}

private static boolean isNotPublic(Member member) {
return !Modifier.isPublic(member.getModifiers()) ||
!Modifier.isPublic(member.getDeclaringClass().getModifiers());
/**
* Determines if a given {@link Member} is considered to be public for reflection usage or not.
*
* @param member checked to see if it is public for reflection usage.
* @return true if member is public for reflection usage, false otherwise.
*/
private static boolean isPublicForReflection(Member member) {
return Modifier.isPublic(member.getModifiers()) &&
Modifier.isPublic(member.getDeclaringClass().getModifiers());
}

static class FieldInjector implements Injector {
Expand All @@ -169,7 +181,7 @@ static class FieldInjector implements Injector {
public FieldInjector(ContainerImpl container, Field field, String name)
throws MissingDependencyException {
this.field = field;
if (isNotPublic(field) && !field.isAccessible()) {
if (!isPublicForReflection(field) && !field.isAccessible()) {
SecurityManager sm = System.getSecurityManager();
try {
if (sm != null) {
Expand All @@ -182,7 +194,7 @@ public FieldInjector(ContainerImpl container, Field field, String name)
}
}

Key<?> key = Key.newInstance(field.getType(), name);
final Key<?> key = Key.newInstance(field.getType(), name);
factory = container.getFactory(key);
if (factory == null) {
throw new MissingDependencyException("No mapping found for dependency " + key + " in " + field + ".");
Expand All @@ -191,8 +203,9 @@ public FieldInjector(ContainerImpl container, Field field, String name)
this.externalContext = ExternalContext.newInstance(field, key, container);
}

@Override
public void inject(InternalContext context, Object o) {
ExternalContext<?> previous = context.getExternalContext();
final ExternalContext<?> previous = context.getExternalContext();
context.setExternalContext(externalContext);
try {
field.set(o, factory.create(context));
Expand All @@ -214,9 +227,9 @@ public void inject(InternalContext context, Object o) {
*/
<M extends AccessibleObject & Member> ParameterInjector<?>[]
getParametersInjectors(M member, Annotation[][] annotations, Class[] parameterTypes, String defaultName) throws MissingDependencyException {
List<ParameterInjector<?>> parameterInjectors = new ArrayList<>();
final List<ParameterInjector<?>> parameterInjectors = new ArrayList<>();

Iterator<Annotation[]> annotationsIterator = Arrays.asList(annotations).iterator();
final Iterator<Annotation[]> annotationsIterator = Arrays.asList(annotations).iterator();
for (Class<?> parameterType : parameterTypes) {
Inject annotation = findInject(annotationsIterator.next());
String name = annotation == null ? defaultName : annotation.value();
Expand All @@ -228,12 +241,12 @@ public void inject(InternalContext context, Object o) {
}

<T> ParameterInjector<T> createParameterInjector(Key<T> key, Member member) throws MissingDependencyException {
InternalFactory<? extends T> factory = getFactory(key);
final InternalFactory<? extends T> factory = getFactory(key);
if (factory == null) {
throw new MissingDependencyException("No mapping found for dependency " + key + " in " + member + ".");
}

ExternalContext<T> externalContext = ExternalContext.newInstance(member, key, this);
final ExternalContext<T> externalContext = ExternalContext.newInstance(member, key, this);
return new ParameterInjector<T>(externalContext, factory);
}

Expand Down Expand Up @@ -261,8 +274,8 @@ static class MethodInjector implements Injector {

public MethodInjector(ContainerImpl container, Method method, String name) throws MissingDependencyException {
this.method = method;
if (isNotPublic(method) && !method.isAccessible()) {
SecurityManager sm = System.getSecurityManager();
if (!isPublicForReflection(method) && !method.isAccessible()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (sm != null) {
sm.checkPermission(new ReflectPermission("suppressAccessChecks"));
Expand All @@ -274,14 +287,15 @@ public MethodInjector(ContainerImpl container, Method method, String name) throw
}
}

Class<?>[] parameterTypes = method.getParameterTypes();
final Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 0) {
throw new DependencyException(method + " has no parameters to inject.");
}
parameterInjectors = container.getParametersInjectors(
method, method.getParameterAnnotations(), parameterTypes, name);
}

@Override
public void inject(InternalContext context, Object o) {
try {
method.invoke(o, getParameters(method, context, parameterInjectors));
Expand Down Expand Up @@ -311,8 +325,8 @@ static class ConstructorInjector<T> {
this.implementation = implementation;

constructor = findConstructorIn(implementation);
if (isNotPublic(constructor) && !constructor.isAccessible()) {
SecurityManager sm = System.getSecurityManager();
if (!isPublicForReflection(constructor) && !constructor.isAccessible()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (sm != null) {
sm.checkPermission(new ReflectPermission("suppressAccessChecks"));
Expand Down Expand Up @@ -359,7 +373,7 @@ ParameterInjector<?>[] constructParameterInjector(
@SuppressWarnings("unchecked")
private Constructor<T> findConstructorIn(Class<T> implementation) {
Constructor<T> found = null;
Constructor<T>[] declaredConstructors = (Constructor<T>[]) implementation.getDeclaredConstructors();
final Constructor<T>[] declaredConstructors = (Constructor<T>[]) implementation.getDeclaredConstructors();
for (Constructor<T> constructor : declaredConstructors) {
if (constructor.getAnnotation(Inject.class) != null) {
if (found != null) {
Expand All @@ -386,7 +400,7 @@ private Constructor<T> findConstructorIn(Class<T> implementation) {
* Construct an instance. Returns {@code Object} instead of {@code T} because it may return a proxy.
*/
Object construct(InternalContext context, Class<? super T> expectedType) {
ConstructionContext<T> constructionContext = context.getConstructionContext(this);
final ConstructionContext<T> constructionContext = context.getConstructionContext(this);

// We have a circular reference between constructors. Return a proxy.
if (constructionContext.isConstructing()) {
Expand All @@ -406,7 +420,7 @@ Object construct(InternalContext context, Class<? super T> expectedType) {
// First time through...
constructionContext.startConstruction();
try {
Object[] parameters = getParameters(constructor, context, parameterInjectors);
final Object[] parameters = getParameters(constructor, context, parameterInjectors);
t = constructor.newInstance(parameters);
constructionContext.setProxyDelegates(t);
} finally {
Expand Down Expand Up @@ -442,7 +456,7 @@ public ParameterInjector(ExternalContext<T> externalContext, InternalFactory<? e
}

T inject(Member member, InternalContext context) {
ExternalContext<?> previous = context.getExternalContext();
final ExternalContext<?> previous = context.getExternalContext();
context.setExternalContext(externalContext);
try {
return factory.create(context);
Expand All @@ -457,23 +471,23 @@ private static Object[] getParameters(Member member, InternalContext context, Pa
return null;
}

Object[] parameters = new Object[parameterInjectors.length];
final Object[] parameters = new Object[parameterInjectors.length];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = parameterInjectors[i].inject(member, context);
}
return parameters;
}

void inject(Object o, InternalContext context) {
List<Injector> injectors = this.injectors.get(o.getClass());
final List<Injector> injectors = this.injectors.get(o.getClass());
for (Injector injector : injectors) {
injector.inject(context, o);
}
}

<T> T inject(Class<T> implementation, InternalContext context) {
try {
ConstructorInjector<T> constructor = getConstructor(implementation);
final ConstructorInjector<T> constructor = getConstructor(implementation);
return implementation.cast(constructor.construct(context, implementation));
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -482,11 +496,11 @@ <T> T inject(Class<T> implementation, InternalContext context) {

@SuppressWarnings("unchecked")
<T> T getInstance(Class<T> type, String name, InternalContext context) {
ExternalContext<?> previous = context.getExternalContext();
Key<T> key = Key.newInstance(type, name);
final ExternalContext<?> previous = context.getExternalContext();
final Key<T> key = Key.newInstance(type, name);
context.setExternalContext(ExternalContext.newInstance(null, key, this));
try {
InternalFactory o = getFactory(key);
final InternalFactory o = getFactory(key);
if (o != null) {
return getFactory(key).create(context);
} else {
Expand All @@ -501,6 +515,7 @@ <T> T getInstance(Class<T> type, InternalContext context) {
return getInstance(type, DEFAULT_NAME, context);
}

@Override
public void inject(final Object o) {
callInContext(new ContextualCallable<Void>() {
public Void call(InternalContext context) {
Expand All @@ -510,6 +525,7 @@ public Void call(InternalContext context) {
});
}

@Override
public <T> T inject(final Class<T> implementation) {
return callInContext(new ContextualCallable<T>() {
public T call(InternalContext context) {
Expand All @@ -518,6 +534,7 @@ public T call(InternalContext context) {
});
}

@Override
public <T> T getInstance(final Class<T> type, final String name) {
return callInContext(new ContextualCallable<T>() {
public T call(InternalContext context) {
Expand All @@ -526,6 +543,7 @@ public T call(InternalContext context) {
});
}

@Override
public <T> T getInstance(final Class<T> type) {
return callInContext(new ContextualCallable<T>() {
public T call(InternalContext context) {
Expand All @@ -534,6 +552,7 @@ public T call(InternalContext context) {
});
}

@Override
public Set<String> getInstanceNames(final Class<?> type) {
Set<String> names = factoryNamesByType.get(type);
if (names == null) {
Expand All @@ -553,7 +572,7 @@ protected Object[] initialValue() {
* Looks up thread local context. Creates (and removes) a new context if necessary.
*/
<T> T callInContext(ContextualCallable<T> callable) {
Object[] reference = localContext.get();
final Object[] reference = localContext.get();
if (reference[0] == null) {
reference[0] = new InternalContext(this);
try {
Expand Down Expand Up @@ -584,10 +603,12 @@ <T> ConstructorInjector<T> getConstructor(Class<T> implementation) {

final ThreadLocal<Object> localScopeStrategy = new ThreadLocal<>();

@Override
public void setScopeStrategy(Scope.Strategy scopeStrategy) {
this.localScopeStrategy.set(scopeStrategy);
}

@Override
public void removeScopeStrategy() {
this.localScopeStrategy.remove();
}
Expand Down

0 comments on commit 642b945

Please sign in to comment.