Spotbugs cleanup.

This commit is contained in:
Erik C. Thauvin 2019-03-30 20:32:42 -07:00
parent 6fcf7daa1c
commit 6f2ce396f3

View file

@ -34,7 +34,6 @@ package net.thauvin.erik.semver;
import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache; import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory; import com.github.mustachejava.MustacheFactory;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.annotation.processing.*; import javax.annotation.processing.*;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@ -47,7 +46,6 @@ import javax.tools.FileObject;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -71,7 +69,6 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s));
} }
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
private VersionInfo findValues(final Version version) private VersionInfo findValues(final Version version)
throws IOException { throws IOException {
final VersionInfo versionInfo = new VersionInfo(version); final VersionInfo versionInfo = new VersionInfo(version);
@ -188,7 +185,6 @@ public class VersionProcessor extends AbstractProcessor {
} else { } else {
template = version.template(); template = version.template();
} }
writeTemplate(version.type(), versionInfo, template); writeTemplate(version.type(), versionInfo, template);
} catch (IOException e) { } catch (IOException e) {
error("IOException occurred while running the annotation processor: " + e.getMessage(), e); error("IOException occurred while running the annotation processor: " + e.getMessage(), e);
@ -203,7 +199,6 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.WARNING, s); log(Diagnostic.Kind.WARNING, s);
} }
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
private void writeTemplate(final String type, private void writeTemplate(final String type,
final VersionInfo versionInfo, final VersionInfo versionInfo,
final String template) final String template)
@ -227,15 +222,19 @@ public class VersionProcessor extends AbstractProcessor {
final String fileName = versionInfo.getClassName() + '.' + type; final String fileName = versionInfo.getClassName() + '.' + type;
if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
final Map<String, String> options = processingEnv.getOptions(); final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME);
final String kaptGenDir = options.get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME);
if (kaptGenDir == null) { if (kaptGenDir == null) {
throw new IOException("Could not find the target directory for generated Kotlin files."); throw new IOException("Could not find the target directory for generated Kotlin files.");
} }
final File versionFile = new File(kaptGenDir, fileName); final File versionFile = new File(kaptGenDir, fileName);
versionFile.getParentFile().mkdirs(); if (!versionFile.getParentFile().exists()) {
try (final FileWriter fw = new FileWriter(versionFile);) { if (!versionFile.getParentFile().mkdirs()) {
mustache.execute(fw, versionInfo).flush(); note("Could not create target directory: " + versionFile.getParentFile().getAbsolutePath());
}
}
try (final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(versionFile),
StandardCharsets.UTF_8)) {
mustache.execute(osw, versionInfo).flush();
} }
note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')'); note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')');
} else { } else {