Search in sources :

Example 91 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project methods-distance by sevntu-checkstyle.

the class MainServlet method processDot.

private void processDot(URL sourceUrl, HttpServletResponse resp) throws CheckstyleException, IOException {
    final DependencyInformationConsumer consumer = (filePath, dependencies) -> {
        try {
            final String dot = DependencyInfoGraphSerializer.serializeInfo(dependencies);
            resp.setContentType("text/vnd.graphviz");
            resp.getWriter().append(dot);
        } catch (final IOException ex) {
            throw new ResponseGenerationException(ex);
        }
    };
    final Map<String, String> config = getCheckConfiguration();
    final MethodCallDependencyCheckInvoker invoker = new MethodCallDependencyCheckInvoker(config, consumer);
    invoker.invoke(Collections.singletonList(downloadSource(sourceUrl)));
}
Also used : OutputStream(java.io.OutputStream) HttpServlet(javax.servlet.http.HttpServlet) MalformedURLException(java.net.MalformedURLException) ServletException(javax.servlet.ServletException) URL(java.net.URL) HttpServletResponse(javax.servlet.http.HttpServletResponse) DependencyInfoGraphSerializer(com.github.sevntu.checkstyle.dot.DependencyInfoGraphSerializer) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) FileUtils(com.github.sevntu.checkstyle.utils.FileUtils) File(java.io.File) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) DependencyInfoMatrixSerializer(com.github.sevntu.checkstyle.dsm.DependencyInfoMatrixSerializer) HttpServletRequest(javax.servlet.http.HttpServletRequest) MethodCallDependencyCheckInvoker(com.github.sevntu.checkstyle.common.MethodCallDependencyCheckInvoker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer) URLConnection(java.net.URLConnection) Map(java.util.Map) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) Collections(java.util.Collections) InputStream(java.io.InputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) MethodCallDependencyCheckInvoker(com.github.sevntu.checkstyle.common.MethodCallDependencyCheckInvoker) IOException(java.io.IOException) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer)

Example 92 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project methods-distance by sevntu-checkstyle.

the class MainServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String requestPath = req.getPathInfo();
    final String sourceUrl = req.getParameter("source_url");
    try {
        final URL url = new URL(sourceUrl);
        switch(requestPath) {
            case "/dsm":
                processDsm(url, resp);
                break;
            case "/dot":
                processDot(url, resp);
                break;
            default:
                processNotFound(resp);
        }
    } catch (final MalformedURLException ex) {
        resp.getWriter().print("Malformed url provided: " + sourceUrl);
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    } catch (final CheckstyleException ex) {
        resp.getWriter().print("Checkstyle exception occurred: " + ex.getMessage());
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URL(java.net.URL)

Example 93 with CheckstyleException

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

the class AbstractGoogleModuleTestSupport method getModuleConfig.

/**
 * Returns {@link Configuration} instance for the given module name.
 * This implementation uses {@link #getModuleConfig(String)} method inside.
 *
 * @param moduleName module name.
 * @param moduleId module id.
 * @return {@link Configuration} instance for the given module name.
 * @throws IllegalStateException if there is a problem retrieving the module or config.
 */
protected static Configuration getModuleConfig(String moduleName, String moduleId) {
    final Configuration result;
    final List<Configuration> configs = getModuleConfigs(moduleName);
    if (configs.size() == 1) {
        result = configs.get(0);
    } else if (configs.isEmpty()) {
        throw new IllegalStateException("no instances of the Module was found: " + moduleName);
    } else if (moduleId == null) {
        throw new IllegalStateException("multiple instances of the same Module are detected");
    } else {
        result = configs.stream().filter(conf -> {
            try {
                return conf.getProperty("id").equals(moduleId);
            } catch (CheckstyleException ex) {
                throw new IllegalStateException("problem to get ID attribute from " + conf, ex);
            }
        }).findFirst().orElseThrow(() -> new IllegalStateException("problem with module config"));
    }
    return result;
}
Also used : List(java.util.List) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) AbstractItModuleTestSupport(org.checkstyle.base.AbstractItModuleTestSupport) Set(java.util.Set) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) IOException(java.io.IOException) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader) CheckUtil(com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil) ModuleReflectionUtil(com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil) ArrayList(java.util.ArrayList) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 94 with CheckstyleException

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

the class JavadocPropertiesGenerator method writePropertiesFile.

/**
 * Creates the .properties file from a .java file.
 *
 * @param options the user-specified options
 * @throws CheckstyleException if a javadoc comment can not be parsed
 */
private static void writePropertiesFile(CliOptions options) throws CheckstyleException {
    try (PrintWriter writer = new PrintWriter(options.outputFile, StandardCharsets.UTF_8)) {
        final DetailAST top = JavaParser.parseFile(options.inputFile, JavaParser.Options.WITH_COMMENTS).getFirstChild();
        final DetailAST objBlock = getClassBody(top);
        if (objBlock != null) {
            iteratePublicStaticIntFields(objBlock, writer::println);
        }
    } catch (IOException ex) {
        throw new CheckstyleException("Failed to write javadoc properties of '" + options.inputFile + "' to '" + options.outputFile + "'", ex);
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 95 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 ignoredModulesOptions {@code OMIT} if modules with severity
 *            'ignore' should be omitted, {@code EXECUTE} otherwise
 * @param threadModeSettings the thread mode configuration
 * @return the check configurations
 * @throws CheckstyleException if an error occurs
 * @noinspection WeakerAccess
 */
public static Configuration loadConfiguration(InputSource configSource, PropertyResolver overridePropsResolver, IgnoredModulesOptions ignoredModulesOptions, ThreadModeSettings threadModeSettings) throws CheckstyleException {
    try {
        final boolean omitIgnoreModules = ignoredModulesOptions == IgnoredModulesOptions.OMIT;
        final ConfigurationLoader loader = new ConfigurationLoader(overridePropsResolver, omitIgnoreModules, threadModeSettings);
        loader.parseInputSource(configSource);
        return loader.configuration;
    } catch (final SAXParseException ex) {
        final String message = String.format(Locale.ROOT, SAX_PARSE_EXCEPTION_FORMAT, 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)146 Test (org.junit.jupiter.api.Test)58 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)56 File (java.io.File)32 IOException (java.io.IOException)27 Test (org.junit.Test)27 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)18 Properties (java.util.Properties)15 ArrayList (java.util.ArrayList)14 URL (java.net.URL)11 Violation (com.puppycrawl.tools.checkstyle.api.Violation)10 FileText (com.puppycrawl.tools.checkstyle.api.FileText)8 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)7 InputStream (java.io.InputStream)7 SAXException (org.xml.sax.SAXException)7 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)6 URI (java.net.URI)6 Set (java.util.Set)6 Checker (com.puppycrawl.tools.checkstyle.Checker)5 MalformedURLException (java.net.MalformedURLException)5