org.codehaus.groovy.ast.expr.TupleExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.TupleExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InnerClassVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void passThisReference(ConstructorCallExpression call) {
    ClassNode cn = call.getType().redirect();
    if (!shouldHandleImplicitThisForInnerClass(cn)) return;

    boolean isInStaticContext = true;
    if (currentMethod != null)
        isInStaticContext = currentMethod.getVariableScope().isInStaticContext();
    else if (currentField != null)
        isInStaticContext = currentField.isStatic();
    else if (processingObjInitStatements)
        isInStaticContext = false;

    // if constructor call is not in static context, return
    if (isInStaticContext) {
        // constructor call is in static context and the inner class is non-static - 1st arg is supposed to be
        // passed as enclosing "this" instance
        //
        Expression args = call.getArguments();
        if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty()) {
            addError("No enclosing instance passed in constructor call of a non-static inner class", call);
        }
        return;
    }
    insertThis0ToSuperCall(call, cn);
}
 
Example #2
Source File: ClassNode.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the given method has a possibly matching instance method with the given name and arguments.
 *
 * @param name      the name of the method of interest
 * @param arguments the arguments to match against
 * @return true if a matching method was found
 */
public boolean hasPossibleMethod(String name, Expression arguments) {
    int count = 0;

    if (arguments instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) arguments;
        // TODO this won't strictly be true when using list expansion in argument calls
        count = tuple.getExpressions().size();
    }
    ClassNode node = this;
    do {
        for (MethodNode method : getMethods(name)) {
            if (hasCompatibleNumberOfArgs(method, count) && !method.isStatic()) {
                return true;
            }
        }
        node = node.getSuperClass();
    }
    while (node != null);

    return false;
}
 
Example #3
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void despreadList(final List<Expression> expressions, final boolean wrap) {
    List<Expression> spreadIndexes = new ArrayList<>();
    List<Expression> spreadExpressions = new ArrayList<>();
    List<Expression> normalArguments = new ArrayList<>();
    for (int i = 0, n = expressions.size(); i < n; i += 1) {
        Expression expr = expressions.get(i);
        if (!(expr instanceof SpreadExpression)) {
            normalArguments.add(expr);
        } else {
            spreadIndexes.add(new ConstantExpression(i - spreadExpressions.size(), true));
            spreadExpressions.add(((SpreadExpression) expr).getExpression());
        }
    }

    // load normal arguments as array
    visitTupleExpression(new ArgumentListExpression(normalArguments), wrap);
    // load spread expressions as array
    new TupleExpression(spreadExpressions).visit(this);
    // load insertion index
    new ArrayExpression(ClassHelper.int_TYPE, spreadIndexes, null).visit(this);

    controller.getOperandStack().remove(1);
    despreadList.call(controller.getMethodVisitor());
}
 
Example #4
Source File: ConstructorCallTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
public MapStyleConstructorCall(
        final StaticCompilationTransformer transformer,
        final ClassNode declaringClass,
        final MapExpression map,
        final ConstructorCallExpression originalCall) {
    super(declaringClass);
    this.staticCompilationTransformer = transformer;
    this.declaringClass = declaringClass;
    this.map = map;
    this.originalCall = originalCall;
    this.setSourcePosition(originalCall);
    this.copyNodeMetaData(originalCall);
    List<Expression> originalExpressions = originalCall.getArguments() instanceof TupleExpression
            ? ((TupleExpression) originalCall.getArguments()).getExpressions()
            : null;
    this.innerClassCall = originalExpressions != null && originalExpressions.size() == 2;
}
 
Example #5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
void visitTupleExpression(final TupleExpression expression, final boolean useWrapper) {
    MethodVisitor mv = controller.getMethodVisitor();
    int size = expression.getExpressions().size();

    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");

    for (int i = 0; i < size; i += 1) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        Expression argument = expression.getExpression(i);
        argument.visit(this);
        controller.getOperandStack().box();
        if (useWrapper && argument instanceof CastExpression) loadWrapper(argument);

        mv.visitInsn(AASTORE);
        controller.getOperandStack().remove(1);
    }
}
 
