OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 180
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils.protocol;
14
 package org.openconcerto.utils.protocol;
15
 
15
 
-
 
16
import org.openconcerto.utils.FileUtils;
-
 
17
 
-
 
18
import java.io.File;
-
 
19
import java.io.IOException;
16
import java.net.URI;
20
import java.net.URI;
-
 
21
import java.util.Arrays;
-
 
22
import java.util.Collections;
17
 
23
 
18
import javax.tools.JavaCompiler;
24
import javax.tools.JavaCompiler;
-
 
25
import javax.tools.JavaFileObject;
19
import javax.tools.SimpleJavaFileObject;
26
import javax.tools.SimpleJavaFileObject;
-
 
27
import javax.tools.StandardJavaFileManager;
-
 
28
import javax.tools.StandardLocation;
-
 
29
import javax.tools.ToolProvider;
20
 
30
 
21
/**
31
/**
22
 * From {@link JavaCompiler} javadoc.
32
 * From {@link JavaCompiler} javadoc.
23
 */
33
 */
24
public class JavaSourceFromString extends SimpleJavaFileObject {
34
public class JavaSourceFromString extends SimpleJavaFileObject {
-
 
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
 
25
    /**
46
    /**
26
     * The source code of this "file".
47
     * The source code of this "file".
27
     */
48
     */
28
    private final String code;
49
    private final String code;
29
 
50
 
Line 31... Line 52...
31
     * Constructs a new JavaSourceFromString.
52
     * Constructs a new JavaSourceFromString.
32
     * 
53
     * 
33
     * @param name the name of the compilation unit represented by this file object
54
     * @param name the name of the compilation unit represented by this file object
34
     * @param code the source code for the compilation unit represented by this file object
55
     * @param code the source code for the compilation unit represented by this file object
35
     */
56
     */
36
    JavaSourceFromString(String name, String code) {
57
    public JavaSourceFromString(String name, String code) {
37
        super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
58
        super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
38
        this.code = code;
59
        this.code = code;
39
    }
60
    }
40
 
61
 
-
 
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
 
41
    @Override
69
    @Override
42
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
70
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
43
        return this.code;
71
        return this.code;
44
    }
72
    }
45
}
73
}