public static void registerQuickFix( PsiMember refElement, PsiJavaCodeReferenceElement place, PsiClass accessObjectClass, HighlightInfo error) { if (refElement instanceof PsiField && place instanceof PsiReferenceExpression) { final PsiField psiField = (PsiField) refElement; final PsiClass containingClass = psiField.getContainingClass(); if (containingClass != null) { if (PsiUtil.isOnAssignmentLeftHand((PsiExpression) place)) { final PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(psiField); final PsiMethod setter = containingClass.findMethodBySignature(setterPrototype, true); if (setter != null && PsiUtil.isAccessible(setter, place, accessObjectClass)) { QuickFixAction.registerQuickFixAction( error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, setter, true)); } } else if (PsiUtil.isAccessedForReading((PsiExpression) place)) { final PsiMethod getterPrototype = PropertyUtil.generateGetterPrototype(psiField); final PsiMethod getter = containingClass.findMethodBySignature(getterPrototype, true); if (getter != null && PsiUtil.isAccessible(getter, place, accessObjectClass)) { QuickFixAction.registerQuickFixAction( error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, getter, false)); } } } } }
private void registerActionShortcuts(@NotNull InspectionToolPresentation presentation) { final QuickFixAction[] fixes = presentation.getQuickFixes(RefEntity.EMPTY_ELEMENTS_ARRAY); if (fixes != null) { for (QuickFixAction fix : fixes) { fix.registerCustomShortcutSet(fix.getShortcutSet(), this); } } }
public static String[] quickFixTexts( RefEntity where, @NotNull InspectionToolPresentation toolPresentation) { QuickFixAction[] quickFixes = toolPresentation.getQuickFixes(new RefEntity[] {where}); if (quickFixes == null) { return null; } List<String> texts = new ArrayList<String>(); for (QuickFixAction quickFix : quickFixes) { final String text = quickFix.getText(where); if (text == null) continue; texts.add(text); } return texts.toArray(new String[texts.size()]); }
public static void register( final HighlightInfo highlightInfo, PsiExpression expression, final PsiType lType) { expression = PsiUtil.deparenthesizeExpression(expression); if (!(expression instanceof PsiNewExpression)) return; final PsiType rType = expression.getType(); PsiType newType = lType; if (rType instanceof PsiClassType && newType instanceof PsiClassType) { final PsiClassType.ClassResolveResult rResolveResult = ((PsiClassType) rType).resolveGenerics(); final PsiClass rClass = rResolveResult.getElement(); if (rClass != null) { final PsiClassType.ClassResolveResult lResolveResult = ((PsiClassType) newType).resolveGenerics(); final PsiClass lClass = lResolveResult.getElement(); if (lClass != null) { PsiSubstitutor substitutor = getInheritorSubstitutorForNewExpression( lClass, rClass, lResolveResult.getSubstitutor(), expression); if (substitutor != null) { newType = JavaPsiFacade.getInstance(lClass.getProject()) .getElementFactory() .createType(rClass, substitutor); } } } } PsiNewExpression newExpression = (PsiNewExpression) expression; QuickFixAction.registerQuickFixAction( highlightInfo, new ChangeNewOperatorTypeFix(newType, newExpression)); }
public void registerCastActions( CandidateInfo[] candidates, PsiCall call, HighlightInfo highlightInfo, final TextRange fixRange) { if (candidates.length == 0) return; List<CandidateInfo> methodCandidates = new ArrayList<CandidateInfo>(Arrays.asList(candidates)); PsiExpressionList list = call.getArgumentList(); PsiExpression[] expressions = list.getExpressions(); if (expressions.length == 0) return; // filter out not castable candidates nextMethod: for (int i = methodCandidates.size() - 1; i >= 0; i--) { CandidateInfo candidate = methodCandidates.get(i); PsiMethod method = (PsiMethod) candidate.getElement(); PsiSubstitutor substitutor = candidate.getSubstitutor(); assert method != null; PsiParameter[] parameters = method.getParameterList().getParameters(); if (expressions.length != parameters.length) { methodCandidates.remove(i); continue; } for (int j = 0; j < parameters.length; j++) { PsiParameter parameter = parameters[j]; PsiExpression expression = expressions[j]; // check if we can cast to this method PsiType exprType = expression.getType(); PsiType parameterType = substitutor.substitute(parameter.getType()); if (exprType == null || parameterType == null || !areTypesConvertible(exprType, parameterType, call)) { methodCandidates.remove(i); continue nextMethod; } } } if (methodCandidates.isEmpty()) return; try { for (int i = 0; i < expressions.length; i++) { PsiExpression expression = expressions[i]; PsiType exprType = expression.getType(); Set<String> suggestedCasts = new THashSet<String>(); // find to which type we can cast this param to get valid method call for (CandidateInfo candidate : methodCandidates) { PsiMethod method = (PsiMethod) candidate.getElement(); PsiSubstitutor substitutor = candidate.getSubstitutor(); assert method != null; PsiParameter[] parameters = method.getParameterList().getParameters(); PsiType originalParameterType = parameters[i].getType(); PsiType parameterType = substitutor.substitute(originalParameterType); if (parameterType instanceof PsiWildcardType) continue; if (!GenericsUtil.isFromExternalTypeLanguage(parameterType)) continue; if (suggestedCasts.contains(parameterType.getCanonicalText())) continue; // strict compare since even widening cast may help if (Comparing.equal(exprType, parameterType)) continue; PsiCall newCall = (PsiCall) call.copy(); PsiExpression modifiedExpression = getModifiedArgument(expression, parameterType); if (modifiedExpression == null) continue; newCall.getArgumentList().getExpressions()[i].replace(modifiedExpression); JavaResolveResult resolveResult = newCall.resolveMethodGenerics(); if (resolveResult.getElement() != null && resolveResult.isValidResult()) { suggestedCasts.add(parameterType.getCanonicalText()); QuickFixAction.registerQuickFixAction( highlightInfo, fixRange, createFix(list, i, parameterType), null); } } } } catch (IncorrectOperationException e) { LOG.error(e); } }