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

The following examples show how to use org.codehaus.groovy.ast.expr.ListExpression. 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: Java8.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression annotationValueToExpression (Object value) {
    if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
        return new ConstantExpression(value);

    if (value instanceof Class)
        return new ClassExpression(ClassHelper.makeWithoutCaching((Class<?>)value));

    if (value.getClass().isArray()) {
        ListExpression elementExprs = new ListExpression();
        int len = Array.getLength(value);
        for (int i = 0; i != len; ++i)
            elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
        return elementExprs;
    }

    return null;
}
 
Example #2
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 6 votes vote down vote up
private List<AnnotationNode> getTargetListFromValue(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, SourceUnit source) {
    Expression memberValue = collector.getMember("value");
    if (memberValue == null) {
        return Collections.emptyList();
    }
    if (!(memberValue instanceof ListExpression)) {
        addError("Annotation collector expected a list of classes, but got a "+memberValue.getClass(), collector, source);
        return Collections.emptyList();
    }
    ListExpression memberListExp = (ListExpression) memberValue;
    List<Expression> memberList = memberListExp.getExpressions();
    if (memberList.isEmpty()) {
        return Collections.emptyList();
    }
    List<AnnotationNode> ret = new ArrayList<>();
    for (Expression e : memberList) {
        AnnotationNode toAdd = new AnnotationNode(e.getType());
        toAdd.setSourcePosition(aliasAnnotationUsage);
        ret.add(toAdd);
    }
    return ret;
}
 
Example #3
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression serialize(Expression e) {
    if (e instanceof AnnotationConstantExpression) {
        AnnotationConstantExpression ace = (AnnotationConstantExpression) e;
        return serialize((AnnotationNode) ace.getValue());
    } else if (e instanceof ListExpression) {
        boolean annotationConstant = false;
        ListExpression le = (ListExpression) e;
        List<Expression> list = le.getExpressions();
        List<Expression> newList = new ArrayList<>(list.size());
        for (Expression exp: list) {
            annotationConstant = annotationConstant || exp instanceof AnnotationConstantExpression;
            newList.add(serialize(exp));
        }
        ClassNode type = ClassHelper.OBJECT_TYPE;
        if (annotationConstant) type = type.makeArray();
        return new ArrayExpression(type, newList);
    }
    return e;
}
 
Example #4
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void newifyDeclaration(DeclarationExpression de, boolean autoFlag, ListExpression list, final Pattern cnPattern) {
    ClassNode cNode = de.getDeclaringClass();
    candidate = de;
    final ListExpression oldClassesToNewify = classesToNewify;
    final boolean oldAuto = auto;
    final Pattern oldCnPattern = classNamePattern;

    classesToNewify = list;
    auto = autoFlag;
    classNamePattern = cnPattern;

    super.visitClass(cNode);

    classesToNewify = oldClassesToNewify;
    auto = oldAuto;
    classNamePattern = oldCnPattern;
}
 
Example #5
Source File: AbstractASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
public List<ClassNode> getMemberClassList(AnnotationNode anno, String name) {
    List<ClassNode> list = new ArrayList<>();
    Expression expr = anno.getMember(name);
    if (expr == null) {
        return null;
    }
    if (expr instanceof ListExpression) {
        final ListExpression listExpression = (ListExpression) expr;
        if (isUndefinedMarkerList(listExpression)) {
            return null;
        }
        list = getTypeList(listExpression);
    } else if (expr instanceof ClassExpression) {
        ClassNode cn = expr.getType();
        if (isUndefined(cn)) return null;
        if (cn != null) list.add(cn);
    }
    return list;
}
 
Example #6
Source File: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static List<String> getKnownImmutableClasses(AbstractASTTransformation xform, ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    final List<String> immutableClasses = new ArrayList<String>();

    if (anno == null) return immutableClasses;
    final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLE_CLASSES);
    if (expression == null) return immutableClasses;

    if (!(expression instanceof ListExpression)) {
        xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable classes via \"" + MEMBER_KNOWN_IMMUTABLE_CLASSES + "\"", anno);
        return immutableClasses;
    }

    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ClassExpression) {
            immutableClasses.add(listItemExpression.getType().getName());
        }
    }

    return immutableClasses;
}
 
