/
AnnBuilder.java
127 lines (115 loc) · 4.43 KB
/
AnnBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright 2013 Alex Yanchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.droidparts.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import org.droidparts.annotation.inject.InjectBundleExtra;
import org.droidparts.annotation.inject.InjectDependency;
import org.droidparts.annotation.inject.InjectFragment;
import org.droidparts.annotation.inject.InjectParentActivity;
import org.droidparts.annotation.inject.InjectResource;
import org.droidparts.annotation.inject.InjectSystemService;
import org.droidparts.annotation.inject.InjectView;
import org.droidparts.annotation.json.Key;
import org.droidparts.annotation.sql.Column;
import org.droidparts.annotation.sql.Table;
import org.droidparts.reflect.ann.Ann;
import org.droidparts.reflect.ann.inject.InjectBundleExtraAnn;
import org.droidparts.reflect.ann.inject.InjectDependencyAnn;
import org.droidparts.reflect.ann.inject.InjectFragmentAnn;
import org.droidparts.reflect.ann.inject.InjectParentActivityAnn;
import org.droidparts.reflect.ann.inject.InjectResourceAnn;
import org.droidparts.reflect.ann.inject.InjectSystemServiceAnn;
import org.droidparts.reflect.ann.inject.InjectViewAnn;
import org.droidparts.reflect.ann.json.KeyAnn;
import org.droidparts.reflect.ann.sql.ColumnAnn;
import org.droidparts.reflect.ann.sql.TableAnn;
public final class AnnBuilder {
@SuppressWarnings("unchecked")
static <T extends Annotation> Ann<T> getClassAnn(
Class<? extends Ann<T>> annCls, Class<?> cls) {
return pickAnn(annCls, (Ann<T>[]) getClassAnns(cls));
}
@SuppressWarnings("unchecked")
static <T extends Annotation> Ann<T> getFieldAnn(
Class<? extends Ann<T>> annCls, Class<?> cls, Field f) {
return pickAnn(annCls, (Ann<T>[]) getFieldAnns(cls, f));
}
private static <T extends Annotation> Ann<T> pickAnn(
Class<? extends Ann<T>> annCls, Ann<T>[] anns) {
for (Ann<?> ann : anns) {
if (ann.getClass().isAssignableFrom(annCls)) {
@SuppressWarnings("unchecked")
Ann<T> typedAnn = (Ann<T>) ann;
return typedAnn;
}
}
return null;
}
static <T extends Annotation> Ann<T>[] getClassAnns(Class<?> cls) {
return toAnns(cls.getAnnotations());
}
static <T extends Annotation> Ann<T>[] getFieldAnns(Class<?> cls, Field f) {
return toAnns(f.getAnnotations());
}
//
@SuppressWarnings("unchecked")
private static <T extends Annotation> Ann<T>[] toAnns(
Annotation[] annotations) {
ArrayList<Ann<?>> anns = new ArrayList<Ann<?>>();
for (Annotation annotation : annotations) {
Ann<?> ann = toAnn(annotation);
if (ann != null) {
anns.add(ann);
}
}
return anns.toArray(new Ann[anns.size()]);
}
private static Ann<?> toAnn(Annotation annotation) {
Class<?> annotationType = annotation.annotationType();
Ann<?> ann = null;
// class
if (Table.class == annotationType) {
ann = new TableAnn((Table) annotation);
}
// field
else if (Column.class == annotationType) {
ann = new ColumnAnn((Column) annotation);
} else if (Key.class == annotationType) {
ann = new KeyAnn((Key) annotation);
}
// inject
else if (InjectBundleExtra.class == annotationType) {
ann = new InjectBundleExtraAnn((InjectBundleExtra) annotation);
} else if (InjectDependency.class == annotationType) {
ann = new InjectDependencyAnn((InjectDependency) annotation);
} else if (InjectResource.class == annotationType) {
ann = new InjectResourceAnn((InjectResource) annotation);
} else if (InjectSystemService.class == annotationType) {
ann = new InjectSystemServiceAnn((InjectSystemService) annotation);
} else if (InjectView.class == annotationType) {
ann = new InjectViewAnn((InjectView) annotation);
}
// inject modern
else if (InjectFragment.class == annotationType) {
ann = new InjectFragmentAnn((InjectFragment) annotation);
} else if (InjectParentActivity.class == annotationType) {
ann = new InjectParentActivityAnn((InjectParentActivity) annotation);
}
return ann;
}
}