Search in sources :

Example 1 with Dependencies

use of com.github.sevntu.checkstyle.domain.Dependencies in project methods-distance by sevntu-checkstyle.

the class MainServlet method processDsm.

private void processDsm(URL sourceUrl, HttpServletResponse resp) throws CheckstyleException, IOException {
    class DsmDependencyInformationConsumer implements DependencyInformationConsumer {

        private Configuration configuration;

        @Override
        public void setConfiguration(Configuration configuration) {
            this.configuration = configuration;
        }

        @Override
        public void accept(String filePath, Dependencies dependencies) {
            try {
                final String javaSource = FileUtils.getFileContents(filePath);
                final MethodOrder methodOrder = new MethodOrder(dependencies);
                final String html = DependencyInfoMatrixSerializer.serialize(methodOrder, javaSource, configuration);
                resp.setContentType("text/html");
                resp.getWriter().append(html);
            } catch (final IOException | CheckstyleException ex) {
                throw new ResponseGenerationException(ex);
            }
        }
    }
    final Map<String, String> config = getCheckConfiguration();
    final DsmDependencyInformationConsumer consumer = new DsmDependencyInformationConsumer();
    final MethodCallDependencyCheckInvoker invoker = new MethodCallDependencyCheckInvoker(config, consumer);
    consumer.setConfiguration(invoker.getConfiguration());
    invoker.invoke(Collections.singletonList(downloadSource(sourceUrl)));
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) MethodCallDependencyCheckInvoker(com.github.sevntu.checkstyle.common.MethodCallDependencyCheckInvoker) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) IOException(java.io.IOException) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer)

Example 2 with Dependencies

use of com.github.sevntu.checkstyle.domain.Dependencies in project methods-distance by sevntu-checkstyle.

the class MethodCallDependenciesModuleTestSupport method mustBeSame.

private static void mustBeSame(final ExpectedDependencies expected, final MethodOrder actual) {
    for (final String expectedMethod : expected.getMethods()) {
        assertTrue("Method " + expectedMethod + " is not present is actual info", actual.getMethods().stream().anyMatch(md -> expectedMethod.equals(md.getSignature())));
    }
    for (final Method actualMethod : actual.getMethods()) {
        assertTrue("Method " + actualMethod.getSignature() + " is not present in expected info", expected.getMethods().stream().anyMatch(mi -> mi.equals(actualMethod.getSignature())));
    }
    for (final String method : expected.getMethods()) {
        final Method caller = actual.getMethods().stream().filter(m -> m.getSignature().equals(method)).findFirst().get();
        final List<Method> dependencies = actual.getMethodDependenciesInAppearanceOrder(caller);
        final List<ExpectedDependencies.MethodInvocation> invocations = expected.getInvocationsFromMethod(method);
        assertEquals("Actual method dependencies count and count of invocations from method " + method + " does not match", invocations.size(), dependencies.size());
        for (int i = 0; i < invocations.size(); ++i) {
            final Method calledMethod = dependencies.get(i);
            final ExpectedDependencies.MethodInvocation invocationOfMethod = invocations.get(i);
            assertTrue("Method " + calledMethod.getSignature() + " is present as actual " + i + " dependency of " + method + " but should not be!", calledMethod.getSignature().equals(expected.getMethodByIndex(invocationOfMethod.callee)));
        }
    }
}
Also used : DependencyInformationConsumerInjector(com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) ExpectedDependencies(com.github.sevntu.checkstyle.domain.ExpectedDependencies) MethodCallDependencyCheckstyleModule(com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule) BaseCheckTestSupport(com.github.sevntu.checkstyle.domain.BaseCheckTestSupport) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) List(java.util.List) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer) Locale(java.util.Locale) Checker(com.puppycrawl.tools.checkstyle.Checker) Map(java.util.Map) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) Assert.assertEquals(org.junit.Assert.assertEquals) Method(com.github.sevntu.checkstyle.ordering.Method) ExpectedDependencies(com.github.sevntu.checkstyle.domain.ExpectedDependencies)

Example 3 with Dependencies

use of com.github.sevntu.checkstyle.domain.Dependencies in project methods-distance by sevntu-checkstyle.

the class DependencyInfoGraphSerializer method serializeInfo.

