Skip to content

Commit 3efcc09

Browse files
pyricauJoanZapata
authored andcommitted
Second step androidannotations#9: adding inner annotations for the new components, testing the generation, and fixing a few bugs
1 parent 0d5b3a3 commit 3efcc09

18 files changed

Lines changed: 222 additions & 84 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void uniqueId(Element element, AnnotationElements validatedElements, IsVa
123123

124124
public void idListenerMethod(Element element, AnnotationElements validatedElements, IsValid valid) {
125125

126-
enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
126+
enclosingElementHasEnhancedViewSupportAnnotation(element, validatedElements, valid);
127127

128128
idsExists(element, Res.ID, valid);
129129

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import android.util.Log;
4141

4242
import com.googlecode.androidannotations.annotations.EActivity;
43+
import com.googlecode.androidannotations.annotations.EProvider;
44+
import com.googlecode.androidannotations.annotations.EReceiver;
45+
import com.googlecode.androidannotations.annotations.EService;
4346
import com.googlecode.androidannotations.annotations.EViewGroup;
4447
import com.googlecode.androidannotations.annotations.Enhanced;
4548
import com.googlecode.androidannotations.annotations.Extra;
@@ -81,7 +84,6 @@ public class ValidatorHelper {
8184
private static final String ANDROID_SQLITE_DB_QUALIFIED_NAME = "android.database.sqlite.SQLiteDatabase";
8285
private static final String GUICE_INJECTOR_QUALIFIED_NAME = "com.google.inject.Injector";
8386
private static final String ROBOGUICE_INJECTOR_PROVIDER_QUALIFIED_NAME = "roboguice.inject.InjectorProvider";
84-
8587

8688
private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", "java.lang.String");
8789

@@ -90,7 +92,10 @@ public class ValidatorHelper {
9092
private static final Collection<Integer> VALID_LOG_LEVELS = Arrays.asList(Log.VERBOSE, Log.DEBUG, Log.INFO, Log.WARN, Log.ERROR);
9193

9294
@SuppressWarnings("unchecked")
93-
private static final List<Class<? extends Annotation>> VALID_EBEAN_ANNOTATIONS = Arrays.asList(EActivity.class, EViewGroup.class, Enhanced.class);
95+
private static final List<Class<? extends Annotation>> VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS = Arrays.asList(EActivity.class, EViewGroup.class, Enhanced.class);
96+
97+
@SuppressWarnings("unchecked")
98+
private static final List<Class<? extends Annotation>> VALID_ENHANCED_COMPONENT_ANNOTATIONS = Arrays.asList(EActivity.class, EViewGroup.class, Enhanced.class, EService.class, EReceiver.class, EProvider.class);
9499

95100
protected final TargetAnnotationHelper annotationHelper;
96101

@@ -139,81 +144,78 @@ public void isNotPrivate(Element element, IsValid valid) {
139144
annotationHelper.printAnnotationError(element, "%s cannot be used on a private element");
140145
}
141146
}
142-
147+
143148
public void enclosingElementHasEnhanced(Element element, AnnotationElements validatedElements, IsValid valid) {
144149
Element enclosingElement = element.getEnclosingElement();
145-
hasEnhanced(element, enclosingElement, validatedElements, valid);
150+
hasClassAnnotation(element, enclosingElement, validatedElements, Enhanced.class, valid);
146151
}
147152

148-
public void hasEnhanced(Element reportElement, Element element, AnnotationElements validatedElements, IsValid valid) {
149-
150-
Set<? extends Element> layoutAnnotatedElements = validatedElements.getAnnotatedElements(Enhanced.class);
151-
152-
if (!layoutAnnotatedElements.contains(element)) {
153-
valid.invalidate();
154-
if (element.getAnnotation(Enhanced.class) == null) {
155-
annotationHelper.printAnnotationError(reportElement, "%s can only be used in a class annotated with " + TargetAnnotationHelper.annotationName(Enhanced.class));
156-
}
157-
}
158-
}
159-
160153
public void enclosingElementHasEActivity(Element element, AnnotationElements validatedElements, IsValid valid) {
161154
Element enclosingElement = element.getEnclosingElement();
162-
hasEActivity(element, enclosingElement, validatedElements, valid);
155+
hasClassAnnotation(element, enclosingElement, validatedElements, EActivity.class, valid);
163156
}
164157

165158
public void hasEActivity(Element element, AnnotationElements validatedElements, IsValid valid) {
166-
hasEActivity(element, element, validatedElements, valid);
159+
hasClassAnnotation(element, element, validatedElements, EActivity.class, valid);
167160
}
168161

169-
public void hasEActivity(Element reportElement, Element element, AnnotationElements validatedElements, IsValid valid) {
170-
171-
Set<? extends Element> layoutAnnotatedElements = validatedElements.getAnnotatedElements(EActivity.class);
172-
173-
if (!layoutAnnotatedElements.contains(element)) {
174-
valid.invalidate();
175-
if (element.getAnnotation(EActivity.class) == null) {
176-
annotationHelper.printAnnotationError(reportElement, "%s can only be used in a class annotated with " + TargetAnnotationHelper.annotationName(EActivity.class));
177-
}
178-
}
162+
public void enclosingElementHasEnhancedViewSupportAnnotation(Element element, AnnotationElements validatedElements, IsValid valid) {
163+
Element enclosingElement = element.getEnclosingElement();
164+
hasOneOfClassAnnotations(element, enclosingElement, validatedElements, VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS, valid);
179165
}
180166

181-
public void enclosingElementHasEBeanAnnotation(Element element, AnnotationElements validatedElements, IsValid valid) {
167+
public void enclosingElementHasEnhancedComponentAnnotation(Element element, AnnotationElements validatedElements, IsValid valid) {
182168
Element enclosingElement = element.getEnclosingElement();
183-
hasEBeanAnnotation(element, enclosingElement, validatedElements, valid);
169+
hasOneOfClassAnnotations(element, enclosingElement, validatedElements, VALID_ENHANCED_COMPONENT_ANNOTATIONS, valid);
184170
}
185-
186-
public void hasEBeanAnnotation(Element element, AnnotationElements validatedElements, IsValid valid) {
187-
hasEBeanAnnotation(element, element, validatedElements, valid);
171+
172+
private void hasClassAnnotation(Element reportElement, Element element, AnnotationElements validatedElements, Class<? extends Annotation> validAnnotation, IsValid valid) {
173+
ArrayList<Class<? extends Annotation>> validAnnotations = new ArrayList<Class<? extends Annotation>>();
174+
validAnnotations.add(validAnnotation);
175+
hasOneOfClassAnnotations(reportElement, element, validatedElements, validAnnotations, valid);
188176
}
189177

190-
public void hasEBeanAnnotation(Element reportElement, Element element, AnnotationElements validatedElements, IsValid valid) {
178+
private void hasOneOfClassAnnotations(Element reportElement, Element element, AnnotationElements validatedElements, List<Class<? extends Annotation>> validAnnotations, IsValid valid) {
179+
180+
boolean foundAnnotation = false;
181+
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
182+
if (element.getAnnotation(validAnnotation) != null) {
183+
184+
Set<? extends Element> layoutAnnotatedElements = validatedElements.getAnnotatedElements(validAnnotation);
191185

192-
boolean ok = false;
193-
for (Class<? extends Annotation> a : VALID_EBEAN_ANNOTATIONS) {
194-
if (element.getAnnotation(a) != null) {
195-
ok = true;
186+
/*
187+
* This is for the case where the element has the right
188+
* annotation, but that annotation was not validated. We do not
189+
* add any compile error (should already exist on the
190+
* annotation), but we still invalidate this element.
191+
*/
192+
if (!layoutAnnotatedElements.contains(element)) {
193+
valid.invalidate();
194+
}
195+
196+
foundAnnotation = true;
197+
break;
196198
}
197199
}
198200

199-
if (!ok) {
201+
if (!foundAnnotation) {
200202
valid.invalidate();
201-
annotationHelper.printAnnotationError(reportElement, "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes() + ".");
203+
annotationHelper.printAnnotationError(reportElement, "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
202204
}
203205
}
204206

205-
private StringBuilder getFormattedValidEnhancedBeanAnnotationTypes() {
207+
private String getFormattedValidEnhancedBeanAnnotationTypes(List<Class<? extends Annotation>> annotations) {
206208
StringBuilder sb = new StringBuilder();
207-
if (VALID_EBEAN_ANNOTATIONS.isEmpty())
208-
return sb;
209-
210-
sb.append(TargetAnnotationHelper.annotationName(VALID_EBEAN_ANNOTATIONS.get(0)));
209+
if (!annotations.isEmpty()) {
210+
sb.append(TargetAnnotationHelper.annotationName(annotations.get(0)));
211211

212-
for (int i = 1; i < VALID_EBEAN_ANNOTATIONS.size(); i++) {
213-
sb.append(TargetAnnotationHelper.annotationName(VALID_EBEAN_ANNOTATIONS.get(i)));
212+
for (int i = 1; i < annotations.size(); i++) {
213+
sb.append(", ");
214+
sb.append(TargetAnnotationHelper.annotationName(annotations.get(i)));
215+
}
214216
}
215217

216-
return sb;
218+
return sb.toString();
217219
}
218220

219221
public void hasExtraValue(Element element, IsValid valid) {
@@ -479,18 +481,18 @@ public void zeroOrOneBundleParameter(ExecutableElement executableElement, IsVali
479481
public void extendsActivity(Element element, IsValid valid) {
480482
extendsType(element, ANDROID_ACTIVITY_QUALIFIED_NAME, valid);
481483
}
482-
484+
483485
public void extendsService(Element element, IsValid valid) {
484486
extendsType(element, ANDROID_SERVICE_QUALIFIED_NAME, valid);
485-
}
486-
487+
}
488+
487489
public void extendsReceiver(Element element, IsValid valid) {
488490
extendsType(element, ANDROID_RECEIVER_QUALIFIED_NAME, valid);
489-
}
490-
491+
}
492+
491493
public void extendsProvider(Element element, IsValid valid) {
492494
extendsType(element, ANDROID_PROVIDER_QUALIFIED_NAME, valid);
493-
}
495+
}
494496

495497
public void extendsView(Element element, IsValid valid) {
496498
extendsType(element, ANDROID_VIEW_QUALIFIED_NAME, valid);
@@ -507,7 +509,7 @@ public void extendsViewGroup(Element element, IsValid valid) {
507509
public void extendsApplication(Element element, IsValid valid) {
508510
extendsType(element, ANDROID_APPLICATION_QUALIFIED_NAME, valid);
509511
}
510-
512+
511513
public void extendsContext(Element element, IsValid valid) {
512514
extendsType(element, ANDROID_CONTEXT_QUALIFIED_NAME, valid);
513515
}
@@ -836,9 +838,9 @@ public void hasEmptyConstructor(Element element, IsValid valid) {
836838
List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements());
837839

838840
if (constructors.size() == 1) {
839-
841+
840842
ExecutableElement constructor = constructors.get(0);
841-
843+
842844
if (!annotationHelper.isPrivate(constructor)) {
843845
if (constructor.getParameters().size() != 0) {
844846
annotationHelper.printError(element, "%s annotated element should have an empty constructor");
@@ -865,7 +867,7 @@ public void hasValidLogLevel(Element element, IsValid isValid) {
865867
}
866868

867869
}
868-
870+
869871
public void componentRegistered(Element element, AndroidManifest androidManifest, IsValid valid) {
870872
TypeElement typeElement = (TypeElement) element;
871873

@@ -888,6 +890,6 @@ public void componentRegistered(Element element, AndroidManifest androidManifest
888890
}
889891
}
890892

891-
}
893+
}
892894

893895
}

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/InjectProcessor.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
4545
String fieldName = element.getSimpleName().toString();
4646

4747
TypeMirror elementType = element.asType();
48-
48+
4949
String typeQualifiedName = elementType.toString();
50-
50+
5151
JClass injectedClass = codeModel.ref(typeQualifiedName + GENERATION_SUFFIX);
52-
52+
5353
{
5454
// getInstance
5555
JBlock body = holder.init.body();
56-
57-
56+
5857
JInvocation getInstance = injectedClass.staticInvoke(GET_INSTANCE_METHOD_NAME).arg(holder.contextRef);
5958
body.assign(ref(fieldName), getInstance);
6059
}
61-
60+
6261
{
6362
// afterSetContentView
64-
65-
JBlock body = holder.afterSetContentView.body();
66-
67-
body.invoke(cast(injectedClass, ref(fieldName)), holder.afterSetContentView);
63+
if (holder.afterSetContentView != null) {
64+
JBlock body = holder.afterSetContentView.body();
65+
66+
body.invoke(cast(injectedClass, ref(fieldName)), holder.afterSetContentView);
67+
}
6868
}
69-
69+
7070
}
7171

7272
}

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/AfterViewsValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4545

4646
IsValid valid = new IsValid();
4747

48-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
48+
validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validatedElements, valid);
4949

5050
ExecutableElement executableElement = (ExecutableElement) element;
5151

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/AppValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4747

4848
IsValid valid = new IsValid();
4949

50-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
50+
validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validatedElements, valid);
5151

5252
validatorHelper.extendsApplication(element, valid);
5353

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/ExtraValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Class<? extends Annotation> getTarget() {
4343
public boolean validate(Element element, AnnotationElements validatedElements) {
4444
IsValid valid = new IsValid();
4545

46-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
46+
validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validatedElements, valid);
4747

4848
validatorHelper.isNotPrivate(element, valid);
4949

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/InjectValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4646

4747
IsValid valid = new IsValid();
4848

49-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
49+
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validatedElements, valid);
5050

5151
validatorHelper.isNotPrivate(element, valid);
5252

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/ResValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4848

4949
IsValid valid = new IsValid();
5050

51-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
51+
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validatedElements, valid);
5252

5353
TypeMirror fieldTypeMirror = element.asType();
5454

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/RestServiceValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4545

4646
IsValid valid = new IsValid();
4747

48-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
48+
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validatedElements, valid);
4949

5050
validatorHelper.isNotPrivate(element, valid);
5151

‎AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/validation/RunnableValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
4646

4747
IsValid valid = new IsValid();
4848

49-
validatorHelper.enclosingElementHasEBeanAnnotation(element, validatedElements, valid);
49+
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validatedElements, valid);
5050

5151
ExecutableElement executableElement = (ExecutableElement) element;
5252

0 commit comments

Comments
 (0)