Dogfood - IdenticalCatchBranches, AvoidFileStream

This commit is contained in:
Andreas Dangel 2018-11-25 15:54:42 +01:00
parent e3ef72cc06
commit e735ee0f84
18 changed files with 45 additions and 101 deletions

View File

@ -44,12 +44,9 @@ public abstract class AbstractApexNode<T extends AstNode> extends AbstractApexNo
return loc != null && Locations.isReal(loc);
} catch (UnexpectedCodePathException e) {
return false;
} catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException | NullPointerException e) {
// bug in apex-jorje? happens on some ReferenceExpression nodes
return false;
} catch (NullPointerException e) {
// bug in apex-jorje?
return false;
}
}

View File

@ -211,9 +211,7 @@ public final class ApexTreeBuilder extends AstVisitor<AdditionalPassScope> {
private static <T extends AstNode> void register(Class<T> nodeType, Class<? extends AbstractApexNode<T>> nodeAdapterType) {
try {
NODE_TYPE_TO_NODE_ADAPTER_TYPE.put(nodeType, nodeAdapterType.getConstructor(nodeType));
} catch (SecurityException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
} catch (SecurityException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
@ -248,9 +246,7 @@ public final class ApexTreeBuilder extends AstVisitor<AdditionalPassScope> {
"There is no Node adapter class registered for the Node class: " + node.getClass());
}
return constructor.newInstance(node);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());

View File

@ -74,11 +74,8 @@ public class RuleChain {
if (visitor == null) {
if (language.getRuleChainVisitorClass() != null) {
try {
visitor = (RuleChainVisitor) language.getRuleChainVisitorClass().newInstance();
} catch (InstantiationException e) {
throw new IllegalStateException(
"Failure to created RuleChainVisitor: " + language.getRuleChainVisitorClass(), e);
} catch (IllegalAccessException e) {
visitor = (RuleChainVisitor) language.getRuleChainVisitorClass().getConstructor().newInstance();
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) {
throw new IllegalStateException(
"Failure to created RuleChainVisitor: " + language.getRuleChainVisitorClass(), e);
}

View File

@ -380,18 +380,12 @@ public class RuleSetFactory {
ruleSetBuilder.filterRulesByPriority(minimumPriority);
return ruleSetBuilder.build();
} catch (ClassNotFoundException cnfe) {
return classNotFoundProblem(cnfe);
} catch (InstantiationException ie) {
return classNotFoundProblem(ie);
} catch (IllegalAccessException iae) {
return classNotFoundProblem(iae);
} catch (ParserConfigurationException pce) {
return classNotFoundProblem(pce);
} catch (IOException ioe) {
return classNotFoundProblem(ioe);
} catch (SAXException se) {
return classNotFoundProblem(se);
} catch (ReflectiveOperationException ex) {
ex.printStackTrace();
throw new RuntimeException("Couldn't find the class " + ex.getMessage(), ex);
} catch (ParserConfigurationException | IOException | SAXException ex) {
ex.printStackTrace();
throw new RuntimeException("Couldn't read the ruleset " + ruleSetReferenceId + ": " + ex.getMessage(), ex);
}
}
@ -432,11 +426,6 @@ public class RuleSetFactory {
return dbf.newDocumentBuilder();
}
private static RuleSet classNotFoundProblem(Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Couldn't find the class " + ex.getMessage());
}
/**
* Parse a rule node.
*

View File

@ -13,7 +13,6 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@ -206,9 +205,7 @@ public class Formatter {
if (res instanceof Charset) {
return ((Charset) res).name();
}
} catch (NoSuchFieldException ignored) {
// fall-through
} catch (IllegalAccessException ignored) {
} catch (ReflectiveOperationException ignored) {
// fall-through
}
return getNativeConsoleEncoding();
@ -224,11 +221,7 @@ public class Formatter {
if (res instanceof String) {
return (String) res;
}
} catch (NoSuchMethodException ignored) {
// fall-through
} catch (InvocationTargetException ignored) {
// fall-through
} catch (IllegalAccessException ignored) {
} catch (ReflectiveOperationException ignored) {
// fall-through
}
return null;

View File

@ -173,12 +173,8 @@ public final class CPDCommandLineInterface {
LOGGER.fine(
String.format("Adding DBURI=%s with DBType=%s", dburi.toString(), dburi.getDbType().toString()));
cpd.add(dburi);
} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
throw new IllegalStateException("uri=" + uri, e);
} catch (URISyntaxException ex) {
throw new IllegalStateException("uri=" + uri, ex);
} catch (Exception ex) {
throw new IllegalStateException("uri=" + uri, ex);
}
}

View File

@ -706,10 +706,7 @@ public class GUI implements CPDListener {
} else {
resultsTextArea.setText(report);
}
} catch (IOException t) {
t.printStackTrace();
JOptionPane.showMessageDialog(frame, "Halted due to " + t.getClass().getName() + "; " + t.getMessage());
} catch (RuntimeException t) {
} catch (IOException | RuntimeException t) {
t.printStackTrace();
JOptionPane.showMessageDialog(frame, "Halted due to " + t.getClass().getName() + "; " + t.getMessage());
}

View File

@ -31,9 +31,7 @@ public final class ClassLoaderUtil {
public static Class<?> getClass(String name) {
try {
return ClassLoaderUtil.class.getClassLoader().loadClass(name);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (NoClassDefFoundError e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new RuntimeException(e);
}
}

View File

@ -80,13 +80,12 @@ public final class RendererFactory {
}
}
}
} catch (InstantiationException e) {
throw new IllegalArgumentException("Unable to construct report renderer class: " + e.getLocalizedMessage());
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Unable to construct report renderer class: " + e.getLocalizedMessage());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(
"Unable to construct report renderer class: " + e.getLocalizedMessage(), e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(
"Unable to construct report renderer class: " + e.getTargetException().getLocalizedMessage());
"Unable to construct report renderer class: " + e.getTargetException().getLocalizedMessage(), e);
}
// Warn about legacy report format usages
if (REPORT_FORMAT_TO_RENDERER.containsKey(reportFormat) && !reportFormat.equals(renderer.getName())) {

View File

@ -142,11 +142,7 @@ public class XSLTRenderer extends XMLRenderer {
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return parser.parse(new InputSource(new StringReader(xml)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return null;

View File

@ -1015,11 +1015,7 @@ public class Designer implements ClipboardOwner {
break;
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
} catch (ParserConfigurationException | IOException | SAXException e) {
e.printStackTrace();
}
}
@ -1056,11 +1052,7 @@ public class Designer implements ClipboardOwner {
Source source = new DOMSource(document);
Result result = new StreamResult(new FileWriter(new File(SETTINGS_FILE_NAME)));
transformer.transform(source, result);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerException e) {
} catch (ParserConfigurationException | IOException | TransformerException e) {
e.printStackTrace();
}
}

View File

@ -4,12 +4,11 @@
package net.sourceforge.pmd.lang.java.rule.errorprone;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -50,7 +49,8 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule {
"Ignore list separator", ',', 4.0f);
public static final FileProperty EXCEPTION_FILE_DESCRIPTOR = new FileProperty("exceptionfile",
"File containing strings to skip (one string per line), only used if ignore list is not set", null, 5.0f);
"File containing strings to skip (one string per line), only used if ignore list is not set. "
+ "File must be UTF-8 encoded.", null, 5.0f);
public static class ExceptionParser {
@ -102,8 +102,9 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule {
definePropertyDescriptor(EXCEPTION_FILE_DESCRIPTOR);
}
private LineNumberReader getLineReader() throws FileNotFoundException {
return new LineNumberReader(new BufferedReader(new FileReader(getProperty(EXCEPTION_FILE_DESCRIPTOR))));
private LineNumberReader getLineReader() throws IOException {
return new LineNumberReader(Files.newBufferedReader(getProperty(EXCEPTION_FILE_DESCRIPTOR).toPath(),
StandardCharsets.UTF_8));
}
@Override

View File

@ -145,9 +145,7 @@ public final class EcmascriptTreeBuilder implements NodeVisitor {
Class<? extends EcmascriptNode<T>> nodeAdapterType) {
try {
NODE_TYPE_TO_NODE_ADAPTER_TYPE.put(nodeType, nodeAdapterType.getConstructor(nodeType));
} catch (SecurityException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
} catch (SecurityException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
@ -164,9 +162,7 @@ public final class EcmascriptTreeBuilder implements NodeVisitor {
"There is no Node adapter class registered for the Node class: " + node.getClass());
}
return constructor.newInstance(node);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());

View File

@ -8,9 +8,9 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import org.junit.After;
import org.junit.Before;
@ -60,10 +60,10 @@ public abstract class BaseCLITest {
protected void createTestOutputFile(String filename) {
try {
PrintStream out = new PrintStream(new FileOutputStream(filename));
PrintStream out = new PrintStream(Files.newOutputStream(new File(filename).toPath()));
System.setOut(out);
System.setErr(out);
} catch (FileNotFoundException e) {
} catch (IOException e) {
fail("Can't create file " + filename + " for test.");
}
}

View File

@ -334,12 +334,8 @@ public abstract class RuleTst {
throw new RuntimeException("Couldn't find " + testXmlFileName);
}
doc = documentBuilder.parse(inputStream);
} catch (FactoryConfigurationError fce) {
throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + fce, fce);
} catch (IOException ioe) {
throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + ioe, ioe);
} catch (SAXException se) {
throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + se, se);
} catch (FactoryConfigurationError | IOException | SAXException e) {
throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + e, e);
}
return parseTests(rule, doc);

View File

@ -5,10 +5,10 @@
package net.sourceforge.pmd.util.fxdesigner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -356,7 +356,7 @@ public class MainDesignerController implements Initializable, SettingsOwner {
private void loadSourceFromFile(File file) {
if (file != null) {
try {
String source = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
String source = IOUtils.toString(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
sourceEditorController.setText(source);
LanguageVersion guess = DesignerUtil.getLanguageVersionFromExtension(file.getName());
if (guess != null) { // guess the language from the extension

View File

@ -6,7 +6,6 @@ package net.sourceforge.pmd.util.fxdesigner.util.beans;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
@ -14,6 +13,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
@ -92,7 +92,7 @@ public final class SettingsPersistenceUtil {
*/
private static Optional<Document> getDocument(File file) {
if (file.exists()) {
try (InputStream stream = new FileInputStream(file)) {
try (InputStream stream = Files.newInputStream(file.toPath())) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(stream);
return Optional.of(document);

View File

@ -5,8 +5,9 @@
package net.sourceforge.pmd.util.fxdesigner.util.beans;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -109,7 +110,7 @@ public abstract class XmlInterface {
Source source = new DOMSource(document);
outputFile.getParentFile().mkdirs();
Result result = new StreamResult(new FileWriter(outputFile));
Result result = new StreamResult(Files.newBufferedWriter(outputFile.toPath(), StandardCharsets.UTF_8));
transformer.transform(source, result);
} catch (TransformerException e) {
throw new IOException("Failed to save settings", e);