16
16
17
17
package org .codehaus .plexus .compiler .javac .errorprone ;
18
18
19
- import com .google .errorprone .ErrorProneCompiler ;
20
- import org . codehaus . plexus . compiler . AbstractCompiler ;
19
+ import com .google .errorprone .ErrorProneJavaCompiler ;
20
+
21
21
import org .codehaus .plexus .compiler .CompilerConfiguration ;
22
22
import org .codehaus .plexus .compiler .CompilerException ;
23
23
import org .codehaus .plexus .compiler .CompilerMessage ;
24
- import org .codehaus .plexus .compiler .CompilerOutputStyle ;
25
24
import org .codehaus .plexus .compiler .CompilerResult ;
25
+ import org .codehaus .plexus .compiler .javac .InProcessCompiler ;
26
26
import org .codehaus .plexus .compiler .javac .JavacCompiler ;
27
+ import org .codehaus .plexus .compiler .javac .JavaxToolsCompiler ;
28
+
29
+ import javax .tools .JavaCompiler ;
30
+ import javax .tools .ToolProvider ;
27
31
28
- import javax .tools .Diagnostic ;
29
- import javax .tools .DiagnosticListener ;
30
- import javax .tools .JavaFileObject ;
31
- import java .io .File ;
32
- import java .lang .reflect .Method ;
33
32
import java .net .MalformedURLException ;
34
33
import java .net .URL ;
35
34
import java .net .URLClassLoader ;
36
- import java .util .ArrayList ;
37
- import java .util .List ;
38
- import java .util .Locale ;
39
35
40
36
/**
41
37
* This class overrides JavacCompiler with modifications to use the error-prone
45
41
* @plexus.component role="org.codehaus.plexus.compiler.Compiler" role-hint="javac-with-errorprone"
46
42
*/
47
43
public class JavacCompilerWithErrorProne
48
- extends AbstractCompiler
44
+ extends JavacCompiler
49
45
{
50
- public JavacCompilerWithErrorProne ()
51
- {
52
- super ( CompilerOutputStyle .ONE_OUTPUT_FILE_PER_INPUT_FILE , ".java" , ".class" , null );
53
- }
54
-
55
- public String [] createCommandLine ( CompilerConfiguration config )
56
- throws CompilerException
57
- {
58
- return new String [0 ];
59
- }
60
-
61
- @ Override
62
- public CompilerResult performCompile ( CompilerConfiguration config )
63
- throws CompilerException
64
- {
65
- File destinationDir = new File ( config .getOutputLocation () );
66
-
67
- if ( !destinationDir .exists () )
68
- {
69
- destinationDir .mkdirs ();
70
- }
71
-
72
- String [] sourceFiles = getSourceFiles ( config );
73
-
74
- if ( ( sourceFiles == null ) || ( sourceFiles .length == 0 ) )
75
- {
76
- return new CompilerResult ();
77
- }
78
-
79
- if ( ( getLogger () != null ) && getLogger ().isInfoEnabled () )
80
- {
81
- getLogger ().info ( "Compiling " + sourceFiles .length + " " //
82
- + "source file" //
83
- + ( sourceFiles .length == 1 ? "" : "s" ) //
84
- + " to " + destinationDir .getAbsolutePath () );
85
- }
86
-
87
- String [] args = JavacCompiler .buildCompilerArguments ( config , sourceFiles );
88
-
89
- try
90
- {
91
- CompilerResult compilerResult = (CompilerResult ) getInvoker ().invoke ( null , new Object []{ args } );
92
- return compilerResult ;
93
- }
94
- catch ( Exception e )
95
- {
96
- throw new CompilerException ( e .getMessage (), e );
97
- }
98
- }
99
-
100
46
private static class NonDelegatingClassLoader
101
47
extends URLClassLoader
102
48
{
@@ -115,6 +61,10 @@ public Class<?> loadClass( String name, boolean complete )
115
61
{
116
62
// Classes loaded inside CompilerInvoker that need to reach back to the caller
117
63
if ( name .contentEquals ( CompilerResult .class .getName () )
64
+ || name .contentEquals ( InProcessCompiler .class .getName () )
65
+ || name .contentEquals ( CompilerConfiguration .class .getName () )
66
+ || name .contentEquals ( CompilerConfiguration .CompilerReuseStrategy .class .getName () )
67
+ || name .contentEquals ( CompilerException .class .getName () )
118
68
|| name .contentEquals ( CompilerMessage .class .getName () )
119
69
|| name .contentEquals ( CompilerMessage .Kind .class .getName () ) )
120
70
{
@@ -140,12 +90,9 @@ public Class<?> loadClass( String name, boolean complete )
140
90
}
141
91
}
142
92
143
- private Method invokerMethod ;
144
-
145
- private Method getInvoker ()
146
- throws CompilerException
93
+ protected InProcessCompiler inProcessCompiler ()
147
94
{
148
- if ( invokerMethod == null )
95
+ if ( Thread . currentThread (). getContextClassLoader (). getResource ( "java/lang/module/ModuleReference.class" ) == null )
149
96
{
150
97
ClassLoader contextClassLoader = Thread .currentThread ().getContextClassLoader ();
151
98
URL [] urls = ( (URLClassLoader ) contextClassLoader ).getURLs ();
@@ -154,14 +101,14 @@ private Method getInvoker()
154
101
{
155
102
loader = new NonDelegatingClassLoader ( urls , contextClassLoader );
156
103
Class <?> clazz = Class .forName ( CompilerInvoker .class .getName (), true , loader );
157
- invokerMethod = clazz . getMethod ( "compile" , String []. class );
104
+ return ( InProcessCompiler ) clazz . newInstance ( );
158
105
}
159
106
catch ( Exception e )
160
107
{
161
- throw new CompilerException ( e . getMessage (), e );
108
+ throw new IllegalStateException ( e );
162
109
}
163
110
}
164
- return invokerMethod ;
111
+ return new CompilerInvoker () ;
165
112
}
166
113
167
114
/**
@@ -170,57 +117,12 @@ private Method getInvoker()
170
117
* javac.jar instead of from the bootclasspath.
171
118
*/
172
119
public static class CompilerInvoker
120
+ extends JavaxToolsCompiler
173
121
{
174
- private static class MessageListener
175
- implements DiagnosticListener <JavaFileObject >
176
- {
177
- private final List <CompilerMessage > messages ;
178
-
179
- MessageListener ( List <CompilerMessage > messages )
180
- {
181
- this .messages = messages ;
182
- }
183
-
184
- public static CompilerMessage .Kind convertKind ( Diagnostic <? extends JavaFileObject > diagnostic )
185
- {
186
- switch ( diagnostic .getKind () )
187
- {
188
- case ERROR :
189
- return CompilerMessage .Kind .ERROR ;
190
- case WARNING :
191
- return CompilerMessage .Kind .WARNING ;
192
- case MANDATORY_WARNING :
193
- return CompilerMessage .Kind .MANDATORY_WARNING ;
194
- case NOTE :
195
- return CompilerMessage .Kind .NOTE ;
196
- default :
197
- return CompilerMessage .Kind .OTHER ;
198
- }
199
- }
200
-
201
- public void report ( Diagnostic <? extends JavaFileObject > diagnostic )
202
- {
203
- CompilerMessage compilerMessage =
204
- new CompilerMessage ( diagnostic .getSource () == null ? null : diagnostic .getSource ().getName (), //
205
- convertKind ( diagnostic ), //
206
- (int ) diagnostic .getLineNumber (), //
207
- (int ) diagnostic .getColumnNumber (), //
208
- -1 , //
209
- -1 ,
210
- // end pos line:column is hard to calculate
211
- diagnostic .getMessage ( Locale .getDefault () ) );
212
- messages .add ( compilerMessage );
213
- }
214
- }
215
-
216
- public static CompilerResult compile ( String [] args )
217
- {
218
- List <CompilerMessage > messages = new ArrayList <>();
219
- ErrorProneCompiler compiler = //
220
- ErrorProneCompiler .builder () //
221
- .listenToDiagnostics ( new MessageListener ( messages ) ) //
222
- .build ();
223
- return new CompilerResult ( compiler .run ( args ).isOK (), messages );
224
- }
122
+ @ Override
123
+ protected JavaCompiler newJavaCompiler ()
124
+ {
125
+ return new ErrorProneJavaCompiler ();
126
+ }
225
127
}
226
128
}
0 commit comments