Skip to content

Commit 3e86a47

Browse files
committed
2 parents 8a27b7a + f17b4a9 commit 3e86a47

File tree

14 files changed

+395
-72
lines changed

14 files changed

+395
-72
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
*/
5151
public class DecoratorBean<T> extends ManagedBean<T> implements Decorator<T> {
5252

53+
54+
5355
public DecoratorBean(AnnotatedType<T> decoratorType, BeanManager beanManager) {
5456
super(decoratorType, beanManager);
5557
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.geektimes.enterprise.inject.standard.beans.interceptor;
17+
package org.geektimes.enterprise.inject.standard.beans.interceptor;
1818

1919
import org.geektimes.enterprise.inject.standard.beans.GenericBean;
2020
import org.geektimes.interceptor.InterceptorInfo;
@@ -36,7 +36,7 @@
3636
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
3737
* @since 1.0.0
3838
*/
39-
public class InterceptorBean<T> extends GenericBean<T> implements Interceptor<T> {
39+
public class InterceptorBean<T> extends GenericBean<T> implements Interceptor<T> {
4040

4141
private final AnnotatedType<?> interceptorType;
4242

@@ -116,15 +116,15 @@ public T create(CreationalContext<T> creationalContext) {
116116

117117
@Override
118118
public void destroy(T instance, CreationalContext<T> creationalContext) {
119-
// TODO
120-
super.destroy(instance,creationalContext);
119+
// TODO
120+
super.destroy(instance, creationalContext);
121121
}
122122

123123
@Override
124124
protected void validate(Class interceptorClass) {
125125
this.interceptorManager.validateInterceptorClass(interceptorClass);
126126
}
127-
127+
128128
@Override
129129
public Annotated getAnnotated() {
130130
return interceptorType;

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

+52-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
import static org.geektimes.commons.lang.util.AnnotationUtils.isAnnotationPresent;
4646
import static org.geektimes.commons.reflect.util.ClassUtils.*;
4747
import static org.geektimes.commons.reflect.util.FieldUtils.getAllFields;
48+
import static org.geektimes.commons.reflect.util.MemberUtils.NON_PRIVATE_METHOD_PREDICATE;
49+
import static org.geektimes.commons.reflect.util.MemberUtils.NON_STATIC_METHOD_PREDICATE;
50+
import static org.geektimes.commons.reflect.util.MethodUtils.getAllDeclaredMethods;
4851
import static org.geektimes.commons.reflect.util.TypeUtils.*;
4952
import static org.geektimes.enterprise.inject.util.Decorators.isDecorator;
5053
import static org.geektimes.enterprise.inject.util.Qualifiers.findQualifier;
5154
import static org.geektimes.interceptor.InterceptorManager.getInstance;
52-
import static org.geektimes.interceptor.util.InterceptorUtils.isInterceptorClass;
5355

5456
/**
5557
* Bean Utilities class
@@ -203,6 +205,55 @@ public static boolean isManagedBean(Class<?> beanClass) {
203205
return true;
204206
}
205207

208+
/**
209+
* The container uses proxies to provide certain functionality. Certain legal bean types cannot be
210+
* proxied by the container:
211+
* <ul>
212+
* <li>classes which don’t have a non-private constructor with no parameters</li>
213+
* <li>classes which are declared final</li>
214+
* <li>classes which have non-static, final methods with public, protected or default visibility</li>
215+
* <li>primitive types</li>
216+
* <li>array types</li>
217+
* </ul>
218+
*
219+
* @param beanClass the type of Bean
220+
* @return <code>true</code> if bean type cannot be proxied by the container:
221+
* * <ul>
222+
* * <li>classes which don’t have a non-private constructor with no parameters</li>
223+
* * <li>classes which are declared final</li>
224+
* * <li>classes which have non-static, final methods with public, protected or default visibility</li>
225+
* * <li>primitive types</li>
226+
* * <li>array types</li>
227+
* * </ul>
228+
*/
229+
public static boolean isUnproxyable(Class<?> beanClass) {
230+
if (isArray(beanClass) ||
231+
isPrimitive(beanClass) ||
232+
isFinal(beanClass) ||
233+
hasFinalMethod(beanClass) ||
234+
hasNonDefaultConstructor(beanClass)) {
235+
return true;
236+
}
237+
return false;
238+
}
239+
240+
private static boolean hasFinalMethod(Class<?> beanClass) {
241+
Set<Method> methods = getAllDeclaredMethods(beanClass, NON_STATIC_METHOD_PREDICATE, NON_PRIVATE_METHOD_PREDICATE);
242+
boolean hasFinalMethod = false;
243+
for (Method method : methods) {
244+
hasFinalMethod = MemberUtils.isFinal(method);
245+
if (hasFinalMethod) {
246+
break;
247+
}
248+
}
249+
return hasFinalMethod;
250+
}
251+
252+
private static boolean hasNonDefaultConstructor(Class<?> beanClass) {
253+
// TODO
254+
return true;
255+
}
256+
206257
private static boolean hasManagedBeanConstructor(Class<?> beanClass) {
207258
boolean hasManagedBeanConstructor = false;
208259
for (Constructor constructor : beanClass.getConstructors()) {

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/function/Predicates.java

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

1919
import java.util.function.Predicate;
2020

21-
import static java.util.stream.Stream.of;
22-
2321
/**
2422
* The utilities class for Java {@link Predicate}
2523
*
@@ -29,14 +27,18 @@ public interface Predicates {
2927

3028
Predicate[] EMPTY_ARRAY = new Predicate[0];
3129

30+
Predicate<?> ALWAYS_TRUE = e -> true;
31+
32+
Predicate<?> ALWAYS_FALSE = e -> false;
33+
3234
/**
3335
* {@link Predicate} always return <code>true</code>
3436
*
3537
* @param <T> the type to test
3638
* @return <code>true</code>
3739
*/
3840
static <T> Predicate<T> alwaysTrue() {
39-
return e -> true;
41+
return (Predicate<T>) ALWAYS_TRUE;
4042
}
4143

4244
/**
@@ -46,29 +48,65 @@ static <T> Predicate<T> alwaysTrue() {
4648
* @return <code>false</code>
4749
*/
4850
static <T> Predicate<T> alwaysFalse() {
49-
return e -> false;
51+
return (Predicate<T>) ALWAYS_FALSE;
5052
}
5153

5254
/**
5355
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
5456
*
55-
* @param predicates {@link Predicate predicates}
57+
* @param predicate {@link Predicate one predicate}
58+
* @param otherPredicates {@link Predicate other predicates}
59+
* @param <T> the type to test
60+
* @return non-null
61+
*/
62+
static <T> Predicate<T> and(Predicate<? super T> predicate, Predicate<? super T>... otherPredicates) {
63+
Predicate<T> andPredicate = alwaysTrue();
64+
andPredicate = andPredicate.and(predicate).and(and(otherPredicates));
65+
return andPredicate;
66+
}
67+
68+
/**
69+
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
70+
*
71+
* @param predicates {@link Predicate other predicates}
5672
* @param <T> the type to test
5773
* @return non-null
5874
*/
59-
static <T> Predicate<T> and(Predicate<T>... predicates) {
60-
return of(predicates).reduce((a, b) -> a.and(b)).orElseGet(Predicates::alwaysTrue);
75+
static <T> Predicate<T> and(Predicate<? super T>... predicates) {
76+
Predicate<T> andPredicate = alwaysTrue();
77+
for (Predicate<? super T> p : predicates) {
78+
andPredicate = andPredicate.and(p);
79+
}
80+
return andPredicate;
81+
}
82+
83+
/**
84+
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
85+
*
86+
* @param predicate {@link Predicate one predicate}
87+
* @param otherPredicates {@link Predicate other predicates}
88+
* @param <T> the detected type
89+
* @return non-null
90+
*/
91+
static <T> Predicate<T> or(Predicate<? super T> predicate, Predicate<? super T>... otherPredicates) {
92+
Predicate<T> orPredicate = alwaysTrue();
93+
orPredicate = orPredicate.or(predicate).or(or(otherPredicates));
94+
return orPredicate;
6195
}
6296

6397
/**
6498
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
6599
*
66-
* @param predicates {@link Predicate predicates}
100+
* @param predicates {@link Predicate other predicates}
67101
* @param <T> the detected type
68102
* @return non-null
69103
*/
70-
static <T> Predicate<T> or(Predicate<T>... predicates) {
71-
return of(predicates).reduce((a, b) -> a.or(b)).orElse(e -> true);
104+
static <T> Predicate<T> or(Predicate<? super T>... predicates) {
105+
Predicate<T> orPredicate = alwaysTrue();
106+
for (Predicate<? super T> p : predicates) {
107+
orPredicate = orPredicate.or(p);
108+
}
109+
return orPredicate;
72110
}
73111

74112
}

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/function/Streams.java

+74-20
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import static java.util.Collections.unmodifiableSet;
3232
import static java.util.stream.Collectors.toList;
3333
import static org.geektimes.commons.collection.util.CollectionUtils.*;
34-
import static org.geektimes.commons.function.Predicates.and;
35-
import static org.geektimes.commons.function.Predicates.or;
34+
import static org.geektimes.commons.function.Predicates.*;
3635

3736
/**
3837
* The utilities class for {@link Stream}
@@ -45,63 +44,118 @@ static <T> Stream<T> stream(Iterable<T> iterable) {
4544
return StreamSupport.stream(iterable.spliterator(), false);
4645
}
4746

48-
static <T, S extends Iterable<T>> Stream<T> filterStream(S values, Predicate<T> predicate) {
49-
return stream(values).filter(predicate);
47+
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T> predicate) {
48+
return filterStream(values, predicate, EMPTY_ARRAY);
5049
}
5150

52-
static <E, L extends List<E>> List<E> filter(L values, Predicate<E> predicate) {
51+
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T>... predicates) {
52+
return filterStream(values, alwaysTrue(), predicates);
53+
}
54+
55+
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T> predicate,
56+
Predicate<? super T>... otherPredicates) {
57+
return stream(values).filter(and(predicate, otherPredicates));
58+
}
59+
60+
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E> predicate) {
61+
return filter(values, predicate, EMPTY_ARRAY);
62+
}
63+
64+
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E>... predicates) {
65+
return filter(values, alwaysTrue(), predicates);
66+
}
67+
68+
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E> predicate, Predicate<? super E>... otherPredicates) {
5369
final L result;
5470
if (predicate == null) {
5571
result = values;
5672
} else {
57-
result = (L) filterStream(values, predicate).collect(toList());
73+
result = (L) filterStream(values, predicate, otherPredicates).collect(toList());
5874
}
5975
return unmodifiableList(result);
6076
}
6177

62-
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<E> predicate) {
78+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate) {
79+
return filter(values, predicate, EMPTY_ARRAY);
80+
}
81+
82+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E>... predicates) {
83+
return filter(values, alwaysTrue(), predicates);
84+
}
85+
86+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate,
87+
Predicate<? super E>... otherPredicates) {
6388
final S result;
6489
if (predicate == null) {
6590
result = values;
6691
} else {
67-
result = (S) filterStream(values, predicate).collect(Collectors.toSet());
92+
result = (S) filterStream(values, predicate, otherPredicates).collect(Collectors.toSet());
6893
}
6994
return unmodifiableSet(result);
7095
}
7196

72-
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<E> predicate) {
97+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate) {
98+
return filter(values, predicate, EMPTY_ARRAY);
99+
}
100+
101+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E>... predicates) {
102+
return filter(values, alwaysTrue(), predicates);
103+
}
104+
105+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate,
106+
Predicate<? super E>... otherPredicates) {
73107
final Q result;
74108
if (predicate == null) {
75109
result = values;
76110
} else {
77-
result = (Q) filterStream(values, predicate).collect(LinkedList::new, List::add, List::addAll);
111+
result = (Q) filterStream(values, predicate, otherPredicates)
112+
.collect(LinkedList::new, List::add, List::addAll);
78113
}
79114
return unmodifiableQueue(result);
80115
}
81116

82-
static <T, S extends Iterable<T>> S filter(S values, Predicate<T> predicate) {
117+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate) {
118+
return (S) filter(values, predicate, EMPTY_ARRAY);
119+
}
120+
121+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T>... predicates) {
122+
return filter(values, alwaysTrue(), predicates);
123+
}
124+
125+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate,
126+
Predicate<? super T>... otherPredicates) {
83127
if (isSet(values)) {
84-
return (S) filter((Set) values, predicate);
128+
return (S) filter((Set) values, predicate, otherPredicates);
85129
} else if (isList(values)) {
86-
return (S) filter((List) values, predicate);
130+
return (S) filter((List) values, predicate, otherPredicates);
87131
} else if (isQueue(values)) {
88-
return (S) filter((Queue) values, predicate);
132+
return (S) filter((Queue) values, predicate, otherPredicates);
89133
}
90134
String message = format("The 'values' type can't be supported!", values.getClass().getName());
91135
throw new UnsupportedOperationException(message);
92136
}
93137

94-
static <T, S extends Iterable<T>> S filterAll(S values, Predicate<T>... predicates) {
95-
return filter(values, and(predicates));
138+
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
139+
return filterAny(values, alwaysTrue(), predicates);
140+
}
141+
142+
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T> predicate,
143+
Predicate<? super T>... otherPredicates) {
144+
return filter(values, or(predicate, otherPredicates));
145+
}
146+
147+
static <T> T filterFirst(Iterable<T> values, Predicate<? super T> predicate) {
148+
return (T) filterFirst(values, predicate, EMPTY_ARRAY);
96149
}
97150

98-
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<T>... predicates) {
99-
return filter(values, or(predicates));
151+
static <T> T filterFirst(Iterable<T> values, Predicate<? super T>... predicates) {
152+
return filterFirst(values, alwaysTrue(), predicates);
100153
}
101154

102-
static <T> T filterFirst(Iterable<T> values, Predicate<T>... predicates) {
155+
static <T> T filterFirst(Iterable<T> values, Predicate<? super T> predicate,
156+
Predicate<? super T>... otherPredicates) {
103157
return stream(values)
104-
.filter(and(predicates))
158+
.filter(and(predicate, otherPredicates))
105159
.findFirst()
106160
.orElse(null);
107161
}

0 commit comments

Comments
 (0)