Skip to content

Commit 122b92c

Browse files
committed
Polish code
1 parent acdb112 commit 122b92c

File tree

19 files changed

+303
-61
lines changed

19 files changed

+303
-61
lines changed

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/GenericBean.java

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import javax.enterprise.context.spi.CreationalContext;
2525
import javax.enterprise.inject.CreationException;
2626
import javax.enterprise.inject.spi.*;
27+
import java.lang.annotation.Annotation;
2728
import java.lang.reflect.Constructor;
2829
import java.util.LinkedList;
2930
import java.util.List;

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/ManagedBean.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
*/
1717
package org.geektimes.enterprise.inject.standard.beans;
1818

19-
import org.geektimes.enterprise.inject.standard.ConstructorParameterInjectionPoint;
2019
import org.geektimes.enterprise.inject.standard.annotation.ReflectiveAnnotatedType;
2120
import org.geektimes.enterprise.inject.standard.beans.producer.ProducerFieldBean;
2221
import org.geektimes.enterprise.inject.standard.beans.producer.ProducerMethodBean;
2322
import org.geektimes.enterprise.inject.util.Beans;
2423

24+
import javax.enterprise.context.NormalScope;
2525
import javax.enterprise.context.spi.CreationalContext;
26-
import javax.enterprise.inject.CreationException;
27-
import javax.enterprise.inject.spi.*;
28-
import java.lang.reflect.Constructor;
26+
import javax.enterprise.inject.spi.Annotated;
27+
import javax.enterprise.inject.spi.AnnotatedType;
28+
import javax.enterprise.inject.spi.Bean;
29+
import javax.enterprise.inject.spi.BeanManager;
30+
import java.lang.annotation.Annotation;
2931
import java.lang.reflect.Type;
3032
import java.util.LinkedHashSet;
31-
import java.util.List;
32-
import java.util.Map;
3333
import java.util.Set;
3434

3535
import static java.util.Collections.unmodifiableSet;
@@ -61,6 +61,12 @@ protected ManagedBean(Class<?> beanClass, BeanManager beanManager) {
6161
this(new ReflectiveAnnotatedType<>(beanClass), beanManager);
6262
}
6363

