use of org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment in project n4js by eclipse.
the class TypeXpectMethod method getTypeArgumentsString.
private String getTypeArgumentsString(IEObjectCoveringRegion offset) {
final EObject eobject = offset != null ? offset.getEObject() : null;
final EObject container = eobject != null ? eobject.eContainer() : null;
if (eobject == null || !(container instanceof ParameterizedCallExpression && ((ParameterizedCallExpression) container).getTarget() == eobject)) {
// missing or invalid offset
return "xpect method error: offset not given or does not point to target of a call expression";
}
if (!(eobject.eResource() instanceof N4JSResource)) {
return "xpect method error: offset does not point to an EObject contained in a N4JSResource";
}
// offset points to the target of a call expression
final ParameterizedCallExpression callExpr = (ParameterizedCallExpression) container;
final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(eobject);
final Result<TypeRef> targetTypeRef = ts.type(G, callExpr.getTarget());
if (targetTypeRef.failed() || !(targetTypeRef.getValue() instanceof FunctionTypeExprOrRef)) {
return "xpect method error: cannot infer type of call expression target OR it's not a FunctionTypeExprOrRef";
}
final List<TypeVariable> typeParams = ((FunctionTypeExprOrRef) targetTypeRef.getValue()).getTypeVars();
// not interested in the actual typeParams, just the size
final int expectedNumOfTypeArgs = typeParams.size();
final List<TypeRef> typeArgs;
if (callExpr.getTypeArgs().isEmpty()) {
// no type arguments given in call expression -> use inferred type arguments
// (should be the standard case when testing)
final List<TypeRef> inferredTypeArgs = ASTMetaInfoUtils.getInferredTypeArgs(callExpr);
if (inferredTypeArgs != null) {
typeArgs = inferredTypeArgs;
} else {
typeArgs = Collections.emptyList();
}
} else {
// call expression is parameterized -> use the explicitly given type arguments
// (just provided for completeness)
typeArgs = callExpr.getTypeArgs();
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < expectedNumOfTypeArgs; i++) {
final TypeRef inferredTypeArg = i < typeArgs.size() ? typeArgs.get(i) : null;
if (sb.length() > 0)
sb.append(", ");
if (inferredTypeArg != null)
sb.append(inferredTypeArg.getTypeRefAsString());
else
sb.append("*missing*");
}
return sb.toString();
}
Aggregations