Example #6
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected Expression transformConstructorCallExpression(ConstructorCallExpression cce) {
    inSpecialConstructorCall = cce.isSpecialCall();
    Expression expression = cce.getArguments();
    if (expression instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) expression;
        if (tuple.getExpressions().size() == 1) {
            expression = tuple.getExpression(0);
            if (expression instanceof NamedArgumentListExpression) {
                NamedArgumentListExpression namedArgs = (NamedArgumentListExpression) expression;
                List<MapEntryExpression> entryExpressions = namedArgs.getMapEntryExpressions();
                for (int i = 0; i < entryExpressions.size(); i++) {
                    entryExpressions.set(i, (MapEntryExpression) transformMapEntryExpression(entryExpressions.get(i), cce.getType()));
                }
            }
        }
    }
    Expression ret = cce.transformExpression(this);
    inSpecialConstructorCall = false;
    return ret;
}
 
Example #7
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static boolean containsSpreadExpression(final Expression arguments) {
    List<Expression> args;
    if (arguments instanceof TupleExpression) {
        TupleExpression tupleExpression = (TupleExpression) arguments;
        args = tupleExpression.getExpressions();
    } else if (arguments instanceof ListExpression) {
        ListExpression le = (ListExpression) arguments;
        args = le.getExpressions();
    } else {
        return arguments instanceof SpreadExpression;
    }
    for (Expression arg : args) {
        if (arg instanceof SpreadExpression) return true;
    }
    return false;
}
 
Example #8
Source File: MacroGroovyMethods.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected static ClosureExpression getClosureArgument(SourceUnit source, MethodCallExpression call) {
    TupleExpression tupleArguments = getMacroArguments(source, call);

    int size = tupleArguments == null ? -1 : tupleArguments.getExpressions().size();
    if (size < 1) {
        source.addError(new SyntaxException("Call arguments should have at least one argument" + '\n', tupleArguments));
        return null;
    }

    Expression result = tupleArguments.getExpression(size - 1);
    if (!(result instanceof ClosureExpression)) {
        source.addError(new SyntaxException("Last call argument should be a closure" + '\n', result));
        return null;
    }

    return (ClosureExpression) result;
}
 
Example #9
Source File: MacroGroovyMethods.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) {
    Expression macroCallArguments = call.getArguments();
    if (macroCallArguments == null) {
        source.addError(new SyntaxException("Call should have arguments" + '\n', call));
        return null;
    }

    if (!(macroCallArguments instanceof TupleExpression)) {
        source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments));
        return null;
    }

    TupleExpression tupleArguments = (TupleExpression) macroCallArguments;

    if (tupleArguments.getExpressions() == null) {
        source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments));
        return null;
    }

    return tupleArguments;
}
 
Example #10
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void checkFinalFieldAccess(final Expression expression) {
    BiConsumer<VariableExpression, ASTNode> checkForFinal = (expr, node) -> {
        Variable variable = expr.getAccessedVariable();
        if (variable != null) {
            if (isFinal(variable.getModifiers()) && variable instanceof Parameter) {
                addError("Cannot assign a value to final variable '" + variable.getName() + "'", node);
            }
            // TODO: handle local variables
        }
    };

    if (expression instanceof VariableExpression) {
        checkForFinal.accept((VariableExpression) expression, expression);
    } else if (expression instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) expression;
        for (Expression tupleExpression : tuple.getExpressions()) {
            checkForFinal.accept((VariableExpression) tupleExpression, expression);
        }
    }
    // currently not looking for PropertyExpression: dealt with at runtime using ReadOnlyPropertyException
}
 
Example #11
Source File: ClosureWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected BlockStatement createBlockStatementForConstructor(final ClosureExpression expression, final ClassNode outerClass, final ClassNode thisClassNode) {
    BlockStatement block = new BlockStatement();
    // this block does not get a source position, because we don't
    // want this synthetic constructor to show up in corbertura reports
    VariableExpression outer = new VariableExpression(OUTER_INSTANCE, outerClass);
    outer.setSourcePosition(expression);
    block.getVariableScope().putReferencedLocalVariable(outer);
    VariableExpression thisObject = new VariableExpression(THIS_OBJECT, thisClassNode);
    thisObject.setSourcePosition(expression);
    block.getVariableScope().putReferencedLocalVariable(thisObject);
    TupleExpression conArgs = new TupleExpression(outer, thisObject);
    block.addStatement(
            new ExpressionStatement(
                    new ConstructorCallExpression(
                            ClassNode.SUPER,
                            conArgs)));
    return block;
}
 
