Search in sources :

Example 71 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class Main method loadProperties.

/**
     * Loads properties from a File.
     * @param file
     *        the properties file
     * @return the properties in file
     * @throws CheckstyleException
     *         when could not load properties file
     */
private static Properties loadProperties(File file) throws CheckstyleException {
    final Properties properties = new Properties();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
        properties.load(fis);
    } catch (final IOException ex) {
        throw new CheckstyleException(String.format("Unable to load properties from file '%s'.", file.getAbsolutePath()), ex);
    } finally {
        Closeables.closeQuietly(fis);
    }
    return properties;
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 72 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class AstTreeStringPrinter method parseFileText.

/**
     * Parse a text and return the parse tree.
     * @param text the text to parse.
     * @param withComments true to include comment nodes to the tree
     * @return the root node of the parse tree.
     * @throws CheckstyleException if the file is not a Java source.
     */
private static DetailAST parseFileText(FileText text, boolean withComments) throws CheckstyleException {
    final FileContents contents = new FileContents(text);
    final DetailAST result;
    try {
        if (withComments) {
            result = TreeWalker.parseWithComments(contents);
        } else {
            result = TreeWalker.parse(contents);
        }
    } catch (RecognitionException | TokenStreamException ex) {
        final String exceptionMsg = String.format(Locale.ROOT, "%s occurred during the analysis of file %s.", ex.getClass().getSimpleName(), text.getFile().getPath());
        throw new CheckstyleException(exceptionMsg, ex);
    }
    return result;
}
Also used : TokenStreamException(antlr.TokenStreamException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) RecognitionException(antlr.RecognitionException)

Example 73 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class Checker method finishLocalSetup.

@Override
public void finishLocalSetup() throws CheckstyleException {
    final Locale locale = new Locale(localeLanguage, localeCountry);
    LocalizedMessage.setLocale(locale);
    if (moduleFactory == null) {
        if (moduleClassLoader == null) {
            throw new CheckstyleException("if no custom moduleFactory is set, " + "moduleClassLoader must be specified");
        }
        final Set<String> packageNames = PackageNamesLoader.getPackageNames(moduleClassLoader);
        moduleFactory = new PackageObjectFactory(packageNames, moduleClassLoader);
    }
    final DefaultContext context = new DefaultContext();
    context.add("charset", charset);
    context.add("classLoader", classLoader);
    context.add("moduleFactory", moduleFactory);
    context.add("severity", severityLevel.getName());
    context.add("basedir", basedir);
    childContext = context;
}
Also used : Locale(java.util.Locale) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 74 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class Checker method processFiles.

/**
     * Processes a list of files with all FileSetChecks.
     * @param files a list of files to process.
     * @throws CheckstyleException if error condition within Checkstyle occurs.
     * @noinspection ProhibitedExceptionThrown
     */
private void processFiles(List<File> files) throws CheckstyleException {
    for (final File file : files) {
        try {
            final String fileName = file.getAbsolutePath();
            final long timestamp = file.lastModified();
            if (cache != null && cache.isInCache(fileName, timestamp) || !CommonUtils.matchesFileExtension(file, fileExtensions) || !acceptFileStarted(fileName)) {
                continue;
            }
            if (cache != null) {
                cache.put(fileName, timestamp);
            }
            fireFileStarted(fileName);
            final SortedSet<LocalizedMessage> fileMessages = processFile(file);
            fireErrors(fileName, fileMessages);
            fireFileFinished(fileName);
        }// processing. See https://github.com/checkstyle/checkstyle/issues/2285
         catch (Exception ex) {
            // We need to catch all exceptions to put a reason failure (file name) in exception
            throw new CheckstyleException("Exception was thrown while processing " + file.getPath(), ex);
        } catch (Error error) {
            // We need to catch all errors to put a reason failure (file name) in error
            throw new Error("Error was thrown while processing " + file.getPath(), error);
        }
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage)

Example 75 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class ConfigurationLoader method loadConfiguration.

/**
     * Returns the module configurations from a specified input source.
     * Note that if the source does wrap an open byte or character
     * stream, clients are required to close that stream by themselves
     *
     * @param configSource the input stream to the Checkstyle configuration
     * @param overridePropsResolver overriding properties
     * @param omitIgnoredModules {@code true} if modules with severity
     *            'ignore' should be omitted, {@code false} otherwise
     * @return the check configurations
     * @throws CheckstyleException if an error occurs
     */
public static Configuration loadConfiguration(InputSource configSource, PropertyResolver overridePropsResolver, boolean omitIgnoredModules) throws CheckstyleException {
    try {
        final ConfigurationLoader loader = new ConfigurationLoader(overridePropsResolver, omitIgnoredModules);
        loader.parseInputSource(configSource);
        return loader.configuration;
    } catch (final SAXParseException ex) {
        final String message = String.format(Locale.ROOT, "%s - %s:%s:%s", UNABLE_TO_PARSE_EXCEPTION_PREFIX, ex.getMessage(), ex.getLineNumber(), ex.getColumnNumber());
        throw new CheckstyleException(message, ex);
    } catch (final ParserConfigurationException | IOException | SAXException ex) {
        throw new CheckstyleException(UNABLE_TO_PARSE_EXCEPTION_PREFIX, ex);
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)78 Test (org.junit.Test)46 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)33 File (java.io.File)15 IOException (java.io.IOException)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)9 Properties (java.util.Properties)8 URL (java.net.URL)6 SAXException (org.xml.sax.SAXException)5 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)4 URI (java.net.URI)4 Checker (com.puppycrawl.tools.checkstyle.Checker)3 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)3 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3