Skip to content

Commit 1fe8511

Browse files
committed
AA fails and stop processing if the R class cannot be found
1 parent 48c318e commit 1fe8511

10 files changed

Lines changed: 93 additions & 24 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,13 @@ private void processThrowing(Set<? extends TypeElement> annotations, RoundEnviro
364364

365365
AndroidManifest androidManifest = androidManifestOption.get();
366366

367-
IRClass rClass = findRClasses(androidManifest);
367+
Option<IRClass> rClassOption = findRClasses(androidManifest);
368+
369+
if (rClassOption.isAbsent()) {
370+
return;
371+
}
372+
373+
IRClass rClass = rClassOption.get();
368374

369375
AndroidSystemServices androidSystemServices = new AndroidSystemServices();
370376

@@ -395,20 +401,25 @@ private Option<AndroidManifest> extractAndroidManifest() {
395401
return manifest;
396402
}
397403

398-
private IRClass findRClasses(AndroidManifest androidManifest) throws IOException {
404+
private Option<IRClass> findRClasses(AndroidManifest androidManifest) throws IOException {
399405
timeStats.start("Find R Classes");
400406
ProjectRClassFinder rClassFinder = new ProjectRClassFinder(processingEnv);
401-
IRClass rClass = rClassFinder.find(androidManifest);
407+
408+
Option<IRClass> rClass = rClassFinder.find(androidManifest);
402409

403410
AndroidRClassFinder androidRClassFinder = new AndroidRClassFinder(processingEnv);
404411

405-
IRClass androidRClass = androidRClassFinder.find();
412+
Option<IRClass> androidRClass = androidRClassFinder.find();
413+
414+
if (rClass.isAbsent() || androidRClass.isAbsent()) {
415+
return Option.absent();
416+
}
406417

407-
CoumpoundRClass coumpoundRClass = new CoumpoundRClass(rClass, androidRClass);
418+
IRClass coumpoundRClass = new CoumpoundRClass(rClass.get(), androidRClass.get());
408419

409420
timeStats.stop("Find R Classes");
410421

411-
return coumpoundRClass;
422+
return Option.of(coumpoundRClass);
412423
}
413424

414425
private AnnotationElements validateAnnotations(AnnotationElementsHolder extractedModel, IRClass rClass, AndroidSystemServices androidSystemServices, AndroidManifest androidManifest) {

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/rclass/AndroidRClassFinder.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.androidannotations.rclass;
1717

18+
import javax.annotation.processing.Messager;
1819
import javax.annotation.processing.ProcessingEnvironment;
1920
import javax.lang.model.element.TypeElement;
21+
import javax.tools.Diagnostic.Kind;
22+
23+
import org.androidannotations.helper.Option;
2024

2125
public class AndroidRClassFinder {
2226

@@ -26,9 +30,13 @@ public AndroidRClassFinder(ProcessingEnvironment processingEnv) {
2630
this.processingEnv = processingEnv;
2731
}
2832

29-
public IRClass find() {
33+
public Option<IRClass> find() {
3034
TypeElement androidRType = processingEnv.getElementUtils().getTypeElement("android.R");
31-
return new RClass(androidRType);
35+
if (androidRType == null) {
36+
Messager messager = processingEnv.getMessager();
37+
messager.printMessage(Kind.ERROR, "The android.R class cannot be found");
38+
return Option.absent();
39+
}
40+
return Option.<IRClass> of(new RClass(androidRType));
3241
}
33-
3442
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/rclass/IRClass.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,4 @@ public String rName() {
2828

2929
IRInnerClass get(Res res);
3030

31-
final IRClass EMPTY_R_CLASS = new IRClass() {
32-
@Override
33-
public IRInnerClass get(Res res) {
34-
return IRInnerClass.EMPTY_R_INNER_CLASS;
35-
}
36-
};
37-
3831
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/rclass/ProjectRClassFinder.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.tools.Diagnostic.Kind;
2323

2424
import org.androidannotations.helper.AndroidManifest;
25+
import org.androidannotations.helper.Option;
2526

2627
public class ProjectRClassFinder {
2728

@@ -31,19 +32,18 @@ public ProjectRClassFinder(ProcessingEnvironment processingEnv) {
3132
this.processingEnv = processingEnv;
3233
}
3334

34-
public IRClass find(AndroidManifest manifest) {
35+
public Option<IRClass> find(AndroidManifest manifest) {
3536

3637
Elements elementUtils = processingEnv.getElementUtils();
3738
String rClass = manifest.getApplicationPackage() + ".R";
3839
TypeElement rType = elementUtils.getTypeElement(rClass);
3940

4041
if (rType == null) {
4142
Messager messager = processingEnv.getMessager();
42-
messager.printMessage(Kind.WARNING, "The AndroidManifest.xml file was found, but not the compiled R class: " + rClass);
43-
return IRClass.EMPTY_R_CLASS;
43+
messager.printMessage(Kind.ERROR, "The generated " + rClass + " class cannot be found");
44+
return Option.absent();
4445
}
4546

46-
return new RClass(rType);
47+
return Option.<IRClass> of(new RClass(rType));
4748
}
48-
4949
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package android;
2+
3+
public class R {
4+
5+
}

‎AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/manifest/AndroidManifestFinderTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void setup() {
3636
}
3737

3838
@Test
39-
public void fails_if_no_manifest() throws Exception {
39+
public void fails_if_no_manifest() {
4040
CompileResult result = compileFiles(SomeClass.class);
4141
assertCompilationErrorWithNoSource(result);
4242
assertCompilationErrorCount(1, result);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.androidannotations.rclass;
2+
3+
import org.androidannotations.AndroidAnnotationProcessor;
4+
import org.androidannotations.manifest.SomeClass;
5+
import org.androidannotations.utils.AAProcessorTestHelper;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
public class RClassFinderTest extends AAProcessorTestHelper {
10+
11+
@Before
12+
public void setup() {
13+
addManifestProcessorParameter(RClassFinderTest.class);
14+
addProcessor(AndroidAnnotationProcessor.class);
15+
}
16+
17+
@Test
18+
public void fails_if_cannot_find_R_class() {
19+
CompileResult result = compileFiles(SomeClass.class);
20+
assertCompilationErrorWithNoSource(result);
21+
assertCompilationErrorCount(1, result);
22+
}
23+
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.androidannotations.testprocessor;
2+
3+
public class R {
4+
5+
}

‎AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/utils/ProcessorTestHelper.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ private static void assertCompilationDiagnostingOn(Kind expectedDiagnosticKind,
158158
}
159159
}
160160
}
161-
162161
}
163162
}
164-
fail("Expected a compilation " + expectedDiagnosticKind + ", diagnostics: " + result.diagnostics);
163+
fail("Expected a compilation " + expectedDiagnosticKind + " in " + expectedErrorClassFile.toString() + " on " + expectedContentInError + ", diagnostics: " + result.diagnostics);
165164
}
166165

167166
private static String[] getContents(File file) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2012 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
20+
package="some.package"
21+
android:versionCode="1"
22+
android:versionName="1.0" >
23+
24+
</manifest>

0 commit comments

Comments
 (0)