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

The following examples show how to use org.codehaus.groovy.ast.expr.MapEntryExpression. 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: DependenciesVisitor.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
private void addDependencyFromMapExpression(final MapExpression mapExpression) {
    final List<MapEntryExpression> mapEntryExpressions = mapExpression.getMapEntryExpressions();

    String group = null;
    String name = null;
    String version = null;
    for (final MapEntryExpression mapEntryExpression : mapEntryExpressions) {
        final String key = mapEntryExpression.getKeyExpression().getText();
        final String value = mapEntryExpression.getValueExpression().getText();
        if ("group".equals(key)) {
            group = value;
        } else if ("name".equals(key)) {
            name = value;
        } else if ("version".equals(key)) {
            version = value;
        }
    }

    addDependency(group, name, version);
}
 
Example #2
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 #3
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 #4
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression transformMapEntryExpression(MapEntryExpression me, ClassNode constructorCallType) {
    Expression key = me.getKeyExpression();
    Expression value = me.getValueExpression();
    ModuleNode module = currentClass.getModule();
    if (module != null && key instanceof ConstantExpression) {
        Map<String, ImportNode> importNodes = module.getStaticImports();
        if (importNodes.containsKey(key.getText())) {
            ImportNode importNode = importNodes.get(key.getText());
            if (importNode.getType().equals(constructorCallType)) {
                String newKey = importNode.getFieldName();
                return new MapEntryExpression(new ConstantExpression(newKey), value.transformExpression(this));
            }
        }
    }
    return me;
}
 
Example #5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMapExpression(final MapExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();

    List<MapEntryExpression> entries = expression.getMapEntryExpressions();
    int size = entries.size();
    BytecodeHelper.pushConstant(mv, size * 2);

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

    int i = 0;
    for (Object object : entries) {
        MapEntryExpression entry = (MapEntryExpression) object;

        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i++);
        entry.getKeyExpression().visit(this);
        controller.getOperandStack().box();
        mv.visitInsn(AASTORE);

        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i++);
        entry.getValueExpression().visit(this);
        controller.getOperandStack().box();
        mv.visitInsn(AASTORE);

        controller.getOperandStack().remove(2);
    }
    createMapMethod.call(mv);
    controller.getOperandStack().push(ClassHelper.MAP_TYPE);
}
 
Example #6
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitMapEntryExpression(MapEntryExpression node) {
	pushASTNode(node);
	try {
		super.visitMapEntryExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #7
Source File: VerifierCodeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visitListExpression(ListExpression expression) {
    for (Expression element : expression.getExpressions()) {
        if (element instanceof MapEntryExpression) {
            throw new RuntimeParserException("No map entry allowed at this place", element);
        }
    }
    super.visitListExpression(expression);
}
 
Example #8
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visitBinaryExpression(BinaryExpression expression) {
    if (expression.getOperation().getType() == Types.LEFT_SQUARE_BRACKET &&
            expression.getRightExpression() instanceof MapEntryExpression) {
        addError("You tried to use a map entry for an index operation, this is not allowed. " +
                "Maybe something should be set in parentheses or a comma is missing?",
                expression.getRightExpression());
    }
    super.visitBinaryExpression(expression);

    if (Types.isAssignment(expression.getOperation().getType())) {
        checkFinalFieldAccess(expression.getLeftExpression());
        checkSuperOrThisOnLHS(expression.getLeftExpression());
    }
}
 
Example #9
Source File: NamedVariantASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean processDelegateParam(final MethodNode mNode, final Parameter mapParam, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam) {
    if (isInnerClass(fromParam.getType())) {
        if (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 next : names) {
        if (hasDuplicates(mNode, propNames, next)) return false;
    }
    List<MapEntryExpression> entries = new ArrayList<>();
    for (PropertyNode pNode : props) {
        String name = pNode.getName();
        // create entry [name: __namedArgs.getOrDefault('name', initialValue)]
        Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> getDefaultExpression(pNode.getType()));
        entries.add(entryX(constX(name), callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue))));
        // create annotation @NamedParam(value='name', type=DelegateType)
        AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
        namedParam.addMember("value", constX(name));
        namedParam.addMember("type", classX(pNode.getType()));
        mapParam.addAnnotation(namedParam);
    }
    Expression delegateMap = mapX(entries);
    args.addExpression(castX(fromParam.getType(), delegateMap));
    return true;
}
 
Example #10
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);
}
 
