use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class InitializerStrategy method createBuilderMethod.
private static MethodNode createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName) {
final BlockStatement body = new BlockStatement();
body.addStatement(returnS(callX(builder, buildMethodName)));
ClassNode returnType = makeClassSafeWithGenerics(builder, unsetGenTypes(numFields));
return new MethodNode(builderMethodName, PUBLIC_STATIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body);
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method visitMethodCallExpression.
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
final String name = call.getMethodAsString();
if (name == null) {
addStaticTypeError("cannot resolve dynamic method name at compile time.", call.getMethod());
return;
}
if (extension.beforeMethodCall(call)) {
extension.afterMethodCall(call);
return;
}
typeCheckingContext.pushEnclosingMethodCall(call);
final Expression objectExpression = call.getObjectExpression();
objectExpression.visit(this);
call.getMethod().visit(this);
// the call is made on a collection type
if (call.isSpreadSafe()) {
//TODO check if this should not be change to iterator based call logic
ClassNode expressionType = getType(objectExpression);
if (!implementsInterfaceOrIsSubclassOf(expressionType, Collection_TYPE) && !expressionType.isArray()) {
addStaticTypeError("Spread operator can only be used on collection types", objectExpression);
return;
} else {
// type check call as if it was made on component type
ClassNode componentType = inferComponentType(expressionType, int_TYPE);
MethodCallExpression subcall = new MethodCallExpression(new CastExpression(componentType, EmptyExpression.INSTANCE), name, call.getArguments());
subcall.setLineNumber(call.getLineNumber());
subcall.setColumnNumber(call.getColumnNumber());
subcall.setImplicitThis(call.isImplicitThis());
visitMethodCallExpression(subcall);
// the inferred type here should be a list of what the subcall returns
ClassNode subcallReturnType = getType(subcall);
ClassNode listNode = LIST_TYPE.getPlainNodeReference();
listNode.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(subcallReturnType)) });
storeType(call, listNode);
// store target method
storeTargetMethod(call, (MethodNode) subcall.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
typeCheckingContext.popEnclosingMethodCall();
return;
}
}
final ClassNode rememberLastItType = typeCheckingContext.lastImplicitItType;
Expression callArguments = call.getArguments();
ArgumentListExpression argumentList = InvocationWriter.makeArgumentList(callArguments);
checkForbiddenSpreadArgument(argumentList);
// for arguments, we need to visit closures *after* the method has been chosen
final ClassNode receiver = getType(objectExpression);
visitMethodCallArguments(receiver, argumentList, false, null);
ClassNode[] args = getArgumentTypes(argumentList);
final boolean isCallOnClosure = isClosureCall(name, objectExpression, callArguments);
try {
boolean callArgsVisited = false;
if (isCallOnClosure) {
// this is a closure.call() call
if (objectExpression == VariableExpression.THIS_EXPRESSION) {
// isClosureCall() check verified earlier that a field exists
FieldNode field = typeCheckingContext.getEnclosingClassNode().getDeclaredField(name);
GenericsType[] genericsTypes = field.getType().getGenericsTypes();
if (genericsTypes != null) {
ClassNode closureReturnType = genericsTypes[0].getType();
Object data = field.getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS);
if (data != null) {
Parameter[] parameters = (Parameter[]) data;
typeCheckClosureCall(callArguments, args, parameters);
}
storeType(call, closureReturnType);
}
} else if (objectExpression instanceof VariableExpression) {
Variable variable = findTargetVariable((VariableExpression) objectExpression);
if (variable instanceof ASTNode) {
Object data = ((ASTNode) variable).getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS);
if (data != null) {
Parameter[] parameters = (Parameter[]) data;
typeCheckClosureCall(callArguments, args, parameters);
}
ClassNode type = getType(((ASTNode) variable));
if (type != null && type.equals(CLOSURE_TYPE)) {
GenericsType[] genericsTypes = type.getGenericsTypes();
type = OBJECT_TYPE;
if (genericsTypes != null) {
if (!genericsTypes[0].isPlaceholder()) {
type = genericsTypes[0].getType();
}
}
}
if (type != null) {
storeType(call, type);
}
}
} else if (objectExpression instanceof ClosureExpression) {
// we can get actual parameters directly
Parameter[] parameters = ((ClosureExpression) objectExpression).getParameters();
typeCheckClosureCall(callArguments, args, parameters);
ClassNode data = getInferredReturnType(objectExpression);
if (data != null) {
storeType(call, data);
}
}
int nbOfArgs = 0;
if (callArguments instanceof ArgumentListExpression) {
ArgumentListExpression list = (ArgumentListExpression) callArguments;
nbOfArgs = list.getExpressions().size();
} else {
// todo : other cases
nbOfArgs = 0;
}
storeTargetMethod(call, nbOfArgs == 0 ? CLOSURE_CALL_NO_ARG : nbOfArgs == 1 ? CLOSURE_CALL_ONE_ARG : CLOSURE_CALL_VARGS);
} else {
// method call receivers are :
// - possible "with" receivers
// - the actual receiver as found in the method call expression
// - any of the potential receivers found in the instanceof temporary table
// in that order
List<Receiver<String>> receivers = new LinkedList<Receiver<String>>();
List<Receiver<String>> owners = makeOwnerList(objectExpression);
addReceivers(receivers, owners, call.isImplicitThis());
List<MethodNode> mn = null;
Receiver<String> chosenReceiver = null;
for (Receiver<String> currentReceiver : receivers) {
mn = findMethod(currentReceiver.getType(), name, args);
// methods are only static if we are in a static context
if (!mn.isEmpty() && typeCheckingContext.isInStaticContext && (call.isImplicitThis() || (objectExpression instanceof VariableExpression && ((VariableExpression) objectExpression).isThisExpression()))) {
// we create a separate method list just to be able to print out
// a nice error message to the user
List<MethodNode> staticMethods = new LinkedList<MethodNode>();
List<MethodNode> nonStaticMethods = new LinkedList<MethodNode>();
for (final MethodNode node : mn) {
if (node.isStatic()) {
staticMethods.add(node);
} else {
nonStaticMethods.add(node);
}
}
mn = staticMethods;
if (staticMethods.isEmpty()) {
// choose an arbitrary method to display an error message
MethodNode node = nonStaticMethods.get(0);
ClassNode owner = node.getDeclaringClass();
addStaticTypeError("Non static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
}
}
if (!mn.isEmpty()) {
chosenReceiver = currentReceiver;
break;
}
}
if (mn.isEmpty() && typeCheckingContext.getEnclosingClosure() != null && args.length == 0) {
// add special handling of getDelegate() and getOwner()
if ("getDelegate".equals(name)) {
mn = Collections.singletonList(GET_DELEGATE);
} else if ("getOwner".equals(name)) {
mn = Collections.singletonList(GET_OWNER);
} else if ("getThisObject".equals(name)) {
mn = Collections.singletonList(GET_THISOBJECT);
}
}
if (mn.isEmpty()) {
mn = extension.handleMissingMethod(receiver, name, argumentList, args, call);
}
if (mn.isEmpty()) {
addNoMatchingMethodError(receiver, name, args, call);
} else {
if (areCategoryMethodCalls(mn, name, args)) {
addCategoryMethodCallError(call);
}
mn = disambiguateMethods(mn, chosenReceiver != null ? chosenReceiver.getType() : null, args, call);
if (mn.size() == 1) {
MethodNode directMethodCallCandidate = mn.get(0);
if (chosenReceiver == null) {
chosenReceiver = Receiver.make(directMethodCallCandidate.getDeclaringClass());
}
ClassNode returnType = getType(directMethodCallCandidate);
if (isUsingGenericsOrIsArrayUsingGenerics(returnType)) {
visitMethodCallArguments(chosenReceiver.getType(), argumentList, true, directMethodCallCandidate);
ClassNode irtg = inferReturnTypeGenerics(chosenReceiver.getType(), directMethodCallCandidate, callArguments, call.getGenericsTypes());
returnType = irtg != null && implementsInterfaceOrIsSubclassOf(irtg, returnType) ? irtg : returnType;
callArgsVisited = true;
}
if (directMethodCallCandidate == GET_DELEGATE && typeCheckingContext.getEnclosingClosure() != null) {
DelegationMetadata md = getDelegationMetadata(typeCheckingContext.getEnclosingClosure().getClosureExpression());
returnType = typeCheckingContext.getEnclosingClassNode();
if (md != null) {
returnType = md.getType();
}
}
if (typeCheckMethodsWithGenericsOrFail(chosenReceiver.getType(), args, mn.get(0), call)) {
returnType = adjustWithTraits(directMethodCallCandidate, chosenReceiver.getType(), args, returnType);
storeType(call, returnType);
storeTargetMethod(call, directMethodCallCandidate);
String data = chosenReceiver.getData();
if (data != null) {
// the method which has been chosen is supposed to be a call on delegate or owner
// so we store the information so that the static compiler may reuse it
call.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, data);
}
// if the object expression is a closure shared variable, we will have to perform a second pass
if (objectExpression instanceof VariableExpression) {
VariableExpression var = (VariableExpression) objectExpression;
if (var.isClosureSharedVariable()) {
SecondPassExpression<ClassNode[]> wrapper = new SecondPassExpression<ClassNode[]>(call, args);
typeCheckingContext.secondPassExpressions.add(wrapper);
}
}
}
} else {
addAmbiguousErrorMessage(mn, name, args, call);
}
}
}
// now that a method has been chosen, we are allowed to visit the closures
if (!callArgsVisited) {
MethodNode mn = (MethodNode) call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
visitMethodCallArguments(receiver, argumentList, true, mn);
// GROOVY-6219
if (mn != null) {
List<Expression> argExpressions = argumentList.getExpressions();
Parameter[] parameters = mn.getParameters();
for (int i = 0; i < argExpressions.size() && i < parameters.length; i++) {
Expression arg = argExpressions.get(i);
ClassNode pType = parameters[i].getType();
ClassNode aType = getType(arg);
if (CLOSURE_TYPE.equals(pType) && CLOSURE_TYPE.equals(aType)) {
if (!isAssignableTo(aType, pType)) {
addNoMatchingMethodError(receiver, name, getArgumentTypes(argumentList), call);
call.removeNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
}
}
}
}
}
} finally {
typeCheckingContext.popEnclosingMethodCall();
extension.afterMethodCall(call);
}
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method collectAllInterfaceMethodsByName.
protected void collectAllInterfaceMethodsByName(final ClassNode receiver, final String name, final List<MethodNode> methods) {
ClassNode[] interfaces = receiver.getInterfaces();
if (interfaces != null && interfaces.length > 0) {
for (ClassNode node : interfaces) {
List<MethodNode> intfMethods = node.getMethods(name);
methods.addAll(intfMethods);
collectAllInterfaceMethodsByName(node, name, methods);
}
}
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method getType.
protected ClassNode getType(ASTNode exp) {
ClassNode cn = exp.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (cn != null)
return cn;
if (exp instanceof ClassExpression) {
ClassNode node = CLASS_Type.getPlainNodeReference();
node.setGenericsTypes(new GenericsType[] { new GenericsType(((ClassExpression) exp).getType()) });
return node;
} else if (exp instanceof VariableExpression) {
VariableExpression vexp = (VariableExpression) exp;
if (vexp == VariableExpression.THIS_EXPRESSION)
return makeThis();
if (vexp == VariableExpression.SUPER_EXPRESSION)
return makeSuper();
ClassNode selfTrait = isTraitSelf(vexp);
if (selfTrait != null)
return makeSelf(selfTrait);
final Variable variable = vexp.getAccessedVariable();
if (variable instanceof FieldNode) {
checkOrMarkPrivateAccess(vexp, (FieldNode) variable);
return getType((FieldNode) variable);
}
if (variable != null && variable != vexp && variable instanceof VariableExpression) {
return getType((Expression) variable);
}
if (variable instanceof Parameter) {
Parameter parameter = (Parameter) variable;
ClassNode type = typeCheckingContext.controlStructureVariables.get(parameter);
TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
ClassNode[] closureParamTypes = (ClassNode[]) (enclosingClosure != null ? enclosingClosure.getClosureExpression().getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS) : null);
if (type == null && enclosingClosure != null && "it".equals(variable.getName()) && closureParamTypes != null) {
final Parameter[] parameters = enclosingClosure.getClosureExpression().getParameters();
if (parameters.length == 0 && getTemporaryTypesForExpression(vexp) == null) {
type = closureParamTypes[0];
}
}
if (type != null) {
storeType((VariableExpression) exp, type);
return type;
}
}
}
if (exp instanceof ListExpression) {
return inferListExpressionType((ListExpression) exp);
} else if (exp instanceof MapExpression) {
return inferMapExpressionType((MapExpression) exp);
}
if (exp instanceof ConstructorCallExpression) {
return ((ConstructorCallExpression) exp).getType();
}
if (exp instanceof MethodNode) {
if ((exp == GET_DELEGATE || exp == GET_OWNER || exp == GET_THISOBJECT) && typeCheckingContext.getEnclosingClosure() != null) {
return typeCheckingContext.getEnclosingClassNode();
}
ClassNode ret = getInferredReturnType(exp);
return ret != null ? ret : ((MethodNode) exp).getReturnType();
}
if (exp instanceof ClosureExpression) {
ClassNode irt = getInferredReturnType(exp);
if (irt != null) {
irt = wrapTypeIfNecessary(irt);
ClassNode result = CLOSURE_TYPE.getPlainNodeReference();
result.setGenericsTypes(new GenericsType[] { new GenericsType(irt) });
return result;
}
}
if (exp instanceof RangeExpression) {
ClassNode plain = ClassHelper.RANGE_TYPE.getPlainNodeReference();
RangeExpression re = (RangeExpression) exp;
ClassNode fromType = getType(re.getFrom());
ClassNode toType = getType(re.getTo());
if (fromType.equals(toType)) {
plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(fromType)) });
} else {
plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(lowestUpperBound(fromType, toType))) });
}
return plain;
}
if (exp instanceof UnaryPlusExpression) {
return getType(((UnaryPlusExpression) exp).getExpression());
}
if (exp instanceof UnaryMinusExpression) {
return getType(((UnaryMinusExpression) exp).getExpression());
}
if (exp instanceof BitwiseNegationExpression) {
return getType(((BitwiseNegationExpression) exp).getExpression());
}
if (exp instanceof MethodCall) {
MethodNode target = (MethodNode) exp.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (target != null) {
return getType(target);
}
}
if (exp instanceof Parameter) {
return ((Parameter) exp).getOriginType();
}
if (exp instanceof FieldNode) {
FieldNode fn = (FieldNode) exp;
return getGenericsResolvedTypeOfFieldOrProperty(fn, fn.getOriginType());
}
if (exp instanceof PropertyNode) {
PropertyNode pn = (PropertyNode) exp;
return getGenericsResolvedTypeOfFieldOrProperty(pn, pn.getOriginType());
}
return exp instanceof VariableExpression ? ((VariableExpression) exp).getOriginType() : ((Expression) exp).getType();
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method inferSAMType.
private void inferSAMType(Parameter param, ClassNode receiver, MethodNode methodWithSAMParameter, ArgumentListExpression originalMethodCallArguments, ClosureExpression openBlock) {
// In a method call with SAM coercion the inference is to be
// understood as a two phase process. We have the normal method call
// to the target method with the closure argument and we have the
// SAM method that will be called inside the normal target method.
// To infer correctly we have to "simulate" this process. We know the
// call to the closure will be done through the SAM type, so the SAM
// type generics deliver information about the Closure. At the same
// time the SAM class is used in the target method parameter,
// providing a connection from the SAM type and the target method
// declaration class.
// First we try to get as much information about the declaration
// class through the receiver
Map<String, GenericsType> targetMethodDeclarationClassConnections = new HashMap<String, GenericsType>();
extractGenericsConnections(targetMethodDeclarationClassConnections, receiver, receiver.redirect());
// then we use the method with the SAM parameter to get more information about the declaration
Parameter[] parametersOfMethodContainingSAM = methodWithSAMParameter.getParameters();
for (int i = 0; i < parametersOfMethodContainingSAM.length; i++) {
Expression callArg = originalMethodCallArguments.getExpression(i);
// we look at the closure later in detail, so skip it here
if (callArg == openBlock)
continue;
ClassNode parameterType = parametersOfMethodContainingSAM[i].getType();
extractGenericsConnections(targetMethodDeclarationClassConnections, getType(callArg), parameterType);
}
// To make a connection to the SAM class we use that new information
// to replace the generics in the SAM type parameter of the target
// method and than that to make the connections to the SAM type generics
ClassNode paramTypeWithReceiverInformation = applyGenericsContext(targetMethodDeclarationClassConnections, param.getOriginType());
Map<String, GenericsType> SAMTypeConnections = new HashMap<String, GenericsType>();
ClassNode classForSAM = paramTypeWithReceiverInformation.redirect();
extractGenericsConnections(SAMTypeConnections, paramTypeWithReceiverInformation, classForSAM);
// should the open block provide final information we apply that
// to the corresponding parameters of the SAM type method
MethodNode methodForSAM = findSAM(classForSAM);
ClassNode[] parameterTypesForSAM = extractTypesFromParameters(methodForSAM.getParameters());
ClassNode[] blockParameterTypes = (ClassNode[]) openBlock.getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS);
if (blockParameterTypes == null) {
Parameter[] p = openBlock.getParameters();
if (p.length == 0 && parameterTypesForSAM.length != 0) {
// implicit it
blockParameterTypes = parameterTypesForSAM;
} else {
blockParameterTypes = extractTypesFromParameters(p);
}
}
for (int i = 0; i < blockParameterTypes.length; i++) {
//TODO: equal length guaranteed?
extractGenericsConnections(SAMTypeConnections, blockParameterTypes[i], parameterTypesForSAM[i]);
}
// store the type of parameter and block type as meta information
for (int i = 0; i < blockParameterTypes.length; i++) {
//TODO: equal length guaranteed?
ClassNode resolvedParameter = applyGenericsContext(SAMTypeConnections, parameterTypesForSAM[i]);
blockParameterTypes[i] = resolvedParameter;
}
openBlock.putNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS, blockParameterTypes);
}
Aggregations