Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/processor/Compile.java @ 21420:fb17e716b03c
The annotation processor should verify proper use of @Child annotation during compilation time and prevent usage of final.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Tue, 19 May 2015 12:30:51 +0200 |
parents | |
children | 24d12404e70a |
comparison
equal
deleted
inserted
replaced
21419:c1bb8028ff63 | 21420:fb17e716b03c |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.dsl.test.processor; | |
24 | |
25 import java.io.ByteArrayOutputStream; | |
26 import java.io.IOException; | |
27 import java.io.OutputStream; | |
28 import java.net.URI; | |
29 import java.net.URISyntaxException; | |
30 import java.util.ArrayList; | |
31 import java.util.Arrays; | |
32 import java.util.HashMap; | |
33 import java.util.List; | |
34 import java.util.Locale; | |
35 import java.util.Map; | |
36 import javax.tools.Diagnostic; | |
37 import javax.tools.DiagnosticListener; | |
38 import javax.tools.FileObject; | |
39 import javax.tools.ForwardingJavaFileManager; | |
40 import javax.tools.JavaFileManager; | |
41 import javax.tools.JavaFileObject; | |
42 import javax.tools.JavaFileObject.Kind; | |
43 import javax.tools.SimpleJavaFileObject; | |
44 import javax.tools.StandardJavaFileManager; | |
45 import javax.tools.ToolProvider; | |
46 import static org.junit.Assert.*; | |
47 | |
48 /** | |
49 * | |
50 * @author Jaroslav Tulach | |
51 */ | |
52 final class Compile implements DiagnosticListener<JavaFileObject> { | |
53 private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>(); | |
54 private final Map<String, byte[]> classes; | |
55 private final String sourceLevel; | |
56 | |
57 private Compile(Class<?> processor, String code, String sl) { | |
58 this.sourceLevel = sl; | |
59 classes = compile(processor, code); | |
60 } | |
61 | |
62 /** | |
63 * Performs compilation of given HTML page and associated Java code. | |
64 */ | |
65 public static Compile create(Class<?> processor, String code) { | |
66 return new Compile(processor, code, "1.7"); | |
67 } | |
68 | |
69 /** Checks for given class among compiled resources. */ | |
70 public byte[] get(String res) { | |
71 return classes.get(res); | |
72 } | |
73 | |
74 /** | |
75 * Obtains errors created during compilation. | |
76 */ | |
77 public List<Diagnostic<? extends JavaFileObject>> getErrors() { | |
78 List<Diagnostic<? extends JavaFileObject>> err; | |
79 err = new ArrayList<>(); | |
80 for (Diagnostic<? extends JavaFileObject> diagnostic : errors) { | |
81 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { | |
82 err.add(diagnostic); | |
83 } | |
84 } | |
85 return err; | |
86 } | |
87 | |
88 private Map<String, byte[]> compile(Class<?> processor, final String code) { | |
89 StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null); | |
90 | |
91 final Map<String, ByteArrayOutputStream> class2BAOS; | |
92 class2BAOS = new HashMap<>(); | |
93 | |
94 JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) { | |
95 @Override | |
96 public CharSequence getCharContent(boolean ignoreEncodingErrors) { | |
97 return code; | |
98 } | |
99 }; | |
100 | |
101 JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) { | |
102 @Override | |
103 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { | |
104 if (kind == Kind.CLASS) { | |
105 final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | |
106 | |
107 class2BAOS.put(className.replace('.', '/') + ".class", buffer); | |
108 return new SimpleJavaFileObject(sibling.toUri(), kind) { | |
109 @Override | |
110 public OutputStream openOutputStream() { | |
111 return buffer; | |
112 } | |
113 }; | |
114 } | |
115 | |
116 if (kind == Kind.SOURCE) { | |
117 final String n = className.replace('.', '/') + ".java"; | |
118 final URI un; | |
119 try { | |
120 un = new URI("mem://" + n); | |
121 } catch (URISyntaxException ex) { | |
122 throw new IOException(ex); | |
123 } | |
124 return new VirtFO(un/* sibling.toUri() */, kind, n); | |
125 } | |
126 | |
127 throw new IllegalStateException(); | |
128 } | |
129 | |
130 @Override | |
131 public boolean isSameFile(FileObject a, FileObject b) { | |
132 if (a instanceof VirtFO && b instanceof VirtFO) { | |
133 return ((VirtFO) a).getName().equals(((VirtFO) b).getName()); | |
134 } | |
135 | |
136 return super.isSameFile(a, b); | |
137 } | |
138 | |
139 class VirtFO extends SimpleJavaFileObject { | |
140 | |
141 private final String n; | |
142 | |
143 public VirtFO(URI uri, Kind kind, String n) { | |
144 super(uri, kind); | |
145 this.n = n; | |
146 } | |
147 | |
148 private final ByteArrayOutputStream data = new ByteArrayOutputStream(); | |
149 | |
150 @Override | |
151 public OutputStream openOutputStream() { | |
152 return data; | |
153 } | |
154 | |
155 @Override | |
156 public String getName() { | |
157 return n; | |
158 } | |
159 | |
160 @Override | |
161 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { | |
162 data.close(); | |
163 return new String(data.toByteArray()); | |
164 } | |
165 } | |
166 }; | |
167 List<String> args = Arrays.asList("-source", sourceLevel, "-target", "1.7", // | |
168 "-processor", processor.getName()); | |
169 | |
170 ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, args, null, Arrays.asList(file)).call(); | |
171 | |
172 Map<String, byte[]> result = new HashMap<>(); | |
173 | |
174 for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) { | |
175 result.put(e.getKey(), e.getValue().toByteArray()); | |
176 } | |
177 | |
178 return result; | |
179 } | |
180 | |
181 @Override | |
182 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { | |
183 errors.add(diagnostic); | |
184 } | |
185 | |
186 void assertErrors() { | |
187 assertFalse("There are supposed to be some errors", getErrors().isEmpty()); | |
188 } | |
189 | |
190 void assertNoErrors() { | |
191 assertTrue("There are supposed to be no errors: " + getErrors(), getErrors().isEmpty()); | |
192 } | |
193 | |
194 void assertError(String expMsg) { | |
195 StringBuilder sb = new StringBuilder(); | |
196 sb.append("Can't find ").append(expMsg).append(" among:"); | |
197 for (Diagnostic<? extends JavaFileObject> e : errors) { | |
198 String msg = e.getMessage(Locale.US); | |
199 if (msg.contains(expMsg)) { | |
200 return; | |
201 } | |
202 sb.append("\n"); | |
203 sb.append(msg); | |
204 } | |
205 fail(sb.toString()); | |
206 } | |
207 } |