Package org.codehaus.groovy.ast.expr

Examples of org.codehaus.groovy.ast.expr.VariableExpression


                }
                addError("Error: annotation " + MY_TYPE_NAME + " can only be used within a Script body.", expr);
                return expr;
            }
        } else if (insideScriptBody && expr instanceof VariableExpression && currentClosure != null) {
            VariableExpression ve = (VariableExpression) expr;
            if (ve.getName().equals(variableName)) {
                // we may only check the variable name because the Groovy compiler
                // already fails if a variable with the same name already exists in the scope.
                // this means that a closure cannot shadow a class variable
                ve.setAccessedVariable(fieldNode);
                final VariableScope variableScope = currentClosure.getVariableScope();
                final Iterator<Variable> iterator = variableScope.getReferencedLocalVariablesIterator();
                while (iterator.hasNext()) {
                    Variable next = iterator.next();
                    if (next.getName().equals(variableName)) iterator.remove();
View Full Code Here


        // let's make the constructor
        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("_outerInstance");
        outer.setSourcePosition(expression);
        block.getVariableScope().putReferencedLocalVariable(outer);
        VariableExpression thisObject = new VariableExpression("_thisObject");
        thisObject.setSourcePosition(expression);
        block.getVariableScope().putReferencedLocalVariable(thisObject);
        TupleExpression conArgs = new TupleExpression(outer, thisObject);
        block.addStatement(
                new ExpressionStatement(
                        new ConstructorCallExpression(
                                ClassNode.SUPER,
                                conArgs)));

        // let's assign all the parameter fields from the outer context
        for (Parameter param : localVariableParams) {
            String paramName = param.getName();
            ClassNode type = param.getType();
            if (true) {
                VariableExpression initialValue = new VariableExpression(paramName);
                initialValue.setAccessedVariable(param);
                initialValue.setUseReferenceDirectly(true);
                ClassNode realType = type;
                type = ClassHelper.makeReference();
                param.setType(ClassHelper.makeReference());
                FieldNode paramField = answer.addField(paramName, ACC_PRIVATE | ACC_SYNTHETIC, type, initialValue);
                paramField.setOriginType(ClassHelper.getWrapper(param.getOriginType()));
View Full Code Here

    private void checkFinalFieldAccess(Expression expression) {
        if (!(expression instanceof VariableExpression) && !(expression instanceof PropertyExpression)) return;
        Variable v = null;
        if (expression instanceof VariableExpression) {
            VariableExpression ve = (VariableExpression) expression;
            v = ve.getAccessedVariable();
        } else {
          PropertyExpression propExp = ((PropertyExpression) expression);
          Expression objectExpression = propExp.getObjectExpression();
          if(objectExpression instanceof VariableExpression) {
            VariableExpression varExp = (VariableExpression) objectExpression;
            if(varExp.isThisExpression()) {
              v = currentClass.getDeclaredField(propExp.getPropertyAsString());
            }
          }
        }
        if (v instanceof FieldNode) {
View Full Code Here

        ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

        Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE, "coll")};

        Statement statement = createPrintlnStatement(new VariableExpression("coll"));
        classNode.addMethod(new MethodNode("oneParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

        Class fooClass = loadClass(classNode);
        assertTrue("Loaded a new class", fooClass != null);
View Full Code Here

*
*/
public class VariableExpressionTest extends TestCase {

    public void testPrimitiveOriginType() {
        VariableExpression boolExpression = new VariableExpression("fo",ClassHelper.boolean_TYPE);
        VariableExpression intExpression = new VariableExpression("foo", ClassHelper.int_TYPE);
        assertEquals(boolExpression.getOriginType().getName(),"boolean");
        assertEquals(intExpression.getOriginType().getName(),"int");
    }
View Full Code Here

        ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

        Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll")};

        Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));

        ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement);
        classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

        Class fooClass = loadClass(classNode);
        assertTrue("Loaded a new class", fooClass != null);
View Full Code Here

        assertEquals(boolExpression.getOriginType().getName(),"boolean");
        assertEquals(intExpression.getOriginType().getName(),"int");
    }
   
    public void testNonPrimitiveOriginType() {
        VariableExpression boolExpression = new VariableExpression("foo",ClassHelper.Boolean_TYPE);
        VariableExpression intExpression = new VariableExpression("foo", ClassHelper.Integer_TYPE);
        assertEquals(boolExpression.getOriginType().getName(),"java.lang.Boolean");
        assertEquals(intExpression.getOriginType().getName(),"java.lang.Integer");       
    }
View Full Code Here

        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

        Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE, "coll1"), new Parameter(ClassHelper.OBJECT_TYPE, "coll2"), new Parameter(ClassHelper.OBJECT_TYPE, "coll3")};

        BlockStatement statement = new BlockStatement();
        statement.addStatement(createPrintlnStatement(new VariableExpression("coll1")));
        statement.addStatement(createPrintlnStatement(new VariableExpression("coll2")));
        statement.addStatement(createPrintlnStatement(new VariableExpression("coll3")));

        classNode.addMethod(new MethodNode("manyParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

        Class fooClass = loadClass(classNode);
        assertTrue("Loaded a new class", fooClass != null);
View Full Code Here

        assertEquals(boolExpression.getOriginType().getName(),"java.lang.Boolean");
        assertEquals(intExpression.getOriginType().getName(),"java.lang.Integer");       
    }
   
    public void testPrimitiveOriginTypeConstructorVariableExpression() {
        VariableExpression boolExpression = new VariableExpression("foo",ClassHelper.boolean_TYPE);
        VariableExpression intExpression = new VariableExpression("foo", ClassHelper.int_TYPE);
        VariableExpression newBoolExpression = new VariableExpression(boolExpression);
        VariableExpression newIntExpression = new VariableExpression(intExpression);
        assertEquals(newBoolExpression.getOriginType().getName(),"boolean");
        assertEquals(newIntExpression.getOriginType().getName(),"int");
    }
View Full Code Here

    }
   
    public void testPrimitiveOriginTypeConstructorParameter() {
        Parameter boolParameter = new Parameter(ClassHelper.boolean_TYPE,"foo");
        Parameter intParameter = new Parameter(ClassHelper.int_TYPE,"foo");
        VariableExpression newBoolExpression = new VariableExpression(boolParameter);
        VariableExpression newIntExpression = new VariableExpression(intParameter);
        assertEquals(newBoolExpression.getOriginType().getName(),"boolean");
        assertEquals(newIntExpression.getOriginType().getName(),"int");
    }
View Full Code Here

TOP

Related Classes of org.codehaus.groovy.ast.expr.VariableExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.