Example #12
Source File: ClosureWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
public boolean addGeneratedClosureConstructorCall(final ConstructorCallExpression call) {
    ClassNode classNode = controller.getClassNode();
    if (!classNode.declaresInterface(ClassHelper.GENERATED_CLOSURE_Type)) return false;

    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();

    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitVarInsn(ALOAD, 0);
    ClassNode callNode = classNode.getSuperClass();
    TupleExpression arguments = (TupleExpression) call.getArguments();
    if (arguments.getExpressions().size()!=2) throw new GroovyBugError("expected 2 arguments for closure constructor super call, but got"+arguments.getExpressions().size());
    arguments.getExpression(0).visit(acg);
    operandStack.box();
    arguments.getExpression(1).visit(acg);
    operandStack.box();
    //TODO: replace with normal String, p not needed
    Parameter p = new Parameter(ClassHelper.OBJECT_TYPE,"_p");
    String descriptor = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, new Parameter[]{p,p});
    mv.visitMethodInsn(INVOKESPECIAL, BytecodeHelper.getClassInternalName(callNode), "<init>", descriptor, false);
    operandStack.remove(2);
    return true;
}
 
Example #13
Source File: AutoNewLineTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethodCallExpression(final MethodCallExpression call) {
    boolean old = inBuilderMethod;
    inBuilderMethod = false;
    if (call.isImplicitThis() && call.getArguments() instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
        if (!expressions.isEmpty()) {
            Expression lastArg = expressions.get(expressions.size() - 1);
            if (lastArg instanceof ClosureExpression) {
                call.getObjectExpression().visit(this);
                call.getMethod().visit(this);
                for (Expression expression : expressions) {
                    inBuilderMethod =  (expression == lastArg);
                    expression.visit(this);
                }
            }
        }
    } else {
        super.visitMethodCallExpression(call);
    }
    inBuilderMethod = old;
}
 
Example #14
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Override
public void visitTupleExpression(TupleExpression tupleExpression) {
  if (!methodCallStack.isEmpty()) {
    MethodCallExpression call = Iterables.getLast(methodCallStack);
    if (call.getArguments() == tupleExpression) {
      String parent = call.getMethodAsString();
      String parentParent = getParentParent();
      if (!(tupleExpression instanceof ArgumentListExpression)) {
        Map<String, String> namedArguments = new HashMap<>();
        for (Expression subExpr : tupleExpression.getExpressions()) {
          if (subExpr instanceof NamedArgumentListExpression) {
            NamedArgumentListExpression nale = (NamedArgumentListExpression) subExpr;
            for (MapEntryExpression mae : nale.getMapEntryExpressions()) {
              namedArguments.put(
                  mae.getKeyExpression().getText(), mae.getValueExpression().getText());
            }
          }
        }
        checkMethodCall(parent, parentParent, namedArguments);
      }
    }
  }
  super.visitTupleExpression(tupleExpression);
}
 
Example #15
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitDeclarationExpression(final DeclarationExpression expression) {
    visitAnnotations(expression);
    // visit right side first to prevent the use of a variable before its declaration
    expression.getRightExpression().visit(this);

    if (expression.isMultipleAssignmentDeclaration()) {
        TupleExpression list = expression.getTupleExpression();
        for (Expression listExpression : list.getExpressions()) {
            declare((VariableExpression) listExpression);
        }
    } else {
        declare(expression.getVariableExpression());
    }
}
 
Example #16
Source File: InnerClassCompletionVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void addThisReference(ConstructorNode node) {
    if (!shouldHandleImplicitThisForInnerClass(classNode)) return;

    // add "this$0" field init

    //add this parameter to node
    Parameter[] params = node.getParameters();
    Parameter[] newParams = new Parameter[params.length + 1];
    System.arraycopy(params, 0, newParams, 1, params.length);
    String name = getUniqueName(params, node);

    Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
    newParams[0] = thisPara;
    node.setParameters(newParams);

    BlockStatement block = getCodeAsBlock(node);
    BlockStatement newCode = block();
    addFieldInit(thisPara, thisField, newCode);
    ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
    if (cce == null) {
        cce = ctorSuperX(new TupleExpression());
        block.getStatements().add(0, stmt(cce));
    }
    if (shouldImplicitlyPassThisPara(cce)) {
        // add thisPara to this(...)
        TupleExpression args = (TupleExpression) cce.getArguments();
        List<Expression> expressions = args.getExpressions();
        VariableExpression ve = varX(thisPara.getName());
        ve.setAccessedVariable(thisPara);
        expressions.add(0, ve);
    }
    if (cce.isSuperCall()) {
        // we have a call to super here, so we need to add
        // our code after that
        block.getStatements().add(1, newCode);
    }
    node.setCode(block);
}
 
