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); }
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); } } } }
private void processChangedMethodCall(PsiElement element) throws IncorrectOperationException { if (element.getParent() instanceof PsiMethodCallExpression) { PsiMethodCallExpression methodCall = (PsiMethodCallExpression) element.getParent(); if (myMethodToReplaceIn == myMethodToSearchFor && PsiTreeUtil.isAncestor(methodCall, myParameterInitializer, false)) return; PsiElementFactory factory = JavaPsiFacade.getInstance(methodCall.getProject()).getElementFactory(); PsiExpression expression = factory.createExpressionFromText(myParameterName, null); final PsiExpressionList argList = methodCall.getArgumentList(); final PsiExpression[] exprs = argList.getExpressions(); boolean first = false; PsiElement anchor = null; if (myMethodToSearchFor.isVarArgs()) { final int oldParamCount = myMethodToSearchFor.getParameterList().getParametersCount() - 1; if (exprs.length >= oldParamCount) { if (oldParamCount > 1) { anchor = exprs[oldParamCount - 2]; } else { first = true; anchor = null; } } else { anchor = exprs[exprs.length - 1]; } } else if (exprs.length > 0) { anchor = exprs[exprs.length - 1]; } if (anchor != null) { argList.addAfter(expression, anchor); } else { if (first && exprs.length > 0) { argList.addBefore(expression, exprs[0]); } else { argList.add(expression); } } removeParametersFromCall(argList); } else { LOG.error(element.getParent()); } }
private void updateRefMethod( PsiElement psiResolved, RefElement refResolved, PsiElement refExpression, final PsiElement psiFrom, final RefElement refFrom) { PsiMethod psiMethod = (PsiMethod) psiResolved; RefMethodImpl refMethod = (RefMethodImpl) refResolved; PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(refExpression, PsiMethodCallExpression.class); if (call != null) { PsiType returnType = psiMethod.getReturnType(); if (!psiMethod.isConstructor() && returnType != PsiType.VOID) { if (!(call.getParent() instanceof PsiExpressionStatement)) { refMethod.setReturnValueUsed(true); } addTypeReference(psiFrom, returnType, refFrom.getRefManager()); } PsiExpressionList argumentList = call.getArgumentList(); if (argumentList.getExpressions().length > 0) { refMethod.updateParameterValues(argumentList.getExpressions()); } final PsiExpression psiExpression = call.getMethodExpression().getQualifierExpression(); if (psiExpression != null) { final PsiType usedType = psiExpression.getType(); if (usedType != null) { final String fqName = psiMethod.getContainingClass().getQualifiedName(); if (fqName != null) { final PsiClassType methodOwnerType = JavaPsiFacade.getInstance(call.getProject()) .getElementFactory() .createTypeByFQClassName( fqName, GlobalSearchScope.allScope(psiMethod.getProject())); if (!usedType.equals(methodOwnerType)) { refMethod.setCalledOnSubClass(true); } } } } } }
public StaticImportMethodFix(@NotNull PsiMethodCallExpression methodCallExpression) { myMethodCall = SmartPointerManager.getInstance(methodCallExpression.getProject()) .createSmartPsiElementPointer(methodCallExpression); }
@NotNull private List<PsiMethod> getMethodsToImport() { PsiShortNamesCache cache = PsiShortNamesCache.getInstance(myMethodCall.getProject()); PsiMethodCallExpression element = myMethodCall.getElement(); PsiReferenceExpression reference = element.getMethodExpression(); PsiExpressionList argumentList = element.getArgumentList(); String name = reference.getReferenceName(); List<PsiMethod> list = new ArrayList<PsiMethod>(); if (name == null) return list; GlobalSearchScope scope = element.getResolveScope(); PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20); List<PsiMethod> applicableList = new ArrayList<PsiMethod>(); final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(element.getProject()).getResolveHelper(); for (PsiMethod method : methods) { ProgressManager.checkCanceled(); if (JavaCompletionUtil.isInExcludedPackage(method)) continue; if (!method.hasModifierProperty(PsiModifier.STATIC)) continue; PsiFile file = method.getContainingFile(); if (file instanceof PsiJavaFile // do not show methods from default package && ((PsiJavaFile) file).getPackageName().length() != 0 && PsiUtil.isAccessible(method, element, method.getContainingClass())) { list.add(method); PsiSubstitutor substitutorForMethod = resolveHelper.inferTypeArguments( method.getTypeParameters(), method.getParameterList().getParameters(), argumentList.getExpressions(), PsiSubstitutor.EMPTY, element.getParent(), DefaultParameterTypeInferencePolicy.INSTANCE); if (PsiUtil.isApplicable(method, substitutorForMethod, argumentList)) { applicableList.add(method); } } } List<PsiMethod> result = applicableList.isEmpty() ? list : applicableList; for (int i = result.size() - 1; i >= 0; i--) { ProgressManager.checkCanceled(); PsiMethod method = result.get(i); PsiClass containingClass = method.getContainingClass(); for (int j = i + 1; j < result.size(); j++) { PsiMethod exMethod = result.get(j); if (!Comparing.strEqual(exMethod.getName(), method.getName())) continue; PsiClass exContainingClass = exMethod.getContainingClass(); if (containingClass != null && exContainingClass != null && !Comparing.equal( containingClass.getQualifiedName(), exContainingClass.getQualifiedName())) continue; // same named methods, drop one result.remove(i); break; } // check for manually excluded if (isExcluded(method)) { result.remove(i); } } Collections.sort(result, new PsiProximityComparator(argumentList)); return result; }
@Override protected void invokeImpl(PsiClass targetClass) { final PsiFile callSite = myMethodCall.getContainingFile(); final Project project = myMethodCall.getProject(); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace(); try { PsiMethod constructor = elementFactory.createConstructor(); constructor = (PsiMethod) targetClass.add(constructor); final TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(constructor); CreateFromUsageUtils.setupMethodParameters( constructor, templateBuilder, myMethodCall.getArgumentList(), getTargetSubstitutor(myMethodCall)); final PsiFile psiFile = myMethodCall.getContainingFile(); templateBuilder.setEndVariableAfter(constructor.getBody().getLBrace()); final RangeMarker rangeMarker = psiFile.getViewProvider().getDocument().createRangeMarker(myMethodCall.getTextRange()); constructor = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(constructor); targetClass = constructor.getContainingClass(); myMethodCall = CodeInsightUtil.findElementInRange( psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), myMethodCall.getClass()); rangeMarker.dispose(); Template template = templateBuilder.buildTemplate(); final Editor editor = positionCursor(project, targetClass.getContainingFile(), targetClass); if (editor == null) return; final TextRange textRange = constructor.getTextRange(); final PsiFile file = targetClass.getContainingFile(); editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset()); editor.getCaretModel().moveToOffset(textRange.getStartOffset()); startTemplate( editor, template, project, new TemplateEditingAdapter() { @Override public void templateFinished(Template template, boolean brokenOff) { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { try { PsiDocumentManager.getInstance(project) .commitDocument(editor.getDocument()); final int offset = editor.getCaretModel().getOffset(); PsiMethod constructor = PsiTreeUtil.findElementOfClassAtOffset( file, offset, PsiMethod.class, false); CreateFromUsageUtils.setupMethodBody(constructor); CreateFromUsageUtils.setupEditor(constructor, editor); UndoUtil.markPsiFileForUndo(callSite); } catch (IncorrectOperationException e) { LOG.error(e); } } }); } }); } catch (IncorrectOperationException e) { LOG.error(e); } }