Search in sources :

Example 81 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy by apache.

the class GenericsUtils method parameterizeSAM.

/**
 * Gets the parameter and return types of the abstract method of SAM.
 *
 * If the abstract method is not parameterized, we will get generics placeholders, e.g. T, U
 * For example, the abstract method of {@link java.util.function.Function} is
 * <pre>
 *      R apply(T t);
 * </pre>
 *
 * We parameterize the above interface as {@code Function<String, Integer>}, then the abstract method will be
 * <pre>
 *      Integer apply(String t);
 * </pre>
 *
 * When we call {@code parameterizeSAM} on the ClassNode {@code Function<String, Integer>},
 * we can get parameter types and return type of the above abstract method,
 * i.e. ClassNode {@code ClassHelper.STRING_TYPE} and {@code ClassHelper.Integer_TYPE}
 *
 * @param samType the class node which contains only one abstract method
 *
 * @since 3.0.0
 */
public static Tuple2<ClassNode[], ClassNode> parameterizeSAM(final ClassNode samType) {
    MethodNode abstractMethod = ClassHelper.findSAM(samType);
    Map<GenericsType, GenericsType> generics = makeDeclaringAndActualGenericsTypeMapOfExactType(abstractMethod.getDeclaringClass(), samType);
    Function<ClassNode, ClassNode> resolver = t -> {
        if (t.isGenericsPlaceHolder()) {
            return findActualTypeByGenericsPlaceholderName(t.getUnresolvedName(), generics);
        }
        return t;
    };
    ClassNode[] parameterTypes = Arrays.stream(abstractMethod.getParameters()).map(Parameter::getType).map(resolver).toArray(ClassNode[]::new);
    ClassNode returnType = resolver.apply(abstractMethod.getReturnType());
    return tuple(parameterTypes, returnType);
}
Also used : Arrays(java.util.Arrays) EvictableCache(org.codehaus.groovy.runtime.memoize.EvictableCache) HashMap(java.util.HashMap) Function(java.util.function.Function) ModuleNode(org.codehaus.groovy.ast.ModuleNode) ResolveVisitor(org.codehaus.groovy.control.ResolveVisitor) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Tuple2(groovy.lang.Tuple2) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Map(java.util.Map) SoftReference(java.lang.ref.SoftReference) LinkedList(java.util.LinkedList) SystemUtil.getSystemPropertySafe(org.apache.groovy.util.SystemUtil.getSystemPropertySafe) Parameter(org.codehaus.groovy.ast.Parameter) SourceUnit(org.codehaus.groovy.control.SourceUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) Predicate(java.util.function.Predicate) ClassHelper(org.codehaus.groovy.ast.ClassHelper) GenericsType(org.codehaus.groovy.ast.GenericsType) Set(java.util.Set) DecompiledClassNode(org.codehaus.groovy.ast.decompiled.DecompiledClassNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) IncorrectTypeHintException(groovy.transform.stc.IncorrectTypeHintException) GroovyBugError(org.codehaus.groovy.GroovyBugError) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ConcurrentSoftCache(org.codehaus.groovy.runtime.memoize.ConcurrentSoftCache) ASTNode(org.codehaus.groovy.ast.ASTNode) List(java.util.List) StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf) ParserPlugin(org.codehaus.groovy.control.ParserPlugin) DefaultGroovyMethods.plus(org.codehaus.groovy.runtime.DefaultGroovyMethods.plus) Tuple.tuple(groovy.lang.Tuple.tuple) Collections(java.util.Collections) ClassNode(org.codehaus.groovy.ast.ClassNode) DecompiledClassNode(org.codehaus.groovy.ast.decompiled.DecompiledClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 82 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy by apache.

the class GeneralUtils method makeDescriptorWithoutReturnType.

/**
 * @deprecated use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
 */
@Deprecated
public static String makeDescriptorWithoutReturnType(final MethodNode mn) {
    StringBuilder sb = new StringBuilder();
    sb.append(mn.getName()).append(':');
    for (Parameter p : mn.getParameters()) {
        sb.append(p.getType()).append(',');
    }
    return sb.toString();
}
Also used : Parameter(org.codehaus.groovy.ast.Parameter)

Example 83 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy by apache.

the class ClassCompletionVerifier method visitMethod.

@Override
public void visitMethod(MethodNode node) {
    inConstructor = false;
    inStaticConstructor = node.isStaticConstructor();
    checkAbstractDeclaration(node);
    checkRepetitiveMethod(node);
    checkOverloadingPrivateAndPublic(node);
    checkMethodModifiers(node);
    checkGenericsUsage(node, node.getParameters());
    checkGenericsUsage(node, node.getReturnType());
    for (Parameter param : node.getParameters()) {
        if (isPrimitiveVoid(param.getType())) {
            addError("The " + getDescription(param) + " in " + getDescription(node) + " has invalid type void", param);
        }
    }
    super.visitMethod(node);
}
Also used : Parameter(org.codehaus.groovy.ast.Parameter)

Example 84 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy by apache.

the class ClassCompletionVerifier method appendParamsDescription.

private void appendParamsDescription(Parameter[] parameters, StringBuilder msg) {
    msg.append("(");
    boolean needsComma = false;
    for (Parameter parameter : parameters) {
        if (needsComma) {
            msg.append(",");
        } else {
            needsComma = true;
        }
        msg.append(parameter.getType());
    }
    msg.append(")");
}
Also used : Parameter(org.codehaus.groovy.ast.Parameter)

Example 85 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy by apache.

the class ClassCompletionVerifier method checkMethodsForOverridingFinal.

private void checkMethodsForOverridingFinal(ClassNode cn) {
    for (MethodNode method : cn.getMethods()) {
        Parameter[] params = method.getParameters();
        for (MethodNode superMethod : cn.getSuperClass().getMethods(method.getName())) {
            Parameter[] superParams = superMethod.getParameters();
            if (!hasEqualParameterTypes(params, superParams))
                continue;
            if (!superMethod.isFinal())
                break;
            addInvalidUseOfFinalError(method, params, superMethod.getDeclaringClass());
            return;
        }
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

Parameter (org.codehaus.groovy.ast.Parameter)342 ClassNode (org.codehaus.groovy.ast.ClassNode)202 MethodNode (org.codehaus.groovy.ast.MethodNode)135 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)117 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)111 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)102 Expression (org.codehaus.groovy.ast.expr.Expression)98 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)90 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)75 FieldNode (org.codehaus.groovy.ast.FieldNode)71 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)71 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)69 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)68 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)66 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)66 Statement (org.codehaus.groovy.ast.stmt.Statement)66 ArrayList (java.util.ArrayList)62 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)56 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)51