Example #17
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean tryPrivateMethod(final MethodNode target, final boolean implicitThis, final Expression receiver, final TupleExpression args, final ClassNode classNode) {
    ClassNode declaringClass = target.getDeclaringClass();
    if ((isPrivateBridgeMethodsCallAllowed(declaringClass, classNode) || isPrivateBridgeMethodsCallAllowed(classNode, declaringClass))
            && declaringClass.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS) != null
            && !declaringClass.equals(classNode)) {
        if (tryBridgeMethod(target, receiver, implicitThis, args, classNode)) {
            return true;
        } else {
            checkAndAddCannotCallPrivateMethodError(target, receiver, classNode, declaringClass);
        }
    }
    checkAndAddCannotCallPrivateMethodError(target, receiver, classNode, declaringClass);
    return false;
}
 
Example #18
Source File: InvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void invokeClosure(final Expression arguments, final String methodName) {
    AsmClassGenerator acg = controller.getAcg();
    acg.visitVariableExpression(new VariableExpression(methodName));
    controller.getOperandStack().box();
    if (arguments instanceof TupleExpression) {
        arguments.visit(acg);
    } else {
        new TupleExpression(arguments).visit(acg);
    }
    invokeClosureMethod.call(controller.getMethodVisitor());
    controller.getOperandStack().replace(ClassHelper.OBJECT_TYPE);
}
 
Example #19
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static int argumentSize(final Expression arguments) {
    if (arguments instanceof TupleExpression) {
        TupleExpression tupleExpression = (TupleExpression) arguments;
        int size = tupleExpression.getExpressions().size();
        return size;
    }
    return 1;
}
 
Example #20
Source File: InvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void writeNormalConstructorCall(final ConstructorCallExpression call) {
    Expression arguments = call.getArguments();
    if (arguments instanceof TupleExpression) {
        TupleExpression tupleExpression = (TupleExpression) arguments;
        int size = tupleExpression.getExpressions().size();
        if (size == 0) {
            arguments = MethodCallExpression.NO_ARGUMENTS;
        }
    }

    Expression receiver = new ClassExpression(call.getType());
    controller.getCallSiteWriter().makeCallSite(receiver, CallSiteWriter.CONSTRUCTOR, arguments, false, false, false, false);
}
 
Example #21
Source File: CallSiteWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void visitBoxedArgument(Expression exp) {
    exp.visit(controller.getAcg());
    if (!(exp instanceof TupleExpression)) {
        // we are not in a tuple, so boxing might be missing for
        // this single argument call
        controller.getOperandStack().box();
    }
}
 
Example #22
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitTupleExpression(TupleExpression node) {
	pushASTNode(node);
	try {
		super.visitTupleExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #23
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void recordFinalVars(Expression leftExpression) {
    if (leftExpression instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) leftExpression;
        if (Modifier.isFinal(var.getModifiers())) {
            declaredFinalVariables.add(var);
        }
    } else if (leftExpression instanceof TupleExpression) {
        TupleExpression te = (TupleExpression) leftExpression;
        for (Expression next : te.getExpressions()) {
            if (next instanceof Variable) {
                declaredFinalVariables.add((Variable) next);
            }
        }
    }
}
 
Example #24
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void recordAssignments(BinaryExpression expression, boolean isDeclaration, Expression leftExpression, Expression rightExpression) {
    if (leftExpression instanceof Variable) {
        boolean uninitialized = isDeclaration && rightExpression instanceof EmptyExpression;
        recordAssignment((Variable) leftExpression, isDeclaration, uninitialized, false, expression);
    } else if (leftExpression instanceof TupleExpression) {
        TupleExpression te = (TupleExpression) leftExpression;
        for (Expression next : te.getExpressions()) {
            if (next instanceof Variable) {
                recordAssignment((Variable) next, isDeclaration, false, false, next);
            }
        }
    }
}
 
Example #25
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visitMethodCallExpression(MethodCallExpression mce) {
    super.visitMethodCallExpression(mce);
    Expression aexp = mce.getArguments();
    if (aexp instanceof TupleExpression) {
        TupleExpression arguments = (TupleExpression) aexp;
        for (Expression e : arguments.getExpressions()) {
            checkForInvalidDeclaration(e);
        }
    } else {
        checkForInvalidDeclaration(aexp);
    }
}
 
Example #26
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression cce) {
    if (!insideScriptBody || !cce.isUsingAnonymousInnerClass()) return;
    ConstructorCallExpression old = currentAIC;
    currentAIC = cce;
    Expression newArgs = transform(cce.getArguments());
    if (cce.getArguments() instanceof TupleExpression && newArgs instanceof TupleExpression) {
        List<Expression> argList = ((TupleExpression) cce.getArguments()).getExpressions();
        argList.clear();
        argList.addAll(((TupleExpression) newArgs).getExpressions());
    }
    currentAIC = old;
}
 
