use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method visitPrefixOrPostifExpression.
private void visitPrefixOrPostifExpression(final Expression origin, final Expression innerExpression, final int operationType) {
boolean isPostfix = origin instanceof PostfixExpression;
ClassNode exprType = getType(innerExpression);
String name = operationType == PLUS_PLUS ? "next" : operationType == MINUS_MINUS ? "previous" : null;
if (isPrimitiveType(exprType) || isPrimitiveType(getUnwrapper(exprType))) {
if (operationType == PLUS_PLUS || operationType == MINUS_MINUS) {
if (!isPrimitiveType(exprType)) {
MethodNode node = findMethodOrFail(new VariableExpression("_dummy_", exprType), exprType, name);
if (node != null) {
storeTargetMethod(origin, node);
storeType(origin, isPostfix ? exprType : getMathWideningClassNode(exprType));
return;
}
}
storeType(origin, exprType);
return;
}
addUnsupportedPreOrPostfixExpressionError(origin);
return;
} else if (implementsInterfaceOrIsSubclassOf(exprType, Number_TYPE) && (operationType == PLUS_PLUS || operationType == MINUS_MINUS)) {
// special case for numbers, improve type checking as we can expect ++ and -- to return the same type
MethodNode node = findMethodOrFail(innerExpression, exprType, name);
if (node != null) {
storeTargetMethod(origin, node);
storeType(origin, getMathWideningClassNode(exprType));
return;
}
}
// not a primitive type. We must find a method which is called next
if (name == null) {
addUnsupportedPreOrPostfixExpressionError(origin);
return;
}
MethodNode node = findMethodOrFail(innerExpression, exprType, name);
if (node != null) {
storeTargetMethod(origin, node);
storeType(origin, isPostfix ? exprType : inferReturnTypeGenerics(exprType, node, ArgumentListExpression.EMPTY_ARGUMENTS));
}
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method prettyPrintMethodList.
protected static String prettyPrintMethodList(List<MethodNode> nodes) {
StringBuilder sb = new StringBuilder("[");
for (int i = 0, nodesSize = nodes.size(); i < nodesSize; i++) {
final MethodNode node = nodes.get(i);
sb.append(node.getReturnType().toString(false));
sb.append(" ");
sb.append(node.getDeclaringClass().toString(false));
sb.append("#");
sb.append(toMethodParametersString(node.getName(), extractTypesFromParameters(node.getParameters())));
if (i < nodesSize - 1)
sb.append(", ");
}
sb.append("]");
return sb.toString();
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class SynchronizedASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
String value = getMemberStringValue(node, "value");
if (parent instanceof MethodNode) {
MethodNode mNode = (MethodNode) parent;
if (mNode.isAbstract()) {
addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
return;
}
ClassNode cNode = mNode.getDeclaringClass();
String lockExpr = determineLock(value, cNode, mNode);
if (lockExpr == null)
return;
Statement origCode = mNode.getCode();
Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
mNode.setCode(newCode);
}
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method hasSetter.
@Deprecated
protected SetterInfo hasSetter(final PropertyExpression pexp) {
String propertyName = pexp.getPropertyAsString();
if (propertyName == null)
return null;
Expression objectExpression = pexp.getObjectExpression();
List<Receiver<String>> receivers = new LinkedList<Receiver<String>>();
List<Receiver<String>> owners = makeOwnerList(objectExpression);
addReceivers(receivers, owners, pexp.isImplicitThis());
String capName = MetaClassHelper.capitalize(propertyName);
boolean isAttributeExpression = pexp instanceof AttributeExpression;
for (Receiver<String> receiver : receivers) {
ClassNode testClass = receiver.getType();
LinkedList<ClassNode> queue = new LinkedList<ClassNode>();
queue.add(testClass);
if (testClass.isInterface()) {
queue.addAll(testClass.getAllInterfaces());
}
while (!queue.isEmpty()) {
ClassNode current = queue.removeFirst();
current = current.redirect();
// check that a setter also exists
String setterName = "set" + capName;
List<MethodNode> setterMethods = findSetters(current, setterName, false);
if (!setterMethods.isEmpty()) {
// storeType(pexp, setterMethod.getParameters()[0].getType());
return new SetterInfo(current, setterName, setterMethods);
}
if (!isAttributeExpression && current.getSuperClass() != null) {
queue.add(current.getSuperClass());
}
}
}
return null;
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method disambiguateMethods.
private List<MethodNode> disambiguateMethods(List<MethodNode> methods, ClassNode receiver, ClassNode[] argTypes, final Expression expr) {
if (methods.size() > 1 && receiver != null && argTypes != null) {
List<MethodNode> filteredWithGenerics = new LinkedList<MethodNode>();
for (MethodNode methodNode : methods) {
if (typeCheckMethodsWithGenerics(receiver, argTypes, methodNode)) {
filteredWithGenerics.add(methodNode);
}
}
if (filteredWithGenerics.size() == 1) {
return filteredWithGenerics;
}
methods = extension.handleAmbiguousMethods(methods, expr);
}
return methods;
}
Aggregations