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
This commit is contained in:
Brian Remedios
2011-09-24 09:20:22 +00:00
parent e13e441d96
commit 03196e88b2
5 changed files with 83 additions and 33 deletions

View File

@ -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());
}
}

View File

@ -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);
}
}
}
}

View File

@ -64,7 +64,6 @@ public class MatchAlgorithm {
Object o = i.next();
if (o instanceof List) {
List<TokenEntry> l = (List<TokenEntry>) o;
Collections.reverse(l);
matchCollector.collect(l);
}

View File

@ -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<String> lines = cl.getCode();
for (int i = startLine == 0 ? startLine :startLine - 1; i < endLine && i < lines.size(); i++) {
if (sb.length() != 0) {

View File

@ -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
}
}
}