*/
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;
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);
@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) {
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;
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.
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<InputFile> inputFiles = Lists.newArrayList();
Collection<File> files = FileUtils.listFiles(javaSourceDirectory, FileFilterUtils.fileFileFilter(), FileFilterUtils.directoryFileFilter());
}
}
+ 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<Class<? extends JavaAstVisitor>> getVisitorClasses() {