use of org.codehaus.groovy.ast.expr.MapExpression in project grails-core by grails.
the class TestMixinTransformation method addTestRuntimeAwareMixinFieldIfNonExistent.
protected FieldNode addTestRuntimeAwareMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) {
if (classNode == null || classNode.getField(fieldName) != null) {
return null;
}
MapExpression constructorArguments = new MapExpression();
constructorArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("testClass"), new ClassExpression(classNode)));
FieldNode mixinInstanceFieldNode = classNode.addField(fieldName, Modifier.STATIC, fieldType, new ConstructorCallExpression(fieldType, constructorArguments));
mixinInstanceFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(MixinInstance.class)));
addJunitRuleFields(classNode);
return mixinInstanceFieldNode;
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy by apache.
the class MacroInvocationTrap method handleTargetMethodCallExpression.
@Override
protected boolean handleTargetMethodCallExpression(MethodCallExpression macroCall) {
final ClosureExpression closureExpression = getClosureArgument(macroCall);
if (closureExpression == null) {
return true;
}
if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
addError("Macro closure arguments are not allowed", closureExpression);
return true;
}
final MapExpression mapExpression = buildSubstitutionMap(closureExpression);
String source = convertClosureToSource(closureExpression);
BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
Boolean asIs = false;
TupleExpression macroArguments = getMacroArguments(macroCall);
if (macroArguments == null) {
return true;
}
List<Expression> macroArgumentsExpressions = macroArguments.getExpressions();
if (macroArgumentsExpressions.size() == 2 || macroArgumentsExpressions.size() == 3) {
Expression asIsArgumentExpression = macroArgumentsExpressions.get(macroArgumentsExpressions.size() - 2);
if ((asIsArgumentExpression instanceof ConstantExpression)) {
ConstantExpression asIsConstantExpression = (ConstantExpression) asIsArgumentExpression;
if (!(asIsConstantExpression.getValue() instanceof Boolean)) {
addError("AsIs argument value should be boolean", asIsConstantExpression);
return true;
}
asIs = (Boolean) asIsConstantExpression.getValue();
}
}
macroArgumentsExpressions.remove(macroArgumentsExpressions.size() - 1);
macroArgumentsExpressions.add(new ConstantExpression(source));
macroArgumentsExpressions.add(mapExpression);
macroArgumentsExpressions.add(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false)));
macroCall.setObjectExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"));
macroCall.setSpreadSafe(false);
macroCall.setSafe(false);
macroCall.setImplicitThis(false);
return true;
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy by apache.
the class MacroInvocationTrap method buildSubstitutionMap.
private MapExpression buildSubstitutionMap(final ASTNode expr) {
final Map<MacroSubstitutionKey, ClosureExpression> map = new HashMap<MacroSubstitutionKey, ClosureExpression>();
final MapExpression mapExpression = new MapExpression();
ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
@Override
protected SourceUnit getSourceUnit() {
return null;
}
@Override
public void visitClass(final ClassNode node) {
super.visitClass(node);
Iterator<InnerClassNode> it = node.getInnerClasses();
while (it.hasNext()) {
InnerClassNode next = it.next();
visitClass(next);
}
}
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
super.visitMethodCallExpression(call);
if (isBuildInvocation(call, MacroTransformation.DOLLAR_VALUE)) {
ClosureExpression substitutionClosureExpression = getClosureArgument(call);
if (substitutionClosureExpression == null) {
return;
}
Statement code = substitutionClosureExpression.getCode();
if (code instanceof BlockStatement) {
((BlockStatement) code).setVariableScope(null);
}
MacroSubstitutionKey key = new MacroSubstitutionKey(call, expr.getLineNumber(), expr.getColumnNumber());
map.put(key, substitutionClosureExpression);
}
}
};
if (expr instanceof ClassNode) {
visitor.visitClass((ClassNode) expr);
} else {
expr.visit(visitor);
}
for (Map.Entry<MacroSubstitutionKey, ClosureExpression> entry : map.entrySet()) {
mapExpression.addMapEntryExpression(entry.getKey().toConstructorCallExpression(), entry.getValue());
}
return mapExpression;
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy by apache.
the class GrabAnnotationTransformation method addGrabResolverAsStaticInitIfNeeded.
private static void addGrabResolverAsStaticInitIfNeeded(ClassNode grapeClassNode, AnnotationNode node, List<Statement> grabResolverInitializers, Map<String, Object> grabResolverMap) {
if ((node.getMember("initClass") == null) || (node.getMember("initClass") == ConstantExpression.TRUE)) {
MapExpression resolverArgs = new MapExpression();
for (Map.Entry<String, Object> next : grabResolverMap.entrySet()) {
resolverArgs.addMapEntryExpression(constX(next.getKey()), constX(next.getValue()));
}
grabResolverInitializers.add(stmt(callX(grapeClassNode, "addResolver", args(resolverArgs))));
}
}
use of org.codehaus.groovy.ast.expr.MapExpression in project groovy by apache.
the class ClassNodeUtils method hasPossibleStaticMethod.
/**
* Returns true if the given method has a possibly matching static method with the given name and arguments.
*
* @param cNode the ClassNode of interest
* @param name the name of the method of interest
* @param arguments the arguments to match against
* @param trySpread whether to try to account for SpreadExpressions within the arguments
* @return true if a matching method was found
*/
public static boolean hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread) {
int count = 0;
boolean foundSpread = false;
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
for (Expression arg : tuple.getExpressions()) {
if (arg instanceof SpreadExpression) {
foundSpread = true;
} else {
count++;
}
}
} else if (arguments instanceof MapExpression) {
count = 1;
}
for (MethodNode method : cNode.getMethods(name)) {
if (method.isStatic()) {
Parameter[] parameters = method.getParameters();
// do fuzzy match for spread case: count will be number of non-spread args
if (trySpread && foundSpread && parameters.length >= count)
return true;
if (parameters.length == count)
return true;
// handle varargs case
if (parameters.length > 0 && parameters[parameters.length - 1].getType().isArray()) {
if (count >= parameters.length - 1)
return true;
// fuzzy match any spread to a varargs
if (trySpread && foundSpread)
return true;
}
// handle parameters with default values
int nonDefaultParameters = 0;
for (Parameter parameter : parameters) {
if (!parameter.hasInitialExpression()) {
nonDefaultParameters++;
}
}
if (count < parameters.length && nonDefaultParameters <= count) {
return true;
}
// TODO handle spread with nonDefaultParams?
}
}
return false;
}
Aggregations