Skip to content

Commit 0e442e9

Browse files
committed
Handle resName on HttpsClient annotation
1 parent 6495da7 commit 0e442e9

4 files changed

Lines changed: 84 additions & 33 deletions

File tree

‎AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/HttpsClient.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* <b>Note</b>:
5151
* <tt>Prior to ICS, Android accepts [Key|Trust]store only in BKS format
5252
* (Bouncycastle Key Store)</tt>
53-
*
53+
*
5454
* <blockquote>
5555
*
5656
* Example :
@@ -93,10 +93,14 @@
9393

9494
int trustStore() default ResId.DEFAULT_VALUE;
9595

96+
String trustStoreResName() default "";
97+
9698
String trustStorePwd() default DEFAULT_PASSWD;
9799

98100
int keyStore() default ResId.DEFAULT_VALUE;
99101

102+
String keyStoreResName() default "";
103+
100104
String keyStorePwd() default DEFAULT_PASSWD;
101105

102106
boolean allowAllHostnames() default true;

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/HttpsClientHandler.java‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import javax.lang.model.element.Element;
2828

2929
import org.androidannotations.annotations.HttpsClient;
30-
import org.androidannotations.annotations.ResId;
30+
import org.androidannotations.helper.AndroidManifest;
31+
import org.androidannotations.helper.IdAnnotationHelper;
3132
import org.androidannotations.holder.EComponentHolder;
33+
import org.androidannotations.model.AndroidSystemServices;
3234
import org.androidannotations.model.AnnotationElements;
3335
import org.androidannotations.process.IsValid;
3436
import org.androidannotations.process.ProcessHolder;
@@ -48,6 +50,8 @@
4850

