Search in sources :

Example 1 with Parameter

use of nl.ramsolutions.sw.magik.analysis.typing.types.Parameter in project magik-tools by StevenLooman.

the class MethodArgumentCountTypedCheck method walkPostMethodInvocation.

@Override
protected void walkPostMethodInvocation(final AstNode node) {
    // Ensure there are arguments to check.
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    if (argumentsNode == null) {
        return;
    }
    // Don't bother checking scatter.
    final boolean anyScatter = argumentsNode.getChildren(MagikGrammar.ARGUMENT).stream().anyMatch(argumentNode -> {
        AstNode unaryExprNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.EXPRESSION, MagikGrammar.UNARY_EXPRESSION);
        String tokenValue = unaryExprNode != null ? unaryExprNode.getTokenValue() : null;
        return tokenValue != null && tokenValue.equalsIgnoreCase(MagikKeyword.SCATTER.getValue());
    });
    if (anyScatter) {
        return;
    }
    // Get type.
    final AbstractType calledType = this.getTypeInvokedOn(node);
    if (calledType == UndefinedType.INSTANCE) {
        // Cannot give any useful information, so abort.
        return;
    }
    // Get methods.
    final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(node);
    final String methodName = helper.getMethodName();
    for (final Method method : calledType.getMethods(methodName)) {
        final List<Parameter> parameters = method.getParameters();
        if (parameters.isEmpty()) {
            continue;
        }
        // Match arguments against method.parameters.
        final List<AstNode> argumentNodes = argumentsNode.getChildren(MagikGrammar.ARGUMENT);
        final List<Parameter> checkedParameters = method.getParameters().stream().filter(parameter -> parameter.is(Parameter.Modifier.NONE)).collect(Collectors.toList());
        if (checkedParameters.size() > argumentNodes.size()) {
            final String message = String.format(MESSAGE, methodName);
            this.addIssue(node, message);
        }
    }
}
Also used : Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) AstNode(com.sonar.sslr.api.AstNode) List(java.util.List) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) MagikKeyword(nl.ramsolutions.sw.magik.api.MagikKeyword) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) Collectors(java.util.stream.Collectors) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) AstNode(com.sonar.sslr.api.AstNode) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)

Example 2 with Parameter

use of nl.ramsolutions.sw.magik.analysis.typing.types.Parameter in project magik-tools by StevenLooman.

the class MethodArgumentParameterTypedCheck method walkPostMethodInvocation.

@Override
protected void walkPostMethodInvocation(final AstNode node) {
    // Ensure there are arguments to check.
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    if (argumentsNode == null) {
        return;
    }
    // Get type.
    final AbstractType calledType = this.getTypeInvokedOn(node);
    if (calledType == UndefinedType.INSTANCE) {
        // Cannot give any useful information, so abort.
        return;
    }
    // Get types for arguments.
    final LocalTypeReasoner reasoner = this.getReasoner();
    final List<AstNode> argumentNodes = argumentsNode.getChildren(MagikGrammar.ARGUMENT).stream().collect(Collectors.toList());
    final List<ExpressionResult> argumentTypes = argumentNodes.stream().map(argumentNode -> argumentNode.getFirstChild(MagikGrammar.EXPRESSION)).map(reasoner::getNodeType).collect(Collectors.toList());
    // Get methods.
    final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(node);
    final String methodName = helper.getMethodName();
    for (final Method method : calledType.getMethods(methodName)) {
        final List<Parameter> parameters = method.getParameters();
        if (parameters.isEmpty()) {
            continue;
        }
        final List<AbstractType> parameterTypes = method.getParameters().stream().filter(parameter -> parameter.is(Parameter.Modifier.NONE) || // Don't check gather.
        parameter.is(Parameter.Modifier.OPTIONAL)).map(Parameter::getType).collect(Collectors.toList());
        // Test parameter type against argument type.
        final int size = Math.min(parameterTypes.size(), argumentTypes.size());
        IntStream.range(0, size).forEach(index -> {
            final AbstractType parameterType = parameterTypes.get(index);
            if (parameterType == UndefinedType.INSTANCE) {
                return;
            }
            final AbstractType argumentType = argumentTypes.get(index).get(0, UndefinedType.INSTANCE);
            if (!TypeMatcher.typeMatches(argumentType, parameterType)) {
                final AstNode argumentNode = argumentNodes.get(index);
                final String message = String.format(MESSAGE, argumentType.getFullName(), parameterType.getFullName());
                this.addIssue(argumentNode, message);
            }
        });
    }
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) LocalTypeReasoner(nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) AstNode(com.sonar.sslr.api.AstNode)