Example #11
Source File: NamedParamsCompletion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isParameterPrefix(NamedArgumentListExpression namedArgsExpression, String name) {
    List<MapEntryExpression> namedArgs = namedArgsExpression.getMapEntryExpressions();

    for (MapEntryExpression namedEntry : namedArgs) {
        String namedArgument = namedEntry.getKeyExpression().getText();
        if (namedArgument != null && name.startsWith(namedArgument)) {
            return true;
        }
    }
    return false;
}
 
Example #12
Source File: NamedParamsCompletion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the given name is in the list of named parameters.
 *
 * @param namedArgsExpression named parameters
 * @param name name
 * @return {@code true} if the given name is in the list of named parameters, {@code false} otherwise
 */
private boolean isAlreadyPresent(NamedArgumentListExpression namedArgsExpression, String name) {
    if (namedArgsExpression == null) {
        return false;
    }
    List<MapEntryExpression> namedArgs = namedArgsExpression.getMapEntryExpressions();

    for (MapEntryExpression namedEntry : namedArgs) {
        String namedArgument = namedEntry.getKeyExpression().getText();
        if (namedArgument != null && namedArgument.equals(name)) {
            return true;
        }
    }
    return false;
}
 
Example #13
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(final MapEntryExpression expression) {
    pushContext(expression);
    super.visitMapEntryExpression(expression);
    popContext();
}
 
Example #14
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(final MapEntryExpression expression) {
    super.visitMapEntryExpression(expression);
    tryFind(MapEntryExpression.class, expression);
}
 
Example #15
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(final MapEntryExpression expression) {
    assertExpressionAuthorized(expression);
    expression.getKeyExpression().visit(this);
    expression.getValueExpression().visit(this);
}
 
Example #16
Source File: ConstructorCallTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(final MethodVisitor mv) {
    WriterController controller = acg.getController();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();

    // create a temporary variable to store the constructed object
    int tmpObj = compileStack.defineTemporaryVariable("tmpObj", declaringClass, false);
    String classInternalName = BytecodeHelper.getClassInternalName(declaringClass);
    mv.visitTypeInsn(NEW, classInternalName);
    mv.visitInsn(DUP);
    String desc = "()V";
    if (innerClassCall && declaringClass.isRedirectNode() && declaringClass.redirect() instanceof InnerClassNode) {
        // load "this"
        mv.visitVarInsn(ALOAD, 0);
        InnerClassNode icn = (InnerClassNode) declaringClass.redirect();
        Parameter[] params = {new Parameter(icn.getOuterClass(), "$p$")};
        desc = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, params);
    }
    mv.visitMethodInsn(INVOKESPECIAL, classInternalName, "<init>", desc, false);
    mv.visitVarInsn(ASTORE, tmpObj); // store it into tmp variable

    // load every field
    for (MapEntryExpression entryExpression : map.getMapEntryExpressions()) {
        Expression keyExpression = staticCompilationTransformer.transform(entryExpression.getKeyExpression());
        Expression valueExpression = staticCompilationTransformer.transform(entryExpression.getValueExpression());
        BinaryExpression bexp = binX(
                propX(
                        bytecodeX(declaringClass, v -> v.visitVarInsn(ALOAD, tmpObj)),
                        keyExpression
                ),
                Token.newSymbol("=", entryExpression.getLineNumber(), entryExpression.getColumnNumber()),
                valueExpression
        );
        bexp.setSourcePosition(entryExpression);
        bexp.visit(acg);
        operandStack.pop(); // consume argument
    }

    // load object
    mv.visitVarInsn(ALOAD, tmpObj);

    // cleanup stack
    compileStack.removeVar(tmpObj);
}
 
Example #17
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(final MapEntryExpression expression) {
    super.visitMapEntryExpression(expression);
    trn.visitMapEntryExpression(expression);
}
 
Example #18
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(MapEntryExpression expression) {
    expression.getKeyExpression().visit(this);
    expression.getValueExpression().visit(this);
}
 
Example #19
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static MapEntryExpression entryX(final Expression key, final Expression value) {
    return new MapEntryExpression(key, value);
}
 
Example #20
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static MapExpression mapX(final List<MapEntryExpression> expressions) {
    return new MapExpression(expressions);
}
 
Example #21
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(final MapEntryExpression expression) {
    throw new GroovyBugError("MapEntryExpression should not be visited here");
}
 
Example #22
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMapEntryExpression(MapEntryExpression node) {
    if (isInside(node, line, column)) {
        super.visitMapEntryExpression(node);
    }
}
 
Example #23
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitMapEntryExpression(MapEntryExpression expression);