@Nullable private static PsiMethodCallExpression checkMethodResolvable( PsiMethodCallExpression methodCall, PsiMethod targetMethod, PsiReferenceExpression context, PsiClass aClass) throws IncorrectOperationException { PsiElementFactory factory = JavaPsiFacade.getInstance(targetMethod.getProject()).getElementFactory(); final PsiElement resolved = methodCall.getMethodExpression().resolve(); if (resolved != targetMethod) { PsiClass containingClass; if (resolved instanceof PsiMethod) { containingClass = ((PsiMethod) resolved).getContainingClass(); } else if (resolved instanceof PsiClass) { containingClass = (PsiClass) resolved; } else { return null; } if (containingClass != null && containingClass.isInheritor(aClass, false)) { final PsiExpression newMethodExpression = factory.createExpressionFromText("super." + targetMethod.getName(), context); methodCall.getMethodExpression().replace(newMethodExpression); } else { methodCall = null; } } return methodCall; }
private static boolean methodCallExpressionsAreEquivalent( @NotNull PsiMethodCallExpression methodExp1, @NotNull PsiMethodCallExpression methodExp2) { final PsiReferenceExpression methodExpression1 = methodExp1.getMethodExpression(); final PsiReferenceExpression methodExpression2 = methodExp2.getMethodExpression(); if (!expressionsAreEquivalent(methodExpression1, methodExpression2)) { return false; } final PsiExpressionList argumentList1 = methodExp1.getArgumentList(); final PsiExpression[] args1 = argumentList1.getExpressions(); final PsiExpressionList argumentList2 = methodExp2.getArgumentList(); final PsiExpression[] args2 = argumentList2.getExpressions(); return expressionListsAreEquivalent(args1, args2); }
public static String buildCollectionText(PsiForStatement forStatement) { PsiStatement body = forStatement.getBody(); while (body instanceof PsiBlockStatement) { final PsiBlockStatement blockStatement = (PsiBlockStatement) body; final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); final PsiStatement[] statements = codeBlock.getStatements(); if (statements.length == 2) { body = statements[1]; } else if (statements.length == 1) { body = statements[0]; } else { return null; } } if (!(body instanceof PsiExpressionStatement)) { return null; } final PsiExpressionStatement expressionStatement = (PsiExpressionStatement) body; final PsiExpression expression = expressionStatement.getExpression(); if (!(expression instanceof PsiMethodCallExpression)) { return null; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression; final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiElement qualifier = methodExpression.getQualifier(); if (qualifier == null) { // fixme for when the array is added to 'this' return null; } return qualifier.getText(); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); @NonNls final String signal = "signal"; if (!signal.equals(methodName)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList.getExpressions().length != 0) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass containingClass = method.getContainingClass(); if (containingClass == null) { return; } if (!ClassUtils.isSubclass(containingClass, "java.util.concurrent.locks.Condition")) { return; } registerMethodCallError(expression); }
private void findUsagesForMethod(PsiMethod method, List<FixableUsageInfo> usages) { final PsiManager psiManager = method.getManager(); final Project project = psiManager.getProject(); final GlobalSearchScope scope = GlobalSearchScope.allScope(project); final Iterable<PsiReference> calls = ReferencesSearch.search(method, scope); for (PsiReference reference : calls) { final PsiElement referenceElement = reference.getElement(); final PsiElement parent = referenceElement.getParent(); if (parent instanceof PsiMethodCallExpression) { final PsiMethodCallExpression call = (PsiMethodCallExpression) parent; if (isInMovedElement(call)) { continue; } final PsiReferenceExpression methodExpression = call.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null || qualifier instanceof PsiThisExpression) { usages.add(new ReplaceThisCallWithDelegateCall(call, delegateFieldName)); } delegationRequired = true; } } if (!delegationRequired && MethodInheritanceUtils.hasSiblingMethods(method)) { delegationRequired = true; } if (delegationRequired) { usages.add(new MakeMethodDelegate(method, delegateFieldName)); } else { usages.add(new RemoveMethod(method)); } }
private static boolean isParameterUsedRecursively( @NotNull PsiElement element, @NotNull List<PsiReference> array) { if (!(element instanceof PsiParameter)) return false; PsiParameter parameter = (PsiParameter) element; PsiElement scope = parameter.getDeclarationScope(); if (!(scope instanceof PsiMethod)) return false; PsiMethod method = (PsiMethod) scope; int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter); for (PsiReference reference : array) { if (!(reference instanceof PsiElement)) return false; PsiElement argument = (PsiElement) reference; PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) new PsiMatcherImpl(argument) .dot(PsiMatchers.hasClass(PsiReferenceExpression.class)) .parent(PsiMatchers.hasClass(PsiExpressionList.class)) .parent(PsiMatchers.hasClass(PsiMethodCallExpression.class)) .getElement(); if (methodCallExpression == null) return false; PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); if (method != methodExpression.resolve()) return false; PsiExpressionList argumentList = methodCallExpression.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); int argumentIndex = ArrayUtilRt.find(arguments, argument); if (paramIndex != argumentIndex) return false; } return true; }
private void processUsagesForMethod( final boolean deleteMethodHierarchy, PsiMethod method, int[] paramPermutation, String getterName, PsiMethod delegatedMethod, List<FixableUsageInfo> usages) { for (PsiReference reference : ReferencesSearch.search(method)) { final PsiElement referenceElement = reference.getElement(); final PsiMethodCallExpression call = (PsiMethodCallExpression) referenceElement.getParent(); final String access; if (call.getMethodExpression().getQualifierExpression() == null) { access = field.getName(); } else { access = getterName + "()"; if (getter == null) { getter = GenerateMembersUtil.generateGetterPrototype(field); } } usages.add( new InlineDelegatingCall(call, paramPermutation, access, delegatedMethod.getName())); } if (deleteMethodHierarchy) { usages.add(new DeleteMethod(method)); } }
@Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String referenceName = methodExpression.getReferenceName(); if (!"toString".equals(referenceName) || ExpressionUtils.isConversionToStringNecessary(expression)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 0) { return; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier != null) { if (qualifier.getType() instanceof PsiArrayType) { // do not warn on nonsensical code return; } else if (qualifier instanceof PsiSuperExpression) { return; } } registerMethodCallError(expression, calculateReplacementText(qualifier)); }
protected void changeSelfUsage(SelfUsageInfo usageInfo) throws IncorrectOperationException { PsiElement parent = usageInfo.getElement().getParent(); LOG.assertTrue(parent instanceof PsiMethodCallExpression); PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parent; final PsiExpression qualifier = methodCall.getMethodExpression().getQualifierExpression(); if (qualifier != null) qualifier.delete(); PsiElementFactory factory = JavaPsiFacade.getInstance(methodCall.getProject()).getElementFactory(); PsiExpressionList args = methodCall.getArgumentList(); PsiElement addParameterAfter = null; if (mySettings.isMakeClassParameter()) { PsiElement arg = factory.createExpressionFromText(mySettings.getClassParameterName(), null); addParameterAfter = args.addAfter(arg, null); } if (mySettings.isMakeFieldParameters()) { List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList(); for (Settings.FieldParameter fieldParameter : parameters) { PsiElement arg = factory.createExpressionFromText(fieldParameter.name, null); if (addParameterAfter == null) { addParameterAfter = args.addAfter(arg, null); } else { addParameterAfter = args.addAfter(arg, addParameterAfter); } } } }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { super.visitMethodCallExpression(call); final PsiReferenceExpression methodExpression = call.getMethodExpression(); final PsiElement qualifier = methodExpression.getQualifier(); if (!(qualifier instanceof PsiReferenceExpression)) { return; } final PsiMethod method = call.resolveMethod(); if (method == null) { return; } if (!method.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiElement referent = ((PsiReference) qualifier).resolve(); if (!(referent instanceof PsiClass)) { return; } final PsiClass referencedClass = (PsiClass) referent; final PsiClass declaringClass = method.getContainingClass(); if (declaringClass == null) { return; } if (declaringClass.equals(referencedClass)) { return; } final PsiClass containingClass = ClassUtils.getContainingClass(call); if (!ClassUtils.isClassVisibleFromClass(containingClass, declaringClass)) { return; } registerMethodCallError(call, declaringClass, referencedClass); }
private static boolean isIndexOfCall(@NotNull PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!HardcodedMethodConstants.INDEX_OF.equals(methodName)) { return false; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return false; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null) { return false; } final PsiType qualifierType = qualifier.getType(); if (qualifierType == null) { return false; } final Project project = expression.getProject(); final GlobalSearchScope projectScope = GlobalSearchScope.allScope(project); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiClass javaUtilListClass = psiFacade.findClass(CommonClassNames.JAVA_UTIL_LIST, projectScope); if (javaUtilListClass == null) { return false; } final PsiElementFactory factory = psiFacade.getElementFactory(); final PsiClassType javaUtilListType = factory.createType(javaUtilListClass); return javaUtilListType.isAssignableFrom(qualifierType); }
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { try { PsiBinaryExpression binaryExpression = (PsiBinaryExpression) descriptor.getPsiElement(); IElementType opSign = binaryExpression.getOperationSign().getTokenType(); PsiExpression lExpr = binaryExpression.getLOperand(); PsiExpression rExpr = binaryExpression.getROperand(); if (rExpr == null) return; PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); PsiMethodCallExpression equalsCall = (PsiMethodCallExpression) factory.createExpressionFromText("a.equals(b)", null); equalsCall.getMethodExpression().getQualifierExpression().replace(lExpr); equalsCall.getArgumentList().getExpressions()[0].replace(rExpr); PsiExpression result = (PsiExpression) binaryExpression.replace(equalsCall); if (opSign == JavaTokenType.NE) { PsiPrefixExpression negation = (PsiPrefixExpression) factory.createExpressionFromText("!a", null); negation.getOperand().replace(result); result.replace(negation); } } catch (IncorrectOperationException e) { LOG.error(e); } }
static String createContainsExpressionText( @NotNull PsiMethodCallExpression call, @NotNull PsiJavaToken sign, boolean flipped) { final IElementType tokenType = sign.getTokenType(); final PsiReferenceExpression methodExpression = call.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); final String qualifierText; if (qualifierExpression == null) { qualifierText = ""; } else { qualifierText = qualifierExpression.getText(); } final PsiExpressionList argumentList = call.getArgumentList(); final PsiExpression expression = argumentList.getExpressions()[0]; @NonNls final String newExpressionText = qualifierText + ".contains(" + expression.getText() + ')'; if (tokenType.equals(JavaTokenType.EQEQ)) { return '!' + newExpressionText; } else if (!flipped && (tokenType.equals(JavaTokenType.LT) || tokenType.equals(JavaTokenType.LE))) { return '!' + newExpressionText; } else if (flipped && (tokenType.equals(JavaTokenType.GT) || tokenType.equals(JavaTokenType.GE))) { return '!' + newExpressionText; } return newExpressionText; }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (methodName == null) { return; } if (!methodName.startsWith("get") && !methodName.startsWith("update")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 0) { return; } final PsiExpression argument = arguments[0]; if (!TypeUtils.expressionHasType(argument, PsiKeyword.INT)) { return; } if (!PsiUtil.isConstantExpression(argument)) { return; } final Integer val = (Integer) ConstantExpressionUtil.computeCastTo(argument, PsiType.INT); if (val == null || val.intValue() != 0) { return; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (!TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) { return; } registerError(argument); }
private static boolean isCollectCall(PsiStatement body, final PsiParameter parameter) { PsiIfStatement ifStatement = extractIfStatement(body); final PsiMethodCallExpression methodCallExpression = extractAddCall(body, ifStatement); if (methodCallExpression != null) { final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); PsiClass qualifierClass = null; if (qualifierExpression instanceof PsiReferenceExpression) { if (ReferencesSearch.search(parameter, new LocalSearchScope(qualifierExpression)) .findFirst() != null) { return false; } final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve(); if (resolve instanceof PsiVariable) { if (ReferencesSearch.search( resolve, new LocalSearchScope(methodCallExpression.getArgumentList())) .findFirst() != null) { return false; } } qualifierClass = PsiUtil.resolveClassInType(qualifierExpression.getType()); } else if (qualifierExpression == null) { final PsiClass enclosingClass = PsiTreeUtil.getParentOfType(body, PsiClass.class); if (PsiUtil.getEnclosingStaticElement(body, enclosingClass) == null) { qualifierClass = enclosingClass; } } if (qualifierClass != null && InheritanceUtil.isInheritor( qualifierClass, false, CommonClassNames.JAVA_UTIL_COLLECTION)) { while (ifStatement != null && PsiTreeUtil.isAncestor(body, ifStatement, false)) { final PsiExpression condition = ifStatement.getCondition(); if (condition != null && isConditionDependsOnUpdatedCollections(condition, qualifierExpression)) return false; ifStatement = PsiTreeUtil.getParentOfType(ifStatement, PsiIfStatement.class); } final PsiElement resolve = methodExpression.resolve(); if (resolve instanceof PsiMethod && "add".equals(((PsiMethod) resolve).getName()) && ((PsiMethod) resolve).getParameterList().getParametersCount() == 1) { final PsiExpression[] args = methodCallExpression.getArgumentList().getExpressions(); if (args.length == 1) { if (args[0] instanceof PsiCallExpression) { final PsiMethod method = ((PsiCallExpression) args[0]).resolveMethod(); return method != null && !method.hasTypeParameters() && !isThrowsCompatible(method); } return true; } } } } return false; }
@Override protected boolean isValidElement(PsiElement element) { PsiMethodCallExpression methodCall = (PsiMethodCallExpression) element; PsiMethod method = (PsiMethod) methodCall.getMethodExpression().resolve(); PsiExpressionList argumentList = methodCall.getArgumentList(); List<PsiClass> classes = getTargetClasses(element); return !classes.isEmpty() && !CreateFromUsageUtils.shouldCreateConstructor(classes.get(0), argumentList, method); }
private static PsiElement getObjectForWhichMethodWasCalled( final PsiMethodCallExpression callExpression) { final PsiExpression qualifierExpression = callExpression.getMethodExpression().getQualifierExpression(); if (!(qualifierExpression instanceof PsiReferenceExpression)) { return null; } return ((PsiReferenceExpression) qualifierExpression).resolve(); }
public static int findFieldAssignmentAnchor( final PsiStatement[] statements, final @Nullable Ref<Pair<PsiField, Boolean>> anchorRef, final PsiClass targetClass, final PsiParameter myParameter) { int i = 0; for (; i < statements.length; i++) { PsiStatement psiStatement = statements[i]; if (psiStatement instanceof PsiExpressionStatement) { PsiExpressionStatement expressionStatement = (PsiExpressionStatement) psiStatement; PsiExpression expression = expressionStatement.getExpression(); if (expression instanceof PsiMethodCallExpression) { PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression; String text = methodCallExpression.getMethodExpression().getText(); if (text.equals("super") || text.equals("this")) { continue; } } else if (expression instanceof PsiAssignmentExpression) { PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expression; PsiExpression lExpression = assignmentExpression.getLExpression(); PsiExpression rExpression = assignmentExpression.getRExpression(); if (!(lExpression instanceof PsiReferenceExpression)) break; if (!(rExpression instanceof PsiReferenceExpression)) break; PsiReferenceExpression lReference = (PsiReferenceExpression) lExpression; PsiReferenceExpression rReference = (PsiReferenceExpression) rExpression; PsiElement lElement = lReference.resolve(); PsiElement rElement = rReference.resolve(); if (!(lElement instanceof PsiField) || ((PsiField) lElement).getContainingClass() != targetClass) break; if (!(rElement instanceof PsiParameter)) break; if (myParameter.getTextRange().getStartOffset() < rElement.getTextRange().getStartOffset()) { if (anchorRef != null) { anchorRef.set(Pair.create((PsiField) lElement, Boolean.TRUE)); } break; } if (anchorRef != null) { anchorRef.set(Pair.create((PsiField) lElement, Boolean.FALSE)); } continue; } } break; } return i; }
private static void removeParensFromMethodCallExpression( @NotNull PsiMethodCallExpression methodCallExpression, boolean ignoreClarifyingParentheses) { final PsiReferenceExpression target = methodCallExpression.getMethodExpression(); final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); removeParentheses(target, ignoreClarifyingParentheses); for (final PsiExpression argument : arguments) { removeParentheses(argument, ignoreClarifyingParentheses); } }
protected final void registerMethodCallError( @NotNull PsiMethodCallExpression expression, @NonNls Object... infos) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final PsiElement nameToken = methodExpression.getReferenceNameElement(); if (nameToken == null) { registerError(expression, infos); } else { registerError(nameToken, infos); } }
public static boolean isJUnitAssertCall(PsiMethodCallExpression call) { final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(call, PsiMethod.class); if (containingMethod == null) { return false; } final PsiReferenceExpression methodExpression = call.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); return methodName != null && (methodName.startsWith("assert") || "fail".equals(methodName)) && isJUnitTestMethod(containingMethod); }
@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { try { if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return; final PsiJavaToken token = (PsiJavaToken) element; final PsiPolyadicExpression expression = SplitConditionUtil.findCondition(element, true, false); final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(expression, PsiLambdaExpression.class); LOG.assertTrue(lambdaExpression != null); final String lambdaParameterName = lambdaExpression.getParameterList().getParameters()[0].getName(); final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(expression, PsiMethodCallExpression.class); LOG.assertTrue(methodCallExpression != null, expression); PsiExpression lOperand = getLOperands(expression, token); PsiExpression rOperand = getROperands(expression, token); final Collection<PsiComment> comments = PsiTreeUtil.findChildrenOfType(expression, PsiComment.class); final PsiMethodCallExpression chainedCall = (PsiMethodCallExpression) JavaPsiFacade.getElementFactory(project) .createExpressionFromText( "a.filter(" + lambdaParameterName + " -> x)", expression); final PsiExpression argExpression = chainedCall.getArgumentList().getExpressions()[0]; final PsiElement rReplaced = ((PsiLambdaExpression) argExpression).getBody().replace(rOperand); final PsiExpression compoundArg = methodCallExpression.getArgumentList().getExpressions()[0]; final int separatorOffset = token.getTextOffset(); for (PsiComment comment : comments) { if (comment.getTextOffset() < separatorOffset) { compoundArg.getParent().add(comment); } else { rReplaced.getParent().add(comment); } } ((PsiLambdaExpression) compoundArg).getBody().replace(lOperand); chainedCall.getMethodExpression().getQualifierExpression().replace(methodCallExpression); methodCallExpression.replace(chainedCall); } catch (IncorrectOperationException e) { LOG.error(e); } }
private void reportCallMayProduceNpe( ProblemsHolder holder, PsiMethodCallExpression callExpression, boolean onTheFly) { LocalQuickFix[] fix = createNPEFixes( callExpression.getMethodExpression().getQualifierExpression(), callExpression, onTheFly); holder.registerProblem( callExpression, InspectionsBundle.message("dataflow.message.npe.method.invocation"), fix); }
@Override protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) descriptor.getPsiElement().getParent().getParent(); final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null) { PsiReplacementUtil.replaceExpression(methodCallExpression, "this"); } else { methodCallExpression.replace(qualifier); } }
@Override public boolean isCallToSuperMethod(PsiExpression expression, PsiMethod method) { if (expression instanceof PsiMethodCallExpression) { PsiMethodCallExpression methodCall = (PsiMethodCallExpression) expression; if (methodCall.getMethodExpression().getQualifierExpression() instanceof PsiSuperExpression) { PsiMethod superMethod = (PsiMethod) methodCall.getMethodExpression().resolve(); if (superMethod == null || !MethodSignatureUtil.areSignaturesEqual(method, superMethod)) return false; PsiExpression[] args = methodCall.getArgumentList().getExpressions(); PsiParameter[] parms = method.getParameterList().getParameters(); for (int i = 0; i < args.length; i++) { PsiExpression arg = args[i]; if (!(arg instanceof PsiReferenceExpression)) return false; if (!parms[i].equals(((PsiReferenceExpression) arg).resolve())) return false; } return true; } } return false; }
@Override protected final void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); final PsiTypeElement castTypeElement; final PsiReferenceExpression reference; if (element instanceof PsiTypeCastExpression) { final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression) element; castTypeElement = typeCastExpression.getCastType(); final PsiExpression operand = typeCastExpression.getOperand(); if (!(operand instanceof PsiReferenceExpression)) { return; } reference = (PsiReferenceExpression) operand; } else if (element instanceof PsiMethodCallExpression) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element; final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (!(qualifier instanceof PsiClassObjectAccessExpression)) { return; } final PsiClassObjectAccessExpression classObjectAccessExpression = (PsiClassObjectAccessExpression) qualifier; castTypeElement = classObjectAccessExpression.getOperand(); final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return; } final PsiExpression argument = arguments[0]; if (!(argument instanceof PsiReferenceExpression)) { return; } reference = (PsiReferenceExpression) argument; } else { return; } if (castTypeElement == null) { return; } final PsiInstanceOfExpression conflictingInstanceof = InstanceOfUtils.getConflictingInstanceof(castTypeElement.getType(), reference, element); final PsiTypeElement instanceofTypeElement = conflictingInstanceof.getCheckType(); if (instanceofTypeElement == null) { return; } final PsiElement newElement = replace(castTypeElement, instanceofTypeElement); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); codeStyleManager.shortenClassReferences(newElement); }
private static PsiReferenceExpression getReference( PsiMethodCallExpression newMethodCallExpression) { final PsiReferenceExpression methodExpression = newMethodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); if (qualifierExpression instanceof PsiMethodCallExpression) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) qualifierExpression; return getReference(methodCallExpression); } if (!(qualifierExpression instanceof PsiReferenceExpression)) { return null; } return (PsiReferenceExpression) qualifierExpression; }
@Nullable private static LookupElementWeigher recursion( CompletionParameters parameters, final ExpectedTypeInfo[] expectedInfos) { final PsiElement position = parameters.getPosition(); final PsiMethodCallExpression expression = PsiTreeUtil.getParentOfType(position, PsiMethodCallExpression.class, true, PsiClass.class); final PsiReferenceExpression reference = expression != null ? expression.getMethodExpression() : PsiTreeUtil.getParentOfType(position, PsiReferenceExpression.class); if (reference == null) return null; return new RecursionWeigher(position, reference, expression, expectedInfos); }
@Override @Nullable public PsiType fun(final PsiMethodCallExpression call) { PsiReferenceExpression methodExpression = call.getMethodExpression(); PsiType theOnly = null; final JavaResolveResult[] results = methodExpression.multiResolve(false); LanguageLevel languageLevel = PsiUtil.getLanguageLevel(call); final PsiElement callParent = PsiUtil.skipParenthesizedExprUp(call.getParent()); final PsiExpressionList parentArgList; if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) { parentArgList = callParent instanceof PsiConditionalExpression && !PsiPolyExpressionUtil.isPolyExpression((PsiExpression) callParent) ? null : PsiTreeUtil.getParentOfType(call, PsiExpressionList.class); } else { parentArgList = null; } final MethodCandidateInfo.CurrentCandidateProperties properties = MethodCandidateInfo.getCurrentMethod(parentArgList); final boolean genericMethodCall = properties != null && properties.getInfo().isToInferApplicability(); for (int i = 0; i < results.length; i++) { final JavaResolveResult candidateInfo = results[i]; if (genericMethodCall && PsiPolyExpressionUtil.isMethodCallPolyExpression( call, (PsiMethod) candidateInfo.getElement())) { if (callParent instanceof PsiAssignmentExpression) { return null; } LOG.error("poly expression evaluation during overload resolution"); } final PsiType type = getResultType(call, methodExpression, candidateInfo, languageLevel); if (type == null) { return null; } if (i == 0) { theOnly = type; } else if (!theOnly.equals(type)) { return null; } } return PsiClassImplUtil.correctType(theOnly, call.getResolveScope()); }
private boolean isListGetLookup(PsiElement element, String indexName, PsiVariable listVariable) { if (!(element instanceof PsiExpression)) { return false; } final PsiExpression expression = (PsiExpression)element; if (!expressionIsListGetLookup(expression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) ParenthesesUtils.stripParentheses(expression); if (methodCallExpression == null) { return false; } final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] expressions = argumentList.getExpressions(); if (expressions.length != 1) { return false; } if (!indexName.equals(expressions[0].getText())) { return false; } if (qualifierExpression == null || qualifierExpression instanceof PsiThisExpression || qualifierExpression instanceof PsiSuperExpression) { return listVariable == null; } if (!(qualifierExpression instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression; final PsiExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier != null && !(qualifier instanceof PsiThisExpression) && !(qualifier instanceof PsiSuperExpression)) { return false; } final PsiElement target = referenceExpression.resolve(); return listVariable.equals(target); }