public static boolean isRecursiveMethodCall(@NotNull PsiMethodCallExpression methodCall) { final PsiExpression qualifier = methodCall.getMethodExpression().getQualifierExpression(); if(qualifier != null && !(qualifier instanceof PsiThisExpression)) { return false; } final PsiMethod method = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true, PsiLambdaExpression.class, PsiClass.class); if(method == null || !method.getName().equals(methodCall.getMethodExpression().getReferenceName())) { return false; } return Comparing.equal(method, methodCall.resolveMethod()); }
/** * 識別子の this 式判定 * @param id 識別子ノード * @return 識別子ノードが、this式の名称か否か (true:this式の識別子、false:this式の識別子でない) */ public static boolean isThisExpression(@NonNull PsiIdentifier id) { // 名称がthis式の識別名か否かをチェック List<PsiElement> elderBrothers = ElementUtil.getElderBrother(id); boolean isThisExpr = false; for (PsiElement brother : elderBrothers) { isThisExpr = isThisExpr || (brother instanceof PsiThisExpression) || (brother instanceof PsiParenthesizedExpression); } return isThisExpr; }
@Override public void visitMethodCallExpression( @NotNull GrMethodCallExpression call) { if (recursive) { return; } super.visitMethodCallExpression(call); final GrExpression methodExpression = call.getInvokedExpression(); if (!(methodExpression instanceof GrReferenceExpression)) { return; } final GrReferenceExpression referenceExpression = (GrReferenceExpression) methodExpression; final String calledMethodName = referenceExpression.getReferenceName(); if (calledMethodName == null) { return; } if (!calledMethodName.equals(methodName)) { return; } final PsiMethod calledMethod = call.resolveMethod(); if (!method.equals(calledMethod)) { return; } if (method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.PRIVATE)) { recursive = true; return; } final GrExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier == null || qualifier instanceof PsiThisExpression) { recursive = true; } }
public void visitMethodCallExpression( @NotNull GrMethodCallExpression call) { if (recursive) { return; } super.visitMethodCallExpression(call); final GrExpression methodExpression = call.getInvokedExpression(); if (!(methodExpression instanceof GrReferenceExpression)) { return; } final GrReferenceExpression referenceExpression = (GrReferenceExpression) methodExpression; final String calledMethodName = referenceExpression.getReferenceName(); if (calledMethodName == null) { return; } if (!calledMethodName.equals(methodName)) { return; } final PsiMethod calledMethod = call.resolveMethod(); if (!method.equals(calledMethod)) { return; } if (method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.PRIVATE)) { recursive = true; return; } final GrExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier == null || qualifier instanceof PsiThisExpression) { recursive = true; } }
@Override public void visitThisExpression(PsiThisExpression expression) { mVisitor.report("PsiThisExpression", expression.getText(), expression); super.visitExpression(expression); }
private boolean qualifierReferencesThis(@Nullable PsiExpression qualifier) { return qualifier == null || qualifier instanceof PsiThisExpression; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final PsiThisExpression thisExpression = RefactoringChangeUtil.createThisExpression(PsiManager.getInstance(project), myContainingClass); ((PsiReferenceExpression)myExpression).setQualifierExpression(thisExpression); }
@Override public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) { Object o = element.getObject(); if(o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) { return StatisticsInfo.EMPTY; } LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY); if(item == null) { return null; } PsiType qualifierType = JavaCompletionUtil.getQualifierType(item); if(o instanceof PsiMember) { final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null; String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember) o); if(o instanceof PsiClass) { PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null; return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2); } PsiClass containingClass = ((PsiMember) o).getContainingClass(); if(containingClass != null) { String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl) firstInfo).getExpectedName() : null; String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###"; String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass); if(o instanceof PsiMethod) { String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod) o)); List<StatisticsInfo> superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue)); for(PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) { superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); } return StatisticsInfo.createComposite(superMethodInfos); } return new StatisticsInfo(context, key2); } } if(qualifierType != null) { return StatisticsInfo.EMPTY; } return null; }