Example #27
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private ArgumentListExpression createArgumentList(final Expression origCallArgs) {
    ArgumentListExpression newArgs = new ArgumentListExpression();
    newArgs.addExpression(new VariableExpression(weaved));
    if (origCallArgs instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) origCallArgs).getExpressions();
        for (Expression expression : expressions) {
            newArgs.addExpression(transform(expression));
        }
    } else {
        newArgs.addExpression(origCallArgs);
    }
    return newArgs;
}
 
Example #28
Source File: SuperCallTraitTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallExpression(final MethodCallExpression exp) {
    if (isTraitSuperPropertyExpression(exp.getObjectExpression())) {
        Expression objectExpression = exp.getObjectExpression();
        ClassNode traitReceiver = ((PropertyExpression) objectExpression).getObjectExpression().getType();

        if (traitReceiver != null) {
            // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
            ClassExpression receiver = new ClassExpression(
                    getHelper(traitReceiver)
            );
            ArgumentListExpression newArgs = new ArgumentListExpression();
            Expression arguments = exp.getArguments();
            newArgs.addExpression(new VariableExpression("this"));
            if (arguments instanceof TupleExpression) {
                List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
                for (Expression expression : expressions) {
                    newArgs.addExpression(transform(expression));
                }
            } else {
                newArgs.addExpression(transform(arguments));
            }
            MethodCallExpression result = new MethodCallExpression(
                    receiver,
                    transform(exp.getMethod()),
                    newArgs
            );
            result.setImplicitThis(false);
            result.setSpreadSafe(exp.isSpreadSafe());
            result.setSafe(exp.isSafe());
            result.setSourcePosition(exp);
            return result;
        }
    }
    return super.transform(exp);
}
 
Example #29
Source File: NamedParamsCompletion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean complete(List<CompletionProposal> proposals, CompletionContext context, int anchor) {
    this.context = context;

    ASTNode leaf = context.path.leaf();

    if (leaf instanceof ConstructorCallExpression) {
        ConstructorCallExpression constructorCall = (ConstructorCallExpression) leaf;
        
        Expression constructorArgs = constructorCall.getArguments();
        if (constructorArgs instanceof TupleExpression) {
            List<Expression> arguments = ((TupleExpression) constructorArgs).getExpressions();

            if (arguments.isEmpty()) {
                completeNamedParams(proposals, anchor, constructorCall, null);
            } else {
                for (Expression argExpression : arguments) {
                    if (argExpression instanceof NamedArgumentListExpression) {
                        completeNamedParams(proposals, anchor, constructorCall, (NamedArgumentListExpression) argExpression);
                    }
                }
            }
        }
    }

    ASTNode leafParent = context.path.leafParent();
    ASTNode leafGrandparent = context.path.leafGrandParent();
    if (leafParent instanceof NamedArgumentListExpression &&
        leafGrandparent instanceof ConstructorCallExpression) {

        completeNamedParams(proposals, anchor, (ConstructorCallExpression) leafGrandparent, (NamedArgumentListExpression) leafParent);
    }

    return false;
}
 
Example #30
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(
                            exp.getObjectExpression(),
                            includeType.getMethodName(),
                            new ArgumentListExpression(
                                    mapEntry.getValueExpression()
                            )
                    );
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                    // not a valid import type, do not modify the code
                }
            }

        }
    }
    return super.transform(exp);
}