Example #7
Source File: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static List<String> getKnownImmutables(AbstractASTTransformation xform, ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    final List<String> immutables = new ArrayList<String>();
    if (anno == null) return immutables;

    final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLES);
    if (expression == null) return immutables;

    if (!(expression instanceof ListExpression)) {
        xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable property names via \"" + MEMBER_KNOWN_IMMUTABLES + "\"", anno);
        return immutables;
    }

    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ConstantExpression) {
            immutables.add((String) ((ConstantExpression) listItemExpression).getValue());
        }
    }
    if (!xform.checkPropertyList(cNode, immutables, "knownImmutables", anno, "immutable class", false)) return immutables;

    return immutables;
}
 
Example #8
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void newifyMethodOrField(AnnotatedNode parent, boolean autoFlag, ListExpression list, final Pattern cnPattern) {

        final ListExpression oldClassesToNewify = classesToNewify;
        final boolean oldAuto = auto;
        final Pattern oldCnPattern = classNamePattern;

        checkClassLevelClashes(list);
        checkAutoClash(autoFlag, parent);

        classesToNewify = list;
        auto = autoFlag;
        classNamePattern = cnPattern;

        if (parent instanceof FieldNode) {
            super.visitField((FieldNode) parent);
        } else {
            super.visitMethod((MethodNode) parent);
        }

        classesToNewify = oldClassesToNewify;
        auto = oldAuto;
        classNamePattern = oldCnPattern;
    }
 
Example #9
Source File: ExpressionUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Given a list of constants, transform each item in the list.
 *
 * @param origList the list to transform
 * @param attrType the target type
 * @return the transformed list or the original if nothing was changed
 */
public static Expression transformListOfConstants(final ListExpression origList, final ClassNode attrType) {
    ListExpression newList = new ListExpression();
    boolean changed = false;
    for (Expression e : origList.getExpressions()) {
        try {
            Expression transformed = transformInlineConstants(e, attrType);
            newList.addExpression(transformed);
            if (transformed != e) changed = true;
        } catch(Exception ignored) {
            newList.addExpression(e);
        }
    }
    if (changed) {
        newList.setSourcePosition(origList);
        return newList;
    }
    return origList;
}
 
Example #10
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void newifyClass(ClassNode cNode, boolean autoFlag, ListExpression list, final Pattern cnPattern) {
    String cName = cNode.getName();
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cName + "'. @"
                + MY_NAME + " not allowed for interfaces.", cNode);
    }

    final ListExpression oldClassesToNewify = classesToNewify;
    final boolean oldAuto = auto;
    final Pattern oldCnPattern = classNamePattern;

    classesToNewify = list;
    auto = autoFlag;
    classNamePattern = cnPattern;

    super.visitClass(cNode);

    classesToNewify = oldClassesToNewify;
    auto = oldAuto;
    classNamePattern = oldCnPattern;
}
 
Example #11
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassNode deriveExpressonType(Expression expression) {
    ClassNode derivedExpressionType = null;
    if (expression instanceof ConstantExpression
            && !expression.getText().equals("null")) { // NOI18N
        derivedExpressionType = ((ConstantExpression) expression).getType();
    } else if (expression instanceof ConstructorCallExpression) {
        derivedExpressionType = ((ConstructorCallExpression) expression).getType();
    } else if (expression instanceof MethodCallExpression) {
        int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
        AstPath newPath = new AstPath(path.root(), newOffset, doc);
        derivedExpressionType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
    } else if (expression instanceof StaticMethodCallExpression) {
        derivedExpressionType = MethodInference.findCallerType(expression, path, doc, cursorOffset);
    } else if (expression instanceof ListExpression) {
        derivedExpressionType = ((ListExpression) expression).getType();
    } else if (expression instanceof MapExpression) {
        derivedExpressionType = ((MapExpression) expression).getType();
    } else if (expression instanceof RangeExpression) {
        // this should work, but the type is Object - nut sure why
        // guessedType = ((RangeExpression)initialExpression).getType();
        derivedExpressionType = ClassHelper.makeWithoutCaching(Range.class, true);                
    } 
    return derivedExpressionType;
}
 
Example #12
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 #13
Source File: PackageScopeASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<groovy.transform.PackageScopeTarget> determineTargets(Expression expr) {
    List<groovy.transform.PackageScopeTarget> list = new ArrayList<groovy.transform.PackageScopeTarget>();
    if (expr instanceof PropertyExpression) {
        list.add(extractTarget((PropertyExpression) expr));
    } else if (expr instanceof ListExpression) {
        final ListExpression expressionList = (ListExpression) expr;
        final List<Expression> expressions = expressionList.getExpressions();
        for (Expression ex : expressions) {
            if (ex instanceof PropertyExpression) {
                list.add(extractTarget((PropertyExpression) ex));
            }
        }
    }
    return list;
}
 
