public static ListExpression listX(List<Expression> args) { return new ListExpression(args); }
public static ListExpression list2args(List args) { ListExpression result = new ListExpression(); for (Object o : args) { result.addExpression(new ConstantExpression(o)); } return result; }
public static ListExpression classList2args(List<String> args) { ListExpression result = new ListExpression(); for (Object o : args) { result.addExpression(new ClassExpression(ClassHelper.make(o.toString()))); } return result; }
protected Expression expressionList(AST node) { List<Expression> expressionList = new ArrayList<Expression>(); for (AST child = node.getFirstChild(); child != null; child = child.getNextSibling()) { expressionList.add(expression(child)); } if (expressionList.size() == 1) { return expressionList.get(0); } else { ListExpression listExpression = new ListExpression(expressionList); listExpression.setWrapped(true); configureAST(listExpression, node); return listExpression; } }
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; }
private AnnotationNode createDataProviderAnnotation(Expression dataProviderExpr, int nextDataVariableIndex) { AnnotationNode ann = new AnnotationNode(resources.getAstNodeCache().DataProviderMetadata); ann.addMember(DataProviderMetadata.LINE, new ConstantExpression(dataProviderExpr.getLineNumber())); List<Expression> dataVariableNames = new ArrayList<>(); for (int i = nextDataVariableIndex; i < dataProcessorVars.size(); i++) dataVariableNames.add(new ConstantExpression(dataProcessorVars.get(i).getName())); ann.addMember(DataProviderMetadata.DATA_VARIABLES, new ListExpression(dataVariableNames)); return ann; }
private void turnIntoSimpleParameterization(List<Expression> column) throws InvalidSpecCompileException { VariableExpression varExpr = ObjectUtil.asInstance(column.get(0), VariableExpression.class); if (varExpr == null) throw new InvalidSpecCompileException(column.get(0), "Header of data table may only contain variable names"); if (AstUtil.isWildcardRef(varExpr)) { // assertion: column has a wildcard header, but the method's // explicit parameter list does not have a wildcard parameter return; // ignore column (see https://github.com/spockframework/spock/pull/48/) } ListExpression listExpr = new ListExpression(column.subList(1, column.size())); BinaryExpression binExpr = new BinaryExpression(varExpr, Token.newSymbol(Types.LEFT_SHIFT, -1, -1), listExpr); // NOTE: varExpr may not be the "perfect" source position here, but as long as we rewrite data tables // into simple parameterizations, it seems like the best approximation; also this source position is // unlikely to make it into a compile error, because header variable has already been checked, and the // assignment itself is unlikely to cause a compile error. (It's more likely that the rval causes a // compile error, but the rval's source position is retained.) rewriteSimpleParameterization(binExpr, varExpr); }
@Override @SuppressWarnings("unchecked") public void visitListExpression(ListExpression expr) { ListExpression conversion = new ListExpression( convertAll(expr.getExpressions())); conversion.setSourcePosition(expr); result = record(conversion); }
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; } }
public Expression transformExpression(ExpressionTransformer transformer) { Expression ret = new ListExpression(transformExpressions(getExpressions(), transformer)); ret.setSourcePosition(this); ret.copyNodeMetaData(this); return ret; }
annos.add(new AnnotationConstantExpression(an)); collector.addMember("value", new ListExpression(annos)); node.addAnnotation(collector); node.getAnnotations().removeAll(next.getValue());
private static Expression makeExpression(Object o) { if (o instanceof Class) { return new ClassExpression(ClassHelper.make((Class) o)); } //TODO: value as Annotation here! if (o instanceof Object[][]) { List<AnnotationNode> annotations = makeListOfAnnotations((Object[][])o); ListExpression le = new ListExpression(); for (AnnotationNode an : annotations) { le.addExpression(new AnnotationConstantExpression(an)); } return le; } else if (o instanceof Object[]) { ListExpression le = new ListExpression(); Object[] values = (Object[]) o; for (Object val : values) { le.addExpression(makeExpression(val)); } return le; } return new ConstantExpression(o,true); }
protected Expression listExpression(AST listNode) { List<Expression> expressions = new ArrayList<Expression>(); AST elist = listNode.getFirstChild(); assertNodeType(ELIST, elist); for (AST node = elist.getFirstChild(); node != null; node = node.getNextSibling()) { // check for stray labeled arguments: switch (node.getType()) { case LABELED_ARG: assertNodeType(COMMA, node); break; // helpful error? case SPREAD_MAP_ARG: assertNodeType(SPREAD_ARG, node); break; // helpful error } expressions.add(expression(node)); } ListExpression listExpression = new ListExpression(expressions); configureAST(listExpression, listNode); return listExpression; }
/** * 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(ListExpression origList, 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; }
private static Expression annotationValueToExpression(Object value, AsmReferenceResolver resolver) { if (value instanceof TypeWrapper) { ClassNode type = resolver.resolveClassNullable(Type.getType(((TypeWrapper) value).desc).getClassName()); return type != null ? new ClassExpression(type) : null; } if (value instanceof EnumConstantWrapper) { EnumConstantWrapper wrapper = (EnumConstantWrapper) value; return new PropertyExpression(new ClassExpression(resolver.resolveType(Type.getType(wrapper.enumDesc))), wrapper.constant); } if (value instanceof AnnotationStub) { AnnotationNode annotationNode = createAnnotationNode((AnnotationStub) value, resolver); return annotationNode != null ? new AnnotationConstantExpression(annotationNode) : ConstantExpression.NULL; } if (value != null && 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), resolver)); } return elementExprs; } if (value instanceof List) { ListExpression elementExprs = new ListExpression(); for (Object o : (List) value) { elementExprs.addExpression(annotationValueToExpression(o, resolver)); } return elementExprs; } return new ConstantExpression(value); }
private void addFeatureMetadata(FeatureMethod feature) { AnnotationNode ann = new AnnotationNode(nodeCache.FeatureMetadata); ann.setMember(FeatureMetadata.NAME, new ConstantExpression(feature.getName())); ann.setMember(FeatureMetadata.ORDINAL, new ConstantExpression(feature.getOrdinal())); ann.setMember(FeatureMetadata.LINE, new ConstantExpression(feature.getAst().getLineNumber())); ann.setMember(FeatureMetadata.BLOCKS, blockAnnElems = new ListExpression()); ListExpression paramNames = new ListExpression(); for (Parameter param : feature.getAst().getParameters()) paramNames.addExpression(new ConstantExpression(param.getName())); ann.setMember(FeatureMetadata.PARAMETER_NAMES, paramNames); feature.getAst().addAnnotation(ann); }
private Expression createDefaultValueInitializer(DeclarationExpression expr) { TupleExpression tupleExpr = ObjectUtil.asInstance(expr.getLeftExpression(), TupleExpression.class); if (tupleExpr == null) { return EmptyExpression.INSTANCE; } assert expr.isMultipleAssignmentDeclaration(); // initializing to empty list only works when none of the element types is primitive, // so create a proper list expression ListExpression listExpr = new ListExpression(); for (Expression elementExpr : tupleExpr.getExpressions()) { Variable variable = (Variable) elementExpr; listExpr.addExpression(new ConstantExpression( ReflectionUtil.getDefaultValue(variable.getOriginType().getTypeClass()))); } return listExpr; }
protected Expression indexExpression(AST indexNode) { AST bracket = indexNode.getFirstChild(); AST leftNode = bracket.getNextSibling(); Expression leftExpression = expression(leftNode); AST rightNode = leftNode.getNextSibling(); Expression rightExpression = expression(rightNode); // easier to massage here than in the grammar if (rightExpression instanceof SpreadExpression) { ListExpression wrapped = new ListExpression(); wrapped.addExpression(rightExpression); rightExpression = wrapped; } BinaryExpression binaryExpression = new BinaryExpression(leftExpression, makeToken(Types.LEFT_SQUARE_BRACKET, bracket), rightExpression); configureAST(binaryExpression, indexNode); return binaryExpression; }
private void addBlockMetadata(Block block, BlockKind kind) { AnnotationNode blockAnn = new AnnotationNode(nodeCache.BlockMetadata); blockAnn.setMember(BlockMetadata.KIND, new PropertyExpression( new ClassExpression(nodeCache.BlockKind), kind.name())); ListExpression textExprs = new ListExpression(); for (String text : block.getDescriptions()) textExprs.addExpression(new ConstantExpression(text)); blockAnn.setMember(BlockMetadata.TEXTS, textExprs); blockAnnElems.addExpression(new AnnotationConstantExpression(blockAnn)); }
public void testIterateOverList() throws Exception { ListExpression listExpression = new ListExpression(); listExpression.addExpression(new ConstantExpression("a")); listExpression.addExpression(new ConstantExpression("b")); listExpression.addExpression(new ConstantExpression("c")); listExpression.addExpression(new ConstantExpression("a")); listExpression.addExpression(new ConstantExpression("b")); listExpression.addExpression(new ConstantExpression("c")); assertIterate("iterateOverList", listExpression); }