Skip to content

Commit 1c52bc2

Browse files
committed
Merge remote-tracking branch 'matboniface/develop' into develop
2 parents 84f132f + 2319193 commit 1c52bc2

File tree

18 files changed

+232
-223
lines changed

18 files changed

+232
-223
lines changed

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/Click.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@
3333
@Retention(RetentionPolicy.SOURCE)
3434
@Target(ElementType.METHOD)
3535
public @interface Click {
36-
int value() default Id.DEFAULT_VALUE;
36+
37+
int [] value() default Id.DEFAULT_VALUE;
38+
3739
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/ItemClick.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
*
3333
* @author Benjamin Fellous
3434
* @author Pierre-Yves Ricau
35+
* @author Mathieu Boniface
3536
*
3637
*/
3738
@Retention(RetentionPolicy.SOURCE)
3839
@Target(ElementType.METHOD)
3940
public @interface ItemClick {
40-
int value() default Id.DEFAULT_VALUE;
41+
int [] value() default Id.DEFAULT_VALUE;
4142
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/ItemLongClick.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
*
3737
* @author Benjamin Fellous
3838
* @author Pierre-Yves Ricau
39+
* @author Mathieu Boniface
40+
*
3941
*/
4042
@Retention(RetentionPolicy.SOURCE)
4143
@Target(ElementType.METHOD)
4244
public @interface ItemLongClick {
43-
int value() default Id.DEFAULT_VALUE;
45+
int [] value() default Id.DEFAULT_VALUE;
4446
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/ItemSelect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
* name will be used as the R.id.* field name.
3535
*
3636
* @author Pierre-Yves Ricau
37-
*
37+
* @author Mathieu Boniface
3838
*/
3939
@Retention(RetentionPolicy.SOURCE)
4040
@Target(ElementType.METHOD)
4141
public @interface ItemSelect {
42-
int value() default Id.DEFAULT_VALUE;
42+
int [] value() default Id.DEFAULT_VALUE;
4343
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/LongClick.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
*
3636
* @author Benjamin Fellous
3737
* @author Pierre-Yves Ricau
38+
* @author Mathieu Boniface
39+
*
3840
*/
3941
@Retention(RetentionPolicy.SOURCE)
4042
@Target(ElementType.METHOD)
4143
public @interface LongClick {
42-
int value() default Id.DEFAULT_VALUE;
44+
int [] value() default Id.DEFAULT_VALUE;
4345
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/annotations/Touch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
* The annotation value should be one of R.id.* fields. If not set, the method
3434
* name will be used as the R.id.* field name.
3535
*
36+
* @author Mathieu Boniface
3637
*/
3738
@Retention(RetentionPolicy.SOURCE)
3839
@Target(ElementType.METHOD)
3940
public @interface Touch {
40-
int value() default Id.DEFAULT_VALUE;
41+
int [] value() default Id.DEFAULT_VALUE;
4142
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/IdAnnotationHelper.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.googlecode.androidannotations.helper;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.ArrayList;
20+
import java.util.List;
1921

2022
import javax.annotation.processing.ProcessingEnvironment;
2123
import javax.lang.model.element.Element;
@@ -34,23 +36,27 @@ public IdAnnotationHelper(ProcessingEnvironment processingEnv, Class<? extends A
3436
this.rClass = rClass;
3537
}
3638

37-
public String extractAnnotationQualifiedId(Element element) {
38-
Integer idValue = extractAnnotationValue(element);
39+
public List<String> extractAnnotationQualifiedIds(Element element) {
40+
int [] idsValues = extractAnnotationValue(element);
3941
IRInnerClass rInnerClass = rClass.get(Res.ID);
40-
String clickQualifiedId;
41-
42-
if (idValue.equals(Id.DEFAULT_VALUE)) {
42+
List<String> clickQualifiedIds = new ArrayList<String>();
43+
44+
if (idsValues.length == 1 && idsValues[0] == Id.DEFAULT_VALUE) {
4345
String fieldName = element.getSimpleName().toString();
4446
int lastIndex = fieldName.lastIndexOf(actionName());
4547
if (lastIndex != -1) {
4648
fieldName = fieldName.substring(0, lastIndex);
4749
}
48-
clickQualifiedId = rInnerClass.getIdQualifiedName(fieldName);
50+
String clickQualifiedId = rInnerClass.getIdQualifiedName(fieldName);
51+
clickQualifiedIds.add(clickQualifiedId);
4952

5053
} else {
51-
clickQualifiedId = rInnerClass.getIdQualifiedName(idValue);
54+
for (int idValue : idsValues) {
55+
String clickQualifiedId = rInnerClass.getIdQualifiedName(idValue);
56+
clickQualifiedIds.add(clickQualifiedId);
57+
}
5258
}
53-
return clickQualifiedId;
59+
return clickQualifiedIds;
5460
}
5561

5662
boolean containsIdValue(Integer idValue, Res res) {

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/IdValidatorHelper.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@ public void idExists(Element element, Res res, IsValid valid) {
4040
idExists(element, res, true, valid);
4141
}
4242

43-
public void idExists(Element element, Res res, boolean defaultUseName,
44-
IsValid valid) {
43+
public void idExists(Element element, Res res, boolean defaultUseName, IsValid valid) {
4544

4645
Integer idValue = annotationHelper.extractAnnotationValue(element);
4746

47+
idExists(element, res, defaultUseName, valid, idValue);
48+
}
49+
50+
public void idsExists(Element element, Res res, IsValid valid) {
51+
52+
int [] idsValues = annotationHelper.extractAnnotationValue(element);
53+
if (idsValues[0] == Id.DEFAULT_VALUE) {
54+
idExists(element, res, true, valid, idsValues[0]);
55+
} else {
56+
for (int idValue : idsValues) {
57+
idExists(element, res, false, valid, idValue);
58+
}
59+
}
60+
}
61+
62+
private void idExists(Element element, Res res, boolean defaultUseName,
63+
IsValid valid, Integer idValue) {
4864
if (idValue.equals(Id.DEFAULT_VALUE)) {
4965
if (defaultUseName) {
5066
String elementName = element.getSimpleName().toString();
@@ -81,45 +97,47 @@ public void idExists(Element element, Res res, boolean defaultUseName,
8197
}
8298
}
8399

84-
public void uniqueId(Element element, AnnotationElements validatedElements,
85-
IsValid valid) {
100+
public void uniqueId(Element element, AnnotationElements validatedElements, IsValid valid) {
101+
86102
if (valid.isValid()) {
87103
Element layoutElement = element.getEnclosingElement();
88-
String annotationQualifiedId = idAnnotationHelper
89-
.extractAnnotationQualifiedId(element);
104+
List<String> annotationQualifiedIds = idAnnotationHelper.extractAnnotationQualifiedIds(element);
90105

91106
Set<? extends Element> annotatedElements = validatedElements
92107
.getAnnotatedElements(annotationHelper.getTarget());
108+
93109
for (Element uniqueCheckElement : annotatedElements) {
94-
Element enclosingElement = uniqueCheckElement
95-
.getEnclosingElement();
110+
Element enclosingElement = uniqueCheckElement.getEnclosingElement();
111+
96112
if (layoutElement.equals(enclosingElement)) {
97-
String checkQualifiedId = idAnnotationHelper
98-
.extractAnnotationQualifiedId(uniqueCheckElement);
99-
if (annotationQualifiedId.equals(checkQualifiedId)) {
100-
valid.invalidate();
101-
String annotationSimpleId = annotationQualifiedId
102-
.substring(annotationQualifiedId
103-
.lastIndexOf('.') + 1);
104-
annotationHelper.printAnnotationError(element,
105-
"The id " + annotationSimpleId
106-
+ " is already used on the following "
107-
+ annotationHelper.annotationName()
108-
+ " method: " + uniqueCheckElement);
109-
return;
110-
}
113+
List<String> checkQualifiedIds = idAnnotationHelper.extractAnnotationQualifiedIds(uniqueCheckElement);
114+
115+
for (String checkQualifiedId : checkQualifiedIds) {
116+
for (String annotationQualifiedId : annotationQualifiedIds) {
117+
118+
if (annotationQualifiedId.equals(checkQualifiedId)) {
119+
valid.invalidate();
120+
String annotationSimpleId = annotationQualifiedId
121+
.substring(annotationQualifiedId.lastIndexOf('.') + 1);
122+
annotationHelper.printAnnotationError(element,
123+
"The id " + annotationSimpleId
124+
+ " is already used on the following "
125+
+ annotationHelper.annotationName()
126+
+ " method: " + uniqueCheckElement);
127+
return;
128+
}
129+
}
130+
}
111131
}
112132
}
113133
}
114134
}
115135

116-
public void idListenerMethod(Element element,
117-
AnnotationElements validatedElements, IsValid valid) {
118-
136+
public void idListenerMethod(Element element, AnnotationElements validatedElements, IsValid valid) {
119137

120138
enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
121139

122-
idExists(element, Res.ID, valid);
140+
idsExists(element, Res.ID, valid);
123141

124142
isNotPrivate(element, valid);
125143

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/ClickProcessor.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import javax.lang.model.element.VariableElement;
2424

2525
import com.googlecode.androidannotations.annotations.Click;
26-
import com.googlecode.androidannotations.annotations.Id;
2726
import com.googlecode.androidannotations.rclass.IRClass;
28-
import com.googlecode.androidannotations.rclass.IRClass.Res;
29-
import com.googlecode.androidannotations.rclass.IRInnerClass;
3027
import com.sun.codemodel.JBlock;
3128
import com.sun.codemodel.JClass;
3229
import com.sun.codemodel.JCodeModel;
@@ -38,12 +35,14 @@
3835
import com.sun.codemodel.JMod;
3936
import com.sun.codemodel.JVar;
4037

41-
public class ClickProcessor implements ElementProcessor {
38+
/**
39+
* @author Mathieu Boniface
40+
*/
41+
public class ClickProcessor extends MultipleResIdsBasedProcessor implements ElementProcessor {
4242

43-
private final IRClass rClass;
4443

4544
public ClickProcessor(IRClass rClass) {
46-
this.rClass = rClass;
45+
super(rClass);
4746
}
4847

4948
@Override
@@ -59,14 +58,18 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
5958
String methodName = element.getSimpleName().toString();
6059

6160
ExecutableElement executableElement = (ExecutableElement) element;
62-
List<? extends VariableElement> parameters = executableElement.getParameters();
61+
List<? extends VariableElement> parameters = executableElement
62+
.getParameters();
6363

6464
boolean hasViewParameter = parameters.size() == 1;
6565

66-
JFieldRef idRef = extractClickQualifiedId(element, holder);
66+
Click annotation = element.getAnnotation(Click.class);
67+
List<JFieldRef> idsRefs = extractQualifiedIds(element, annotation.value(), "Clicked", holder);
6768

68-
JDefinedClass onClickListenerClass = codeModel.anonymousClass(holder.refClass("android.view.View.OnClickListener"));
69-
JMethod onClickMethod = onClickListenerClass.method(JMod.PUBLIC, codeModel.VOID, "onClick");
69+
JDefinedClass onClickListenerClass = codeModel.anonymousClass(holder
70+
.refClass("android.view.View.OnClickListener"));
71+
JMethod onClickMethod = onClickListenerClass.method(JMod.PUBLIC,
72+
codeModel.VOID, "onClick");
7073
JClass viewClass = holder.refClass("android.view.View");
7174
JVar onClickViewParam = onClickMethod.param(viewClass, "view");
7275

@@ -76,30 +79,16 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
7679
clickCall.arg(onClickViewParam);
7780
}
7881

79-
JBlock block = holder.afterSetContentView.body().block();
80-
81-
JInvocation findViewById = JExpr.invoke("findViewById");
82-
83-
JVar view = block.decl(viewClass, "view", findViewById.arg(idRef));
84-
block._if(view.ne(JExpr._null()))._then().invoke(view, "setOnClickListener").arg(JExpr._new(onClickListenerClass));
85-
86-
}
82+
for (JFieldRef idRef : idsRefs) {
83+
JBlock block = holder.afterSetContentView.body().block();
8784

88-
private JFieldRef extractClickQualifiedId(Element element, EBeanHolder holder) {
89-
Click annotation = element.getAnnotation(Click.class);
90-
int idValue = annotation.value();
91-
IRInnerClass rInnerClass = rClass.get(Res.ID);
92-
if (idValue == Id.DEFAULT_VALUE) {
93-
String fieldName = element.getSimpleName().toString();
94-
int lastIndex = fieldName.lastIndexOf("Clicked");
95-
if (lastIndex != -1) {
96-
fieldName = fieldName.substring(0, lastIndex);
97-
}
98-
return rInnerClass.getIdStaticRef(fieldName, holder);
99-
100-
} else {
101-
return rInnerClass.getIdStaticRef(idValue, holder);
85+
JInvocation findViewById = JExpr.invoke("findViewById");
86+
JVar view = block.decl(viewClass, "view", findViewById.arg(idRef));
87+
block._if(view.ne(JExpr._null()))._then()
88+
.invoke(view, "setOnClickListener")
89+
.arg(JExpr._new(onClickListenerClass));
10290
}
103-
}
10491

92+
}
93+
10594
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/ItemClickProcessor.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
import javax.lang.model.element.ExecutableElement;
2323
import javax.lang.model.element.VariableElement;
2424

25-
import com.googlecode.androidannotations.annotations.Id;
2625
import com.googlecode.androidannotations.annotations.ItemClick;
2726
import com.googlecode.androidannotations.rclass.IRClass;
28-
import com.googlecode.androidannotations.rclass.IRClass.Res;
29-
import com.googlecode.androidannotations.rclass.IRInnerClass;
3027
import com.sun.codemodel.JBlock;
3128
import com.sun.codemodel.JClass;
3229
import com.sun.codemodel.JCodeModel;
@@ -41,13 +38,12 @@
4138
/**
4239
* @author Benjamin Fellous
4340
* @author Pierre-Yves Ricau
41+
* @author Mathieu Boniface
4442
*/
45-
public class ItemClickProcessor implements ElementProcessor {
46-
47-
private final IRClass rClass;
43+
public class ItemClickProcessor extends MultipleResIdsBasedProcessor implements ElementProcessor {
4844

4945
public ItemClickProcessor(IRClass rClass) {
50-
this.rClass = rClass;
46+
super (rClass);
5147
}
5248

5349
@Override
@@ -66,7 +62,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
6662

6763
boolean hasItemParameter = parameters.size() == 1;
6864

69-
JFieldRef idRef = extractClickQualifiedId(element, holder);
65+
ItemClick annotation = element.getAnnotation(ItemClick.class);
66+
List<JFieldRef> idsRefs = extractQualifiedIds(element, annotation.value(), "ItemClicked", holder);
7067

7168
JDefinedClass onItemClickListenerClass = codeModel.anonymousClass(holder.refClass("android.widget.AdapterView.OnItemClickListener"));
7269
JMethod onItemClickMethod = onItemClickListenerClass.method(JMod.PUBLIC, codeModel.VOID, "onItemClick");
@@ -86,31 +83,14 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
8683
String parameterQualifiedName = parameter.asType().toString();
8784
itemClickCall.arg(JExpr.cast(holder.refClass(parameterQualifiedName), JExpr.invoke(onItemClickParentParam, "getAdapter").invoke("getItem").arg(onItemClickPositionParam)));
8885
}
89-
90-
JBlock block = holder.afterSetContentView.body().block();
91-
92-
JInvocation findViewById = JExpr.invoke("findViewById");
9386

94-
JVar view = block.decl(narrowAdapterViewClass, "view", JExpr.cast(narrowAdapterViewClass, findViewById.arg(idRef)));
95-
block._if(view.ne(JExpr._null()))._then().invoke(view, "setOnItemClickListener").arg(JExpr._new(onItemClickListenerClass));
96-
}
97-
98-
private JFieldRef extractClickQualifiedId(Element element, EBeanHolder holder) {
99-
ItemClick annotation = element.getAnnotation(ItemClick.class);
100-
int idValue = annotation.value();
101-
IRInnerClass rInnerClass = rClass.get(Res.ID);
102-
if (idValue == Id.DEFAULT_VALUE) {
103-
String fieldName = element.getSimpleName().toString();
104-
int lastIndex = fieldName.lastIndexOf("ItemClicked");
105-
if (lastIndex != -1) {
106-
fieldName = fieldName.substring(0, lastIndex);
107-
}
108-
return rInnerClass.getIdStaticRef(fieldName, holder);
109-
110-
} else {
111-
return rInnerClass.getIdStaticRef(idValue, holder);
87+
for (JFieldRef idRef : idsRefs) {
88+
JBlock block = holder.afterSetContentView.body().block();
89+
JInvocation findViewById = JExpr.invoke("findViewById");
90+
91+
JVar view = block.decl(narrowAdapterViewClass, "view", JExpr.cast(narrowAdapterViewClass, findViewById.arg(idRef)));
92+
block._if(view.ne(JExpr._null()))._then().invoke(view, "setOnItemClickListener").arg(JExpr._new(onItemClickListenerClass));
11293
}
11394
}
11495

115-
11696
}

0 commit comments

Comments
 (0)