64+
@Override
65+
public Class<? extends Annotation> getScope() {
66+
Class<? extends Annotation> scope = super.getScope();
67+
return scope == null ? NormalScope.class : scope;
68+
}
69+
6470
@Override
6571
protected void validate(Class<T> beanClass) {
6672
validateManagedBeanType(beanClass);
@@ -93,15 +99,13 @@ public void destroy(T instance, CreationalContext<T> creationalContext) {
9399
public Set<ProducerMethodBean> getProducerMethodBeans() {
94100
if (producerMethodBeans == null) {
95101
producerMethodBeans = resolveProducerMethodBeans(this);
96-
97102
}
98103
return producerMethodBeans;
99104
}
100105

101106
public Set<ProducerFieldBean> getProducerFieldBeans() {
102107
if (producerFieldBeans == null) {
103108
producerFieldBeans = resolveProducerFieldBeans(this);
104-
105109
}
106110
return producerFieldBeans;
107111
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/decorator/DecoratorBean.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.geektimes.enterprise.inject.standard.beans.ManagedBean;
2020

21+
import javax.enterprise.context.Dependent;
2122
import javax.enterprise.context.spi.CreationalContext;
2223
import javax.enterprise.inject.spi.AnnotatedType;
2324
import javax.enterprise.inject.spi.Bean;
@@ -27,6 +28,8 @@
2728
import java.lang.reflect.Type;
2829
import java.util.Set;
2930

31+
import static org.geektimes.enterprise.inject.util.Exceptions.newDefinitionException;
32+
3033
/**
3134
* {@link Decorator} {@link Bean}
3235
* <p>
@@ -50,12 +53,21 @@
5053
*/
5154
public class DecoratorBean<T> extends ManagedBean<T> implements Decorator<T> {
5255

53-
54-
5556
public DecoratorBean(AnnotatedType<T> decoratorType, BeanManager beanManager) {
5657
super(decoratorType, beanManager);
5758
}
5859

60+
@Override
61+
public Class<? extends Annotation> getScope() {
62+
Class<? extends Annotation> scope = super.getScope();
63+
if (scope == null) {
64+
scope = Dependent.class;
65+
} else if (scope != null && !Dependent.class.equals(scope)) {
66+
throw newDefinitionException("The scope of decorator must be declared as @%s!", Dependent.class.getName());
67+
}
68+
return scope;
69+
}
70+
5971
@Override
6072
public Type getDelegateType() {
6173
return null;

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/interceptor/InterceptorBean.java

+13
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
import org.geektimes.interceptor.InterceptorInfo;
2121
import org.geektimes.interceptor.InterceptorManager;
2222

23+
import javax.enterprise.context.Dependent;
2324
import javax.enterprise.context.spi.CreationalContext;
2425
import javax.enterprise.inject.spi.*;
2526
import javax.interceptor.InvocationContext;
2627
import java.lang.annotation.Annotation;
2728
import java.util.Set;
2829

2930
import static java.lang.String.format;
31+
import static org.geektimes.enterprise.inject.util.Exceptions.newDefinitionException;
3032
import static org.geektimes.interceptor.InterceptorManager.getInstance;
3133

3234
/**
@@ -62,6 +64,17 @@ private Set<Annotation> resolveInterceptorBindings() {
6264
return interceptorInfo.getInterceptorBindings().getDeclaredAnnotations();
6365
}
6466

67+
@Override
68+
public Class<? extends Annotation> getScope() {
69+
Class<? extends Annotation> scope = super.getScope();
70+
if (scope == null) {
71+
scope = Dependent.class;
72+
} else if (scope != null && !Dependent.class.equals(scope)) {
73+
throw newDefinitionException("The scope of interceptor must be declared as @%s!", Dependent.class.getName());
74+
}
75+
return scope;
76+
}
77+
6578
@Override
6679
public Set<Annotation> getInterceptorBindings() {
6780
return interceptorBindings;

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/manager/StandardBeanManager.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,10 @@ private void performAfterTypeDiscovery() {
687687
* the container must perform bean discovery, as defined in Bean discovery.
688688
*/
689689
private void performBeanDiscovery() {
690-
determineManagedBeans();
691690
determineAlternativeBeans();
692691
determineInterceptorBeans();
693692
determineDecoratorBeans();
693+
determineManagedBeans();
694694
}
695695

696696
private void determineManagedBeans() {
@@ -731,7 +731,7 @@ private void determineManagedBean(AnnotatedType beanType) {
731731
determineProducerFields(managedBean);
732732
determineDisposerMethods(managedBean);
733733
determineObserverMethods(managedBean);
734-
registerManagedBean(managedBean);
734+
registerBean(managedBean);
735735
}
736736
}
737737

@@ -826,7 +826,7 @@ private void determineInterceptorBean(AnnotatedType<?> interceptorType) {
826826
if (!interceptorBean.isVetoed()) {
827827
fireProcessBeanEvent(interceptorType, interceptorBean);
828828
registerInterceptorClass(interceptorType);
829-
registerInterceptorBean(interceptorBean);
829+
registerBean(interceptorBean);
830830
}
831831
}
832832

@@ -840,6 +840,7 @@ public void registerBean(Bean<?> bean) {
840840
} else {
841841
genericBeans.add(bean);
842842
}
843+
contextManager.addBean(bean);
843844
}
844845

845846
private void registerInterceptorBean(Interceptor<?> interceptorBean) {
@@ -862,7 +863,7 @@ private void determineDecoratorBean(AnnotatedType<?> decoratorType) {
862863
fireProcessBeanAttributesEvent(decoratorType, decoratorBean);
863864
if (!decoratorBean.isVetoed()) { // vetoed if ProcessBeanAttributes.veto() method was invoked
864865
fireProcessBeanEvent(decoratorType, decoratorBean);
865-
registerDecoratorBean(decoratorBean);
866+
registerBean(decoratorBean);
866867
}
867868
}
868869

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/context/AbstractContext.java

+47-11
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import javax.enterprise.context.spi.Context;
2020
import javax.enterprise.context.spi.Contextual;
2121
import javax.enterprise.context.spi.CreationalContext;
22-
import javax.enterprise.inject.spi.AnnotatedType;
22+
import javax.enterprise.inject.spi.Bean;
2323
import javax.enterprise.inject.spi.BeanManager;
2424
import java.lang.annotation.Annotation;
25+
import java.lang.reflect.Type;
26+
import java.util.LinkedList;
27+
import java.util.List;
2528

26-
import static org.geektimes.enterprise.inject.util.Contexts.getBeanClass;
29+
import static org.geektimes.enterprise.inject.util.Contexts.getBeanType;
2730

2831
/**
2932
* Abstract implementation {@link Context}
@@ -37,11 +40,14 @@ public abstract class AbstractContext implements Context {
3740

3841
protected final Class<? extends Annotation> scope;
3942

43+
protected final List<Bean<?>> beans;
44+
4045
protected boolean active;
4146

4247
public AbstractContext(BeanManager beanManager, Class<? extends Annotation> scope) {
4348
this.beanManager = beanManager;
4449
this.scope = scope;
50+
this.beans = new LinkedList<>();
4551
active();
4652
}
4753

@@ -54,23 +60,49 @@ public final Class<? extends Annotation> getScope() {
5460
return scope;
5561
}
5662

63+
/**
64+
* @param contextual
65+
* @param creationalContext
66+
* @param <T>
67+
* @return <ul>
68+
* <li>return an existing instance of the given contextual type, or</li>
69+
* <li>if no CreationalContext is given, return a null value, or</li>
70+
* <li>if a CreationalContext is given, create a new instance of the given contextual type by calling
71+
* Contextual.create(), passing the given CreationalContext, and return the new instance.</li>
72+
* </ul>
73+
*/
5774
@Override
5875
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
59-
Class<T> beanClass = getBeanClass(contextual);
60-
CreationalContext<T> context = creationalContext;
61-
if (context == null) {
62-
context = beanManager.createCreationalContext(contextual);
76+
T instance = getInstance(contextual);
77+
if (instance == null) {
78+
if (creationalContext != null) {
79+
instance = contextual.create(creationalContext);
80+
}
6381
}
64-
AnnotatedType<T> beanType = beanManager.createAnnotatedType(beanClass);
65-
return get(contextual, context, beanType);
82+
return instance;
83+
// Class<T> beanClass = getBeanClass(contextual);
84+
// Type requiredBeanType = getBeanType(contextual);
85+
// CreationalContext<T> context = creationalContext;
86+
// if (context == null) {
87+
// context = beanManager.createCreationalContext(contextual);
88+
// }
89+
// AnnotatedType<T> beanType = beanManager.createAnnotatedType(beanClass);
90+
// return get(contextual, context, beanType);
91+
}
92+
93+
private <T> T getInstance(Contextual<T> contextual) {
94+
Type requiredBeanType = getBeanType(contextual);
95+
return getInstance(contextual, requiredBeanType);
6696
}
6797

68-
protected abstract <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext,
69-
AnnotatedType<T> beanType);
98+
protected <T> T getInstance(Contextual<T> contextual, Type requiredBeanType) {
99+
// TODO
100+
return null;
101+
}
70102

71103
@Override
72104
public <T> T get(Contextual<T> contextual) {
73-
return get(contextual, null);
105+
return get(contextual, beanManager.createCreationalContext(contextual));
74106
}
75107

76108
public final void inactive() {
@@ -85,4 +117,8 @@ public final void active() {
85117
public final boolean isActive() {
86118
return active;
87119
}
120+
121+
public void addBean(Bean<?> bean) {
122+
this.beans.add(bean);
123+
}
88124
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/context/ApplicationScopedContext.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.enterprise.context.spi.CreationalContext;
2222
import javax.enterprise.inject.spi.AnnotatedType;
2323
import javax.enterprise.inject.spi.BeanManager;
24+
import java.lang.reflect.Type;
2425

2526
/**
2627
* The Context for {@link ApplicationScoped}
@@ -33,10 +34,4 @@ public class ApplicationScopedContext extends AbstractAlterableContext {
3334
public ApplicationScopedContext(BeanManager beanManager) {
3435
super(beanManager, ApplicationScoped.class);
3536
}
36-
37-
@Override
38-
protected <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext,
39-
AnnotatedType<T> beanType) {
40-
return null;
41-
}
4237
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/context/DependentScopeContext.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import javax.enterprise.context.Dependent;
2020
import javax.enterprise.context.spi.AlterableContext;
2121
import javax.enterprise.context.spi.Contextual;
22-
import javax.enterprise.context.spi.CreationalContext;
23-
import javax.enterprise.inject.spi.AnnotatedType;
2422
import javax.enterprise.inject.spi.BeanManager;
23+
import java.lang.reflect.Type;
2524

2625
/**
2726
* The Context for {@link Dependent @Dependent}
@@ -39,10 +38,4 @@ public class DependentScopeContext extends AbstractAlterableContext {
3938
public DependentScopeContext(BeanManager beanManager) {
4039
super(beanManager, Dependent.class);
4140
}
42-
43-
@Override
44-
protected <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext,
45-
AnnotatedType<T> beanType) {
46-
return null;
47-
}
4841
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/context/mananger/ContextManager.java

+11
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package org.geektimes.enterprise.inject.standard.context.mananger;
1818

19+
import org.geektimes.enterprise.inject.standard.context.AbstractContext;
1920
import org.geektimes.enterprise.inject.util.Scopes;
2021

2122
import javax.enterprise.context.ContextNotActiveException;
2223
import javax.enterprise.context.spi.Context;
24+
import javax.enterprise.inject.spi.Bean;
2325
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
2426
import javax.enterprise.inject.spi.DeploymentException;
2527
import javax.inject.Scope;
@@ -82,6 +84,15 @@ public void addContext(Context context) throws DeploymentException {
8284
contexts.put(scope, context);
8385
}
8486

87+
public void addBean(Bean<?> bean) {
88+
Class<? extends Annotation> scope = bean.getScope();
89+
Context context = getContext(scope);
90+
if(context instanceof AbstractContext){
91+
AbstractContext abstractContext = (AbstractContext) context;
92+
abstractContext.addBean(bean);
93+
}
94+
}
95+
8596
public Context getContext(Class<? extends Annotation> scopeType) {
8697
Context context = contexts.get(scopeType);
8798
if (!context.isActive()) {

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/util/Contexts.java

+7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import javax.enterprise.context.spi.Context;
2020
import javax.enterprise.context.spi.Contextual;
21+
import java.lang.reflect.Type;
2122

2223
import static org.geektimes.commons.reflect.util.TypeUtils.findActualTypeArgumentClass;
24+
import static org.geektimes.commons.reflect.util.TypeUtils.findActualTypeArguments;
2325

2426
/**
2527
* The utilities class for {@link Context}
@@ -34,4 +36,9 @@ public static <T> Class<T> getBeanClass(Contextual<T> contextual) {
3436
return findActualTypeArgumentClass(contextualClass, Contextual.class, 0);
3537
}
3638

39+
public static <T> Type getBeanType(Contextual<T> contextual) {
40+
Class<?> contextualClass = contextual.getClass();
41+
return findActualTypeArguments(contextualClass, Contextual.class).get(0);
42+
}
43+
3744
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/util/Scopes.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ public abstract class Scopes {
3535

3636
public static Class<? extends Annotation> getScopeType(AnnotatedElement annotatedElement) {
3737
Annotation scope = findAnnotation(annotatedElement, Scopes::isScope);
38-
final Class<? extends Annotation> scopeType;
38+
Class<? extends Annotation> scopeType = null;
3939
if (scope != null) {
4040
scopeType = scope.annotationType();
41-
} else {
42-
scopeType = NormalScope.class;
4341
}
4442
return scopeType;
4543
}

0 commit comments

Comments
 (0)