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