Search in sources :

Example 1 with ClassDefinition

use of com.github.sevntu.checkstyle.domain.ClassDefinition 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)

Example 2 with ClassDefinition

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

the class MethodCallDependencyCheckstyleModule method tryResolveRefCall.

private static Optional<ResolvedCall> tryResolveRefCall(ClassDefinition classDefinition, DetailAST refCallNode) {
    final RefCall call = new RefCall(classDefinition, refCallNode);
    return Optional.of(call).filter(RefCall::isRefToMethodOfEnclosingClass).map(refCall -> {
        return refCall.isRefToStaticMethodOfEnclosingClass() ? classDefinition.getStaticMethodsByName(refCall.getMethodName()) : classDefinition.getInstanceMethodsByName(refCall.getMethodName());
    }).filter(list -> !list.isEmpty()).map(possibleMethods -> {
        final MethodDefinition callee = possibleMethods.get(0);
        final MethodDefinition caller = classDefinition.getMethodByAstNode(call.getEnclosingMethod());
        return new ResolvedCall(refCallNode, caller, callee);
    });
}
Also used : ResolvedCall(com.github.sevntu.checkstyle.domain.ResolvedCall) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) MethodCall(com.github.sevntu.checkstyle.domain.MethodCall) ClassDefinition(com.github.sevntu.checkstyle.domain.ClassDefinition) File(java.io.File) ArrayList(java.util.ArrayList) UnexpectedTokenTypeException(com.github.sevntu.checkstyle.common.UnexpectedTokenTypeException) RefCall(com.github.sevntu.checkstyle.domain.RefCall) TokenTypes(com.puppycrawl.tools.checkstyle.api.TokenTypes) List(java.util.List) TokenUtils(com.puppycrawl.tools.checkstyle.utils.TokenUtils) Optional(java.util.Optional) MethodDefinition(com.github.sevntu.checkstyle.domain.MethodDefinition) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) ResolvedCall(com.github.sevntu.checkstyle.domain.ResolvedCall) RefCall(com.github.sevntu.checkstyle.domain.RefCall) MethodDefinition(com.github.sevntu.checkstyle.domain.MethodDefinition)

Aggregations

ClassDefinition (com.github.sevntu.checkstyle.domain.ClassDefinition)2 Dependencies (com.github.sevntu.checkstyle.domain.Dependencies)2 ResolvedCall (com.github.sevntu.checkstyle.domain.ResolvedCall)2 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)2 ArrayList (java.util.ArrayList)2 UnexpectedTokenTypeException (com.github.sevntu.checkstyle.common.UnexpectedTokenTypeException)1 MethodCall (com.github.sevntu.checkstyle.domain.MethodCall)1 MethodDefinition (com.github.sevntu.checkstyle.domain.MethodDefinition)1 RefCall (com.github.sevntu.checkstyle.domain.RefCall)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)1 TokenUtils (com.puppycrawl.tools.checkstyle.utils.TokenUtils)1 File (java.io.File)1 List (java.util.List)1 Optional (java.util.Optional)1