use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy-core by groovy.
the class Verifier method addDefaultParameters.
protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
Parameter[] parameters = method.getParameters();
int counter = 0;
List paramValues = new ArrayList();
int size = parameters.length;
for (int i = size - 1; i >= 0; i--) {
Parameter parameter = parameters[i];
if (parameter != null && parameter.hasInitialExpression()) {
paramValues.add(Integer.valueOf(i));
paramValues.add(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
counter++;
}
}
for (int j = 1; j <= counter; j++) {
Parameter[] newParams = new Parameter[parameters.length - j];
ArgumentListExpression arguments = new ArgumentListExpression();
int index = 0;
int k = 1;
for (int i = 0; i < parameters.length; i++) {
if (k > counter - j && parameters[i] != null && parameters[i].hasInitialExpression()) {
arguments.addExpression(new CastExpression(parameters[i].getType(), parameters[i].getInitialExpression()));
k++;
} else if (parameters[i] != null && parameters[i].hasInitialExpression()) {
newParams[index++] = parameters[i];
arguments.addExpression(new CastExpression(parameters[i].getType(), new VariableExpression(parameters[i].getName())));
k++;
} else {
newParams[index++] = parameters[i];
arguments.addExpression(new CastExpression(parameters[i].getType(), new VariableExpression(parameters[i].getName())));
}
}
action.call(arguments, newParams, method);
}
for (Parameter parameter : parameters) {
// remove default expression and store it as node metadata
parameter.putNodeMetaData(Verifier.INITIAL_EXPRESSION, parameter.getInitialExpression());
parameter.setInitialExpression(null);
}
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy-core by groovy.
the class AssertionWriter method writeSourcelessAssertText.
private void writeSourcelessAssertText(AssertStatement statement) {
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
BooleanExpression booleanExpression = statement.getBooleanExpression();
// push expression string onto stack
String expressionText = booleanExpression.getText();
List<String> list = new ArrayList<String>();
addVariableNames(booleanExpression, list);
if (list.isEmpty()) {
mv.visitLdcInsn(expressionText);
} else {
boolean first = true;
// let's create a new expression
mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
mv.visitInsn(DUP);
mv.visitLdcInsn(expressionText + ". Values: ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false);
//TODO: maybe use more special type StringBuffer here
operandStack.push(ClassHelper.OBJECT_TYPE);
int tempIndex = controller.getCompileStack().defineTemporaryVariable("assert", true);
for (String name : list) {
String text = name + " = ";
if (first) {
first = false;
} else {
text = ", " + text;
}
mv.visitVarInsn(ALOAD, tempIndex);
mv.visitLdcInsn(text);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;", false);
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, tempIndex);
new VariableExpression(name).visit(controller.getAcg());
operandStack.box();
mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "toString", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false);
mv.visitInsn(POP);
operandStack.remove(1);
}
mv.visitVarInsn(ALOAD, tempIndex);
controller.getCompileStack().removeVar(tempIndex);
}
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy-core by groovy.
the class BinaryExpressionMultiTypeDispatcher method doAssignmentToLocalVariable.
private boolean doAssignmentToLocalVariable(String method, BinaryExpression binExp) {
Expression left = binExp.getLeftExpression();
if (left instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) left;
Variable v = ve.getAccessedVariable();
if (v instanceof DynamicVariable)
return false;
if (v instanceof PropertyExpression)
return false;
/* field and declaration we don't return false */
} else {
return false;
}
evaluateBinaryExpression(method, binExp);
getController().getOperandStack().dup();
getController().getCompileStack().pushLHS(true);
binExp.getLeftExpression().visit(getController().getAcg());
getController().getCompileStack().popLHS();
return true;
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy-core by groovy.
the class StatementMetaTypeChooser method resolveType.
public ClassNode resolveType(final Expression exp, final ClassNode current) {
if (exp instanceof ClassExpression)
return ClassHelper.CLASS_Type;
OptimizingStatementWriter.StatementMeta meta = (OptimizingStatementWriter.StatementMeta) exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
ClassNode type = null;
if (meta != null)
type = meta.type;
if (type != null)
return type;
if (exp instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) exp;
if (ve.isClosureSharedVariable())
return ve.getType();
type = ve.getOriginType();
if (ve.getAccessedVariable() instanceof FieldNode) {
FieldNode fn = (FieldNode) ve.getAccessedVariable();
if (!fn.getDeclaringClass().equals(current))
return fn.getOriginType();
}
} else if (exp instanceof Variable) {
Variable v = (Variable) exp;
type = v.getOriginType();
} else {
type = exp.getType();
}
return type.redirect();
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.
protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
FieldNode fieldNode = classNode.getField(apiProperty);
if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
classNode.addField(fieldNode);
String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
Parameter setterParameter = new Parameter(implementationNode, apiProperty);
BlockStatement setterBody = new BlockStatement();
setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
}
}
Aggregations