From 77cd591b636d6b3fbb1e33421a7048d696d431ac Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Mon, 20 Aug 2018 07:27:40 +0200 Subject: [PATCH] Remove redundancies --- .../tools/ant/ProjectHelperRepository.java | 6 +-- .../tools/ant/filters/FixCrLfFilter.java | 14 +++---- .../tools/ant/taskdefs/AntStructure.java | 37 ++++--------------- .../apache/tools/ant/taskdefs/Execute.java | 4 +- .../constantpool/InvokeDynamicCPInfo.java | 2 +- .../constantpool/MethodHandleCPInfo.java | 2 +- .../depend/constantpool/MethodTypeCPInfo.java | 2 +- .../junit/BriefJUnitResultFormatter.java | 3 +- .../junitlauncher/LauncherSupport.java | 2 +- .../apache/tools/ant/types/PatternSet.java | 2 +- .../types/resources/selectors/Majority.java | 2 - .../tools/ant/util/FlatFileNameMapper.java | 4 +- .../ant/util/optional/JavaxScriptRunner.java | 2 +- .../zip/UnsupportedZipFeatureException.java | 3 +- .../ant/taskdefs/DefaultExcludesTest.java | 2 +- .../apache/tools/ant/taskdefs/JavaTest.java | 10 ++--- .../tools/ant/util/ReaderInputStreamTest.java | 2 +- 17 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/main/org/apache/tools/ant/ProjectHelperRepository.java b/src/main/org/apache/tools/ant/ProjectHelperRepository.java index 532374b56..d40418684 100644 --- a/src/main/org/apache/tools/ant/ProjectHelperRepository.java +++ b/src/main/org/apache/tools/ant/ProjectHelperRepository.java @@ -181,10 +181,8 @@ public class ProjectHelperRepository { try { // This code is needed by EBCDIC and other strange systems. // It's a fix for bugs reported in xerces - InputStreamReader isr; - isr = new InputStreamReader(is, StandardCharsets.UTF_8); - BufferedReader rd = new BufferedReader(isr); - + BufferedReader rd = new BufferedReader(new InputStreamReader(is, + StandardCharsets.UTF_8)); String helperClassName = rd.readLine(); rd.close(); diff --git a/src/main/org/apache/tools/ant/filters/FixCrLfFilter.java b/src/main/org/apache/tools/ant/filters/FixCrLfFilter.java index f5e23c433..5cb463334 100644 --- a/src/main/org/apache/tools/ant/filters/FixCrLfFilter.java +++ b/src/main/org/apache/tools/ant/filters/FixCrLfFilter.java @@ -442,11 +442,11 @@ public final class FixCrLfFilter extends BaseParamFilterReader implements Chaina return in instanceof SimpleFilterReader && ((SimpleFilterReader) in).editsBlocked(); } - public int read() throws java.io.IOException { + public int read() throws IOException { return preemptIndex > 0 ? preempt[--preemptIndex] : in.read(); } - public void close() throws java.io.IOException { + public void close() throws IOException { in.close(); } @@ -458,23 +458,23 @@ public final class FixCrLfFilter extends BaseParamFilterReader implements Chaina return in.markSupported(); } - public boolean ready() throws java.io.IOException { + public boolean ready() throws IOException { return in.ready(); } - public void mark(int i) throws java.io.IOException { + public void mark(int i) throws IOException { in.mark(i); } - public long skip(long i) throws java.io.IOException { + public long skip(long i) throws IOException { return in.skip(i); } - public int read(char[] buf) throws java.io.IOException { + public int read(char[] buf) throws IOException { return read(buf, 0, buf.length); } - public int read(char[] buf, int start, int length) throws java.io.IOException { + public int read(char[] buf, int start, int length) throws IOException { int count = 0; int c = 0; diff --git a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java index 11529a6be..10473d3df 100644 --- a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java +++ b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java @@ -87,22 +87,8 @@ public class AntStructure extends Task { throw new BuildException("output attribute is required", getLocation()); } - PrintWriter out = null; - try { - OutputStream fos = null; - try { - fos = Files.newOutputStream(output.toPath()); - out = new PrintWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)); - } catch (final UnsupportedEncodingException ue) { - FileUtils.close(fos); - /* - * Plain impossible with UTF8, see - * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html - * - * fallback to platform specific anyway. - */ - out = new PrintWriter(new FileWriter(output)); - } + try (PrintWriter out = new PrintWriter(new OutputStreamWriter( + Files.newOutputStream(output.toPath()), StandardCharsets.UTF_8))) { printer.printHead(out, getProject(), new Hashtable<>(getProject().getTaskDefinitions()), @@ -110,31 +96,24 @@ public class AntStructure extends Task { printer.printTargetDecl(out); - for (final String typeName : getProject().getCopyOfDataTypeDefinitions() - .keySet()) { - printer.printElementDecl( - out, getProject(), typeName, - getProject().getDataTypeDefinitions().get(typeName)); + for (final String typeName : getProject().getCopyOfDataTypeDefinitions().keySet()) { + printer.printElementDecl(out, getProject(), typeName, + getProject().getDataTypeDefinitions().get(typeName)); } for (final String tName : getProject().getCopyOfTaskDefinitions().keySet()) { printer.printElementDecl(out, getProject(), tName, - getProject().getTaskDefinitions().get(tName)); + getProject().getTaskDefinitions().get(tName)); } printer.printTail(out); if (out.checkError()) { - throw new IOException( - "Encountered an error writing Ant structure"); + throw new IOException("Encountered an error writing Ant structure"); } } catch (final IOException ioe) { throw new BuildException("Error writing " - + output.getAbsolutePath(), ioe, getLocation()); - } finally { - if (out != null) { - out.close(); - } + + output.getAbsolutePath(), ioe, getLocation()); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Execute.java b/src/main/org/apache/tools/ant/taskdefs/Execute.java index 913ba36a4..2f29256ed 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Execute.java +++ b/src/main/org/apache/tools/ant/taskdefs/Execute.java @@ -428,7 +428,7 @@ public class Execute { * Runs a process defined by the command line and returns its exit status. * * @return the exit status of the subprocess or INVALID. - * @exception java.io.IOException The exception is thrown, if launching + * @exception IOException The exception is thrown, if launching * of the subprocess failed. */ public int execute() throws IOException { @@ -484,7 +484,7 @@ public class Execute { * Starts a process defined by the command line. * Ant will not wait for this process, nor log its output. * - * @throws java.io.IOException The exception is thrown, if launching + * @throws IOException The exception is thrown, if launching * of the subprocess failed. * @since Ant 1.6 */ diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java index 176b99ac8..d4158262b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/InvokeDynamicCPInfo.java @@ -43,7 +43,7 @@ public class InvokeDynamicCPInfo extends ConstantCPInfo { * * @param cpStream the DataInputStream which contains the constant pool * entry to be read. - * @exception java.io.IOException if there is a problem reading the entry from + * @exception IOException if there is a problem reading the entry from * the stream. */ @Override diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java index 1da73655e..32ba9ade7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodHandleCPInfo.java @@ -58,7 +58,7 @@ public class MethodHandleCPInfo extends ConstantPoolEntry { * * @param cpStream the DataInputStream which contains the constant pool * entry to be read. - * @exception java.io.IOException if there is a problem reading the entry from + * @exception IOException if there is a problem reading the entry from * the stream. */ @Override diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java index 7b6f9a68b..660bb3698 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/MethodTypeCPInfo.java @@ -41,7 +41,7 @@ public class MethodTypeCPInfo extends ConstantCPInfo { * * @param cpStream the DataInputStream which contains the constant pool * entry to be read. - * @exception java.io.IOException if there is a problem reading the entry from + * @exception IOException if there is a problem reading the entry from * the stream. */ @Override diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/BriefJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/BriefJUnitResultFormatter.java index ecf33e722..d1ac28b3c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/BriefJUnitResultFormatter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/BriefJUnitResultFormatter.java @@ -21,6 +21,7 @@ package org.apache.tools.ant.taskdefs.optional.junit; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.StringWriter; import java.text.NumberFormat; @@ -91,7 +92,7 @@ public class BriefJUnitResultFormatter implements JUnitResultFormatter, IgnoredT @Override public void setOutput(OutputStream out) { this.out = out; - output = new BufferedWriter(new java.io.OutputStreamWriter(out)); + output = new BufferedWriter(new OutputStreamWriter(out)); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java index 6a8027b3e..f020f6635 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java @@ -238,7 +238,7 @@ class LauncherSupport { throw new BuildException("Listener class " + className + " is not of type " + TestExecutionListener.class.getName()); } try { - return TestExecutionListener.class.cast(klass.newInstance()); + return (TestExecutionListener) klass.newInstance(); } catch (Exception e) { throw new BuildException("Failed to create an instance of listener " + className, e); } diff --git a/src/main/org/apache/tools/ant/types/PatternSet.java b/src/main/org/apache/tools/ant/types/PatternSet.java index 167a246f0..bf20fcfa6 100644 --- a/src/main/org/apache/tools/ant/types/PatternSet.java +++ b/src/main/org/apache/tools/ant/types/PatternSet.java @@ -218,7 +218,7 @@ public class PatternSet extends DataType implements Cloneable { public String toString() { String baseString = super.toString(); return encoding == null ? baseString - : new StringBuilder(baseString).append(";encoding->").append(encoding).toString(); + : baseString + ";encoding->" + encoding; } } diff --git a/src/main/org/apache/tools/ant/types/resources/selectors/Majority.java b/src/main/org/apache/tools/ant/types/resources/selectors/Majority.java index 7adcbf79c..ded9205a2 100644 --- a/src/main/org/apache/tools/ant/types/resources/selectors/Majority.java +++ b/src/main/org/apache/tools/ant/types/resources/selectors/Majority.java @@ -17,8 +17,6 @@ */ package org.apache.tools.ant.types.resources.selectors; -import java.util.Iterator; - import org.apache.tools.ant.types.Resource; /** diff --git a/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java b/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java index 07ba4c6ed..203da5f90 100644 --- a/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java +++ b/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java @@ -18,6 +18,8 @@ package org.apache.tools.ant.util; +import java.io.File; + /** * Implementation of FileNameMapper that always returns the source * file name without any leading directory information. @@ -53,6 +55,6 @@ public class FlatFileNameMapper implements FileNameMapper { @Override public String[] mapFileName(String sourceFileName) { return sourceFileName == null ? null - : new String[] {new java.io.File(sourceFileName).getName()}; + : new String[] {new File(sourceFileName).getName()}; } } diff --git a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java index 28246d386..2e7c04418 100644 --- a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java +++ b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java @@ -110,7 +110,7 @@ public class JavaxScriptRunner extends ScriptRunnerBase { "Unable to create javax script engine for %s", getLanguage()); } - if (Compilable.class.isInstance(engine)) { + if (engine instanceof Compilable) { getProject().log("compile script " + execName, Project.MSG_VERBOSE); diff --git a/src/main/org/apache/tools/zip/UnsupportedZipFeatureException.java b/src/main/org/apache/tools/zip/UnsupportedZipFeatureException.java index 34e2117cc..112e7e414 100644 --- a/src/main/org/apache/tools/zip/UnsupportedZipFeatureException.java +++ b/src/main/org/apache/tools/zip/UnsupportedZipFeatureException.java @@ -18,6 +18,7 @@ package org.apache.tools.zip; +import java.io.Serializable; import java.util.zip.ZipException; /** @@ -66,7 +67,7 @@ public class UnsupportedZipFeatureException extends ZipException { * ZIP Features that may or may not be supported. */ @SuppressWarnings("serial") - public static class Feature implements java.io.Serializable { + public static class Feature implements Serializable { /** * The entry is encrypted. */ diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java index 44c90b1fd..e028469bb 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/DefaultExcludesTest.java @@ -161,7 +161,7 @@ public class DefaultExcludesTest { assertEquals("current default excludes: string array length match", expected.length, actual.length); for (String element : expected) { assertTrue("current default excludes: didn't find element " + element + " in array match", - Arrays.stream(actual).anyMatch(member -> member.equals(element))); + Arrays.asList(actual).contains(element)); } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java index 2630a81a2..1406600f2 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java @@ -418,7 +418,7 @@ public class JavaTest { * @throws Exception */ @Test - public void testSimpleSourceFile() throws Exception { + public void testSimpleSourceFile() { requireJava11(); buildRule.executeTarget("simpleSourceFile"); } @@ -429,7 +429,7 @@ public class JavaTest { * @throws Exception */ @Test - public void testSourceFileRequiresFork() throws Exception { + public void testSourceFileRequiresFork() { requireJava11(); thrown.expect(BuildException.class); thrown.expectMessage("Cannot execute sourcefile in non-forked mode. Please set fork='true'"); @@ -443,7 +443,7 @@ public class JavaTest { * @throws Exception */ @Test - public void testSourceFileCantUseClassname() throws Exception { + public void testSourceFileCantUseClassname() { requireJava11(); thrown.expect(BuildException.class); thrown.expectMessage("Cannot use 'sourcefile' in combination with"); @@ -457,7 +457,7 @@ public class JavaTest { * @throws Exception */ @Test - public void testSourceFileCantUseJar() throws Exception { + public void testSourceFileCantUseJar() { requireJava11(); thrown.expect(BuildException.class); thrown.expectMessage("Cannot use 'sourcefile' in combination with"); @@ -471,7 +471,7 @@ public class JavaTest { * @throws Exception */ @Test - public void testSourceFileCantUseModule() throws Exception { + public void testSourceFileCantUseModule() { requireJava11(); thrown.expect(BuildException.class); thrown.expectMessage("Cannot use 'sourcefile' in combination with"); diff --git a/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java b/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java index a82189e4a..332af76ad 100644 --- a/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/ReaderInputStreamTest.java @@ -81,7 +81,7 @@ public class ReaderInputStreamTest { } @Test - public void testPreample() throws Exception { + public void testPreample() { byte[] bytes = "".getBytes(StandardCharsets.UTF_16); System.out.println("Preample len is " + bytes.length); }