use of org.codehaus.groovy.ast.expr.MapExpression 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.expr.MapExpression in project groovy by apache.
the class ImmutableASTTransformation method createConstructorOrdered.
private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMap.
private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
final BlockStatement body = new BlockStatement();
body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
for (PropertyNode pNode : list) {
body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
}
// check for missing properties
body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
createConstructorMapCommon(cNode, body);
if (!list.isEmpty()) {
createNoArgConstructor(cNode);
}
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorOrdered.
private void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy-core by groovy.
the class MarkupBuilderCodeTransformer method tryTransformInclude.
private Expression tryTransformInclude(final MethodCallExpression exp) {
Expression arguments = exp.getArguments();
if (arguments instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
MapExpression map = (MapExpression) expressions.get(0);
List<MapEntryExpression> entries = map.getMapEntryExpressions();
if (entries.size() == 1) {
MapEntryExpression mapEntry = entries.get(0);
Expression keyExpression = mapEntry.getKeyExpression();
try {
IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
call.setImplicitThis(true);
call.setSafe(exp.isSafe());
call.setSpreadSafe(exp.isSpreadSafe());
call.setSourcePosition(exp);
return call;
} catch (IllegalArgumentException e) {
// not a valid import type, do not modify the code
}
}
}
}
return super.transform(exp);
}
Aggregations