Java Code Examples for org.codehaus.groovy.ast.expr.ClosureExpression#setVariableScope()
The following examples show how to use
org.codehaus.groovy.ast.expr.ClosureExpression#setVariableScope() .
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: VariableScopeVisitor.java From groovy with Apache License 2.0 | 6 votes |
@Override public void visitClosureExpression(final ClosureExpression expression) { pushState(); expression.setVariableScope(currentScope); inClosure = !isAnonymous(currentScope.getParent().getClassScope()); if (expression.isParameterSpecified()) { for (Parameter parameter : expression.getParameters()) { parameter.setInStaticContext(currentScope.isInStaticContext()); if (parameter.hasInitialExpression()) { parameter.getInitialExpression().visit(this); } declare(parameter, expression); } } else if (expression.getParameters() != null) { Parameter var = new Parameter(ClassHelper.OBJECT_TYPE, "it"); var.setInStaticContext(currentScope.isInStaticContext()); currentScope.putDeclaredVariable(var); } super.visitClosureExpression(expression); markClosureSharedVariables(); popState(); }
Example 2
Source File: AutoCloneASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
private void createCloneSerialization(ClassNode cNode) { final BlockStatement body = new BlockStatement(); // def baos = new ByteArrayOutputStream() final Expression baos = localVarX("baos"); body.addStatement(declS(baos, ctorX(BAOS_TYPE))); // baos.withObjectOutputStream{ it.writeObject(this) } MethodCallExpression writeObject = callX(castX(OOS_TYPE, varX("it")), "writeObject", varX("this")); writeObject.setImplicitThis(false); ClosureExpression writeClos = closureX(block(stmt(writeObject))); writeClos.setVariableScope(new VariableScope()); body.addStatement(stmt(callX(baos, "withObjectOutputStream", args(writeClos)))); // def bais = new ByteArrayInputStream(baos.toByteArray()) final Expression bais = localVarX("bais"); body.addStatement(declS(bais, ctorX(BAIS_TYPE, args(callX(baos, "toByteArray"))))); // return bais.withObjectInputStream(getClass().classLoader){ (<type>) it.readObject() } MethodCallExpression readObject = callX(castX(OIS_TYPE, varX("it")), "readObject"); readObject.setImplicitThis(false); ClosureExpression readClos = closureX(block(stmt(castX(GenericsUtils.nonGeneric(cNode), readObject)))); readClos.setVariableScope(new VariableScope()); Expression classLoader = callX(callThisX("getClass"), "getClassLoader"); body.addStatement(returnS(callX(bais, "withObjectInputStream", args(classLoader, readClos)))); new VariableScopeVisitor(sourceUnit, true).visitClass(cNode); ClassNode[] exceptions = {make(CloneNotSupportedException.class)}; addGeneratedMethod(cNode, "clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, body); }
Example 3
Source File: MarkupBuilderCodeTransformer.java From groovy with Apache License 2.0 | 4 votes |
private Expression transformMethodCall(final MethodCallExpression exp) { String name = exp.getMethodAsString(); if (exp.isImplicitThis() && "include".equals(name)) { return tryTransformInclude(exp); } else if (exp.isImplicitThis() && name.startsWith(":")) { List<Expression> args; if (exp.getArguments() instanceof ArgumentListExpression) { args = ((ArgumentListExpression) exp.getArguments()).getExpressions(); } else { args = Collections.singletonList(exp.getArguments()); } Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args))); MethodCallExpression call = new MethodCallExpression( new VariableExpression("this"), "methodMissing", newArguments ); call.setImplicitThis(true); call.setSafe(exp.isSafe()); call.setSpreadSafe(exp.isSpreadSafe()); call.setSourcePosition(exp); return call; } else if (name!=null && name.startsWith("$")) { MethodCallExpression reformatted = new MethodCallExpression( exp.getObjectExpression(), name.substring(1), exp.getArguments() ); reformatted.setImplicitThis(exp.isImplicitThis()); reformatted.setSafe(exp.isSafe()); reformatted.setSpreadSafe(exp.isSpreadSafe()); reformatted.setSourcePosition(exp); // wrap in a stringOf { ... } closure call ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted)); clos.setVariableScope(new VariableScope()); MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"), "stringOf", clos); stringOf.setImplicitThis(true); stringOf.setSourcePosition(reformatted); return stringOf; } return super.transform(exp); }