diff options
author | Yohann Roussel <yroussel@google.com> | 2014-10-30 16:00:48 +0100 |
---|---|---|
committer | Yohann Roussel <yroussel@google.com> | 2014-10-30 16:15:39 +0100 |
commit | 41a1ddc6f667aa7b4770f71bac7d8752c14e3ce9 (patch) | |
tree | 51de74c2aa8aa3118fba89e1a9a3d260fe2a51b3 | |
parent | a3a1602ec3a07f1b904902bf676ca0228401f78a (diff) | |
download | toolchain_jack-41a1ddc6f667aa7b4770f71bac7d8752c14e3ce9.zip toolchain_jack-41a1ddc6f667aa7b4770f71bac7d8752c14e3ce9.tar.gz toolchain_jack-41a1ddc6f667aa7b4770f71bac7d8752c14e3ce9.tar.bz2 |
Allow jack preprocessor to add phantoms.
This will allow to do legacy multidex without adding the annotation library
in the classpath.
Change-Id: If300fd64afd2f05f5e0605b8056af59e44e8972f
5 files changed, 159 insertions, 12 deletions
diff --git a/jack/src/com/android/jack/preprocessor/AddAnnotationExpression.java b/jack/src/com/android/jack/preprocessor/AddAnnotationExpression.java index 65f252b..748416d 100644 --- a/jack/src/com/android/jack/preprocessor/AddAnnotationExpression.java +++ b/jack/src/com/android/jack/preprocessor/AddAnnotationExpression.java @@ -17,7 +17,7 @@ package com.android.jack.preprocessor; import com.android.jack.ir.ast.Annotable; -import com.android.jack.ir.ast.JDefinedAnnotation; +import com.android.jack.ir.ast.JAnnotation; import java.util.Collection; @@ -29,11 +29,11 @@ import javax.annotation.Nonnull; public class AddAnnotationExpression<T> implements Expression<Collection<T>, Scope> { @Nonnull - private final JDefinedAnnotation toAdd; + private final JAnnotation toAdd; @Nonnull private final Expression<Collection<T>, Scope> on; - public AddAnnotationExpression(@Nonnull JDefinedAnnotation toAdd, + public AddAnnotationExpression(@Nonnull JAnnotation toAdd, @Nonnull Expression<Collection<T>, Scope> on) { this.toAdd = toAdd; this.on = on; diff --git a/jack/src/com/android/jack/preprocessor/AddAnnotationStep.java b/jack/src/com/android/jack/preprocessor/AddAnnotationStep.java index 6c6a946..0912a19 100644 --- a/jack/src/com/android/jack/preprocessor/AddAnnotationStep.java +++ b/jack/src/com/android/jack/preprocessor/AddAnnotationStep.java @@ -17,9 +17,11 @@ package com.android.jack.preprocessor; import com.android.jack.ir.ast.Annotable; +import com.android.jack.ir.ast.JAnnotation; import com.android.jack.ir.ast.JAnnotationLiteral; import com.android.jack.ir.ast.JDefinedAnnotation; import com.android.jack.ir.ast.JNode; +import com.android.jack.ir.ast.JRetentionPolicy; import com.android.jack.ir.sourceinfo.SourceInfo; import com.android.jack.transformations.request.TransformationStep; @@ -33,12 +35,12 @@ import javax.annotation.Nonnull; public class AddAnnotationStep implements TransformationStep { @Nonnull - private final JDefinedAnnotation annotation; + private final JAnnotation annotation; @Nonnull private final Collection<?> toAnnotate; - public AddAnnotationStep(@Nonnull JDefinedAnnotation annotation, + public AddAnnotationStep(@Nonnull JAnnotation annotation, @Nonnull Collection<?> toAnnotate) { this.annotation = annotation; this.toAnnotate = toAnnotate; @@ -51,8 +53,12 @@ public class AddAnnotationStep implements TransformationStep { Annotable annotable = (Annotable) candidate; // Do not override existing annotation if (annotable.getAnnotations(annotation).isEmpty()) { + JRetentionPolicy retention = JRetentionPolicy.SOURCE; + if (annotation instanceof JDefinedAnnotation) { + retention = ((JDefinedAnnotation) annotation).getRetentionPolicy(); + } JAnnotationLiteral literal = new JAnnotationLiteral( - SourceInfo.UNKNOWN, annotation.getRetentionPolicy(), annotation); + SourceInfo.UNKNOWN, retention, annotation); annotable.addAnnotation(literal); literal.updateParents((JNode) annotable); } diff --git a/jack/src/com/android/jack/preprocessor/PreProcessor.g b/jack/src/com/android/jack/preprocessor/PreProcessor.g index a0c7755..280849f 100644 --- a/jack/src/com/android/jack/preprocessor/PreProcessor.g +++ b/jack/src/com/android/jack/preprocessor/PreProcessor.g @@ -21,7 +21,7 @@ import Java; package com.android.jack.preprocessor; import com.android.jack.ir.ast.HasModifier; -import com.android.jack.ir.ast.JDefinedAnnotation; +import com.android.jack.ir.ast.JAnnotation; import com.android.jack.ir.ast.JClassOrInterface; import com.android.jack.ir.ast.JNode; import com.android.jack.ir.ast.JSession; @@ -68,7 +68,7 @@ set returns [Expression<Collection<?>, Scope> set] $toAdd.add, (Expression<Collection<JNode>, Scope>)(Object)$annotated.set);}) ; -addAnnotation returns [JDefinedAnnotation add] +addAnnotation returns [JAnnotation add] : '@@' name=Identifier {add = builder.getAnnotation($name.text);} ; diff --git a/jack/src/com/android/jack/preprocessor/RuleBuilder.java b/jack/src/com/android/jack/preprocessor/RuleBuilder.java index bf575c0..b0a50cf 100644 --- a/jack/src/com/android/jack/preprocessor/RuleBuilder.java +++ b/jack/src/com/android/jack/preprocessor/RuleBuilder.java @@ -16,10 +16,9 @@ package com.android.jack.preprocessor; -import com.android.jack.ir.ast.JDefinedAnnotation; +import com.android.jack.ir.ast.JAnnotation; import com.android.jack.ir.ast.JSession; import com.android.jack.ir.ast.JType; -import com.android.jack.lookup.JLookupException; import com.android.jack.util.NamingTools; import java.util.Collection; @@ -49,8 +48,8 @@ class RuleBuilder { } @Nonnull - public JDefinedAnnotation getAnnotation(@Nonnull String annotationName) throws JLookupException { - return session.getLookup().getAnnotation( + public JAnnotation getAnnotation(@Nonnull String annotationName) { + return session.getPhantomLookup().getAnnotation( NamingTools.getTypeSignatureName(annotationName)); } diff --git a/jack/tests/com/android/jack/multidex/MultiDexTests.java b/jack/tests/com/android/jack/multidex/MultiDexTests.java index 39a9018..bf53e86 100644 --- a/jack/tests/com/android/jack/multidex/MultiDexTests.java +++ b/jack/tests/com/android/jack/multidex/MultiDexTests.java @@ -116,6 +116,67 @@ public class MultiDexTests { return; } + @Test + public void versionedTest001a_withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test001"); + File out = TestTools.createTempDir("out", ""); + Options app1Options = createCommonOptionsForMultiDex(new File(testFolder, "config-001.jpp")); + + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "multidex"); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath(), + out, false); + + File outList = getListingOfDex(new File(out, "classes.dex")); + ListingComparator.compare(new File(testFolder, "ref-list-001.txt"), outList); + Assert.assertFalse(new File(out, "classes2.dex").exists()); + return; + } + + @Test + public void versionedTest001b__withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test001"); + File out = TestTools.createTempDir("out", ""); + Options app1Options = createCommonOptionsForMultiDex(new File(testFolder, "config-001.jpp")); + + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "minimal-multidex"); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath(), + out, false); + + File outList = getListingOfDex(new File(out, "classes.dex")); + ListingComparator.compare(new File(testFolder, "ref-list-002-1.txt"), outList); + File outList2 = getListingOfDex(new File(out, "classes2.dex")); + ListingComparator.compare(new File(testFolder, "ref-list-002-2.txt"), outList2); + Assert.assertFalse(new File(out, "classes3.dex").exists()); + return; + } + + @Test + public void versionedTest001c_withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test001"); + File out = TestTools.createTempDir("out", ""); + Options app1Options = createCommonOptionsForMultiDex(new File(testFolder, "config-003.jpp")); + + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "minimal-multidex"); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath(), + out, false); + + File outList = getListingOfDex(new File(out, "classes.dex")); + ListingComparator.compare(new File(testFolder, "ref-list-003-1.txt"), outList); + File outList2 = getListingOfDex(new File(out, "classes2.dex")); + ListingComparator.compare(new File(testFolder, "ref-list-003-2.txt"), outList2); + Assert.assertFalse(new File(out, "classes3.dex").exists()); + return; + } + private Options createCommonOptionsForMultiDex(@Nonnull File configFile) { Options app1Options = new Options(); app1Options.addProperty(MultiDexLegacy.MULTIDEX_LEGACY.getName(), "true"); @@ -271,4 +332,85 @@ public class MultiDexTests { return; } + @Test + public void legacyAppTest002a_withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test002"); + File out = TestTools.createTempDir("out", ""); + Options app1Options = createCommonOptionsForMultiDex( + new File(testFolder,"config-001.jpp")); + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "multidex"); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath() + + File.pathSeparator + library.getPath(), out, false); + + File classesDex = new File(out, "classes.dex"); + Assert.assertTrue(classesDex.exists()); + File classes2Dex = new File(out, "classes2.dex"); + Assert.assertTrue(classes2Dex.exists()); + File classes3Dex = new File(out, "classes3.dex"); + Assert.assertFalse(classes3Dex.exists()); + int totalTypeNumber = getTypeCountInDex(classesDex) + getTypeCountInDex(classes2Dex); + Assert.assertEquals(100, totalTypeNumber); + return; + } + + @Test + public void legacyAppTest002b_withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test002"); + File out = TestTools.createTempDir("out", ""); + Options app1Options = createCommonOptionsForMultiDex( + new File(testFolder,"config-001.jpp")); + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "minimal-multidex"); + app1Options.addJayceImport(library); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath(), + out, false); + + File outList = getListingOfDex(new File(out, "classes.dex")); + // The old toolchain is doing a little better than us here it seems to identify when + // InterfaceWithEnum.class instance is used or not. + ListingComparator.compare( + new File(testFolder,"ref-list-002-1.txt"), outList); + File outList2 = getListingOfDex(new File(out, "classes2.dex")); + ListingComparator.compare( + new File(testFolder,"ref-list-002-2.txt"), outList2); + Assert.assertFalse(new File(out, "classes3.dex").exists()); + return; + } + + @Test + public void legacyAppTest002b_auto_withoutAnnotations() throws Exception { + + File testFolder = TestTools.getJackTestsWithJackFolder("multidex/test002"); + File autoLibrary = prepareLibrary(frameworks); + File jackInf = new File(autoLibrary, "JACK-INF"); + Assert.assertTrue(jackInf.mkdir()); + Files.copy(new File(testFolder,"config-001.jpp"), new File(jackInf, "config-001.jpp")); + + File out = TestTools.createTempDir("out", ""); + Options app1Options = new Options(); + app1Options.addProperty(MultiDexLegacy.MULTIDEX_LEGACY.getName(), "true"); + app1Options.addProperty(DexFileWriter.DEX_WRITING_POLICY.getName(), "minimal-multidex"); + app1Options.addJayceImport(autoLibrary); + + TestTools.compileSourceToDex(app1Options, testFolder, TestTools.getDefaultBootclasspathString() + + File.pathSeparator + frameworks.getPath(), + out, false); + + File outList = getListingOfDex(new File(out, "classes.dex")); + // The old toolchain is doing a little better than us here it seems to identify when + // InterfaceWithEnum.class instance is used or not. + ListingComparator.compare( + new File(testFolder,"ref-list-002-1.txt"), outList); + File outList2 = getListingOfDex(new File(out, "classes2.dex")); + ListingComparator.compare( + new File(testFolder,"ref-list-002-2.txt"), outList2); + Assert.assertFalse(new File(out, "classes3.dex").exists()); + return; + } + } |