Skip to content

Commit 0732dc3

Browse files
WonderCsabododgex
authored andcommitted
Validate referenced generated types
If some annotation depends on other generating annotations, we should validate if the other annotations are ok, otherwise we will generate not compilable code.
1 parent c0d303e commit 0732dc3

8 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationsEnvironment.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -61,6 +62,8 @@ public interface AndroidAnnotationsEnvironment {
6162

6263
AndroidManifest getAndroidManifest();
6364

65+
AnnotationElements getExtractedElements();
66+
6467
AnnotationElements getValidatedElements();
6568

6669
JCodeModel getCodeModel();

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ public void typeHasAnnotation(Class<? extends Annotation> annotation, Element el
308308
typeHasAnnotation(annotation, elementType, valid);
309309
}
310310

311+
public void typeHasValidAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) {
312+
typeHasAnnotation(annotation, element, valid);
313+
314+
if (valid.isValid()) {
315+
typeIsValid(annotation, element.asType(), valid);
316+
}
317+
}
318+
311319
public void typeHasAnnotation(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation valid) {
312320
Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);
313321
if (!elementHasAnnotationSafe(annotation, typeElement)) {
@@ -324,14 +332,14 @@ public void typeOrTargetValueHasAnnotation(Class<? extends Annotation> annotatio
324332
DeclaredType targetAnnotationClassValue = annotationHelper.extractAnnotationClassParameter(element);
325333

326334
if (targetAnnotationClassValue != null) {
327-
typeHasAnnotation(annotation, targetAnnotationClassValue, valid);
335+
targetElement = targetAnnotationClassValue.asElement();
328336

329337
if (!annotationHelper.getTypeUtils().isAssignable(targetAnnotationClassValue, targetElement.asType())) {
330338
valid.addError("The value of %s must be assignable into the annotated field");
331339
}
332-
} else {
333-
typeHasAnnotation(annotation, targetElement, valid);
334340
}
341+
342+
typeHasValidAnnotation(annotation, targetElement, valid);
335343
}
336344

337345
Element findTargetElement(Element element, ElementValidation valid) {
@@ -352,6 +360,20 @@ Element findTargetElement(Element element, ElementValidation valid) {
352360
return element;
353361
}
354362

363+
public void typeIsValid(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation elementValidation) {
364+
Set<? extends Element> validElements = validatedModel().getRootAnnotatedElements(annotation.getName());
365+
366+
Set<? extends Element> extractedElements = environment().getExtractedElements().getRootAnnotatedElements(annotation.getName());
367+
368+
Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);
369+
370+
if (!extractedElements.contains(typeElement) || validElements.contains(typeElement)) {
371+
return;
372+
}
373+
374+
elementValidation.addError("The type " + typeElement.getSimpleName() + " is invalid, " + "please check the messages on that type.");
375+
}
376+
355377
private boolean elementHasAnnotationSafe(Class<? extends Annotation> annotation, Element element) {
356378
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
357379
for (AnnotationMirror annotationMirror : annotationMirrors) {

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/AndroidAnnotationProcessor.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ private void processThrowing(Set<? extends TypeElement> annotations, RoundEnviro
154154
}
155155

156156
AnnotationElementsHolder extractedModel = extractAnnotations(annotations, roundEnv);
157+
androidAnnotationsEnv.setExtractedElements(extractedModel);
158+
157159
AnnotationElementsHolder validatingHolder = extractedModel.validatingHolder();
158160
androidAnnotationsEnv.setValidatedElements(validatingHolder);
159161

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/InternalAndroidAnnotationsEnvironment.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -48,6 +49,8 @@ public class InternalAndroidAnnotationsEnvironment implements AndroidAnnotations
4849
private IRClass rClass;
4950
private AndroidManifest androidManifest;
5051

52+
private AnnotationElements extractedElements;
53+
5154
private AnnotationElements validatedElements;
5255

5356
private ProcessHolder processHolder;
@@ -73,6 +76,10 @@ public void setAndroidEnvironment(IRClass rClass, AndroidManifest androidManifes
7376
this.androidManifest = androidManifest;
7477
}
7578

79+
public void setExtractedElements(AnnotationElements extractedElements) {
80+
this.extractedElements = extractedElements;
81+
}
82+
7683
public void setValidatedElements(AnnotationElements validatedElements) {
7784
this.validatedElements = validatedElements;
7885
}
@@ -141,6 +148,11 @@ public AndroidManifest getAndroidManifest() {
141148
return androidManifest;
142149
}
143150

151+
@Override
152+
public AnnotationElements getExtractedElements() {
153+
return extractedElements;
154+
}
155+
144156
@Override
145157
public AnnotationElements getValidatedElements() {
146158
return validatedElements;

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AppHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void validate(Element element, ElementValidation validation) {
5252
validatorHelper.isNotPrivate(element, validation);
5353

5454
Element param = injectHelper.getParam(element);
55-
validatorHelper.typeHasAnnotation(EApplication.class, param, validation);
55+
validatorHelper.typeHasValidAnnotation(EApplication.class, param, validation);
5656
}
5757

5858
@Override

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/NonConfigurationInstanceHandler.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -51,6 +52,10 @@ public void validate(Element element, ElementValidation validation) {
5152
validatorHelper.isNotPrivate(element, validation);
5253

5354
validatorHelper.isNotFinal(element, validation);
55+
56+
if (element.getAnnotation(Bean.class) != null) {
57+
validatorHelper.typeIsValid(EBean.class, element.asType(), validation);
58+
}
5459
}
5560

5661
@Override

‎AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/RestServiceHandler.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -53,7 +54,7 @@ public void validate(Element element, ElementValidation validation) {
5354
validatorHelper.isNotPrivate(element, validation);
5455

5556
Element param = injectHelper.getParam(element);
56-
validatorHelper.typeHasAnnotation(Rest.class, param, validation);
57+
validatorHelper.typeHasValidAnnotation(Rest.class, param, validation);
5758
}
5859

5960
@Override

‎AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestSpringValidatorHelper.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
3+
* Copyright (C) 2016-2017 the AndroidAnnotations project
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
56
* use this file except in compliance with the License. You may obtain a copy of
@@ -263,6 +264,8 @@ public void validateConverters(Element element, ElementValidation valid) {
263264
if (!hasPublicWithNoArgumentConstructor) {
264265
valid.addError("The converter class must have a public no argument constructor");
265266
}
267+
} else {
268+
typeIsValid(EBean.class, converterType, valid);
266269
}
267270
} else {
268271
valid.addError("The converter class must not be abstract");
@@ -300,6 +303,8 @@ public void validateInterceptors(Element element, ElementValidation valid) {
300303
if (!hasPublicWithNoArgumentConstructor) {
301304
valid.addError("The interceptor class must have a public no argument constructor or be annotated with @EBean");
302305
}
306+
} else {
307+
typeIsValid(EBean.class, interceptorType, valid);
303308
}
304309
} else {
305310
valid.addError("The interceptor class must not be abstract");
@@ -322,6 +327,7 @@ public void validateRestSimpleParameter(Element element, String requiredClass, S
322327
if (parameterElement.getKind().isClass()) {
323328
if (!annotationHelper.isAbstract(parameterElement)) {
324329
if (parameterElement.getAnnotation(EBean.class) != null) {
330+
typeIsValid(EBean.class, paramterType, validation);
325331
return;
326332
}
327333
List<ExecutableElement> constructors = ElementFilter.constructorsIn(parameterElement.getEnclosedElements());

0 commit comments

Comments
 (0)