From 98dbd6b94e06ba82383ac44a6d3a48c50045466d Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 8 Feb 2012 18:49:44 +0400 Subject: [PATCH] Fix JavaAstScanner It should close original stream instead of newly created ByteArrayInputStream --- .../sonar/java/ast/CheckstyleSquidBridge.java | 16 +++-- .../org/sonar/java/ast/JavaAstScanner.java | 71 +++++++++++-------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/CheckstyleSquidBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/CheckstyleSquidBridge.java index 9f2631c2b8d..2bdeb423c70 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/CheckstyleSquidBridge.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/CheckstyleSquidBridge.java @@ -19,10 +19,9 @@ */ package org.sonar.java.ast; -import java.io.File; -import java.util.*; - import com.google.common.collect.Maps; +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.resources.InputFile; @@ -32,9 +31,12 @@ import org.sonar.java.squid.JavaSquidConfiguration; import org.sonar.squid.recognizer.CodeRecognizer; import org.sonar.squid.text.Source; -import com.puppycrawl.tools.checkstyle.api.Check; -import com.puppycrawl.tools.checkstyle.api.DetailAST; +import java.io.File; +import java.util.*; +/** + * Delegate from Checkstyle {@link Check} to {@link JavaAstVisitor}s. + */ public class CheckstyleSquidBridge extends Check { private static Logger logger = LoggerFactory.getLogger(CheckstyleSquidBridge.class); @@ -79,11 +81,13 @@ public class CheckstyleSquidBridge extends Check { @Override public void beginTree(DetailAST ast) { try { + String filename = getFileContents().getFilename(); Source source = createSource(); + InputFile inputFile = getInputFile(new java.io.File(filename)); for (JavaAstVisitor visitor : visitors) { visitor.setFileContents(getFileContents()); visitor.setSource(source); - visitor.setInputFile(getInputFile(new java.io.File(getFileContents().getFilename()))); + visitor.setInputFile(inputFile); visitor.visitFile(ast); } } catch (RuntimeException e) { diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/JavaAstScanner.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/JavaAstScanner.java index fddd5c494ac..32d3b9c38b4 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/JavaAstScanner.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/JavaAstScanner.java @@ -20,6 +20,11 @@ package org.sonar.java.ast; import com.google.common.collect.Lists; +import com.google.common.io.Closeables; +import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.ConfigurationLoader; +import com.puppycrawl.tools.checkstyle.PropertiesExpander; +import com.puppycrawl.tools.checkstyle.api.Configuration; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.FileFilterUtils; @@ -35,16 +40,15 @@ import org.sonar.squid.api.CodeVisitor; import org.sonar.squid.api.SourceCode; import org.xml.sax.InputSource; -import com.puppycrawl.tools.checkstyle.Checker; -import com.puppycrawl.tools.checkstyle.ConfigurationLoader; -import com.puppycrawl.tools.checkstyle.PropertiesExpander; -import com.puppycrawl.tools.checkstyle.api.Configuration; - import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Stack; /** * Squid uses Checkstyle to get an out-of-the-box java parser with AST generation and visitor pattern support. @@ -60,31 +64,6 @@ public class JavaAstScanner extends CodeScanner { this.project = project; } - private Checker createChecker(Charset charset) { - InputStream checkstyleConfig = null; - try { - checkstyleConfig = JavaAstScanner.class.getClassLoader().getResourceAsStream("checkstyle-configuration.xml"); - String readenConfig = IOUtils.toString(checkstyleConfig); - readenConfig = readenConfig.replace("${charset}", charset.toString()); - checkstyleConfig = new ByteArrayInputStream(readenConfig.getBytes()); - Configuration config = ConfigurationLoader.loadConfiguration(new InputSource(checkstyleConfig), new PropertiesExpander(System - .getProperties()), false); - Checker c = new Checker(); - final ClassLoader moduleClassLoader = Checker.class.getClassLoader(); - c.setModuleClassLoader(moduleClassLoader); - c.configure(config); - c.addListener(new CheckstyleAuditListener()); - return c; - - } catch (Exception e) { // NOSONAR We want to be sure to catch any unexpected exception - throw new AnalysisException( - "Unable to create Checkstyle Checker object with 'checkstyle-configuration.xml' as Checkstyle configuration file name", e); - - } finally { - IOUtils.closeQuietly(checkstyleConfig); - } - } - public JavaAstScanner scanDirectory(File javaSourceDirectory) { List inputFiles = Lists.newArrayList(); Collection files = FileUtils.listFiles(javaSourceDirectory, FileFilterUtils.fileFileFilter(), FileFilterUtils.directoryFileFilter()); @@ -133,6 +112,36 @@ public class JavaAstScanner extends CodeScanner { } } + private Checker createChecker(Charset charset) { + String checkstyleConfig = loadCheckstyleConfigToString() + .replace("${charset}", charset.toString()); + try { + Configuration config = ConfigurationLoader.loadConfiguration( + new InputSource(new ByteArrayInputStream(checkstyleConfig.getBytes())), + new PropertiesExpander(System.getProperties()), + false); + Checker checker = new Checker(); + final ClassLoader moduleClassLoader = Checker.class.getClassLoader(); + checker.setModuleClassLoader(moduleClassLoader); + checker.configure(config); + checker.addListener(new CheckstyleAuditListener()); + return checker; + } catch (Exception e) { // NOSONAR We want to be sure to catch any unexpected exception + throw new AnalysisException("Unable to create Checkstyle Checker", e); + } + } + + private static String loadCheckstyleConfigToString() { + InputStream is = null; + try { + is = JavaAstScanner.class.getClassLoader().getResourceAsStream("checkstyle-configuration.xml"); + return IOUtils.toString(is); + } catch (IOException e) { + throw new AnalysisException("Unable to load Checkstyle configuration for Java Squid", e); + } finally { + Closeables.closeQuietly(is); + } + } @Override public Collection> getVisitorClasses() { -- 2.39.5