use of org.codehaus.groovy.ast.ClassCodeVisitorSupport in project groovy by apache.
the class ImmutableASTTransformation method doAddConstructor.
private static void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
cNode.addConstructor(constructorNode);
// GROOVY-5814: Immutable is not compatible with @CompileStatic
Parameter argsParam = null;
for (Parameter p : constructorNode.getParameters()) {
if ("args".equals(p.getName())) {
argsParam = p;
break;
}
}
if (argsParam != null) {
final Parameter arg = argsParam;
ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {
@Override
protected SourceUnit getSourceUnit() {
return cNode.getModule().getContext();
}
@Override
public void visitVariableExpression(final VariableExpression expression) {
super.visitVariableExpression(expression);
if ("args".equals(expression.getName())) {
expression.setAccessedVariable(arg);
}
}
};
variableExpressionFix.visitConstructor(constructorNode);
}
}
use of org.codehaus.groovy.ast.ClassCodeVisitorSupport in project groovy-core by groovy.
the class ImmutableASTTransformation method doAddConstructor.
private void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
cNode.addConstructor(constructorNode);
// GROOVY-5814: Immutable is not compatible with @CompileStatic
Parameter argsParam = null;
for (Parameter p : constructorNode.getParameters()) {
if ("args".equals(p.getName())) {
argsParam = p;
break;
}
}
if (argsParam != null) {
final Parameter arg = argsParam;
ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {
@Override
protected SourceUnit getSourceUnit() {
return cNode.getModule().getContext();
}
@Override
public void visitVariableExpression(final VariableExpression expression) {
super.visitVariableExpression(expression);
if ("args".equals(expression.getName())) {
expression.setAccessedVariable(arg);
}
}
};
variableExpressionFix.visitConstructor(constructorNode);
}
}
use of org.codehaus.groovy.ast.ClassCodeVisitorSupport in project grails-core by grails.
the class PluginAstReader method visitContents.
protected void visitContents(String className, final ClassNode classNode) {
ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
@Override
public void visitProperty(PropertyNode node) {
String name = node.getName();
final Expression expr = node.getField().getInitialExpression();
if (expr != null) {
Object value = null;
if (expr instanceof ListExpression) {
final List<String> list = new ArrayList<String>();
for (Expression i : ((ListExpression) expr).getExpressions()) {
list.add(i.getText());
}
value = list;
} else if (expr instanceof MapExpression) {
final Map<String, String> map = new LinkedHashMap<String, String>();
value = map;
for (MapEntryExpression mee : ((MapExpression) expr).getMapEntryExpressions()) {
map.put(mee.getKeyExpression().getText(), mee.getValueExpression().getText());
}
} else {
if (expr instanceof ConstantExpression) {
value = expr.getText();
}
}
if (value != null) {
pluginInfo.setProperty(name, value);
super.visitProperty(node);
}
}
}
@Override
protected SourceUnit getSourceUnit() {
return classNode.getModule().getContext();
}
};
classNode.visitContents(visitor);
pluginInfo.setName(GrailsNameUtils.getPluginName(className + ".groovy"));
}
use of org.codehaus.groovy.ast.ClassCodeVisitorSupport 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;
}
Aggregations