Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jul 30, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSimpleCompiler.java
More file actions
Latest commit
155 lines (134 loc) · 5.4 KB
/
Copy pathSimpleCompiler.java
File metadata and controls
155 lines (134 loc) · 5.4 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2010, Mysema Ltd
*
* 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.
*/
packagecom.mysema.codegen;
importjava.io.*;
importjava.net.URL;
importjava.net.URLClassLoader;
importjava.net.URLDecoder;
importjava.nio.charset.Charset;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Locale;
importjava.util.Set;
importjava.util.jar.Manifest;
importjavax.lang.model.SourceVersion;
importjavax.tools.*;
importcom.google.common.base.Joiner;
/**
* SimpleCompiler provides a convenience wrapper of the JavaCompiler interface
* with automatic classpath generation
*
* @author tiwe
*
*/
publicclassSimpleCompilerimplementsJavaCompiler {
privatestaticfinalJoinerpathJoiner = Joiner.on(File.pathSeparator);
protectedstaticbooleanisSureFireBooter(URLClassLoadercl) {
for (URLurl : cl.getURLs()) {
if (url.getPath().contains("surefirebooter")) {
returntrue;
}
}
returnfalse;
}
publicstaticStringgetClassPath(URLClassLoadercl) {
try {
List<String> paths = newArrayList<String>();
if (isSureFireBooter(cl)) {
// extract MANIFEST.MF Class-Path entry, since the Java Compiler doesn't handle
// manifest only jars in the classpath correctly
URLurl = cl.findResource("META-INF/MANIFEST.MF");
Manifestmanifest = newManifest(url.openStream());
Stringclasspath = manifest.getMainAttributes().getValue("Class-Path");
for (Stringentry : classpath.split(" ")) {
URLentryUrl = newURL(entry);
StringdecodedPath = URLDecoder.decode(entryUrl.getPath(), "UTF-8");
paths.add(newFile(decodedPath).getAbsolutePath());
}
} else {
ClassLoaderc = cl;
while (cinstanceofURLClassLoader) {
for (URLurl : ((URLClassLoader)c).getURLs()) {
StringdecodedPath = URLDecoder.decode(url.getPath(), "UTF-8");
paths.add(newFile(decodedPath).getAbsolutePath());
}
c = c.getParent();
}
}
returnpathJoiner.join(paths);
} catch (UnsupportedEncodingExceptione) {
thrownewCodegenException(e);
} catch (IOExceptione) {
thrownewCodegenException(e);
}
}
privatefinalClassLoaderclassLoader;
privateStringclassPath;
privatefinalJavaCompilercompiler;
publicSimpleCompiler() {
this(ToolProvider.getSystemJavaCompiler(), Thread.currentThread().getContextClassLoader());
}
publicSimpleCompiler(JavaCompilercompiler, ClassLoaderclassLoader) {
this.compiler = compiler;
this.classLoader = classLoader;
}
privateStringgetClasspath() {
if (classPath == null) {
if (classLoaderinstanceofURLClassLoader) {
classPath = getClassPath((URLClassLoader) classLoader);
} else {
thrownewIllegalArgumentException("Unsupported ClassLoader " + classLoader);
}
}
returnclassPath;
}
@Override
publicSet<SourceVersion> getSourceVersions() {
returncompiler.getSourceVersions();
}
@Override
publicStandardJavaFileManagergetStandardFileManager(
DiagnosticListener<? superJavaFileObject> diagnosticListener, Localelocale,
Charsetcharset) {
returncompiler.getStandardFileManager(diagnosticListener, locale, charset);
}
@Override
publicCompilationTaskgetTask(Writerout, JavaFileManagerfileManager,
DiagnosticListener<? superJavaFileObject> diagnosticListener,
Iterable<String> options, Iterable<String> classes,
Iterable<? extendsJavaFileObject> compilationUnits) {
returncompiler.getTask(out, fileManager, diagnosticListener, options, classes,
compilationUnits);
}
@Override
publicintisSupportedOption(Stringoption) {
returncompiler.isSupportedOption(option);
}
@Override
publicintrun(InputStreamin, OutputStreamout, OutputStreamerr, String... arguments) {
for (Stringa : arguments) {
if (a.equals("-classpath")) {
returncompiler.run(in, out, err, arguments);
}
}
// no classpath given
List<String> args = newArrayList<String>(arguments.length + 2);
args.add("-classpath");
args.add(getClasspath());
for (Stringarg : arguments) {
args.add(arg);
}
returncompiler.run(in, out, err, args.toArray(newString[args.size()]));
}
}