Example #14
Source File: Traits.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Collects all the self types that a type should extend or implement, given
 * the traits is implements.
 * @param receiver a class node that may implement a trait
 * @param selfTypes a collection where the list of self types will be written
 * @param checkInterfaces should the interfaces that the node implements be collected too
 * @param checkSuper should we collect from the superclass too
 * @return the selfTypes collection itself
 * @since 2.4.0
 */
public static LinkedHashSet<ClassNode> collectSelfTypes(
        ClassNode receiver,
        LinkedHashSet<ClassNode> selfTypes,
        boolean checkInterfaces,
        boolean checkSuper) {
    if (Traits.isTrait(receiver)) {
        List<AnnotationNode> annotations = receiver.getAnnotations(SELFTYPE_CLASSNODE);
        for (AnnotationNode annotation : annotations) {
            Expression value = annotation.getMember("value");
            if (value instanceof ClassExpression) {
                selfTypes.add(value.getType());
            } else if (value instanceof ListExpression) {
                List<Expression> expressions = ((ListExpression) value).getExpressions();
                for (Expression expression : expressions) {
                    if (expression instanceof ClassExpression) {
                        selfTypes.add(expression.getType());
                    }
                }
            }
        }
    }
    if (checkInterfaces) {
        ClassNode[] interfaces = receiver.getInterfaces();
        for (ClassNode anInterface : interfaces) {
            collectSelfTypes(anInterface, selfTypes, true, checkSuper);
        }
    }

    if (checkSuper) {
        ClassNode superClass = receiver.getSuperClass();
        if (superClass != null) {
            collectSelfTypes(superClass, selfTypes, checkInterfaces, true);
        }
    }
    return selfTypes;
}
 
Example #15
Source File: ListExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private List<Expression> transformArguments(final ListExpression expr) {
    List<Expression> expressions = expr.getExpressions();
    List<Expression> transformedArgs = new LinkedList<Expression>();
    for (Expression expression : expressions) {
        transformedArgs.add(transformer.transform(expression));
    }
    return transformedArgs;
}
 
