use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GdkMethodUtil method createMethod.
@NotNull
private static GrMethod createMethod(@NotNull GrClosureSignature signature, @NotNull String name, @NotNull GrAssignmentExpression statement, @NotNull PsiClass closure) {
final GrLightMethodBuilder builder = new GrLightMethodBuilder(statement.getManager(), name);
GrClosureParameter[] parameters = signature.getParameters();
for (int i = 0; i < parameters.length; i++) {
GrClosureParameter parameter = parameters[i];
final String parameterName = parameter.getName() != null ? parameter.getName() : "p" + i;
final PsiType type = parameter.getType() != null ? parameter.getType() : TypesUtil.getJavaLangObject(statement);
builder.addParameter(parameterName, type, parameter.isOptional());
}
builder.setNavigationElement(statement.getLValue());
builder.setReturnType(signature.getReturnType());
builder.setContainingClass(closure);
return builder;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class SubstitutorComputer method inferMethodTypeParameters.
private PsiSubstitutor inferMethodTypeParameters(@NotNull PsiMethod method, @NotNull PsiSubstitutor partialSubstitutor, @NotNull PsiTypeParameter[] typeParameters, @NotNull PsiType[] argTypes) {
if (typeParameters.length == 0 || myArgumentTypes == null)
return partialSubstitutor;
final GrClosureSignature erasedSignature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor, true);
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor);
final GrClosureParameter[] params = signature.getParameters();
final GrClosureSignatureUtil.ArgInfo<PsiType>[] argInfos = GrClosureSignatureUtil.mapArgTypesToParameters(erasedSignature, argTypes, myPlace, true);
if (argInfos == null)
return partialSubstitutor;
int max = Math.max(params.length, argTypes.length);
PsiType[] parameterTypes = PsiType.createArray(max);
PsiType[] argumentTypes = PsiType.createArray(max);
int i = 0;
for (int paramIndex = 0; paramIndex < argInfos.length; paramIndex++) {
PsiType paramType = params[paramIndex].getType();
GrClosureSignatureUtil.ArgInfo<PsiType> argInfo = argInfos[paramIndex];
if (argInfo != null) {
if (argInfo.isMultiArg) {
if (paramType instanceof PsiArrayType)
paramType = ((PsiArrayType) paramType).getComponentType();
}
for (PsiType type : argInfo.args) {
argumentTypes[i] = handleConversion(paramType, type);
parameterTypes[i] = paramType;
i++;
}
} else {
parameterTypes[i] = paramType;
argumentTypes[i] = PsiType.NULL;
i++;
}
}
PsiSubstitutor substitutor = myHelper.inferTypeArguments(typeParameters, parameterTypes, argumentTypes, LanguageLevel.JDK_1_7);
for (PsiTypeParameter typeParameter : typeParameters) {
if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
substitutor = inferFromContext(typeParameter, PsiUtil.getSmartReturnType(method), substitutor);
if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
substitutor = substitutor.put(typeParameter, null);
}
}
}
return partialSubstitutor.putAll(substitutor);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GrMethodConflictUtil method checkForClosurePropertySignatureOverload.
private static void checkForClosurePropertySignatureOverload(PsiClass clazz, GrMethod prototype, final GrMethod refactoredMethod, final MultiMap<PsiElement, String> conflicts, final List<MethodSignature> prototypeSignatures) {
final boolean isStatic = prototype.hasModifierProperty(PsiModifier.STATIC);
final String name = prototype.getName();
if (!GroovyPropertyUtils.isProperty(clazz, name, isStatic))
return;
final PsiMethod getter = GroovyPropertyUtils.findPropertyGetter(clazz, name, isStatic, true);
final PsiType returnType;
if (getter instanceof GrMethod) {
returnType = ((GrMethod) getter).getInferredReturnType();
} else if (getter instanceof GrAccessorMethod) {
returnType = ((GrAccessorMethod) getter).getInferredReturnType();
} else {
return;
}
if (!(returnType instanceof GrClosureType))
return;
final GrSignature signature = ((GrClosureType) returnType).getSignature();
signature.accept(new GrRecursiveSignatureVisitor() {
@Override
public void visitClosureSignature(GrClosureSignature signature) {
NextSignature: for (MethodSignature prototypeSignature : prototypeSignatures) {
final GrClosureParameter[] params = signature.getParameters();
final PsiType[] types = prototypeSignature.getParameterTypes();
if (types.length != params.length)
continue;
for (int i = 0; i < types.length; i++) {
if (!TypesUtil.isAssignableByMethodCallConversion(types[i], params[i].getType(), refactoredMethod.getParameterList())) {
continue NextSignature;
}
}
conflicts.putValue(getter, GroovyRefactoringBundle.message("refactored.method.will.cover.closure.property", name, RefactoringUIUtil.getDescription(getter.getContainingClass(), false)));
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method updateUI.
@Override
public void updateUI(Object o, @NotNull ParameterInfoUIContext context) {
CodeInsightSettings settings = CodeInsightSettings.getInstance();
if (o == null)
return;
Object element;
if (o instanceof GroovyResolveResult) {
element = ((GroovyResolveResult) o).getElement();
if (element == null || !((PsiElement) element).isValid()) {
context.setUIComponentEnabled(false);
return;
}
} else if (o instanceof GrClosureSignature) {
if (!((GrClosureSignature) o).isValid()) {
context.setUIComponentEnabled(false);
return;
}
element = o;
} else {
return;
}
int highlightStartOffset = -1;
int highlightEndOffset = -1;
final int currentParameter = context.getCurrentParameterIndex();
StringBuilder buffer = new StringBuilder();
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
if (method instanceof GrReflectedMethod)
method = ((GrReflectedMethod) method).getBaseMethod();
if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
if (!method.isConstructor()) {
PsiType returnType = PsiUtil.getSmartReturnType(method);
if (returnType != null) {
buffer.append(returnType.getPresentableText());
buffer.append(' ');
}
}
buffer.append(method.getName());
buffer.append('(');
}
PsiParameter[] params = method.getParameterList().getParameters();
params = updateConstructorParams(method, params, context.getParameterOwner());
int numParams = params.length;
if (numParams > 0) {
LOG.assertTrue(o instanceof GroovyResolveResult, o.getClass());
final PsiSubstitutor substitutor = ((GroovyResolveResult) o).getSubstitutor();
for (int j = 0; j < numParams; j++) {
PsiParameter param = params[j];
int startOffset = buffer.length();
appendParameterText(param, substitutor, buffer);
int endOffset = buffer.length();
if (j < numParams - 1) {
buffer.append(", ");
}
if (context.isUIComponentEnabled() && (j == currentParameter || (j == numParams - 1 && param.isVarArgs() && currentParameter >= numParams))) {
highlightStartOffset = startOffset;
highlightEndOffset = endOffset;
}
}
} else {
buffer.append("no parameters");
}
if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
buffer.append(")");
}
} else if (element instanceof PsiClass) {
buffer.append("no parameters");
} else if (element instanceof GrClosureSignature) {
GrClosureParameter[] parameters = ((GrClosureSignature) element).getParameters();
if (parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
if (i > 0)
buffer.append(", ");
int startOffset = buffer.length();
final PsiType psiType = parameters[i].getType();
if (psiType == null) {
buffer.append("def");
} else {
buffer.append(psiType.getPresentableText());
}
buffer.append(' ').append(parameters[i].getName() != null ? parameters[i].getName() : "<unknown>");
int endOffset = buffer.length();
if (context.isUIComponentEnabled() && (i == currentParameter || (i == parameters.length - 1 && ((GrClosureSignature) element).isVarargs() && currentParameter >= parameters.length))) {
highlightStartOffset = startOffset;
highlightEndOffset = endOffset;
}
final GrExpression initializer = parameters[i].getDefaultInitializer();
if (initializer != null) {
buffer.append(" = ").append(initializer.getText());
}
}
} else {
buffer.append("no parameters");
}
}
final boolean isDeprecated = o instanceof PsiDocCommentOwner && ((PsiDocCommentOwner) o).isDeprecated();
context.setupUIComponentPresentation(buffer.toString(), highlightStartOffset, highlightEndOffset, !context.isUIComponentEnabled(), isDeprecated, false, context.getDefaultParameterColor());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method updateParameterInfo.
@Override
public void updateParameterInfo(@NotNull GroovyPsiElement place, @NotNull UpdateParameterInfoContext context) {
final PsiElement parameterOwner = context.getParameterOwner();
if (parameterOwner != place) {
context.removeHint();
return;
}
int offset = context.getEditor().getCaretModel().getOffset();
offset = CharArrayUtil.shiftForward(context.getEditor().getDocument().getText(), offset, " \t\n");
final int currIndex = getCurrentParameterIndex(place, offset);
context.setCurrentParameter(currIndex);
final Object[] objects = context.getObjectsToView();
Outer: for (int i = 0; i < objects.length; i++) {
PsiType[] parameterTypes = null;
PsiType[] argTypes = null;
PsiSubstitutor substitutor = null;
if (objects[i] instanceof GroovyResolveResult) {
final GroovyResolveResult resolveResult = (GroovyResolveResult) objects[i];
PsiNamedElement namedElement = (PsiNamedElement) resolveResult.getElement();
if (namedElement instanceof GrReflectedMethod)
namedElement = ((GrReflectedMethod) namedElement).getBaseMethod();
substitutor = resolveResult.getSubstitutor();
assert namedElement != null;
if (!namedElement.isValid()) {
context.setUIComponentEnabled(i, false);
continue Outer;
}
if (namedElement instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) namedElement;
PsiParameter[] parameters = method.getParameterList().getParameters();
parameters = updateConstructorParams(method, parameters, context.getParameterOwner());
parameterTypes = PsiType.createArray(parameters.length);
for (int j = 0; j < parameters.length; j++) {
parameterTypes[j] = parameters[j].getType();
}
argTypes = PsiUtil.getArgumentTypes(place, false);
}
if (argTypes == null)
continue;
} else if (objects[i] instanceof GrClosureSignature) {
final GrClosureSignature signature = (GrClosureSignature) objects[i];
argTypes = PsiUtil.getArgumentTypes(place, false);
parameterTypes = PsiType.createArray(signature.getParameterCount());
int j = 0;
for (GrClosureParameter parameter : signature.getParameters()) {
parameterTypes[j++] = parameter.getType();
}
} else {
continue Outer;
}
assert argTypes != null;
if (argTypes.length > currIndex) {
if (parameterTypes.length <= currIndex) {
context.setUIComponentEnabled(i, false);
continue;
} else {
for (int j = 0; j < currIndex; j++) {
PsiType argType = argTypes[j];
final PsiType paramType = substitutor != null ? substitutor.substitute(parameterTypes[j]) : parameterTypes[j];
if (!TypesUtil.isAssignableByMethodCallConversion(paramType, argType, place)) {
context.setUIComponentEnabled(i, false);
break Outer;
}
}
}
}
context.setUIComponentEnabled(i, true);
}
}
Aggregations