org.codehaus.groovy.transform.ASTTestTransformation.groovy Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.transform
import groovy.transform.CompilationUnitAware
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.AnnotationNode
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.Parameter
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.PropertyExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.ast.stmt.EmptyStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.ErrorCollector
import org.codehaus.groovy.control.Janitor
import org.codehaus.groovy.control.ProcessingUnit
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.control.customizers.ImportCustomizer
import org.codehaus.groovy.control.io.ReaderSource
import org.codehaus.groovy.runtime.MethodClosure
import org.codehaus.groovy.syntax.SyntaxException
import org.codehaus.groovy.tools.Utilities
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
class ASTTestTransformation implements ASTTransformation, CompilationUnitAware {
CompilationUnit compilationUnit
@Override
void visit(final ASTNode[] nodes, final SourceUnit source) {
AnnotationNode annotationNode = nodes[0]
def member = annotationNode.getMember('phase')
CompilePhase phase = null
if (member) {
if (member instanceof VariableExpression) {
phase = CompilePhase.valueOf(member.text)
} else if (member instanceof PropertyExpression) {
phase = CompilePhase.valueOf(member.propertyAsString)
}
annotationNode.setMember('phase', propX(classX(ClassHelper.make(CompilePhase)), phase.toString()))
// GRECLIPSE add
PropertyExpression p = annotationNode.getMember('phase')
if (member instanceof VariableExpression) {
p.getProperty().setSourcePosition(member)
p.setStart(member.getStart())
p.setEnd(member.getEnd())
} else if (member instanceof PropertyExpression) {
ClassNode type = ClassHelper.makeWithoutCaching(CompilePhase.name)
type.setRedirect(p.getObjectExpression().getType())
p.getObjectExpression().setType(type)
p.getObjectExpression().setSourcePosition(member.objectExpression)
p.getProperty().setSourcePosition(member.property)
p.setSourcePosition(member)
}
// GRECLIPSE end
}
member = annotationNode.getMember('value')
if (member && !(member instanceof ClosureExpression)) {
throw new SyntaxException('ASTTest value must be a closure', member.lineNumber, member.columnNumber)
}
if (!member && !annotationNode.getNodeMetaData(ASTTestTransformation)) {
throw new SyntaxException('Missing test expression', annotationNode.lineNumber, annotationNode.columnNumber)
}
// convert value into node metadata so that the expression doesn't mix up with other AST xforms like STC
annotationNode.setNodeMetaData(ASTTestTransformation, member)
/* GRECLIPSE edit
annotationNode.members.remove('value')
*/
new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE).with {
annotationNode.setMember('value', it)
setStart(member.getStart())
setEnd(member.getEnd())
}
member.variableScope.@parent = null
// GRECLIPSE end
def pcallback = compilationUnit.progressCallback
def callback = new CompilationUnit.ProgressCallback() {
private final Binding binding = new Binding([:].withDefault { null })
@Override
void call(final ProcessingUnit context, final int phaseNumber) {
if (phase == null || phaseNumber == phase.phaseNumber) {
ClosureExpression testClosure = nodes[0].getNodeMetaData(ASTTestTransformation)
StringBuilder sb = new StringBuilder()
for (int i = testClosure.lineNumber; i <= testClosure.lastLineNumber; i += 1) {
sb.append(source.source.getLine(i, new Janitor())).append('\n')
}
def testSource = sb[testClosure.columnNumber.. 0) {
String marker = Utilities.repeatString(' ', column - 1) + '^'
if (column > 40) {
int start = column - 30 - 1
int end = (column + 10 > text.length() ? text.length() : column + 10 - 1)
sample = ' ' + text[start.. chain = [] as LinkedList
ProgressCallbackChain(final CompilationUnit.ProgressCallback... callbacks) {
if (callbacks) {
callbacks.each { addCallback(it) }
}
}
void addCallback(final CompilationUnit.ProgressCallback callback) {
chain << callback
}
@Override
void call(final ProcessingUnit context, final int phase) {
chain*.call(context, phase)
}
}
static class LabelFinder extends ClassCodeVisitorSupport {
static List lookup(final MethodNode node, final String label) {
LabelFinder finder = new LabelFinder(label, null)
node.code.visit(finder)
finder.targets
}
static List lookup(final ClassNode node, final String label) {
LabelFinder finder = new LabelFinder(label, null)
node.methods*.code*.visit(finder)
node.declaredConstructors*.code*.visit(finder)
finder.targets
}
private final String label
private final SourceUnit unit
private final List targets = [] as LinkedList
LabelFinder(final String label, final SourceUnit unit) {
this.label = label
this.unit = unit
}
@Override
protected SourceUnit getSourceUnit() {
unit
}
@Override
protected void visitStatement(final Statement statement) {
super.visitStatement(statement)
if (label in statement.statementLabels) targets << statement
}
List getTargets() {
Collections.unmodifiableList(targets)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy