From 03196e88b27439b4bfff54c88ee4e9d7a50f9d58 Mon Sep 17 00:00:00 2001 From: Brian Remedios Date: Sat, 24 Sep 2011 09:20:22 +0000 Subject: [PATCH] Improved resource handling, better living through utility methods ;) git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7335 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/cpd/AnyTokenizer.java | 7 ++- .../net/sourceforge/pmd/cpd/CPPTokenizer.java | 43 ++++++++------- .../sourceforge/pmd/cpd/MatchAlgorithm.java | 1 - .../net/sourceforge/pmd/cpd/SourceCode.java | 13 ++--- pmd/src/net/sourceforge/pmd/util/IOUtil.java | 52 +++++++++++++++++++ 5 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 pmd/src/net/sourceforge/pmd/util/IOUtil.java diff --git a/pmd/src/net/sourceforge/pmd/cpd/AnyTokenizer.java b/pmd/src/net/sourceforge/pmd/cpd/AnyTokenizer.java index cd2bd17ade..931a639558 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/AnyTokenizer.java +++ b/pmd/src/net/sourceforge/pmd/cpd/AnyTokenizer.java @@ -8,6 +8,8 @@ import java.io.CharArrayReader; import java.util.NoSuchElementException; import java.util.StringTokenizer; +import net.sourceforge.pmd.util.IOUtil; + /** * This class does a best-guess try-anything tokenization. * @@ -42,10 +44,7 @@ public class AnyTokenizer implements Tokenizer { } catch (Exception ex) { ex.printStackTrace(); } finally { - try { - reader.close(); - } catch (Exception ex) { - } + IOUtil.closeQuietly(reader); tokenEntries.add(TokenEntry.getEOF()); } } diff --git a/pmd/src/net/sourceforge/pmd/cpd/CPPTokenizer.java b/pmd/src/net/sourceforge/pmd/cpd/CPPTokenizer.java index 921269cbe2..d36fed5105 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/CPPTokenizer.java +++ b/pmd/src/net/sourceforge/pmd/cpd/CPPTokenizer.java @@ -10,27 +10,32 @@ import net.sourceforge.pmd.lang.LanguageVersionHandler; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.TokenMgrError; import net.sourceforge.pmd.lang.cpp.ast.Token; +import net.sourceforge.pmd.util.IOUtil; public class CPPTokenizer implements Tokenizer { - public void tokenize(SourceCode sourceCode, Tokens tokenEntries) { - StringBuffer buffer = sourceCode.getCodeBuffer(); - try { - LanguageVersionHandler languageVersionHandler = LanguageVersion.CPP.getLanguageVersionHandler(); - TokenManager tokenManager = languageVersionHandler.getParser( - languageVersionHandler.getDefaultParserOptions()).getTokenManager(sourceCode.getFileName(), - new StringReader(buffer.toString())); - Token currentToken = (Token) tokenManager.getNextToken(); - while (currentToken.image.length() > 0) { - tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine)); - currentToken = (Token) tokenManager.getNextToken(); - } - tokenEntries.add(TokenEntry.getEOF()); - System.err.println("Added " + sourceCode.getFileName()); - } catch (TokenMgrError err) { - err.printStackTrace(); - System.err.println("Skipping " + sourceCode.getFileName() + " due to parse error"); - tokenEntries.add(TokenEntry.getEOF()); + public void tokenize(SourceCode sourceCode, Tokens tokenEntries) { + StringBuffer buffer = sourceCode.getCodeBuffer(); + StringReader reader = null; + try { + LanguageVersionHandler languageVersionHandler = LanguageVersion.CPP.getLanguageVersionHandler(); + reader = new StringReader(buffer.toString()); + TokenManager tokenManager = languageVersionHandler.getParser( + languageVersionHandler.getDefaultParserOptions()) + .getTokenManager(sourceCode.getFileName(), reader); + Token currentToken = (Token) tokenManager.getNextToken(); + while (currentToken.image.length() > 0) { + tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine)); + currentToken = (Token) tokenManager.getNextToken(); + } + tokenEntries.add(TokenEntry.getEOF()); + System.err.println("Added " + sourceCode.getFileName()); + } catch (TokenMgrError err) { + err.printStackTrace(); + System.err.println("Skipping " + sourceCode.getFileName() + " due to parse error"); + tokenEntries.add(TokenEntry.getEOF()); + } finally { + IOUtil.closeQuietly(reader); + } } - } } diff --git a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java index f5c8e1a932..86443125c6 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java +++ b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java @@ -64,7 +64,6 @@ public class MatchAlgorithm { Object o = i.next(); if (o instanceof List) { List l = (List) o; - Collections.reverse(l); matchCollector.collect(l); } diff --git a/pmd/src/net/sourceforge/pmd/cpd/SourceCode.java b/pmd/src/net/sourceforge/pmd/cpd/SourceCode.java index 37bf9575ad..653650e36c 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/SourceCode.java +++ b/pmd/src/net/sourceforge/pmd/cpd/SourceCode.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.util.IOUtil; public class SourceCode { @@ -50,13 +51,7 @@ public class SourceCode { e.printStackTrace(); throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); } finally { - try { - if (lnr != null) { - lnr.close(); - } - } catch (Exception e) { - throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); - } + IOUtil.closeQuietly(lnr); } } } @@ -77,7 +72,7 @@ public class SourceCode { @Override public String getFileName() { - return this.file.getAbsolutePath(); + return file.getAbsolutePath(); } } @@ -129,7 +124,7 @@ public class SourceCode { } public String getSlice(int startLine, int endLine) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); List lines = cl.getCode(); for (int i = startLine == 0 ? startLine :startLine - 1; i < endLine && i < lines.size(); i++) { if (sb.length() != 0) { diff --git a/pmd/src/net/sourceforge/pmd/util/IOUtil.java b/pmd/src/net/sourceforge/pmd/util/IOUtil.java new file mode 100644 index 0000000000..713213a3fe --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/util/IOUtil.java @@ -0,0 +1,52 @@ +package net.sourceforge.pmd.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; + +/** + * + * @author Brian Remedios + */ +public class IOUtil { + + private IOUtil() {} + + public static void closeQuietly(OutputStream stream) { + if (stream == null) return; + try { + stream.close(); + } catch (IOException ex) { + // ignore + } + } + + public static void closeQuietly(InputStream stream) { + if (stream == null) return; + try { + stream.close(); + } catch (IOException ex) { + // ignore + } + } + + public static void closeQuietly(Writer writer) { + if (writer == null) return; + try { + writer.close(); + } catch (IOException ex) { + // ignore it + } + } + + public static void closeQuietly(Reader reader) { + if (reader == null) return; + try { + reader.close(); + } catch (IOException ex) { + //ignore + } + } +}