use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class GrabAnnotationTransformation method callGrabAsStaticInitIfNeeded.
private void callGrabAsStaticInitIfNeeded(ClassNode classNode, ClassNode grapeClassNode, List<Map<String, Object>> grabMapsInit, List<Map<String, Object>> grabExcludeMaps) {
List<Statement> grabInitializers = new ArrayList<Statement>();
MapExpression basicArgs = new MapExpression();
if (autoDownload != null) {
basicArgs.addMapEntryExpression(constX(AUTO_DOWNLOAD_SETTING), constX(autoDownload));
}
if (disableChecksums != null) {
basicArgs.addMapEntryExpression(constX(DISABLE_CHECKSUMS_SETTING), constX(disableChecksums));
}
if (systemProperties != null && !systemProperties.isEmpty()) {
BlockStatement block = new BlockStatement();
for (Map.Entry e : systemProperties.entrySet()) {
block.addStatement(stmt(callX(SYSTEM_CLASSNODE, "setProperty", args(constX(e.getKey()), constX(e.getValue())))));
}
StaticMethodCallExpression enabled = callX(SYSTEM_CLASSNODE, "getProperty", args(constX("groovy.grape.enable"), constX("true")));
grabInitializers.add(ifS(eqX(enabled, constX("true")), block));
}
if (!grabExcludeMaps.isEmpty()) {
ListExpression list = new ListExpression();
for (Map<String, Object> map : grabExcludeMaps) {
Set<Map.Entry<String, Object>> entries = map.entrySet();
MapExpression inner = new MapExpression();
for (Map.Entry<String, Object> entry : entries) {
inner.addMapEntryExpression(constX(entry.getKey()), constX(entry.getValue()));
}
list.addExpression(inner);
}
basicArgs.addMapEntryExpression(constX("excludes"), list);
}
List<Expression> argList = new ArrayList<Expression>();
argList.add(basicArgs);
if (grabMapsInit.isEmpty())
return;
for (Map<String, Object> grabMap : grabMapsInit) {
// add Grape.grab(excludeArgs, [group:group, module:module, version:version, classifier:classifier])
// or Grape.grab([group:group, module:module, version:version, classifier:classifier])
MapExpression dependencyArg = new MapExpression();
for (String s : GRAB_REQUIRED) {
dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
}
for (String s : GRAB_OPTIONAL) {
if (grabMap.containsKey(s))
dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
}
argList.add(dependencyArg);
}
grabInitializers.add(stmt(callX(grapeClassNode, "grab", args(argList))));
// insert at beginning so we have the classloader set up before the class is called
classNode.addStaticInitializerStatements(grabInitializers, true);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class GrabAnnotationTransformation method addInitContextClassLoaderIfNeeded.
private void addInitContextClassLoaderIfNeeded(ClassNode classNode) {
if (initContextClassLoader) {
Statement initStatement = stmt(callX(callX(THREAD_CLASSNODE, "currentThread"), "setContextClassLoader", callX(callThisX("getClass"), "getClassLoader")));
classNode.addObjectInitializerStatements(initStatement);
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AutoCloneASTTransformation method createCloneCopyConstructor.
private static void createCloneCopyConstructor(ClassNode cNode, List<FieldNode> list, List<String> excludes) {
if (cNode.getDeclaredConstructors().isEmpty()) {
// add no-arg constructor
BlockStatement noArgBody = new BlockStatement();
noArgBody.addStatement(EmptyStatement.INSTANCE);
cNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, noArgBody);
}
boolean hasThisCons = false;
for (ConstructorNode consNode : cNode.getDeclaredConstructors()) {
Parameter[] parameters = consNode.getParameters();
if (parameters.length == 1 && parameters[0].getType().equals(cNode)) {
hasThisCons = true;
}
}
if (!hasThisCons) {
BlockStatement initBody = new BlockStatement();
Parameter initParam = param(GenericsUtils.nonGeneric(cNode), "other");
final Expression other = varX(initParam);
boolean hasParent = cNode.getSuperClass() != ClassHelper.OBJECT_TYPE;
if (hasParent) {
initBody.addStatement(stmt(ctorX(ClassNode.SUPER, other)));
}
for (FieldNode fieldNode : list) {
String name = fieldNode.getName();
if (excludes != null && excludes.contains(name))
continue;
ClassNode fieldType = fieldNode.getType();
Expression direct = propX(other, name);
Expression to = propX(varX("this"), name);
Statement assignDirect = assignS(to, direct);
Statement assignCloned = assignS(to, castX(fieldType, callCloneDirectX(direct)));
Statement assignClonedDynamic = assignS(to, castX(fieldType, callCloneDynamicX(direct)));
if (isCloneableType(fieldType)) {
initBody.addStatement(assignCloned);
} else if (!possiblyCloneable(fieldType)) {
initBody.addStatement(assignDirect);
} else {
initBody.addStatement(ifElseS(isInstanceOfX(direct, CLONEABLE_TYPE), assignClonedDynamic, assignDirect));
}
}
cNode.addConstructor(ACC_PROTECTED, params(initParam), ClassNode.EMPTY_ARRAY, initBody);
}
ClassNode[] exceptions = { make(CloneNotSupportedException.class) };
cNode.addMethod("clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, block(stmt(ctorX(cNode, args(varX("this"))))));
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AutoCloneASTTransformation method addSimpleCloneHelperMethod.
private static void addSimpleCloneHelperMethod(ClassNode cNode, List<FieldNode> fieldNodes, List<String> excludes) {
Parameter methodParam = new Parameter(GenericsUtils.nonGeneric(cNode), "other");
final Expression other = varX(methodParam);
boolean hasParent = cNode.getSuperClass() != ClassHelper.OBJECT_TYPE;
BlockStatement methodBody = new BlockStatement();
if (hasParent) {
methodBody.addStatement(stmt(callSuperX("cloneOrCopyMembers", args(other))));
}
for (FieldNode fieldNode : fieldNodes) {
String name = fieldNode.getName();
if (excludes != null && excludes.contains(name))
continue;
ClassNode fieldType = fieldNode.getType();
Expression direct = propX(varX("this"), name);
Expression to = propX(other, name);
Statement assignDirect = assignS(to, direct);
Statement assignCloned = assignS(to, castX(fieldType, callCloneDirectX(direct)));
Statement assignClonedDynamic = assignS(to, castX(fieldType, callCloneDynamicX(direct)));
if (isCloneableType(fieldType)) {
methodBody.addStatement(assignCloned);
} else if (!possiblyCloneable(fieldType)) {
methodBody.addStatement(assignDirect);
} else {
methodBody.addStatement(ifElseS(isInstanceOfX(direct, CLONEABLE_TYPE), assignClonedDynamic, assignDirect));
}
}
ClassNode[] exceptions = { make(CloneNotSupportedException.class) };
cNode.addMethod("cloneOrCopyMembers", ACC_PROTECTED, ClassHelper.VOID_TYPE, params(methodParam), exceptions, methodBody);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method ifStatement.
protected Statement ifStatement(AST ifNode) {
AST node = ifNode.getFirstChild();
assertNodeType(EXPR, node);
BooleanExpression booleanExpression = booleanExpression(node);
node = node.getNextSibling();
Statement ifBlock = statement(node);
Statement elseBlock = EmptyStatement.INSTANCE;
node = node.getNextSibling();
if (node != null) {
elseBlock = statement(node);
}
IfStatement ifStatement = new IfStatement(booleanExpression, ifBlock, elseBlock);
configureAST(ifStatement, ifNode);
return ifStatement;
}
Aggregations