Skip to content

Commit c12f808

Browse files
authored
Modernize (#191)
Some but not all of issues are fixed
1 parent 8bdb721 commit c12f808

6 files changed

Lines changed: 40 additions & 48 deletions

File tree

takari-lifecycle-plugin/src/main/java/io/takari/maven/plugins/compile/javac/CompilerJavacForked.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URLDecoder;
2222
import java.net.URLEncoder;
2323
import java.nio.charset.Charset;
24+
import java.nio.charset.StandardCharsets;
2425
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.StringTokenizer;
@@ -36,8 +37,6 @@ public class CompilerJavacForked {
3637

3738
private static final String EOL = "\n";
3839

39-
private static final String ENCODING = "UTF-8";
40-
4140
public static class CompilerConfiguration {
4241

4342
private final Charset encoding;
@@ -65,8 +64,7 @@ public Iterable<File> getSources() {
6564
}
6665

6766
public void write(File file) throws IOException {
68-
Writer writer = newWriter(file);
69-
try {
67+
try (Writer writer = newWriter(file)) {
7068
// encoding
7169
if (encoding != null) {
7270
writer.write('C');
@@ -87,18 +85,15 @@ public void write(File file) throws IOException {
8785
writer.write(source.getCanonicalPath());
8886
writer.write(EOL);
8987
}
90-
} finally {
91-
writer.close();
9288
}
9389
}
9490

9591
public static CompilerConfiguration read(File file) throws IOException {
9692
Charset encoding = null;
97-
List<String> options = new ArrayList<String>();
98-
List<File> sources = new ArrayList<File>();
93+
List<String> options = new ArrayList<>();
94+
List<File> sources = new ArrayList<>();
9995

100-
BufferedReader reader = newBufferedReader(file);
101-
try {
96+
try (BufferedReader reader = newBufferedReader(file)) {
10297
String str;
10398
while ((str = reader.readLine()) != null) {
10499
String value = str.substring(1);
@@ -114,8 +109,6 @@ public static CompilerConfiguration read(File file) throws IOException {
114109
break;
115110
}
116111
}
117-
} finally {
118-
reader.close();
119112
}
120113

121114
return new CompilerConfiguration(encoding, options, sources);
@@ -133,9 +126,12 @@ public CompilerOutput(File file) throws IOException {
133126
public void processOutput(File inputFile, File outputFile) {
134127
try {
135128
writer.write('O');
136-
writer.write(inputFile != null ? URLEncoder.encode(inputFile.getCanonicalPath(), ENCODING) : ".");
129+
writer.write(
130+
inputFile != null
131+
? URLEncoder.encode(inputFile.getCanonicalPath(), StandardCharsets.UTF_8)
132+
: ".");
137133
writer.write(' ');
138-
writer.write(URLEncoder.encode(outputFile.getCanonicalPath(), ENCODING));
134+
writer.write(URLEncoder.encode(outputFile.getCanonicalPath(), StandardCharsets.UTF_8));
139135
writer.write(EOL);
140136
} catch (IOException e) {
141137
handleException(e);
@@ -145,7 +141,7 @@ public void processOutput(File inputFile, File outputFile) {
145141
public void addMessage(String path, int line, int column, String message, Kind kind) {
146142
try {
147143
writer.write('M');
148-
writer.write(URLEncoder.encode(path, ENCODING));
144+
writer.write(URLEncoder.encode(path, StandardCharsets.UTF_8));
149145
writer.write(' ');
150146
writer.write(Integer.toString(line));
151147
writer.write(' ');
@@ -163,7 +159,7 @@ public void addMessage(String path, int line, int column, String message, Kind k
163159
break;
164160
}
165161
writer.write(' ');
166-
writer.write(URLEncoder.encode(message, ENCODING));
162+
writer.write(URLEncoder.encode(message, StandardCharsets.UTF_8));
167163
writer.write(EOL);
168164
} catch (IOException e) {
169165
handleException(e);
@@ -190,27 +186,26 @@ private void handleException(IOException e) {
190186
}
191187

192188
public static void process(File file, CompilerOutputProcessor callback) throws IOException {
193-
BufferedReader reader = newBufferedReader(file);
194-
try {
189+
try (BufferedReader reader = newBufferedReader(file)) {
195190
String str;
196191
while ((str = reader.readLine()) != null) {
197192
String value = str.substring(1);
198193
switch (str.charAt(0)) {
199194
case 'O': {
200195
StringTokenizer st = new StringTokenizer(value, " ");
201-
String inputPath = URLDecoder.decode(st.nextToken(), ENCODING);
202-
String outputPath = URLDecoder.decode(st.nextToken(), ENCODING);
196+
String inputPath = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
197+
String outputPath = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
203198
callback.processOutput(
204199
!".".equals(inputPath) ? new File(inputPath) : null, new File(outputPath));
205200
break;
206201
}
207202
case 'M': {
208203
StringTokenizer st = new StringTokenizer(value, " ");
209-
String path = URLDecoder.decode(st.nextToken(), ENCODING);
204+
String path = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
210205
int line = Integer.parseInt(st.nextToken());
211206
int column = Integer.parseInt(st.nextToken());
212207
MessageSeverity severity = toSeverity(st.nextToken());
213-
String message = URLDecoder.decode(st.nextToken(), ENCODING);
208+
String message = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
214209
callback.addMessage(path, line, column, message, severity);
215210
break;
216211
}
@@ -222,8 +217,6 @@ public static void process(File file, CompilerOutputProcessor callback) throws I
222217
throw new IllegalArgumentException();
223218
}
224219
}
225-
} finally {
226-
reader.close();
227220
}
228221
}
229222

@@ -239,20 +232,20 @@ private static MessageSeverity toSeverity(String token) {
239232
}
240233
}
241234

242-
public static interface CompilerOutputProcessor {
243-
public void processOutput(File inputFile, File outputFile);
235+
public interface CompilerOutputProcessor {
236+
void processOutput(File inputFile, File outputFile);
244237

245-
public void addMessage(String path, int line, int column, String message, MessageSeverity kind);
238+
void addMessage(String path, int line, int column, String message, MessageSeverity kind);
246239

247-
public void addLogMessage(String message);
240+
void addLogMessage(String message);
248241
}
249242

250243
static Writer newWriter(File file) throws IOException {
251-
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), ENCODING));
244+
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
252245
}
253246

254247
static BufferedReader newBufferedReader(File file) throws IOException {
255-
return new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING));
248+
return new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
256249
}
257250

258251
public static void main(String[] args) throws IOException {
@@ -280,7 +273,7 @@ private static void compile(final CompilerConfiguration config, final CompilerOu
280273
}
281274

282275
final Charset sourceEncoding = config.getSourceEncoding();
283-
final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
276+
final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
284277
final StandardJavaFileManager standardFileManager =
285278
compiler.getStandardFileManager(diagnosticCollector, null, sourceEncoding);
286279
final Iterable<? extends JavaFileObject> fileObjects =

takari-lifecycle-plugin/src/main/java/io/takari/maven/plugins/configurator/MojoConfigurationProcessor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.InputStream;
1111
import java.io.InputStreamReader;
1212
import java.io.StringReader;
13+
import java.nio.charset.StandardCharsets;
1314
import java.util.List;
1415
import org.apache.maven.plugin.descriptor.MojoDescriptor;
1516
import org.apache.maven.plugin.descriptor.PluginDescriptor;
@@ -30,19 +31,17 @@
3031
//
3132
public class MojoConfigurationProcessor {
3233

33-
private static PluginDescriptorBuilder pluginDescriptorBuilder = new PluginDescriptorBuilder();
34+
private static final PluginDescriptorBuilder pluginDescriptorBuilder = new PluginDescriptorBuilder();
3435

3536
public PlexusConfiguration mojoConfigurationFor(
3637
Object mojoInstance, PlexusConfiguration pluginConfigurationFromMaven)
3738
throws ComponentConfigurationException {
3839
try (InputStream is = mojoInstance.getClass().getResourceAsStream("/META-INF/maven/plugin.xml")) {
39-
PluginDescriptor pd =
40-
pluginDescriptorBuilder.build(new InputStreamReader(is, "UTF-8")); // closes input stream too
40+
PluginDescriptor pd = pluginDescriptorBuilder.build(
41+
new InputStreamReader(is, StandardCharsets.UTF_8)); // closes input stream too
4142
String goal = determineGoal(mojoInstance.getClass().getName(), pd);
4243
PlexusConfiguration defaultMojoConfiguration = pd.getMojo(goal).getMojoConfiguration();
43-
PlexusConfiguration mojoConfiguration =
44-
extractAndMerge(goal, pluginConfigurationFromMaven, defaultMojoConfiguration);
45-
return mojoConfiguration;
44+
return extractAndMerge(goal, pluginConfigurationFromMaven, defaultMojoConfiguration);
4645
} catch (Exception e) {
4746
throw new ComponentConfigurationException(e);
4847
}

takari-lifecycle-plugin/src/main/java/io/takari/maven/plugins/plugin/GeneratorUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static String decodeJavadocTags(String description) {
7878
return "";
7979
}
8080

81-
StringBuffer decoded = new StringBuffer(description.length() + 1024);
81+
StringBuilder decoded = new StringBuilder(description.length() + 1024);
8282

8383
Matcher matcher = Pattern.compile("\\{@(\\w+)\\s*([^\\}]*)\\}").matcher(description);
8484
while (matcher.find()) {

takari-lifecycle-plugin/src/main/java/io/takari/maven/plugins/plugin/PluginDescriptorMojo.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.InputStream;
2727
import java.io.OutputStream;
2828
import java.io.OutputStreamWriter;
29+
import java.nio.charset.StandardCharsets;
2930
import java.nio.file.Files;
3031
import java.util.HashMap;
3132
import java.util.LinkedHashSet;
@@ -35,10 +36,10 @@
3536
import java.util.stream.Collectors;
3637
import java.util.zip.ZipEntry;
3738
import java.util.zip.ZipFile;
39+
import javax.inject.Inject;
3840
import org.apache.maven.artifact.Artifact;
3941
import org.apache.maven.model.Resource;
4042
import org.apache.maven.plugin.MojoExecutionException;
41-
import org.apache.maven.plugins.annotations.Component;
4243
import org.apache.maven.plugins.annotations.LifecyclePhase;
4344
import org.apache.maven.plugins.annotations.Mojo;
4445
import org.apache.maven.plugins.annotations.Parameter;
@@ -98,7 +99,7 @@ public class PluginDescriptorMojo extends TakariLifecycleMojo {
9899
@Incremental(configuration = ignore)
99100
private List<Resource> resources;
100101

101-
@Component
102+
@Inject
102103
private AggregatorBuildContext context;
103104

104105
@Override
@@ -316,8 +317,8 @@ protected void createEclipseMetadataXml(Output<File> output, File mojosXml, File
316317
}
317318

318319
private void writeEclipseMetadataXml(OutputStream out, List<String> goals) throws IOException {
319-
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
320-
XMLWriter w = new PrettyPrintXMLWriter(writer, "UTF-8", null);
320+
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
321+
XMLWriter w = new PrettyPrintXMLWriter(writer, StandardCharsets.UTF_8.name(), null);
321322

322323
w.writeMarkup("\n<!-- Generated by " + this.getClass().getSimpleName() + " -->\n\n");
323324

takari-lifecycle-plugin/src/main/java/io/takari/maven/plugins/plugin/PluginDescriptorWriter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.IOException;
3535
import java.io.OutputStream;
3636
import java.io.OutputStreamWriter;
37+
import java.nio.charset.StandardCharsets;
3738
import java.util.ArrayList;
3839
import java.util.LinkedHashSet;
3940
import java.util.List;
@@ -44,12 +45,11 @@
4445

4546
// originally copied from org.apache.maven.tools.plugin.generator.PluginDescriptorGenerator
4647
class PluginDescriptorWriter {
47-
private static final String encoding = "UTF-8";
4848

4949
public void writeDescriptor(OutputStream outputStream, PluginDescriptor pluginDescriptor) throws IOException {
50-
OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding);
50+
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
5151

52-
XMLWriter w = new PrettyPrintXMLWriter(writer, encoding, null);
52+
XMLWriter w = new PrettyPrintXMLWriter(writer, StandardCharsets.UTF_8.name(), null);
5353

5454
w.writeMarkup("\n<!-- Generated by takari-plugin-tools -->\n\n");
5555
w.startElement("plugin");
@@ -103,7 +103,6 @@ private void element(XMLWriter w, String name, String value) {
103103
/**
104104
* @param mojoDescriptor not null
105105
* @param w not null
106-
* @param helpDescriptor will clean html content from description fields
107106
*/
108107
protected void writeMojoDescriptor(MojoDescriptor mojoDescriptor, XMLWriter w) {
109108
w.startElement("mojo");
@@ -155,7 +154,7 @@ protected void writeMojoDescriptor(MojoDescriptor mojoDescriptor, XMLWriter w) {
155154
// Parameters
156155
// ----------------------------------------------------------------------
157156

158-
Set<MojoParameter> configuration = new LinkedHashSet<MojoParameter>();
157+
Set<MojoParameter> configuration = new LinkedHashSet<>();
159158

160159
w.startElement("parameters");
161160
List<MojoParameter> parameters = new ArrayList<>(mojoDescriptor.getParameters());

takari-lifecycle-plugin/src/test/java/io/takari/maven/plugins/compile/jdt/FilerImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void testResourceDoesNotExist() throws Exception {
183183
}
184184

185185
private Classpath createClasspath() throws IOException {
186-
final List<ClasspathEntry> entries = new ArrayList<ClasspathEntry>();
186+
final List<ClasspathEntry> entries = new ArrayList<>();
187187
final List<MutableClasspathEntry> mutableentries = new ArrayList<MutableClasspathEntry>();
188188
for (Path file : JavaInstallation.getDefault().getClasspath()) {
189189
if (Files.isRegularFile(file)) {

0 commit comments

Comments
 (0)