|
Groovy example source code file (AstBuilderTransformation.java)
The Groovy AstBuilderTransformation.java source code
/*
* Copyright 2003-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.ast.builder;
import groovy.lang.MissingPropertyException;
import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.io.ReaderSource;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.ASTTransformation;
import org.codehaus.groovy.transform.GroovyASTTransformation;
import java.util.ArrayList;
import java.util.List;
/**
* Transformation to capture ASTBuilder from code statements.
* <p/>
* The AstBuilder "from code" approach is used with a single Closure
* parameter. This transformation converts the ClosureExpression back
* into source code and rewrites the AST so that the "from string"
* builder is invoked on the source. In order for this to work, the
* closure source must be given a goto label. It is the "from string"
* approach's responsibility to remove the BlockStatement created
* by the label.
*
* @author Hamlet D'Arcy
*/
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
public class AstBuilderTransformation implements ASTTransformation {
public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
// todo : are there other import types that can be specified?
AstBuilderInvocationTrap transformer = new AstBuilderInvocationTrap(
sourceUnit.getAST().getImports(),
sourceUnit.getAST().getStarImports(),
sourceUnit.getSource(),
sourceUnit
);
if (nodes != null) {
for (ASTNode it : nodes) {
if (!(it instanceof AnnotationNode) && !(it instanceof ClassNode)) {
it.visit(transformer);
}
}
}
if (sourceUnit.getAST() != null) {
sourceUnit.getAST().visit(transformer);
if (sourceUnit.getAST().getStatementBlock() != null) {
sourceUnit.getAST().getStatementBlock().visit(transformer);
}
if (sourceUnit.getAST().getClasses() != null) {
for (ClassNode classNode : sourceUnit.getAST().getClasses()) {
if (classNode.getMethods() != null) {
for (MethodNode node : classNode.getMethods()) {
if (node != null && node.getCode() != null) {
node.getCode().visit(transformer);
}
}
}
try {
if (classNode.getDeclaredConstructors() != null) {
for (MethodNode node : classNode.getDeclaredConstructors()) {
if (node != null && node.getCode() != null) {
node.getCode().visit(transformer);
}
}
}
} catch (MissingPropertyException ignored) {
// todo: inner class nodes don't have a constructors field available
}
// all properties are also always fields
if (classNode.getFields() != null) {
for (FieldNode node : classNode.getFields()) {
if (node.getInitialValueExpression() != null) {
node.getInitialValueExpression().visit(transformer);
}
}
}
try {
if (classNode.getObjectInitializerStatements() != null) {
for (Statement node : classNode.getObjectInitializerStatements()) {
if (node != null) {
node.visit(transformer);
}
}
}
} catch (MissingPropertyException ignored) {
// todo: inner class nodes don't have a objectInitializers field available
}
// todo: is there anything to do with the module ???
}
}
if (sourceUnit.getAST().getMethods() != null) {
for (MethodNode node : sourceUnit.getAST().getMethods()) {
if (node != null) {
if (node.getParameters() != null) {
for (Parameter parameter : node.getParameters()) {
if (parameter != null && parameter.getInitialExpression() != null) {
parameter.getInitialExpression().visit(transformer);
}
}
}
if (node.getCode() != null) {
node.getCode().visit(transformer);
}
}
}
}
}
}
/**
* This class traps invocations of AstBuilder.build(CompilePhase, boolean, Closure) and converts
* the contents of the closure into expressions by reading the source of the Closure and sending
* that as a String to AstBuilder.build(String, CompilePhase, boolean) at runtime.
*/
private static class AstBuilderInvocationTrap extends CodeVisitorSupport {
private final List<String> factoryTargets = new ArrayList
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy AstBuilderTransformation.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.