Skip to content

Commit

Permalink
Modernize (#191)
Browse files Browse the repository at this point in the history
Some but not all of issues are fixed
  • Loading branch information
cstamas authored Sep 10, 2024
1 parent 8bdb721 commit c12f808
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
Expand All @@ -36,8 +37,6 @@ public class CompilerJavacForked {

private static final String EOL = "\n";

private static final String ENCODING = "UTF-8";

public static class CompilerConfiguration {

private final Charset encoding;
Expand Down Expand Up @@ -65,8 +64,7 @@ public Iterable<File> getSources() {
}

public void write(File file) throws IOException {
Writer writer = newWriter(file);
try {
try (Writer writer = newWriter(file)) {
// encoding
if (encoding != null) {
writer.write('C');
Expand All @@ -87,18 +85,15 @@ public void write(File file) throws IOException {
writer.write(source.getCanonicalPath());
writer.write(EOL);
}
} finally {
writer.close();
}
}

public static CompilerConfiguration read(File file) throws IOException {
Charset encoding = null;
List<String> options = new ArrayList<String>();
List<File> sources = new ArrayList<File>();
List<String> options = new ArrayList<>();
List<File> sources = new ArrayList<>();

BufferedReader reader = newBufferedReader(file);
try {
try (BufferedReader reader = newBufferedReader(file)) {
String str;
while ((str = reader.readLine()) != null) {
String value = str.substring(1);
Expand All @@ -114,8 +109,6 @@ public static CompilerConfiguration read(File file) throws IOException {
break;
}
}
} finally {
reader.close();
}

return new CompilerConfiguration(encoding, options, sources);
Expand All @@ -133,9 +126,12 @@ public CompilerOutput(File file) throws IOException {
public void processOutput(File inputFile, File outputFile) {
try {
writer.write('O');
writer.write(inputFile != null ? URLEncoder.encode(inputFile.getCanonicalPath(), ENCODING) : ".");
writer.write(
inputFile != null
? URLEncoder.encode(inputFile.getCanonicalPath(), StandardCharsets.UTF_8)
: ".");
writer.write(' ');
writer.write(URLEncoder.encode(outputFile.getCanonicalPath(), ENCODING));
writer.write(URLEncoder.encode(outputFile.getCanonicalPath(), StandardCharsets.UTF_8));
writer.write(EOL);
} catch (IOException e) {
handleException(e);
Expand All @@ -145,7 +141,7 @@ public void processOutput(File inputFile, File outputFile) {
public void addMessage(String path, int line, int column, String message, Kind kind) {
try {
writer.write('M');
writer.write(URLEncoder.encode(path, ENCODING));
writer.write(URLEncoder.encode(path, StandardCharsets.UTF_8));
writer.write(' ');
writer.write(Integer.toString(line));
writer.write(' ');
Expand All @@ -163,7 +159,7 @@ public void addMessage(String path, int line, int column, String message, Kind k
break;
}
writer.write(' ');
writer.write(URLEncoder.encode(message, ENCODING));
writer.write(URLEncoder.encode(message, StandardCharsets.UTF_8));
writer.write(EOL);
} catch (IOException e) {
handleException(e);
Expand All @@ -190,27 +186,26 @@ private void handleException(IOException e) {
}

public static void process(File file, CompilerOutputProcessor callback) throws IOException {
BufferedReader reader = newBufferedReader(file);
try {
try (BufferedReader reader = newBufferedReader(file)) {
String str;
while ((str = reader.readLine()) != null) {
String value = str.substring(1);
switch (str.charAt(0)) {
case 'O': {
StringTokenizer st = new StringTokenizer(value, " ");
String inputPath = URLDecoder.decode(st.nextToken(), ENCODING);
String outputPath = URLDecoder.decode(st.nextToken(), ENCODING);
String inputPath = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
String outputPath = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
callback.processOutput(
!".".equals(inputPath) ? new File(inputPath) : null, new File(outputPath));
break;
}
case 'M': {
StringTokenizer st = new StringTokenizer(value, " ");
String path = URLDecoder.decode(st.nextToken(), ENCODING);
String path = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
int line = Integer.parseInt(st.nextToken());
int column = Integer.parseInt(st.nextToken());
MessageSeverity severity = toSeverity(st.nextToken());
String message = URLDecoder.decode(st.nextToken(), ENCODING);
String message = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8);
callback.addMessage(path, line, column, message, severity);
break;
}
Expand All @@ -222,8 +217,6 @@ public static void process(File file, CompilerOutputProcessor callback) throws I
throw new IllegalArgumentException();
}
}
} finally {
reader.close();
}
}

Expand All @@ -239,20 +232,20 @@ private static MessageSeverity toSeverity(String token) {
}
}

public static interface CompilerOutputProcessor {
public void processOutput(File inputFile, File outputFile);
public interface CompilerOutputProcessor {
void processOutput(File inputFile, File outputFile);

public void addMessage(String path, int line, int column, String message, MessageSeverity kind);
void addMessage(String path, int line, int column, String message, MessageSeverity kind);

public void addLogMessage(String message);
void addLogMessage(String message);
}

static Writer newWriter(File file) throws IOException {
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), ENCODING));
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
}

static BufferedReader newBufferedReader(File file) throws IOException {
return new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING));
return new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
}

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

final Charset sourceEncoding = config.getSourceEncoding();
final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
final StandardJavaFileManager standardFileManager =
compiler.getStandardFileManager(diagnosticCollector, null, sourceEncoding);
final Iterable<? extends JavaFileObject> fileObjects =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
Expand All @@ -30,19 +31,17 @@
//
public class MojoConfigurationProcessor {

private static PluginDescriptorBuilder pluginDescriptorBuilder = new PluginDescriptorBuilder();
private static final PluginDescriptorBuilder pluginDescriptorBuilder = new PluginDescriptorBuilder();

public PlexusConfiguration mojoConfigurationFor(
Object mojoInstance, PlexusConfiguration pluginConfigurationFromMaven)
throws ComponentConfigurationException {
try (InputStream is = mojoInstance.getClass().getResourceAsStream("/META-INF/maven/plugin.xml")) {
PluginDescriptor pd =
pluginDescriptorBuilder.build(new InputStreamReader(is, "UTF-8")); // closes input stream too
PluginDescriptor pd = pluginDescriptorBuilder.build(
new InputStreamReader(is, StandardCharsets.UTF_8)); // closes input stream too
String goal = determineGoal(mojoInstance.getClass().getName(), pd);
PlexusConfiguration defaultMojoConfiguration = pd.getMojo(goal).getMojoConfiguration();
PlexusConfiguration mojoConfiguration =
extractAndMerge(goal, pluginConfigurationFromMaven, defaultMojoConfiguration);
return mojoConfiguration;
return extractAndMerge(goal, pluginConfigurationFromMaven, defaultMojoConfiguration);
} catch (Exception e) {
throw new ComponentConfigurationException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static String decodeJavadocTags(String description) {
return "";
}

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

Matcher matcher = Pattern.compile("\\{@(\\w+)\\s*([^\\}]*)\\}").matcher(description);
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand All @@ -35,10 +36,10 @@
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.inject.Inject;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -98,7 +99,7 @@ public class PluginDescriptorMojo extends TakariLifecycleMojo {
@Incremental(configuration = ignore)
private List<Resource> resources;

@Component
@Inject
private AggregatorBuildContext context;

@Override
Expand Down Expand Up @@ -316,8 +317,8 @@ protected void createEclipseMetadataXml(Output<File> output, File mojosXml, File
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -44,12 +45,11 @@

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

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

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

w.writeMarkup("\n<!-- Generated by takari-plugin-tools -->\n\n");
w.startElement("plugin");
Expand Down Expand Up @@ -103,7 +103,6 @@ private void element(XMLWriter w, String name, String value) {
/**
* @param mojoDescriptor not null
* @param w not null
* @param helpDescriptor will clean html content from description fields
*/
protected void writeMojoDescriptor(MojoDescriptor mojoDescriptor, XMLWriter w) {
w.startElement("mojo");
Expand Down Expand Up @@ -155,7 +154,7 @@ protected void writeMojoDescriptor(MojoDescriptor mojoDescriptor, XMLWriter w) {
// Parameters
// ----------------------------------------------------------------------

Set<MojoParameter> configuration = new LinkedHashSet<MojoParameter>();
Set<MojoParameter> configuration = new LinkedHashSet<>();

w.startElement("parameters");
List<MojoParameter> parameters = new ArrayList<>(mojoDescriptor.getParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void testResourceDoesNotExist() throws Exception {
}

private Classpath createClasspath() throws IOException {
final List<ClasspathEntry> entries = new ArrayList<ClasspathEntry>();
final List<ClasspathEntry> entries = new ArrayList<>();
final List<MutableClasspathEntry> mutableentries = new ArrayList<MutableClasspathEntry>();
for (Path file : JavaInstallation.getDefault().getClasspath()) {
if (Files.isRegularFile(file)) {
Expand Down

0 comments on commit c12f808

Please sign in to comment.