From 089ff659597bcd5d3d9036623ea0c69e2bd28e4a Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Thu, 15 Jun 2017 17:07:04 +0300 Subject: [PATCH] Introduce static-factory methods to PyCallableParameterImpl to avoid ambiguous PyCallableParameterImpl.new calls. --- .../python/PyParameterInfoHandler.java | 4 +- .../codeInsight/stdlib/PyNamedTupleType.java | 2 +- .../typing/PyTypingTypeProvider.java | 2 +- .../quickfix/PyRemoveParameterQuickFix.java | 2 +- .../python/psi/impl/ParamHelper.java | 6 +-- .../psi/impl/PyCallExpressionHelper.java | 6 +-- .../python/psi/impl/PyFunctionImpl.java | 2 +- .../psi/impl/PyLambdaExpressionImpl.java | 2 +- .../python/psi/impl/PyNamedParameterImpl.java | 6 +-- .../psi/types/PyCallableParameterImpl.java | 46 +++++++++++-------- .../python/psi/types/PyFunctionTypeImpl.java | 2 +- .../python/psi/types/PyTypeChecker.java | 4 +- .../python/psi/types/PyTypeParser.java | 4 +- 13 files changed, 49 insertions(+), 39 deletions(-) diff --git a/python/src/com/jetbrains/python/PyParameterInfoHandler.java b/python/src/com/jetbrains/python/PyParameterInfoHandler.java index 7ddc6fbf58a7..45e40ea2d7fe 100644 --- a/python/src/com/jetbrains/python/PyParameterInfoHandler.java +++ b/python/src/com/jetbrains/python/PyParameterInfoHandler.java @@ -340,7 +340,7 @@ public class PyParameterInfoHandler implements ParameterInfoHandler results = new ArrayList<>(); for (PyParameter component : parameter.getContents()) { if (component instanceof PyNamedParameter) { - results.add(new PyCallableParameterImpl(component)); + results.add(PyCallableParameterImpl.psi(component)); } else if (component instanceof PyTupleParameter) { results.addAll(getFlattenedTupleParameterComponents((PyTupleParameter)component)); @@ -392,7 +392,7 @@ public class PyParameterInfoHandler implements ParameterInfoHandler parameters = new ArrayList<>(); final PyListLiteralExpression listExpr = (PyListLiteralExpression)parametersExpr; for (PyExpression argExpr : listExpr.getElements()) { - parameters.add(new PyCallableParameterImpl(null, Ref.deref(getType(argExpr, context)))); + parameters.add(PyCallableParameterImpl.nonPsi(Ref.deref(getType(argExpr, context)))); } final PyType returnType = Ref.deref(getType(returnTypeExpr, context)); return new PyCallableTypeImpl(parameters, returnType); diff --git a/python/src/com/jetbrains/python/inspections/quickfix/PyRemoveParameterQuickFix.java b/python/src/com/jetbrains/python/inspections/quickfix/PyRemoveParameterQuickFix.java index 33ecb1178ced..6f51a54e22c8 100644 --- a/python/src/com/jetbrains/python/inspections/quickfix/PyRemoveParameterQuickFix.java +++ b/python/src/com/jetbrains/python/inspections/quickfix/PyRemoveParameterQuickFix.java @@ -53,7 +53,7 @@ public class PyRemoveParameterQuickFix implements LocalQuickFix { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PyParameter psi = PyUtil.as(descriptor.getPsiElement(), PyParameter.class); assert psi != null; - final PyCallableParameter parameter = new PyCallableParameterImpl(psi); + final PyCallableParameter parameter = PyCallableParameterImpl.psi(psi); final PyFunction function = PsiTreeUtil.getParentOfType(psi, PyFunction.class); if (function != null) { diff --git a/python/src/com/jetbrains/python/psi/impl/ParamHelper.java b/python/src/com/jetbrains/python/psi/impl/ParamHelper.java index 9321d433e9e3..01fe69b72972 100644 --- a/python/src/com/jetbrains/python/psi/impl/ParamHelper.java +++ b/python/src/com/jetbrains/python/psi/impl/ParamHelper.java @@ -41,7 +41,7 @@ public class ParamHelper { * @param walker the walker with callbacks. */ public static void walkDownParamArray(PyParameter[] params, ParamWalker walker) { - walkDownParameters(ContainerUtil.map(params, PyCallableParameterImpl::new), walker); + walkDownParameters(ContainerUtil.map(params, PyCallableParameterImpl::psi), walker); } public static void walkDownParameters(@NotNull List parameters, @NotNull ParamWalker walker) { @@ -74,7 +74,7 @@ public class ParamHelper { public static String getPresentableText(@NotNull PyParameter[] parameters, boolean includeDefaultValue, @Nullable TypeEvalContext context) { - return getPresentableText(ContainerUtil.map(parameters, PyCallableParameterImpl::new), includeDefaultValue, context); + return getPresentableText(ContainerUtil.map(parameters, PyCallableParameterImpl::psi), includeDefaultValue, context); } @NotNull @@ -100,7 +100,7 @@ public class ParamHelper { @Override public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) { - visitNonPsiParameter(new PyCallableParameterImpl(param), first, last); + visitNonPsiParameter(PyCallableParameterImpl.psi(param), first, last); } @Override diff --git a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java index 639715409a4b..bc967314fe81 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java +++ b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java @@ -1101,7 +1101,7 @@ public class PyCallExpressionHelper { final PyExpression arg = argumentComponents[i]; if (arg != null) { if (param instanceof PyNamedParameter) { - mappedParameters.put(arg, new PyCallableParameterImpl(param)); + mappedParameters.put(arg, PyCallableParameterImpl.psi(param)); } else if (param instanceof PyTupleParameter) { final TupleMappingResults nestedResults = mapComponentsOfTupleParameter(arg, (PyTupleParameter)param); @@ -1114,11 +1114,11 @@ public class PyCallExpressionHelper { } } else { - unmappedParameters.add(new PyCallableParameterImpl(param)); + unmappedParameters.add(PyCallableParameterImpl.psi(param)); } } else { - unmappedParameters.add(new PyCallableParameterImpl(param)); + unmappedParameters.add(PyCallableParameterImpl.psi(param)); } } if (argumentComponents.length > parameterComponents.length) { diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java index 61125049813e..ec40da9ecaa1 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java @@ -170,7 +170,7 @@ public class PyFunctionImpl extends PyBaseElementImpl implements .filter(PyCallableType.class::isInstance) .map(PyCallableType.class::cast) .map(callableType -> callableType.getParameters(context)) - .orElseGet(() -> ContainerUtil.map(getParameterList().getParameters(), PyCallableParameterImpl::new)); + .orElseGet(() -> ContainerUtil.map(getParameterList().getParameters(), PyCallableParameterImpl::psi)); } @Override diff --git a/python/src/com/jetbrains/python/psi/impl/PyLambdaExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyLambdaExpressionImpl.java index cd5c1f78ff18..5c96766db0d9 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyLambdaExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyLambdaExpressionImpl.java @@ -74,7 +74,7 @@ public class PyLambdaExpressionImpl extends PyElementImpl implements PyLambdaExp .filter(PyCallableType.class::isInstance) .map(PyCallableType.class::cast) .map(callableType -> callableType.getParameters(context)) - .orElseGet(() -> ContainerUtil.map(getParameterList().getParameters(), PyCallableParameterImpl::new)); + .orElseGet(() -> ContainerUtil.map(getParameterList().getParameters(), PyCallableParameterImpl::psi)); } @Nullable diff --git a/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java b/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java index a8fc92ca43e1..9c18595f9242 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java @@ -177,13 +177,13 @@ public class PyNamedParameterImpl extends PyBaseElementImpl types = new ArrayList<>(); final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context); - final PyCallableParameter parameter = new PyCallableParameterImpl(this); + final PyCallableParameter parameter = PyCallableParameterImpl.psi(this); processLocalCalls( func, call -> { diff --git a/python/src/com/jetbrains/python/psi/types/PyCallableParameterImpl.java b/python/src/com/jetbrains/python/psi/types/PyCallableParameterImpl.java index 6eb611cc867d..659751181cc7 100644 --- a/python/src/com/jetbrains/python/psi/types/PyCallableParameterImpl.java +++ b/python/src/com/jetbrains/python/psi/types/PyCallableParameterImpl.java @@ -36,29 +36,39 @@ public class PyCallableParameterImpl implements PyCallableParameter { @Nullable private final PyExpression myDefaultValue; @Nullable private final PyParameter myElement; - public PyCallableParameterImpl(@Nullable String name, @Nullable PyType type) { - this(name, type, null); - } - - public PyCallableParameterImpl(@Nullable String name, @Nullable PyType type, @Nullable PyExpression defaultValue) { + private PyCallableParameterImpl(@Nullable String name, + @Nullable Ref type, + @Nullable PyExpression defaultValue, + @Nullable PyParameter element) { myName = name; - myType = Ref.create(type); + myType = type; myDefaultValue = defaultValue; - myElement = null; - } - - public PyCallableParameterImpl(@NotNull PyParameter element) { - myName = null; - myType = null; - myDefaultValue = null; myElement = element; } - public PyCallableParameterImpl(@NotNull PyParameter element, @Nullable PyType type) { - myName = null; - myType = Ref.create(type); - myDefaultValue = null; - myElement = element; + @NotNull + public static PyCallableParameter nonPsi(@Nullable PyType type) { + return nonPsi(null, type); + } + + @NotNull + public static PyCallableParameter nonPsi(@Nullable String name, @Nullable PyType type) { + return nonPsi(name, type, null); + } + + @NotNull + public static PyCallableParameter nonPsi(@Nullable String name, @Nullable PyType type, @Nullable PyExpression defaultValue) { + return new PyCallableParameterImpl(name, Ref.create(type), defaultValue, null); + } + + @NotNull + public static PyCallableParameter psi(@NotNull PyParameter parameter) { + return new PyCallableParameterImpl(null, null, null, parameter); + } + + @NotNull + public static PyCallableParameter psi(@NotNull PyParameter parameter, @Nullable PyType type) { + return new PyCallableParameterImpl(null, Ref.create(type), null, parameter); } @Nullable diff --git a/python/src/com/jetbrains/python/psi/types/PyFunctionTypeImpl.java b/python/src/com/jetbrains/python/psi/types/PyFunctionTypeImpl.java index 48544661effa..7757339fde48 100644 --- a/python/src/com/jetbrains/python/psi/types/PyFunctionTypeImpl.java +++ b/python/src/com/jetbrains/python/psi/types/PyFunctionTypeImpl.java @@ -44,7 +44,7 @@ public class PyFunctionTypeImpl implements PyFunctionType { @NotNull private final List myParameters; public PyFunctionTypeImpl(@NotNull PyCallable callable) { - this(callable, ContainerUtil.map(callable.getParameterList().getParameters(), PyCallableParameterImpl::new)); + this(callable, ContainerUtil.map(callable.getParameterList().getParameters(), PyCallableParameterImpl::psi)); } public PyFunctionTypeImpl(@NotNull PyCallable callable, @NotNull List parameters) { diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java index 1a3a4f323284..76bdcdd468fb 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java @@ -517,8 +517,8 @@ public class PyTypeChecker { final PyType substType = substitute(parameter.getType(context), substitutions, context); final PyParameter psi = parameter.getParameter(); final PyCallableParameter subst = psi != null ? - new PyCallableParameterImpl(psi, substType) : - new PyCallableParameterImpl(parameter.getName(), substType, parameter.getDefaultValue()); + PyCallableParameterImpl.psi(psi, substType) : + PyCallableParameterImpl.nonPsi(parameter.getName(), substType, parameter.getDefaultValue()); substParams.add(subst); } } diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeParser.java b/python/src/com/jetbrains/python/psi/types/PyTypeParser.java index 8308efd6bd21..3b104aaeb372 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeParser.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeParser.java @@ -266,10 +266,10 @@ public class PyTypeParser { final ParseResult first = firstPair.getFirst(); final List second = firstPair.getSecond(); result = first; - parameters.add(new PyCallableParameterImpl(null, first.getType())); + parameters.add(PyCallableParameterImpl.nonPsi(first.getType())); for (ParseResult r : second) { result = result.merge(r); - parameters.add(new PyCallableParameterImpl(null, r.getType())); + parameters.add(PyCallableParameterImpl.nonPsi(r.getType())); } result = result.merge(returnResult); }