public static String serializeInfo(Dependencies dependencies) {
    final MethodOrder info = new MethodOrder(dependencies);
    final Graph graph = new Graph("dependencies");
    graph.setRankdir(Rankdirs.LR);
    final Cluster simpleMethods = new Cluster("simple");
    final Map<Method, Node> methodToNode = info.getMethods().stream().filter(method -> !info.isInterfaceMethod(method)).collect(Collectors.toMap(Function.identity(), DependencyInfoGraphSerializer::createNode));
    methodToNode.entrySet().stream().forEach(methodAndNode -> {
        if (info.hasMethodDependencies(methodAndNode.getKey())) {
            graph.addComponent(methodAndNode.getValue());
        } else {
            simpleMethods.addComponent(methodAndNode.getValue());
        }
    });
    graph.addComponent(simpleMethods);
    for (final Method caller : methodToNode.keySet()) {
        for (final Method callee : info.getMethodDependenciesInAppearanceOrder(caller)) {
            graph.addComponent(createEdge(caller, callee, methodToNode, info));
        }
    }
    final Comment comment = new Comment(getDescription());
    graph.addComponent(comment);
    return serialize(graph);
}
Also used : PrintWriter(java.io.PrintWriter) Cluster(com.github.sevntu.checkstyle.dot.domain.Cluster) Graph(com.github.sevntu.checkstyle.dot.domain.Graph) Element(com.github.sevntu.checkstyle.dot.domain.Element) FileUtils(com.github.sevntu.checkstyle.utils.FileUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Shapes(com.github.sevntu.checkstyle.dot.domain.Shapes) Method(com.github.sevntu.checkstyle.ordering.Method) List(java.util.List) Colors(com.github.sevntu.checkstyle.dot.domain.Colors) Rankdirs(com.github.sevntu.checkstyle.dot.domain.Rankdirs) Map(java.util.Map) AttributeHolder(com.github.sevntu.checkstyle.dot.domain.AttributeHolder) Comment(com.github.sevntu.checkstyle.dot.domain.Comment) Node(com.github.sevntu.checkstyle.dot.domain.Node) Edge(com.github.sevntu.checkstyle.dot.domain.Edge) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) Comment(com.github.sevntu.checkstyle.dot.domain.Comment) Graph(com.github.sevntu.checkstyle.dot.domain.Graph) Node(com.github.sevntu.checkstyle.dot.domain.Node) Cluster(com.github.sevntu.checkstyle.dot.domain.Cluster) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method)

Example 4 with Dependencies

use of com.github.sevntu.checkstyle.domain.Dependencies 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 5 with Dependencies

use of com.github.sevntu.checkstyle.domain.Dependencies in project methods-distance by sevntu-checkstyle.

the class MethodCallDependencyCheckstyleModule method buildDependencies.

private static Dependencies buildDependencies(DetailAST topLevelClass, List<DetailAST> methodInvocations) {
    final ClassDefinition classDefinition = new ClassDefinition(topLevelClass);
    final List<ResolvedCall> callOccurrences = new ArrayList<>();
    for (final DetailAST invocation : methodInvocations) {
        if (classDefinition.isInsideMethodOfClass(invocation)) {
            final Optional<ResolvedCall> occurrence = tryResolveCall(classDefinition, invocation);
            occurrence.ifPresent(callOccurrences::add);
        }
    }
    return new Dependencies(classDefinition, callOccurrences);
}
Also used : ResolvedCall(com.github.sevntu.checkstyle.domain.ResolvedCall) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) ClassDefinition(com.github.sevntu.checkstyle.domain.ClassDefinition)

Aggregations

Dependencies (com.github.sevntu.checkstyle.domain.Dependencies)6 MethodOrder (com.github.sevntu.checkstyle.ordering.MethodOrder)4 DependencyInformationConsumer (com.github.sevntu.checkstyle.module.DependencyInformationConsumer)3 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)3 File (java.io.File)3 Map (java.util.Map)3 MethodCallDependencyCheckInvoker (com.github.sevntu.checkstyle.common.MethodCallDependencyCheckInvoker)2 Method (com.github.sevntu.checkstyle.ordering.Method)2 FileUtils (com.github.sevntu.checkstyle.utils.FileUtils)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)1 BaseCheckTestSupport (com.github.sevntu.checkstyle.domain.BaseCheckTestSupport)1 ClassDefinition (com.github.sevntu.checkstyle.domain.ClassDefinition)1 ExpectedDependencies (com.github.sevntu.checkstyle.domain.ExpectedDependencies)1 ResolvedCall (com.github.sevntu.checkstyle.domain.ResolvedCall)1 DependencyInfoGraphSerializer (com.github.sevntu.checkstyle.dot.DependencyInfoGraphSerializer)1 AttributeHolder (com.github.sevntu.checkstyle.dot.domain.AttributeHolder)1