1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.jack.preprocessor;
import com.android.jack.Main;
import com.android.jack.Options;
import com.android.jack.TestTools;
import com.android.jack.ir.ast.JAnnotation;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JMethod;
import com.android.jack.ir.ast.JSession;
import com.android.jack.lookup.JNodeLookup;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.JackBasedToolchain;
import com.android.sched.util.RunnableHooks;
import junit.framework.Assert;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.util.Collection;
public class PreProcessorTests {
@BeforeClass
public static void setUpClass() throws Exception {
Main.class.getClassLoader().setDefaultAssertionStatus(true);
}
@Test
public void test001_002() throws Exception {
File testDir = AbstractTestTools.getTestRootDir("com.android.jack.preprocessor.test001.jack");
JackBasedToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class);
toolchain.addProperty(PreProcessor.ENABLE.getName(), "true");
toolchain.addProperty(PreProcessor.FILE.getName(),
new File(testDir, "config.jpp").getAbsolutePath());
toolchain.addToClasspath(toolchain.getDefaultBootClasspath())
.srcToExe(AbstractTestTools.createTempFile("annotationAdderTest", ".out.zip"),
/* zipFile = */ true,
testDir);
}
@Test
public void test001_001() throws Exception {
File testDir = AbstractTestTools.getTestRootDir("com.android.jack.preprocessor.test001.jack");
File tempDir = AbstractTestTools.createTempDir();
JackBasedToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class);
toolchain.addToClasspath(toolchain.getDefaultBootClasspath())
.srcToLib(tempDir,
/* zipFiles = */ false,
testDir);
Options args = TestTools.buildCommandLineArgs(
new File(testDir, "app1/ApplicationActivity1.java"));
args.setClasspath(AbstractTestTools.getClasspathsAsString(toolchain.getDefaultBootClasspath(),
new File[] {tempDir}));
RunnableHooks hooks = new RunnableHooks();
JSession session = TestTools.buildSession(args, hooks);
ANTLRFileStream in = new ANTLRFileStream(new File(testDir, "config.jpp").getAbsolutePath());
PreProcessorLexer lexer = new PreProcessorLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PreProcessorParser parser = new PreProcessorParser(tokens);
Collection<Rule> rules = parser.rules(session);
Scope scope = new TypeToEmitScope(session);
for (Rule rule : rules) {
Context context = new Context();
if (!rule.getSet().eval(scope, context).isEmpty()) {
context.getRequest(session).commit();
}
}
JAnnotation installerAnnotation = session.getPhantomLookup().getAnnotation(
"Lcom/android/jack/preprocessor/test001/jack/MultiDexInstaller;");
JNodeLookup lookup = session.getLookup();
{
JDefinedClassOrInterface coi = lookup.getClass(
"Lcom/android/jack/preprocessor/test001/jack/app1/ApplicationActivity1;");
Assert.assertFalse(coi.getAnnotations(installerAnnotation).isEmpty());
for (JMethod method : coi.getMethods()) {
if (method.getName().equals("noAnnotation")) {
Assert.assertTrue(method.getAnnotations(installerAnnotation).isEmpty());
} else {
Assert.assertFalse(method.getAnnotations(installerAnnotation).isEmpty());
}
}
}
hooks.runHooks();
}
}
|