use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.
the class ContextualClassCodeVisitor method pathUpTo.
public List<TreeContext> pathUpTo(Class<ASTNode> node, ASTNodePredicate predicate) {
List<TreeContext> path = new LinkedList<TreeContext>();
TreeContext current = lastContext;
boolean found = false;
while (current != null && !found) {
path.add(current);
ASTNode currentNode = current.node;
if (node == null) {
if (predicate.matches(currentNode)) {
found = true;
}
} else {
if (predicate == null) {
if (currentNode == null || node == currentNode.getClass()) {
found = true;
}
} else {
found = currentNode != null && node == currentNode.getClass() && predicate.matches(currentNode);
}
}
current = current.parent;
}
if (found) {
return path;
}
return Collections.emptyList();
}
use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.
the class GenericsUtils method resolveClassNode.
private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode) {
ClassNode dummyClass = new ClassNode("dummy", 0, ClassHelper.OBJECT_TYPE);
dummyClass.setModule(new ModuleNode(sourceUnit));
dummyClass.setGenericsTypes(mn.getDeclaringClass().getGenericsTypes());
MethodNode dummyMN = new MethodNode("dummy", 0, parsedNode, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
dummyMN.setGenericsTypes(mn.getGenericsTypes());
dummyClass.addMethod(dummyMN);
ResolveVisitor visitor = new ResolveVisitor(compilationUnit) {
@Override
public void addError(final String msg, final ASTNode expr) {
sourceUnit.addError(new IncorrectTypeHintException(mn, msg, usage.getLineNumber(), usage.getColumnNumber()));
}
};
visitor.startResolving(dummyClass, sourceUnit);
return dummyMN.getReturnType();
}
use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.
the class AbstractTypeCheckingExtension method makeDynamic.
/**
* Used to instruct the type checker that the call is a dynamic method call.
* Calling this method automatically sets the handled flag to true.
* @param call the method call which is a dynamic method call
* @param returnType the expected return type of the dynamic call
* @return a virtual method node with the same name as the expected call
*/
public MethodNode makeDynamic(MethodCall call, ClassNode returnType) {
TypeCheckingContext.EnclosingClosure enclosingClosure = context.getEnclosingClosure();
MethodNode enclosingMethod = context.getEnclosingMethod();
((ASTNode) call).putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, returnType);
if (enclosingClosure != null) {
enclosingClosure.getClosureExpression().putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
} else {
enclosingMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
}
setHandled(true);
if (debug) {
LOG.info("Turning " + call.getText() + " into a dynamic method call returning " + returnType.toString(false));
}
return new MethodNode(call.getMethodAsString(), 0, returnType, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
}
use of org.codehaus.groovy.ast.ASTNode in project groovy by apache.
the class NotYetImplementedASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
ASTNode node = nodes[1];
if (!(node instanceof MethodNode)) {
addError("@NotYetImplemented must only be applied on test methods!", node);
return;
}
MethodNode methodNode = (MethodNode) node;
ArrayList<Statement> statements = new ArrayList<Statement>();
Statement statement = methodNode.getCode();
if (statement instanceof BlockStatement) {
statements.addAll(((BlockStatement) statement).getStatements());
}
if (statements.isEmpty())
return;
BlockStatement rewrittenMethodCode = new BlockStatement();
rewrittenMethodCode.addStatement(tryCatchAssertionFailedError(annotationNode, methodNode, statements));
rewrittenMethodCode.addStatement(throwAssertionFailedError(annotationNode));
methodNode.setCode(rewrittenMethodCode);
}
use of org.codehaus.groovy.ast.ASTNode in project intellij-community by JetBrains.
the class GroovyCompilerWrapper method addErrorMessage.
private void addErrorMessage(GroovyRuntimeException exception) {
ASTNode astNode = exception.getNode();
ModuleNode module = exception.getModule();
if (module == null) {
module = findModule(astNode);
}
String moduleName = module == null ? "<no module>" : module.getDescription();
int lineNumber = astNode == null ? -1 : astNode.getLineNumber();
int columnNumber = astNode == null ? -1 : astNode.getColumnNumber();
collector.add(new CompilerMessage(GroovyCompilerMessageCategories.ERROR, getExceptionMessage(exception), moduleName, lineNumber, columnNumber));
}
Aggregations