177 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.protocol;
|
|
|
15 |
|
180 |
ilm |
16 |
import org.openconcerto.utils.FileUtils;
|
|
|
17 |
|
|
|
18 |
import java.io.File;
|
|
|
19 |
import java.io.IOException;
|
177 |
ilm |
20 |
import java.net.URI;
|
180 |
ilm |
21 |
import java.util.Arrays;
|
|
|
22 |
import java.util.Collections;
|
177 |
ilm |
23 |
|
|
|
24 |
import javax.tools.JavaCompiler;
|
180 |
ilm |
25 |
import javax.tools.JavaFileObject;
|
177 |
ilm |
26 |
import javax.tools.SimpleJavaFileObject;
|
180 |
ilm |
27 |
import javax.tools.StandardJavaFileManager;
|
|
|
28 |
import javax.tools.StandardLocation;
|
|
|
29 |
import javax.tools.ToolProvider;
|
177 |
ilm |
30 |
|
|
|
31 |
/**
|
|
|
32 |
* From {@link JavaCompiler} javadoc.
|
|
|
33 |
*/
|
|
|
34 |
public class JavaSourceFromString extends SimpleJavaFileObject {
|
180 |
ilm |
35 |
|
|
|
36 |
static public boolean compile(final File outputDir, final JavaFileObject... classes) throws IOException {
|
|
|
37 |
FileUtils.mkdir_p(outputDir);
|
|
|
38 |
|
|
|
39 |
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
40 |
try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
|
|
41 |
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(outputDir));
|
|
|
42 |
return compiler.getTask(null, fileManager, null, null, null, Arrays.asList(classes)).call().booleanValue();
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
177 |
ilm |
46 |
/**
|
|
|
47 |
* The source code of this "file".
|
|
|
48 |
*/
|
|
|
49 |
private final String code;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Constructs a new JavaSourceFromString.
|
|
|
53 |
*
|
|
|
54 |
* @param name the name of the compilation unit represented by this file object
|
|
|
55 |
* @param code the source code for the compilation unit represented by this file object
|
|
|
56 |
*/
|
180 |
ilm |
57 |
public JavaSourceFromString(String name, String code) {
|
177 |
ilm |
58 |
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
|
|
|
59 |
this.code = code;
|
|
|
60 |
}
|
|
|
61 |
|
180 |
ilm |
62 |
public final String getClassFile() {
|
|
|
63 |
// /pkg/Inner.java
|
|
|
64 |
final String name = this.getName();
|
|
|
65 |
// pkg/Inner.class
|
|
|
66 |
return name.substring(1, name.length() - Kind.SOURCE.extension.length()) + Kind.CLASS.extension;
|
|
|
67 |
}
|
|
|
68 |
|
177 |
ilm |
69 |
@Override
|
|
|
70 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
|
|
71 |
return this.code;
|
|
|
72 |
}
|
|
|
73 |
}
|