1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jp.liq.container.reflect;
17
18 import java.lang.annotation.Annotation;
19
20 import jp.liq.container.util.AbstractFinder;
21 import jp.liq.container.util.Matcher;
22
23
24
25
26
27
28
29 public abstract class MemberFinder<T extends Member, S extends MemberFinder<T, S>>
30 extends AbstractFinder<T, S> {
31
32
33
34
35 public MemberFinder() {
36 }
37
38
39
40
41
42
43
44 public S withModifier(int i) {
45 addMatcher(new ModifierMatcher<T>(i));
46 return getThis();
47 }
48
49
50
51
52
53
54 public S withAnnotation(Class<? extends Annotation> ann) {
55 addMatcher(new AnnotationMatcher<T>(ann));
56 return getThis();
57 }
58
59
60
61
62
63 public S withPrefix(String prefix) {
64 addMatcher(new PrefixMatcher<T>(prefix));
65 return getThis();
66 }
67
68 private class ModifierMatcher<M extends Member> implements Matcher<M> {
69 private final int modifier;
70 ModifierMatcher(int modifier) {
71 this.modifier = modifier;
72 }
73
74 public boolean matches(M m) {
75 return m.hasModifier(modifier);
76 }
77
78 }
79
80 private class AnnotationMatcher<M extends Member> implements Matcher<M> {
81 private final Class<? extends Annotation> annotationClass;
82
83 AnnotationMatcher(Class<? extends Annotation> ann) {
84 annotationClass = ann;
85 }
86
87 public boolean matches(M m) {
88 return m.isAnnotationPresent(annotationClass);
89 }
90
91 }
92
93 private class PrefixMatcher<M extends Member> implements Matcher<M> {
94 private final String prefix;
95 PrefixMatcher(String prefix) {
96 this.prefix = prefix;
97 }
98 public boolean matches(M m) {
99 return m.name().startsWith(prefix);
100 }
101
102 }
103
104 }