Repository navigation
Expand file tree
/
Copy pathFragmentByTagProcessor.java
More file actions
112 lines (83 loc) · 4.08 KB
/
Copy pathFragmentByTagProcessor.java
File metadata and controls
112 lines (83 loc) · 4.08 KB
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
/**
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
*
* 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.androidannotations.processing;
import static com.sun.codemodel.JExpr._null;
import static com.sun.codemodel.JExpr.cast;
import static com.sun.codemodel.JExpr.invoke;
import static com.sun.codemodel.JExpr.lit;
import static com.sun.codemodel.JExpr.ref;
import static com.sun.codemodel.JMod.PRIVATE;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import org.androidannotations.annotations.FragmentByTag;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.processing.EBeansHolder.Classes;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JVar;
public class FragmentByTagProcessor implements DecoratingElementProcessor {
private final AnnotationHelper annotationHelper;
public FragmentByTagProcessor(ProcessingEnvironment processingEnv) {
annotationHelper = new AnnotationHelper(processingEnv);
}
@Override
public String getTarget() {
return FragmentByTag.class.getName();
}
@Override
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
Classes classes = holder.classes();
String fieldName = element.getSimpleName().toString();
TypeMirror elementType = element.asType();
String typeQualifiedName = elementType.toString();
FragmentByTag annotation = element.getAnnotation(FragmentByTag.class);
String tagValue = annotation.value();
if (tagValue.equals("")) {
tagValue = fieldName;
}
TypeMirror nativeFragmentType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.FRAGMENT).asType();
JMethod findFragmentByTag;
if (annotationHelper.isSubtype(elementType, nativeFragmentType)) {
// Injecting native fragment
findFragmentByTag = null;
if (holder.findNativeFragmentByTag == null) {
holder.findNativeFragmentByTag = holder.generatedClass.method(PRIVATE, classes.FRAGMENT, "findNativeFragmentByTag");
JVar tagParam = holder.findNativeFragmentByTag.param(classes.STRING, "tag");
JBlock body = holder.findNativeFragmentByTag.body();
body._if(holder.contextRef._instanceof(classes.ACTIVITY).not())._then()._return(_null());
JVar activityVar = body.decl(classes.ACTIVITY, "activity_", cast(classes.ACTIVITY, holder.contextRef));
body._return(activityVar.invoke("getFragmentManager").invoke("findFragmentByTag").arg(tagParam));
}
findFragmentByTag = holder.findNativeFragmentByTag;
} else {
// Injecting support fragment
if (holder.findSupportFragmentByTag == null) {
holder.findSupportFragmentByTag = holder.generatedClass.method(PRIVATE, classes.SUPPORT_V4_FRAGMENT, "findSupportFragmentByTag");
JVar tagParam = holder.findSupportFragmentByTag.param(classes.STRING, "tag");
JBlock body = holder.findSupportFragmentByTag.body();
body._if(holder.contextRef._instanceof(classes.FRAGMENT_ACTIVITY).not())._then()._return(_null());
JVar activityVar = body.decl(classes.FRAGMENT_ACTIVITY, "activity_", cast(classes.FRAGMENT_ACTIVITY, holder.contextRef));
body._return(activityVar.invoke("getSupportFragmentManager").invoke("findFragmentByTag").arg(tagParam));
}
findFragmentByTag = holder.findSupportFragmentByTag;
}
JBlock methodBody = holder.onViewChanged().body();
methodBody.assign(ref(fieldName), cast(holder.refClass(typeQualifiedName), invoke(findFragmentByTag).arg(lit(tagValue))));
}
}