23
23
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
24
* SOFTWARE.
25
25
*/
26
-
27
- import org .codehaus .plexus .util .DirectoryScanner ;
28
- import org .slf4j .Logger ;
29
- import org .slf4j .LoggerFactory ;
30
-
31
26
import java .io .File ;
32
27
import java .io .IOException ;
33
28
import java .util .HashSet ;
34
29
import java .util .List ;
35
30
import java .util .Set ;
36
31
32
+ import org .codehaus .plexus .util .DirectoryScanner ;
33
+ import org .slf4j .Logger ;
34
+ import org .slf4j .LoggerFactory ;
35
+
37
36
/**
38
37
* @author <a href="mailto:[email protected] ">Jason van Zyl </a>
39
38
* @author <a href="mailto:[email protected] ">Michal Maczka </a>
40
39
* @author <a href="mailto:[email protected] ">Trygve Laugstøl</a>
41
40
*/
42
- public abstract class AbstractCompiler
43
- implements Compiler
44
- {
45
- protected Logger log = LoggerFactory .getLogger ( getClass () );
41
+ public abstract class AbstractCompiler implements Compiler {
42
+ protected Logger log = LoggerFactory .getLogger (getClass ());
46
43
protected static final String EOL = System .lineSeparator ();
47
44
48
- protected static final String PS = System .getProperty ( "path.separator" );
45
+ protected static final String PS = System .getProperty ("path.separator" );
49
46
50
47
private final CompilerOutputStyle compilerOutputStyle ;
51
48
@@ -59,9 +56,11 @@ public abstract class AbstractCompiler
59
56
//
60
57
// ----------------------------------------------------------------------
61
58
62
- protected AbstractCompiler ( CompilerOutputStyle compilerOutputStyle , String inputFileEnding ,
63
- String outputFileEnding , String outputFile )
64
- {
59
+ protected AbstractCompiler (
60
+ CompilerOutputStyle compilerOutputStyle ,
61
+ String inputFileEnding ,
62
+ String outputFileEnding ,
63
+ String outputFile ) {
65
64
this .compilerOutputStyle = compilerOutputStyle ;
66
65
67
66
this .inputFileEnding = inputFileEnding ;
@@ -77,91 +76,71 @@ protected AbstractCompiler( CompilerOutputStyle compilerOutputStyle, String inpu
77
76
78
77
public abstract String getCompilerId ();
79
78
80
- public CompilerResult performCompile (CompilerConfiguration configuration )
81
- throws CompilerException
82
- {
79
+ public CompilerResult performCompile (CompilerConfiguration configuration ) throws CompilerException {
83
80
throw new CompilerNotImplementedException ("The performCompile method has not been implemented." );
84
81
}
85
82
86
- public CompilerOutputStyle getCompilerOutputStyle ()
87
- {
83
+ public CompilerOutputStyle getCompilerOutputStyle () {
88
84
return compilerOutputStyle ;
89
85
}
90
86
91
- public String getInputFileEnding ( CompilerConfiguration configuration )
92
- throws CompilerException
93
- {
87
+ public String getInputFileEnding (CompilerConfiguration configuration ) throws CompilerException {
94
88
return inputFileEnding ;
95
89
}
96
90
97
- public String getOutputFileEnding ( CompilerConfiguration configuration )
98
- throws CompilerException
99
- {
100
- if ( compilerOutputStyle != CompilerOutputStyle .ONE_OUTPUT_FILE_PER_INPUT_FILE )
101
- {
102
- throw new RuntimeException ( "This compiler implementation doesn't have one output file per input file." );
91
+ public String getOutputFileEnding (CompilerConfiguration configuration ) throws CompilerException {
92
+ if (compilerOutputStyle != CompilerOutputStyle .ONE_OUTPUT_FILE_PER_INPUT_FILE ) {
93
+ throw new RuntimeException ("This compiler implementation doesn't have one output file per input file." );
103
94
}
104
95
105
96
return outputFileEnding ;
106
97
}
107
98
108
- public String getOutputFile ( CompilerConfiguration configuration )
109
- throws CompilerException
110
- {
111
- if ( compilerOutputStyle != CompilerOutputStyle .ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES )
112
- {
113
- throw new RuntimeException ( "This compiler implementation doesn't have one output file for all files." );
99
+ public String getOutputFile (CompilerConfiguration configuration ) throws CompilerException {
100
+ if (compilerOutputStyle != CompilerOutputStyle .ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES ) {
101
+ throw new RuntimeException ("This compiler implementation doesn't have one output file for all files." );
114
102
}
115
103
116
104
return outputFile ;
117
105
}
118
106
119
- public boolean canUpdateTarget ( CompilerConfiguration configuration )
120
- throws CompilerException
121
- {
107
+ public boolean canUpdateTarget (CompilerConfiguration configuration ) throws CompilerException {
122
108
return true ;
123
109
}
124
110
125
111
// ----------------------------------------------------------------------
126
112
// Utility Methods
127
113
// ----------------------------------------------------------------------
128
114
129
- public static String getPathString ( List <String > pathElements )
130
- {
115
+ public static String getPathString (List <String > pathElements ) {
131
116
StringBuilder sb = new StringBuilder ();
132
117
133
- for ( String pathElement : pathElements )
134
- {
135
- sb .append ( pathElement ).append ( File .pathSeparator );
118
+ for (String pathElement : pathElements ) {
119
+ sb .append (pathElement ).append (File .pathSeparator );
136
120
}
137
121
138
122
return sb .toString ();
139
123
}
140
124
141
- protected static Set <String > getSourceFilesForSourceRoot ( CompilerConfiguration config , String sourceLocation )
142
- {
125
+ protected static Set <String > getSourceFilesForSourceRoot (CompilerConfiguration config , String sourceLocation ) {
143
126
DirectoryScanner scanner = new DirectoryScanner ();
144
127
145
- scanner .setBasedir ( sourceLocation );
128
+ scanner .setBasedir (sourceLocation );
146
129
147
130
Set <String > includes = config .getIncludes ();
148
131
149
- if ( includes != null && !includes .isEmpty () )
150
- {
151
- String [] inclStrs = includes .toArray ( new String [0 ] );
152
- scanner .setIncludes ( inclStrs );
153
- }
154
- else
155
- {
156
- scanner .setIncludes ( new String []{ "**/*.java" } );
132
+ if (includes != null && !includes .isEmpty ()) {
133
+ String [] inclStrs = includes .toArray (new String [0 ]);
134
+ scanner .setIncludes (inclStrs );
135
+ } else {
136
+ scanner .setIncludes (new String [] {"**/*.java" });
157
137
}
158
138
159
139
Set <String > excludes = config .getExcludes ();
160
140
161
- if ( excludes != null && !excludes .isEmpty () )
162
- {
163
- String [] exclStrs = excludes .toArray ( new String [0 ] );
164
- scanner .setExcludes ( exclStrs );
141
+ if (excludes != null && !excludes .isEmpty ()) {
142
+ String [] exclStrs = excludes .toArray (new String [0 ]);
143
+ scanner .setExcludes (exclStrs );
165
144
}
166
145
167
146
scanner .scan ();
@@ -170,125 +149,105 @@ protected static Set<String> getSourceFilesForSourceRoot( CompilerConfiguration
170
149
171
150
Set <String > sources = new HashSet <>();
172
151
173
- for ( String sourceDirectorySource : sourceDirectorySources )
174
- {
175
- File f = new File ( sourceLocation , sourceDirectorySource );
152
+ for (String sourceDirectorySource : sourceDirectorySources ) {
153
+ File f = new File (sourceLocation , sourceDirectorySource );
176
154
177
- sources .add ( f .getPath () );
155
+ sources .add (f .getPath ());
178
156
}
179
157
180
158
return sources ;
181
159
}
182
160
183
- protected static String [] getSourceFiles ( CompilerConfiguration config )
184
- {
161
+ protected static String [] getSourceFiles (CompilerConfiguration config ) {
185
162
Set <String > sources = new HashSet <>();
186
163
187
164
Set <File > sourceFiles = config .getSourceFiles ();
188
165
189
- if ( sourceFiles != null && !sourceFiles .isEmpty () )
190
- {
191
- for ( File sourceFile : sourceFiles )
192
- {
193
- sources .add ( sourceFile .getAbsolutePath () );
166
+ if (sourceFiles != null && !sourceFiles .isEmpty ()) {
167
+ for (File sourceFile : sourceFiles ) {
168
+ sources .add (sourceFile .getAbsolutePath ());
194
169
}
195
- }
196
- else
197
- {
198
- for ( String sourceLocation : config .getSourceLocations () )
199
- {
200
- sources .addAll ( getSourceFilesForSourceRoot ( config , sourceLocation ) );
170
+ } else {
171
+ for (String sourceLocation : config .getSourceLocations ()) {
172
+ sources .addAll (getSourceFilesForSourceRoot (config , sourceLocation ));
201
173
}
202
174
}
203
175
204
176
String [] result ;
205
177
206
- if ( sources .isEmpty () )
207
- {
178
+ if (sources .isEmpty ()) {
208
179
result = new String [0 ];
209
- }
210
- else
211
- {
212
- result = sources .toArray ( new String [0 ] );
180
+ } else {
181
+ result = sources .toArray (new String [0 ]);
213
182
}
214
183
215
184
return result ;
216
185
}
217
186
218
- protected static String makeClassName ( String fileName , String sourceDir )
219
- throws CompilerException
220
- {
221
- File origFile = new File ( fileName );
187
+ protected static String makeClassName (String fileName , String sourceDir ) throws CompilerException {
188
+ File origFile = new File (fileName );
222
189
223
190
String canonical = null ;
224
191
225
- if ( origFile .exists () )
226
- {
227
- canonical = getCanonicalPath ( origFile ).replace ( '\\' , '/' );
192
+ if (origFile .exists ()) {
193
+ canonical = getCanonicalPath (origFile ).replace ('\\' , '/' );
228
194
}
229
195
230
- if ( sourceDir != null )
231
- {
232
- String prefix = getCanonicalPath ( new File ( sourceDir ) ).replace ( '\\' , '/' );
196
+ if (sourceDir != null ) {
197
+ String prefix = getCanonicalPath (new File (sourceDir )).replace ('\\' , '/' );
233
198
234
- if ( canonical != null )
235
- {
236
- if ( canonical .startsWith ( prefix ) )
237
- {
238
- String result = canonical .substring ( prefix .length () + 1 , canonical .length () - 5 );
199
+ if (canonical != null ) {
200
+ if (canonical .startsWith (prefix )) {
201
+ String result = canonical .substring (prefix .length () + 1 , canonical .length () - 5 );
239
202
240
- result = result .replace ( '/' , '.' );
203
+ result = result .replace ('/' , '.' );
241
204
242
205
return result ;
243
206
}
244
- }
245
- else
246
- {
247
- File t = new File ( sourceDir , fileName );
207
+ } else {
208
+ File t = new File (sourceDir , fileName );
248
209
249
- if ( t .exists () )
250
- {
251
- String str = getCanonicalPath ( t ).replace ( '\\' , '/' );
210
+ if (t .exists ()) {
211
+ String str = getCanonicalPath (t ).replace ('\\' , '/' );
252
212
253
- return str .substring ( prefix .length () + 1 , str .length () - 5 ).replace ( '/' , '.' );
213
+ return str .substring (prefix .length () + 1 , str .length () - 5 ).replace ('/' , '.' );
254
214
}
255
215
}
256
216
}
257
217
258
- if ( fileName .endsWith ( ".java" ) )
259
- {
260
- fileName = fileName .substring ( 0 , fileName .length () - 5 );
218
+ if (fileName .endsWith (".java" )) {
219
+ fileName = fileName .substring (0 , fileName .length () - 5 );
261
220
}
262
221
263
- fileName = fileName .replace ( '\\' , '.' );
222
+ fileName = fileName .replace ('\\' , '.' );
264
223
265
- return fileName .replace ( '/' , '.' );
224
+ return fileName .replace ('/' , '.' );
266
225
}
267
226
268
- private static String getCanonicalPath ( File origFile )
269
- throws CompilerException
270
- {
271
- try
272
- {
227
+ private static String getCanonicalPath (File origFile ) throws CompilerException {
228
+ try {
273
229
return origFile .getCanonicalPath ();
274
- }
275
- catch ( IOException e )
276
- {
230
+ } catch (IOException e ) {
277
231
throw new CompilerException (
278
- "Error while getting the canonical path of '" + origFile .getAbsolutePath () + "'." , e );
232
+ "Error while getting the canonical path of '" + origFile .getAbsolutePath () + "'." , e );
279
233
}
280
234
}
281
235
282
- protected void logCompiling ( String [] sourceFiles , CompilerConfiguration config )
283
- {
284
- if ( log .isInfoEnabled () )
285
- {
286
- String to = ( config .getWorkingDirectory () == null ) ? config .getOutputLocation () :
287
- config .getWorkingDirectory ().toPath ().relativize ( new File ( config .getOutputLocation () ).toPath () ).toString ();
288
- log .info ( "Compiling " +
289
- ( sourceFiles == null ? "" : ( sourceFiles .length + " source file" + ( sourceFiles .length == 1 ? " " : "s " ) ) ) +
290
- "with " + getCompilerId () + " [" + config .describe () + "]" +
291
- " to " + to );
236
+ protected void logCompiling (String [] sourceFiles , CompilerConfiguration config ) {
237
+ if (log .isInfoEnabled ()) {
238
+ String to = (config .getWorkingDirectory () == null )
239
+ ? config .getOutputLocation ()
240
+ : config .getWorkingDirectory ()
241
+ .toPath ()
242
+ .relativize (new File (config .getOutputLocation ()).toPath ())
243
+ .toString ();
244
+ log .info ("Compiling "
245
+ + (sourceFiles == null
246
+ ? ""
247
+ : (sourceFiles .length + " source file" + (sourceFiles .length == 1 ? " " : "s " )))
248
+ + "with "
249
+ + getCompilerId () + " [" + config .describe () + "]" + " to "
250
+ + to );
292
251
}
293
- }
252
+ }
294
253
}
0 commit comments