comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/processor/Compile.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/processor/Compile.java@b1530a6cce8c
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 static org.junit.Assert.*;
26
27 import java.io.*;
28 import java.net.*;
29 import java.util.*;
30
31 import javax.tools.*;
32 import javax.tools.JavaFileObject.Kind;
33
34 final class Compile implements DiagnosticListener<JavaFileObject> {
35 private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
36 private final Map<String, byte[]> classes;
37 private final String sourceLevel;
38
39 private Compile(Class<?> processor, String code, String sl) {
40 this.sourceLevel = sl;
41 classes = compile(processor, code);
42 }
43
44 /**
45 * Performs compilation of given HTML page and associated Java code.
46 */
47 public static Compile create(Class<?> processor, String code) {
48 return new Compile(processor, code, "1.7");
49 }
50
51 /** Checks for given class among compiled resources. */
52 public byte[] get(String res) {
53 return classes.get(res);
54 }
55
56 /**
57 * Obtains errors created during compilation.
58 */
59 public List<Diagnostic<? extends JavaFileObject>> getErrors() {
60 List<Diagnostic<? extends JavaFileObject>> err;
61 err = new ArrayList<>();
62 for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
63 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
64 err.add(diagnostic);
65 }
66 }
67 return err;
68 }
69
70 private Map<String, byte[]> compile(Class<?> processor, final String code) {
71 StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
72
73 final Map<String, ByteArrayOutputStream> class2BAOS;
74 class2BAOS = new HashMap<>();
75
76 JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
77 @Override
78 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
79 return code;
80 }
81 };
82
83 JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
84 @Override
85 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
86 if (kind == Kind.CLASS) {
87 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
88
89 class2BAOS.put(className.replace('.', '/') + ".class", buffer);
90 return new SimpleJavaFileObject(sibling.toUri(), kind) {
91 @Override
92 public OutputStream openOutputStream() {
93 return buffer;
94 }
95 };
96 }
97
98 if (kind == Kind.SOURCE) {
99 final String n = className.replace('.', '/') + ".java";
100 final URI un;
101 try {
102 un = new URI("mem://" + n);
103 } catch (URISyntaxException ex) {
104 throw new IOException(ex);
105 }
106 return new VirtFO(un/* sibling.toUri() */, kind, n);
107 }
108
109 throw new IllegalStateException();
110 }
111
112 @Override
113 public boolean isSameFile(FileObject a, FileObject b) {
114 if (a instanceof VirtFO && b instanceof VirtFO) {
115 return ((VirtFO) a).getName().equals(((VirtFO) b).getName());
116 }
117
118 return super.isSameFile(a, b);
119 }
120
121 class VirtFO extends SimpleJavaFileObject {
122
123 private final String n;
124
125 public VirtFO(URI uri, Kind kind, String n) {
126 super(uri, kind);
127 this.n = n;
128 }
129
130 private final ByteArrayOutputStream data = new ByteArrayOutputStream();
131
132 @Override
133 public OutputStream openOutputStream() {
134 return data;
135 }
136
137 @Override
138 public String getName() {
139 return n;
140 }
141
142 @Override
143 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
144 data.close();
145 return new String(data.toByteArray());
146 }
147 }
148 };
149 List<String> args = Arrays.asList("-source", sourceLevel, "-target", "1.7", //
150 "-processor", processor.getName());
151
152 ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, args, null, Arrays.asList(file)).call();
153
154 Map<String, byte[]> result = new HashMap<>();
155
156 for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
157 result.put(e.getKey(), e.getValue().toByteArray());
158 }
159
160 return result;
161 }
162
163 @Override
164 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
165 errors.add(diagnostic);
166 }
167
168 void assertErrors() {
169 assertFalse("There are supposed to be some errors", getErrors().isEmpty());
170 }
171
172 void assertNoErrors() {
173 assertTrue("There are supposed to be no errors: " + getErrors(), getErrors().isEmpty());
174 }
175
176 void assertError(String expMsg) {
177 StringBuilder sb = new StringBuilder();
178 sb.append("Can't find ").append(expMsg).append(" among:");
179 for (Diagnostic<? extends JavaFileObject> e : errors) {
180 String msg = e.getMessage(Locale.US);
181 if (msg.contains(expMsg)) {
182 return;
183 }
184 sb.append("\n");
185 sb.append(msg);
186 }
187 fail(sb.toString());
188 }
189 }