4951
public class HttpsClientHandler extends BaseAnnotationHandler<EComponentHolder> {
5052

53+
private IdAnnotationHelper annotationHelper;
54+
5155
public HttpsClientHandler(ProcessingEnvironment processingEnvironment) {
5256
super(HttpsClient.class, processingEnvironment);
5357
}
@@ -62,17 +66,24 @@ public void validate(Element element, AnnotationElements validatedElements, IsVa
6266
validatorHelper.isNotPrivate(element, valid);
6367
}
6468

69+
@Override
70+
public void setAndroidEnvironment(IRClass rClass, AndroidSystemServices androidSystemServices, AndroidManifest androidManifest) {
71+
super.setAndroidEnvironment(rClass, androidSystemServices, androidManifest);
72+
annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass);
73+
}
74+
6575
@Override
6676
public void process(Element element, EComponentHolder holder) throws Exception {
77+
IRInnerClass rInnerClass = rClass.get(IRClass.Res.RAW);
6778
HttpsClient annotation = element.getAnnotation(HttpsClient.class);
68-
int trustStoreRawId = annotation.trustStore();
79+
JFieldRef trustStoreRawIdRef = annotationHelper.extractOneAnnotationFieldRef(processHolder, element, getTarget(), rInnerClass, false, "trustStore", "trustStoreResName");
80+
JFieldRef keyStoreRawIdRef = annotationHelper.extractOneAnnotationFieldRef(processHolder, element, getTarget(), rInnerClass, false, "keyStore", "keyStoreResName");
6981
String trustStorePwd = annotation.trustStorePwd();
70-
int keyStoreRawId = annotation.keyStore();
7182
String keyStorePwd = annotation.keyStorePwd();
7283

7384
boolean allowAllHostnames = annotation.allowAllHostnames();
74-
boolean useCustomTrustStore = ResId.DEFAULT_VALUE != trustStoreRawId;
75-
boolean useCustomKeyStore = ResId.DEFAULT_VALUE != keyStoreRawId;
85+
boolean useCustomTrustStore = trustStoreRawIdRef != null;
86+
boolean useCustomKeyStore = keyStoreRawIdRef != null;
7687

7788
String fieldName = element.getSimpleName().toString();
7889
JBlock methodBody = holder.getInitBody();
@@ -110,16 +121,13 @@ public void process(Element element, EComponentHolder holder) throws Exception {
110121
jVarRes = jTryBlock.body().decl(classes.RESOURCES, "res", invoke("getResources"));
111122
}
112123

113-
IRInnerClass rInnerClass = rClass.get(IRClass.Res.RAW);
114124
if (useCustomKeyStore) {
115-
JFieldRef rawIdRef = rInnerClass.getIdStaticRef(keyStoreRawId, processHolder);
116-
JInvocation jInvRawKey = jVarRes.invoke("openRawResource").arg(rawIdRef);
125+
JInvocation jInvRawKey = jVarRes.invoke("openRawResource").arg(keyStoreRawIdRef);
117126
jVarKeyFile = jTryBlock.body().decl(classes.INPUT_STREAM, "inKeystore", jInvRawKey);
118127
}
119128

120129
if (useCustomTrustStore) {
121-
JFieldRef rawIdRef = rInnerClass.getIdStaticRef(trustStoreRawId, processHolder);
122-
JInvocation jInvRawTrust = jVarRes.invoke("openRawResource").arg(rawIdRef);
130+
JInvocation jInvRawTrust = jVarRes.invoke("openRawResource").arg(trustStoreRawIdRef);
123131
jVarTrstFile = jTryBlock.body().decl(classes.INPUT_STREAM, "inTrustStore", jInvRawTrust);
124132

125133
} else if (useCustomKeyStore) {

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,27 @@
1515
*/
1616
package org.androidannotations.helper;
1717

18-
import com.sun.codemodel.JFieldRef;
18+
import java.lang.annotation.Annotation;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import javax.annotation.processing.ProcessingEnvironment;
26+
import javax.lang.model.element.AnnotationMirror;
27+
import javax.lang.model.element.AnnotationValue;
28+
import javax.lang.model.element.Element;
29+
import javax.lang.model.element.ExecutableElement;
30+
import javax.lang.model.element.Modifier;
31+
import javax.lang.model.element.NestingKind;
32+
import javax.lang.model.element.TypeElement;
33+
import javax.lang.model.type.DeclaredType;
34+
import javax.lang.model.type.MirroredTypeException;
35+
import javax.lang.model.type.TypeMirror;
36+
import javax.lang.model.util.Elements;
37+
import javax.lang.model.util.Types;
38+
1939
import org.androidannotations.annotations.OnActivityResult;
2040
import org.androidannotations.annotations.OptionsItem;
2141
import org.androidannotations.annotations.ResId;
@@ -26,22 +46,13 @@
2646
import org.androidannotations.rclass.IRInnerClass;
2747
import org.androidannotations.rclass.RInnerClass;
2848

29-
import javax.annotation.processing.ProcessingEnvironment;
30-
import javax.lang.model.element.*;
31-
import javax.lang.model.type.DeclaredType;
32-
import javax.lang.model.type.MirroredTypeException;
33-
import javax.lang.model.type.TypeMirror;
34-
import javax.lang.model.util.Elements;
35-
import javax.lang.model.util.Types;
36-
import java.lang.annotation.Annotation;
37-
import java.lang.reflect.InvocationTargetException;
38-
import java.lang.reflect.Method;
39-
import java.util.ArrayList;
40-
import java.util.List;
41-
import java.util.Map;
49+
import com.sun.codemodel.JFieldRef;
4250

4351
public class AnnotationHelper {
4452

53+
public static final String DEFAULT_FIELD_NAME_VALUE = "value";
54+
public static final String DEFAULT_FIELD_NAME_RESNAME = "resName";
55+
4556
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationHelper.class);
4657

4758
private final ProcessingEnvironment processingEnv;
@@ -150,9 +161,13 @@ public Types getTypeUtils() {
150161
* @see #extractAnnotationResources(Element, String, IRInnerClass, boolean)
151162
*/
152163
public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName) {
164+
return extractAnnotationFieldRefs(holder, element, annotationName, rInnerClass, useElementName, DEFAULT_FIELD_NAME_VALUE, DEFAULT_FIELD_NAME_RESNAME);
165+
}
166+
167+
public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName, String idFieldName, String resFieldName) {
153168
List<JFieldRef> fieldRefs = new ArrayList<JFieldRef>();
154169

155-
for (String refQualifiedName : extractAnnotationResources(element, annotationName, rInnerClass, useElementName)) {
170+
for (String refQualifiedName : extractAnnotationResources(element, annotationName, rInnerClass, useElementName, idFieldName, resFieldName)) {
156171
fieldRefs.add(RInnerClass.extractIdStaticRef(holder, refQualifiedName));
157172
}
158173

@@ -178,7 +193,11 @@ public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element
178193
* class
179194
*/
180195
public List<String> extractAnnotationResources(Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName) {
181-
int[] values = extractAnnotationResIdValueParameter(element, annotationName);
196+
return extractAnnotationResources(element, annotationName, rInnerClass, useElementName, DEFAULT_FIELD_NAME_VALUE, DEFAULT_FIELD_NAME_RESNAME);
197+
}
198+
199+
public List<String> extractAnnotationResources(Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName, String idFieldName, String resFieldName) {
200+
int[] values = extractAnnotationResIdValueParameter(element, annotationName, idFieldName);
182201

183202
List<String> resourceIdQualifiedNames = new ArrayList<String>();
184203
/*
@@ -187,7 +206,7 @@ public List<String> extractAnnotationResources(Element element, String annotatio
187206
*/
188207
if (defaultResIdValue(values)) {
189208

190-
String[] resNames = extractAnnotationResNameParameter(element, annotationName);
209+
String[] resNames = extractAnnotationResNameParameter(element, annotationName, resFieldName);
191210

192211
if (defaultResName(resNames)) {
193212
/*
@@ -244,10 +263,14 @@ public boolean defaultResIdValue(int[] values) {
244263
}
245264

246265
public String[] extractAnnotationResNameParameter(Element element, String annotationName) {
266+
return extractAnnotationResNameParameter(element, annotationName, DEFAULT_FIELD_NAME_RESNAME);
267+
}
268+
269+
public String[] extractAnnotationResNameParameter(Element element, String annotationName, String fieldName) {
247270
/*
248271
* Annotation resName() parameter can be a String or a String[]
249272
*/
250-
Object annotationResName = extractAnnotationParameter(element, annotationName, "resName");
273+
Object annotationResName = extractAnnotationParameter(element, annotationName, fieldName);
251274
if (annotationResName == null) {
252275
// This case happened during refactoring, if the id has been changed
253276
// in the layout and compiler throws an error on the annotation
@@ -266,10 +289,14 @@ public String[] extractAnnotationResNameParameter(Element element, String annota
266289
}
267290

268291
public int[] extractAnnotationResIdValueParameter(Element element, String annotationName) {
292+
return extractAnnotationResIdValueParameter(element, annotationName, DEFAULT_FIELD_NAME_VALUE);
293+
}
294+
295+
public int[] extractAnnotationResIdValueParameter(Element element, String annotationName, String fieldName) {
269296
/*
270297
* Annotation value() parameter can be an int or an int[]
271298
*/
272-
Object annotationValue = extractAnnotationParameter(element, annotationName, "value");
299+
Object annotationValue = extractAnnotationParameter(element, annotationName, fieldName);
273300
if (annotationValue == null) {
274301
// This case happened during refactoring, if the id has been changed
275302
// in the layout and compiler throws an error on the annotation
@@ -375,7 +402,7 @@ public DeclaredType extractAnnotationClassParameter(Element element, String anno
375402
}
376403

377404
public DeclaredType extractAnnotationClassParameter(Element element, String annotationName) {
378-
return extractAnnotationClassParameter(element, annotationName, "value");
405+
return extractAnnotationClassParameter(element, annotationName, DEFAULT_FIELD_NAME_VALUE);
379406
}
380407

381408
public boolean hasOneOfClassAnnotations(Element element, Class<? extends Annotation> validAnnotation) {

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/IdAnnotationHelper.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,27 @@ public List<String> extractAnnotationResources(Element element, Res res, boolean
5151
}
5252

5353
public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element element, Res res, boolean useElementName) {
54-
return super.extractAnnotationFieldRefs(holder, element, getTarget(), rClass.get(res), useElementName);
54+
return extractAnnotationFieldRefs(holder, element, res, useElementName, DEFAULT_FIELD_NAME_VALUE, DEFAULT_FIELD_NAME_RESNAME);
55+
}
56+
57+
public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element element, Res res, boolean useElementName, String idFieldName, String resFieldName) {
58+
return super.extractAnnotationFieldRefs(holder, element, getTarget(), rClass.get(res), useElementName, idFieldName, resFieldName);
5559
}
5660

5761
public JFieldRef extractOneAnnotationFieldRef(ProcessHolder holder, Element element, Res res, boolean useElementName) {
5862
return extractOneAnnotationFieldRef(holder, element, getTarget(), res, useElementName);
5963
}
6064

6165
public JFieldRef extractOneAnnotationFieldRef(ProcessHolder holder, Element element, String annotationName, Res res, boolean useElementName) {
62-
List<JFieldRef> jFieldRefs = extractAnnotationFieldRefs(holder, element, annotationName, rClass.get(res), useElementName);
66+
return extractOneAnnotationFieldRef(holder, element, annotationName, res, useElementName, DEFAULT_FIELD_NAME_VALUE, DEFAULT_FIELD_NAME_RESNAME);
67+
}
68+
69+
public JFieldRef extractOneAnnotationFieldRef(ProcessHolder holder, Element element, String annotationName, Res res, boolean useElementName, String idFieldName, String resFieldName) {
70+
return extractOneAnnotationFieldRef(holder, element, annotationName, rClass.get(res), useElementName, idFieldName, resFieldName);
71+
}
72+
73+
public JFieldRef extractOneAnnotationFieldRef(ProcessHolder holder, Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName, String idFieldName, String resFieldName) {
74+
List<JFieldRef> jFieldRefs = extractAnnotationFieldRefs(holder, element, annotationName, rInnerClass, useElementName, idFieldName, resFieldName);
6375

6476
if (jFieldRefs.size() == 1) {
6577
return jFieldRefs.get(0);

0 commit comments

Comments
 (0)