Example #16
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitListExpression(ListExpression node) {
	pushASTNode(node);
	try {
		super.visitListExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #17
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkDuplicateNameClashes(ListExpression list) {
    final Set<String> seen = new HashSet<String>();
    @SuppressWarnings("unchecked")
    List<ClassExpression> classes = (List) list.getExpressions();
    for (ClassExpression ce : classes) {
        final String name = ce.getType().getNameWithoutPackage();
        if (seen.contains(name)) {
            addError("Duplicate name '" + name + "' found during @" + MY_NAME + " processing.", ce);
        }
        seen.add(name);
    }
}
 
Example #18
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkClassLevelClashes(ListExpression list) {
    @SuppressWarnings("unchecked")
    List<ClassExpression> classes = (List) list.getExpressions();
    for (ClassExpression ce : classes) {
        final String name = ce.getType().getNameWithoutPackage();
        if (findClassWithMatchingBasename(name)) {
            addError("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at " +
                    "method/constructor/field level if it already appears at the class level.", ce);
        }
    }
}
 
Example #19
Source File: StaticTypesTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void addTypeCheckingExtensions(StaticTypeCheckingVisitor visitor, Expression extensions) {
    if (extensions instanceof ConstantExpression) {
        visitor.addTypeCheckingExtension(new GroovyTypeCheckingExtensionSupport(
                visitor,
                extensions.getText(),
                compilationUnit
        ));
    } else if (extensions instanceof ListExpression) {
        ListExpression list = (ListExpression) extensions;
        for (Expression ext : list.getExpressions()) {
            addTypeCheckingExtensions(visitor, ext);
        }
    }
}
 
Example #20
Source File: EnumHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static FieldNode addEnumConstant(ClassNode enumClass, String name, Expression init) {
    int modifiers = PUBLIC_FS | Opcodes.ACC_ENUM;
    if (init != null && !(init instanceof ListExpression)) {
        ListExpression list = new ListExpression();
        list.addExpression(init);
        init = list;
    }
    FieldNode fn = new FieldNode(name, modifiers, enumClass.getPlainNodeReference(), enumClass, init);
    enumClass.addField(fn);
    return fn;
}
 
Example #21
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ListExpression list2args(final List<?> args) {
    ListExpression result = new ListExpression();
    for (Object o : args) {
        result.addExpression(constX(o));
    }
    return result;
}
 
Example #22
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ListExpression classList2args(final List<String> args) {
    ListExpression result = new ListExpression();
    for (Object o : args) {
        result.addExpression(classX(ClassHelper.make(o.toString())));
    }
    return result;
}
 
Example #23
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 #24
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void visitArrayAttributes(final AnnotationNode an, final Map<String, ListExpression> arrayAttr, final AnnotationVisitor av) {
    if (arrayAttr.isEmpty()) return;
    for (Map.Entry<String, ListExpression> entry : arrayAttr.entrySet()) {
        AnnotationVisitor av2 = av.visitArray(entry.getKey());
        List<Expression> values = entry.getValue().getExpressions();
        if (!values.isEmpty()) {
            int arrayElementType = determineCommonArrayType(values);
            for (Expression exprChild : values) {
                visitAnnotationArrayElement(exprChild, arrayElementType, av2);
            }
        }
        av2.visitEnd();
    }
}
 
Example #25
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean containsOnlyConstants(final ListExpression list) {
    for (Expression exp : list.getExpressions()) {
        if (exp instanceof ConstantExpression) continue;
        return false;
    }
    return true;
}
 
Example #26
Source File: ListExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
    // can be replaced with a direct constructor call
    List<Expression> transformedArgs = transformArguments(expr);
    ConstructorCallExpression cce = new ConstructorCallExpression(
            target.getDeclaringClass(),
            new ArgumentListExpression(transformedArgs)
    );
    cce.setSourcePosition(expr);
    cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
    return cce;
}
 
Example #27
Source File: ListExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
Expression transformListExpression(final ListExpression expr) {
    MethodNode target = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (target instanceof ConstructorNode) {
        if (target.getDeclaringClass().isArray()) {
            return transformArrayConstructor(expr, target);
        }
        return transformRegularConstructor(expr, target);

    } else {
        return transformer.superTransform(expr);
    }
}
 
Example #28
Source File: StaticCompilationTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(Expression expr) {
    if (expr instanceof StaticMethodCallExpression) {
        return staticMethodCallExpressionTransformer.transformStaticMethodCallExpression((StaticMethodCallExpression) expr);
    }
    if (expr instanceof BinaryExpression) {
        return binaryExpressionTransformer.transformBinaryExpression((BinaryExpression)expr);
    }
    if (expr instanceof MethodCallExpression) {
        return methodCallExpressionTransformer.transformMethodCallExpression((MethodCallExpression) expr);
    }
    if (expr instanceof ClosureExpression) {
        return closureExpressionTransformer.transformClosureExpression((ClosureExpression) expr);
    }
    if (expr instanceof ConstructorCallExpression) {
        return constructorCallTransformer.transformConstructorCall((ConstructorCallExpression) expr);
    }
    if (expr instanceof BooleanExpression) {
        return booleanExpressionTransformer.transformBooleanExpression((BooleanExpression)expr);
    }
    if (expr instanceof VariableExpression) {
        return variableExpressionTransformer.transformVariableExpression((VariableExpression)expr);
    }
    if (expr instanceof RangeExpression) {
        return rangeExpressionTransformer.transformRangeExpression(((RangeExpression)expr));
    }
    if (expr instanceof ListExpression) {
        return listExpressionTransformer.transformListExpression((ListExpression) expr);
    }
    if (expr instanceof CastExpression) {
        return castExpressionTransformer.transformCastExpression(((CastExpression)expr));
    }
    return super.transform(expr);
}
 
Example #29
Source File: AbstractASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<ClassNode> getTypeList(ListExpression listExpression) {
    List<ClassNode> list = new ArrayList<>();
    for (Expression itemExpr : listExpression.getExpressions()) {
        if (itemExpr instanceof ClassExpression) {
            ClassNode cn = itemExpr.getType();
            if (cn != null) list.add(cn);
        }
    }
    return list;
}
 
Example #30
Source File: AbstractASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<String> getValueStringList(ListExpression listExpression) {
    List<String> list = new ArrayList<>();
    for (Expression itemExpr : listExpression.getExpressions()) {
        if (itemExpr instanceof ConstantExpression) {
            Object value = ((ConstantExpression) itemExpr).getValue();
            if (value != null) list.add(value.toString());
        }
    }
    return list;
}