Example 3 with Parameter

use of nl.ramsolutions.sw.magik.analysis.typing.types.Parameter in project magik-tools by StevenLooman.

the class MethodArgumentParameterTypeCheckTest method testArgumentTypeMatches.

@Test
void testArgumentTypeMatches() {
    final String code = "integer.m1(:symbol)";
    final ITypeKeeper typeKeeper = new TypeKeeper();
    final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
    final AbstractType symbolType = typeKeeper.getType(GlobalReference.of("sw:symbol"));
    final Parameter param1 = new Parameter("p1", Parameter.Modifier.NONE, symbolType);
    integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "m1()", List.of(param1), null, ExpressionResult.UNDEFINED);
    final MagikTypedCheck check = new MethodArgumentParameterTypedCheck();
    final List<MagikIssue> checkResults = this.runCheck(code, typeKeeper, check);
    assertThat(checkResults).isEmpty();
}
Also used : MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) TypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) MagikIssue(nl.ramsolutions.sw.magik.checks.MagikIssue) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 4 with Parameter

use of nl.ramsolutions.sw.magik.analysis.typing.types.Parameter in project magik-tools by StevenLooman.

the class MethodArgumentParameterTypeCheckTest method testArgumentTypeNotMatches.

@Test
void testArgumentTypeNotMatches() {
    final String code = "integer.m1(1)";
    final ITypeKeeper typeKeeper = new TypeKeeper();
    final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
    final AbstractType symbolType = typeKeeper.getType(GlobalReference.of("sw:symbol"));
    final Parameter param1 = new Parameter("p1", Parameter.Modifier.NONE, symbolType);
    integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "m1()", List.of(param1), null, ExpressionResult.UNDEFINED);
    final MagikTypedCheck check = new MethodArgumentParameterTypedCheck();
    final List<MagikIssue> checkResults = this.runCheck(code, typeKeeper, check);
    assertThat(checkResults).hasSize(1);
}
Also used : MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) TypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) MagikIssue(nl.ramsolutions.sw.magik.checks.MagikIssue) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 5 with Parameter

use of nl.ramsolutions.sw.magik.analysis.typing.types.Parameter in project magik-tools by StevenLooman.

the class MethodArgumentParameterTypeCheckTest method testTooManyArguments.

@Test
void testTooManyArguments() {
    final String code = "integer.m1(:symbol, :symbol)";
    final ITypeKeeper typeKeeper = new TypeKeeper();
    final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
    final AbstractType symbolType = typeKeeper.getType(GlobalReference.of("sw:symbol"));
    final Parameter param1 = new Parameter("p1", Parameter.Modifier.NONE, symbolType);
    integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "m1()", List.of(param1), null, ExpressionResult.UNDEFINED);
    final MagikTypedCheck check = new MethodArgumentParameterTypedCheck();
    final List<MagikIssue> checkResults = this.runCheck(code, typeKeeper, check);
    assertThat(checkResults).isEmpty();
}
Also used : MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) TypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) MagikIssue(nl.ramsolutions.sw.magik.checks.MagikIssue) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Aggregations

Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)11 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)10 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)7 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)7 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)6 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)6 AstNode (com.sonar.sslr.api.AstNode)5 Location (nl.ramsolutions.sw.magik.analysis.Location)5 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)4 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)4 URI (java.net.URI)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 MagikTypedCheck (nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck)3 Token (com.sonar.sslr.api.Token)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 Collections (java.util.Collections)2 EnumSet (java.util.EnumSet)2