Search in sources :

Example 1 with NamedParam

use of groovy.transform.NamedParam in project groovy by apache.

the class NamedVariantASTTransformation method processDelegateParam.

private boolean processDelegateParam(final MethodNode mNode, final Parameter mapParam, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam, final boolean coerce) {
    if (isInnerClass(fromParam.getType()) && mNode.isStatic()) {
        addError("Error during " + NAMED_VARIANT + " processing. Delegate type '" + fromParam.getType().getNameWithoutPackage() + "' is an inner class which is not supported.", mNode);
        return false;
    }
    Set<String> names = new HashSet<>();
    List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
    for (String name : names) {
        if (hasDuplicates(this, mNode, propNames, name))
            return false;
    }
    for (PropertyNode prop : props) {
        // create annotation @NamedParam(value='name', type=PropertyType)
        AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
        namedParam.addMember("value", constX(prop.getName()));
        namedParam.addMember("type", classX(prop.getType()));
        mapParam.addAnnotation(namedParam);
    }
    Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new);
    Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs));
    args.addExpression(castX(fromParam.getType(), delegateMap));
    return true;
}
Also used : GeneralUtils.stmt(org.codehaus.groovy.ast.tools.GeneralUtils.stmt) CompilePhase(org.codehaus.groovy.control.CompilePhase) GeneralUtils.classX(org.codehaus.groovy.ast.tools.GeneralUtils.classX) NamedVariant(groovy.transform.NamedVariant) GeneralUtils.getAllProperties(org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties) GeneralUtils.throwS(org.codehaus.groovy.ast.tools.GeneralUtils.throwS) ClassHelper.make(org.codehaus.groovy.ast.ClassHelper.make) GeneralUtils.block(org.codehaus.groovy.ast.tools.GeneralUtils.block) GeneralUtils.propX(org.codehaus.groovy.ast.tools.GeneralUtils.propX) GeneralUtils.ctorX(org.codehaus.groovy.ast.tools.GeneralUtils.ctorX) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) GeneralUtils.args(org.codehaus.groovy.ast.tools.GeneralUtils.args) AnnotatedNodeUtils(org.apache.groovy.ast.tools.AnnotatedNodeUtils) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Set(java.util.Set) GeneralUtils.callX(org.codehaus.groovy.ast.tools.GeneralUtils.callX) GeneralUtils.plusX(org.codehaus.groovy.ast.tools.GeneralUtils.plusX) List(java.util.List) ClassHelper.makeWithoutCaching(org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassNodeUtils.addGeneratedConstructor(org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedConstructor) GeneralUtils.elvisX(org.codehaus.groovy.ast.tools.GeneralUtils.elvisX) STRING_TYPE(org.codehaus.groovy.ast.ClassHelper.STRING_TYPE) GeneralUtils.boolX(org.codehaus.groovy.ast.tools.GeneralUtils.boolX) GeneralUtils.constX(org.codehaus.groovy.ast.tools.GeneralUtils.constX) GeneralUtils.ternaryX(org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) VisibilityUtils.getVisibility(org.apache.groovy.ast.tools.VisibilityUtils.getVisibility) MethodNode(org.codehaus.groovy.ast.MethodNode) GeneralUtils.asX(org.codehaus.groovy.ast.tools.GeneralUtils.asX) GeneralUtils.param(org.codehaus.groovy.ast.tools.GeneralUtils.param) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ClassNodeUtils.addGeneratedMethod(org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod) GeneralUtils.varX(org.codehaus.groovy.ast.tools.GeneralUtils.varX) Expression(org.codehaus.groovy.ast.expr.Expression) GeneralUtils.isNullX(org.codehaus.groovy.ast.tools.GeneralUtils.isNullX) Parameter(org.codehaus.groovy.ast.Parameter) MAP_TYPE(org.codehaus.groovy.ast.ClassHelper.MAP_TYPE) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneralUtils.ifS(org.codehaus.groovy.ast.tools.GeneralUtils.ifS) ClassNode(org.codehaus.groovy.ast.ClassNode) GeneralUtils.castX(org.codehaus.groovy.ast.tools.GeneralUtils.castX) NamedDelegate(groovy.transform.NamedDelegate) ClassNodeUtils.isInnerClass(org.apache.groovy.ast.tools.ClassNodeUtils.isInnerClass) NamedParam(groovy.transform.NamedParam) GeneralUtils.callThisX(org.codehaus.groovy.ast.tools.GeneralUtils.callThisX) GeneralUtils.list2args(org.codehaus.groovy.ast.tools.GeneralUtils.list2args) ASTNode(org.codehaus.groovy.ast.ASTNode) ClassHelper.isPrimitiveType(org.codehaus.groovy.ast.ClassHelper.isPrimitiveType) GenericsUtils(org.codehaus.groovy.ast.tools.GenericsUtils) GeneralUtils.defaultValueX(org.codehaus.groovy.ast.tools.GeneralUtils.defaultValueX) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) HashSet(java.util.HashSet)

Aggregations

NamedDelegate (groovy.transform.NamedDelegate)1 NamedParam (groovy.transform.NamedParam)1 NamedVariant (groovy.transform.NamedVariant)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 AnnotatedNodeUtils (org.apache.groovy.ast.tools.AnnotatedNodeUtils)1 ClassNodeUtils.addGeneratedConstructor (org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedConstructor)1 ClassNodeUtils.addGeneratedMethod (org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod)1 ClassNodeUtils.isInnerClass (org.apache.groovy.ast.tools.ClassNodeUtils.isInnerClass)1 VisibilityUtils.getVisibility (org.apache.groovy.ast.tools.VisibilityUtils.getVisibility)1 ASTNode (org.codehaus.groovy.ast.ASTNode)1 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)1 MAP_TYPE (org.codehaus.groovy.ast.ClassHelper.MAP_TYPE)1 STRING_TYPE (org.codehaus.groovy.ast.ClassHelper.STRING_TYPE)1 ClassHelper.isPrimitiveType (org.codehaus.groovy.ast.ClassHelper.isPrimitiveType)1 ClassHelper.make (org.codehaus.groovy.ast.ClassHelper.make)1 ClassHelper.makeWithoutCaching (org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1