private static LookupElementBuilder createGenerateMethodElement(PsiMethod prototype, PsiSubstitutor substitutor, Icon icon, String typeText, InsertHandler<LookupElement> insertHandler) { String methodName = prototype.getName(); String visibility = VisibilityUtil.getVisibilityModifier(prototype.getModifierList()); String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " "); PsiType type = substitutor.substitute(prototype.getReturnType()); String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + methodName; String parameters = PsiFormatUtil.formatMethod(prototype, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME); String overrideSignature = " @Override " + signature; // leading space to make it a middle match, under all annotation suggestions LookupElementBuilder element = LookupElementBuilder.create(prototype, signature).withLookupString(methodName). withLookupString(signature).withLookupString(overrideSignature).withInsertHandler(insertHandler). appendTailText(parameters, false).appendTailText(" {...}", true).withTypeText(typeText).withIcon(icon); element.putUserData(GENERATE_ELEMENT, true); return element; }
@Contract("_, null -> null") public static PsiMethod setVisibility(PsiMember member, PsiMethod prototype) { if (prototype == null) return null; String visibility = CodeStyleSettingsManager.getSettings(member.getProject()).VISIBILITY; @PsiModifier.ModifierConstant String newVisibility; if (VisibilityUtil.ESCALATE_VISIBILITY.equals(visibility)) { PsiClass aClass = member instanceof PsiClass ? (PsiClass)member : member.getContainingClass(); newVisibility = PsiUtil.getMaximumModifierForMember(aClass, false); } else { //noinspection MagicConstant newVisibility = visibility; } VisibilityUtil.setVisibility(prototype.getModifierList(), newVisibility); return prototype; }
protected void setupVisibility(PsiClass parentClass, @NotNull PsiClass targetClass, PsiModifierList list) throws IncorrectOperationException { if (targetClass.isInterface() && list.getFirstChild() != null) { list.deleteChildRange(list.getFirstChild(), list.getLastChild()); return; } if (targetClass.isInterface()) { return; } final String visibility = getVisibility(parentClass, targetClass); if (VisibilityUtil.ESCALATE_VISIBILITY.equals(visibility)) { list.setModifierProperty(PsiModifier.PRIVATE, true); VisibilityUtil.escalateVisibility(list, parentClass); } else { VisibilityUtil.setVisibility(list, visibility); } }
protected void performRefactoring(@NotNull UsageInfo[] usageInfos) { final PsiClass psiClass = buildClass(); if (psiClass != null) { fixJavadocForConstructor(psiClass); super.performRefactoring(usageInfos); if (!myUseExistingClass) { for (PsiReference reference : ReferencesSearch.search(method)) { final PsiElement place = reference.getElement(); VisibilityUtil.escalateVisibility(psiClass, place); for (PsiMethod constructor : psiClass.getConstructors()) { VisibilityUtil.escalateVisibility(constructor, place); } } } } }
public ConvertToInstanceMethodProcessor(final Project project, final PsiMethod method, final PsiParameter targetParameter, final String newVisibility) { super(project); myMethod = method; myTargetParameter = targetParameter; LOG.assertTrue(method.hasModifierProperty(PsiModifier.STATIC)); LOG.assertTrue(myTargetParameter.getDeclarationScope() == myMethod); LOG.assertTrue(myTargetParameter.getType() instanceof PsiClassType); final PsiType type = myTargetParameter.getType(); LOG.assertTrue(type instanceof PsiClassType); myTargetClass = ((PsiClassType)type).resolve(); myOldVisibility = VisibilityUtil.getVisibilityModifier(method.getModifierList()); myNewVisibility = newVisibility; }
private void fixVisibility(final PsiMethod method, final UsageInfo[] usages) throws IncorrectOperationException { final PsiModifierList modifierList = method.getModifierList(); if (VisibilityUtil.ESCALATE_VISIBILITY.equals(myNewVisibility)) { for (UsageInfo usage : usages) { if (usage instanceof MethodCallUsageInfo) { final PsiElement place = usage.getElement(); if (place != null) { VisibilityUtil.escalateVisibility(method, place); } } } } else if (myNewVisibility != null && !myNewVisibility.equals(myOldVisibility)) { modifierList.setModifierProperty(myNewVisibility, true); } }
private void fixModifierList(PsiMember member, PsiMember newMember, final UsageInfo[] usages) throws IncorrectOperationException { PsiModifierList modifierList = newMember.getModifierList(); if (modifierList != null && myTargetClass.isInterface()) { modifierList.setModifierProperty(PsiModifier.PUBLIC, false); modifierList.setModifierProperty(PsiModifier.PROTECTED, false); modifierList.setModifierProperty(PsiModifier.PRIVATE, false); if (newMember instanceof PsiClass) { modifierList.setModifierProperty(PsiModifier.STATIC, false); } return; } if (myNewVisibility == null) return; final List<UsageInfo> filtered = new ArrayList<UsageInfo>(); for (UsageInfo usage : usages) { if (usage instanceof MoveMembersUsageInfo && member == ((MoveMembersUsageInfo)usage).member) { filtered.add(usage); } } UsageInfo[] infos = filtered.toArray(new UsageInfo[filtered.size()]); VisibilityUtil.fixVisibility(UsageViewUtil.toElements(infos), newMember, myNewVisibility); }
private PsiMethod getPatternMethod() throws IncorrectOperationException { final PsiMethod methodCopy = (PsiMethod)myMethod.copy(); String name = myTargetClass.isInterface() ? PsiModifier.PUBLIC : !Comparing.strEqual(myNewVisibility, VisibilityUtil.ESCALATE_VISIBILITY) ? myNewVisibility : null; if (name != null) { PsiUtil.setModifierProperty(methodCopy, name, true); } if (myTargetVariable instanceof PsiParameter) { final int index = myMethod.getParameterList().getParameterIndex((PsiParameter)myTargetVariable); methodCopy.getParameterList().getParameters()[index].delete(); } addParameters(JavaPsiFacade.getInstance(myProject).getElementFactory(), methodCopy, myTargetClass.isInterface()); return methodCopy; }
private static JavaChangeInfo generateChangeInfo(PsiMethod method, boolean generateDelegate, @PsiModifier.ModifierConstant String newVisibility, String newName, CanonicalTypes.Type newType, @NotNull ParameterInfoImpl[] parameterInfo, ThrownExceptionInfo[] thrownExceptions, Set<PsiMethod> propagateParametersMethods, Set<PsiMethod> propagateExceptionsMethods) { LOG.assertTrue(method.isValid()); if (propagateParametersMethods == null) { propagateParametersMethods = ContainerUtil.newHashSet(); } if (propagateExceptionsMethods == null) { propagateExceptionsMethods = ContainerUtil.newHashSet(); } if (newVisibility == null) { newVisibility = VisibilityUtil.getVisibilityModifier(method.getModifierList()); } return new JavaChangeInfoImpl(newVisibility, method, newName, newType, parameterInfo, thrownExceptions, generateDelegate, propagateParametersMethods, propagateExceptionsMethods); }
private static void addDefaultConstructor(JavaChangeInfo changeInfo, PsiClass aClass, final UsageInfo[] usages) throws IncorrectOperationException { if (!(aClass instanceof PsiAnonymousClass)) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject()); PsiMethod defaultConstructor = factory.createMethodFromText(aClass.getName() + "(){}", aClass); defaultConstructor = (PsiMethod)CodeStyleManager.getInstance(aClass.getProject()).reformat(defaultConstructor); defaultConstructor = (PsiMethod)aClass.add(defaultConstructor); PsiUtil.setModifierProperty(defaultConstructor, VisibilityUtil.getVisibilityModifier(aClass.getModifierList()), true); addSuperCall(changeInfo, defaultConstructor, null, usages); } else { final PsiElement parent = aClass.getParent(); if (parent instanceof PsiNewExpression) { final PsiExpressionList argumentList = ((PsiNewExpression)parent).getArgumentList(); final PsiClass baseClass = changeInfo.getMethod().getContainingClass(); final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, aClass, PsiSubstitutor.EMPTY); fixActualArgumentsList(argumentList, changeInfo, true, substitutor); } } }
@Nullable public String getVisibility() { if (myRbPublic.isSelected()) { return PsiModifier.PUBLIC; } if (myRbPackageLocal.isSelected()) { return PsiModifier.PACKAGE_LOCAL; } if (myRbProtected.isSelected()) { return PsiModifier.PROTECTED; } if (myRbPrivate.isSelected()) { return PsiModifier.PRIVATE; } if (myRbEscalate != null && myRbEscalate.isSelected()) { return VisibilityUtil.ESCALATE_VISIBILITY; } return null; }
public boolean processAddDefaultConstructor(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) { if (!(usage.getElement() instanceof PsiClass) || !isJavaUsage(usage)) return true; PsiClass aClass = (PsiClass)usage.getElement(); if (!(aClass instanceof PsiAnonymousClass)) { final PsiElementFactory factory = JavaPsiFacade.getInstance(data.getProject()).getElementFactory(); PsiMethod constructor = factory.createMethodFromText(aClass.getName() + "(){}", aClass); constructor = (PsiMethod)CodeStyleManager.getInstance(data.getProject()).reformat(constructor); constructor = (PsiMethod)aClass.add(constructor); PsiUtil.setModifierProperty(constructor, VisibilityUtil.getVisibilityModifier(aClass.getModifierList()), true); processAddSuperCall(data, new UsageInfo(constructor), usages); } else { return true; } return false; }
@Override @Nullable public String getConfirmDuplicatePrompt(final Match match) { final PsiElement matchStart = match.getMatchStart(); String visibility = VisibilityUtil.getPossibleVisibility(myMethod, matchStart); final boolean shouldBeStatic = isEssentialStaticContextAbsent(match); final String signature = MatchUtil .getChangedSignature(match, myMethod, myMethod.hasModifierProperty(PsiModifier.STATIC) || shouldBeStatic, visibility); if (signature != null) { return RefactoringBundle.message("replace.this.code.fragment.and.change.signature", signature); } final boolean needToEscalateVisibility = !PsiUtil.isAccessible(myMethod, matchStart, null); if (needToEscalateVisibility) { final String visibilityPresentation = VisibilityUtil.toPresentableText(visibility); return shouldBeStatic ? RefactoringBundle.message("replace.this.code.fragment.and.make.method.static.visible", visibilityPresentation) : RefactoringBundle.message("replace.this.code.fragment.and.make.method.visible", visibilityPresentation); } if (shouldBeStatic) { return RefactoringBundle.message("replace.this.code.fragment.and.make.method.static"); } return null; }
public static void checkAccessibilityConflicts(@NotNull PsiMember member, @PsiModifier.ModifierConstant @Nullable String newVisibility, @Nullable PsiClass targetClass, @NotNull Set<? extends PsiMember> membersToMove, @NotNull MultiMap<PsiElement, String> conflicts) { PsiModifierList modifierListCopy = member.getModifierList(); if (modifierListCopy != null) { modifierListCopy = (PsiModifierList)modifierListCopy.copy(); final PsiClass containingClass = member.getContainingClass(); if (containingClass != null && containingClass.isInterface()) { VisibilityUtil.setVisibility(modifierListCopy, PsiModifier.PUBLIC); } } if (newVisibility != null && modifierListCopy != null) { try { VisibilityUtil.setVisibility(modifierListCopy, newVisibility); } catch (IncorrectOperationException ignore) { } // do nothing and hope for the best } checkAccessibilityConflicts(member, modifierListCopy, targetClass, membersToMove, conflicts); }
public static void checkAccessibility(PsiMember refMember, @NotNull PsiElement newContext, @Nullable PsiClass accessClass, PsiMember member, MultiMap<PsiElement, String> conflicts) { if (!PsiUtil.isAccessible(refMember, newContext, accessClass)) { String message = RefactoringBundle.message("0.is.1.and.will.not.be.accessible.from.2.in.the.target.class", RefactoringUIUtil.getDescription(refMember, true), VisibilityUtil.getVisibilityStringToDisplay(refMember), RefactoringUIUtil.getDescription(member, false)); message = CommonRefactoringUtil.capitalize(message); conflicts.putValue(refMember, message); } else if (newContext instanceof PsiClass && refMember instanceof PsiField && refMember.getContainingClass() == member.getContainingClass()) { final PsiField fieldInSubClass = ((PsiClass)newContext).findFieldByName(refMember.getName(), false); if (fieldInSubClass != null && fieldInSubClass != refMember) { conflicts.putValue(refMember, CommonRefactoringUtil.capitalize(RefactoringUIUtil.getDescription(fieldInSubClass, true) + " would hide " + RefactoringUIUtil.getDescription(refMember, true) + " which is used by moved " + RefactoringUIUtil.getDescription(member, false))); } } }
private static GrMethodCallExpression createSetterCall(FieldDescriptor fieldDescriptor, GrExpression setterArgument, GrReferenceExpression expr, PsiClass aClass, PsiMethod setter) throws IncorrectOperationException { final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(fieldDescriptor.getField().getProject()); final String setterName = fieldDescriptor.getSetterName(); @NonNls String text = setterName + "(a)"; GrExpression qualifier = expr.getQualifierExpression(); if (qualifier != null) { text = "q." + text; } GrMethodCallExpression methodCall = (GrMethodCallExpression)factory.createExpressionFromText(text, expr); methodCall.getArgumentList().getExpressionArguments()[0].replace(setterArgument); if (qualifier != null) { ((GrReferenceExpression)methodCall.getInvokedExpression()).getQualifierExpression().replace(qualifier); } methodCall = checkMethodResolvable(methodCall, setter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
@Nullable private static GrMethodCallExpression createGetterCall(FieldDescriptor fieldDescriptor, GrReferenceExpression expr, PsiClass aClass, PsiMethod getter) throws IncorrectOperationException { final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(fieldDescriptor.getField().getProject()); final String getterName = fieldDescriptor.getGetterName(); @NonNls String text = getterName + "()"; GrExpression qualifier = expr.getQualifierExpression(); if (qualifier != null) { text = "q." + text; } GrMethodCallExpression methodCall = (GrMethodCallExpression)factory.createExpressionFromText(text, expr); if (qualifier != null) { ((GrReferenceExpression)methodCall.getInvokedExpression()).getQualifierExpression().replace(qualifier); } methodCall = checkMethodResolvable(methodCall, getter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
private static LookupElementBuilder createGenerateMethodElement(PsiMethod prototype, PsiSubstitutor substitutor, Icon icon, String typeText, InsertHandler<LookupElement> insertHandler) { String methodName = prototype.getName(); String visibility = VisibilityUtil.getVisibilityModifier(prototype.getModifierList()); String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " "); PsiType type = substitutor.substitute(prototype.getReturnType()); String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + methodName; String parameters = PsiFormatUtil.formatMethod(prototype, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME); LookupElementBuilder element = LookupElementBuilder.create(prototype, signature).withLookupString(methodName). withLookupString(signature).withInsertHandler(insertHandler). appendTailText(parameters, false).appendTailText(" {...}", true).withTypeText(typeText).withIcon(icon); element.putUserData(GENERATE_ELEMENT, true); return element; }
private PsiMethodCallExpression createSetterCall(final int fieldIndex, final PsiExpression setterArgument, PsiReferenceExpression expr) throws IncorrectOperationException { String[] setterNames = myDescriptor.getSetterNames(); PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); final String setterName = setterNames[fieldIndex]; @NonNls String text = setterName + "(a)"; PsiExpression qualifier = expr.getQualifierExpression(); if (qualifier != null){ text = "q." + text; } PsiMethodCallExpression methodCall = (PsiMethodCallExpression)factory.createExpressionFromText(text, expr); methodCall = (PsiMethodCallExpression)CodeStyleManager.getInstance(myProject).reformat(methodCall); methodCall.getArgumentList().getExpressions()[0].replace(setterArgument); if (qualifier != null){ methodCall.getMethodExpression().getQualifierExpression().replace(qualifier); } final PsiMethod targetMethod = myNameToSetter.get(setterName); methodCall = checkMethodResolvable(methodCall, targetMethod, expr); if (methodCall == null) { VisibilityUtil.escalateVisibility(myFields[fieldIndex], expr); } return methodCall; }
@Nullable private PsiMethodCallExpression createGetterCall(final int fieldIndex, PsiReferenceExpression expr) throws IncorrectOperationException { String[] getterNames = myDescriptor.getGetterNames(); PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); final String getterName = getterNames[fieldIndex]; @NonNls String text = getterName + "()"; PsiExpression qualifier = expr.getQualifierExpression(); if (qualifier != null){ text = "q." + text; } PsiMethodCallExpression methodCall = (PsiMethodCallExpression)factory.createExpressionFromText(text, expr); methodCall = (PsiMethodCallExpression)CodeStyleManager.getInstance(myProject).reformat(methodCall); if (qualifier != null){ methodCall.getMethodExpression().getQualifierExpression().replace(qualifier); } final PsiMethod targetMethod = myNameToGetter.get(getterName); methodCall = checkMethodResolvable(methodCall, targetMethod, expr); if(methodCall == null) { VisibilityUtil.escalateVisibility(myFields[fieldIndex], expr); } return methodCall; }
protected void performRefactoring(UsageInfo[] usageInfos) { final PsiClass psiClass = buildClass(); if (psiClass != null) { fixJavadocForConstructor(psiClass); super.performRefactoring(usageInfos); if (!myUseExistingClass) { for (PsiReference reference : ReferencesSearch.search(method)) { final PsiElement place = reference.getElement(); VisibilityUtil.escalateVisibility(psiClass, place); for (PsiMethod constructor : psiClass.getConstructors()) { VisibilityUtil.escalateVisibility(constructor, place); } } } } }
private static JavaChangeInfo generateChangeInfo(PsiMethod method, boolean generateDelegate, @PsiModifier.ModifierConstant String newVisibility, String newName, CanonicalTypes.Type newType, @NotNull ParameterInfoImpl[] parameterInfo, ThrownExceptionInfo[] thrownExceptions, Set<PsiMethod> propagateParametersMethods, Set<PsiMethod> propagateExceptionsMethods) { Set<PsiMethod> myPropagateParametersMethods = propagateParametersMethods != null ? propagateParametersMethods : new HashSet<PsiMethod>(); Set<PsiMethod> myPropagateExceptionsMethods = propagateExceptionsMethods != null ? propagateExceptionsMethods : new HashSet<PsiMethod>(); LOG.assertTrue(method.isValid()); if (newVisibility == null) { newVisibility = VisibilityUtil.getVisibilityModifier(method.getModifierList()); } return new JavaChangeInfoImpl(newVisibility, method, newName, newType, parameterInfo, thrownExceptions, generateDelegate, myPropagateParametersMethods, myPropagateExceptionsMethods); }
public static void checkAccessibilityConflicts(@NotNull PsiMember member, @PsiModifier.ModifierConstant @Nullable String newVisibility, @Nullable PsiClass targetClass, @NotNull Set<PsiMember> membersToMove, @NotNull MultiMap<PsiElement, String> conflicts) { PsiModifierList modifierListCopy = member.getModifierList(); if (modifierListCopy != null) { modifierListCopy = (PsiModifierList)modifierListCopy.copy(); final PsiClass containingClass = member.getContainingClass(); if (containingClass != null && containingClass.isInterface()) { VisibilityUtil.setVisibility(modifierListCopy, PsiModifier.PUBLIC); } } if (newVisibility != null && modifierListCopy != null) { try { VisibilityUtil.setVisibility(modifierListCopy, newVisibility); } catch (IncorrectOperationException ignore) { } // do nothing and hope for the best } checkAccessibilityConflicts(member, modifierListCopy, targetClass, membersToMove, conflicts); }
@Contract("_, null -> null") public static PsiMethod setVisibility(PsiMember member, PsiMethod prototype) { if(prototype == null) { return null; } String visibility = CodeStyleSettingsManager.getSettings(member.getProject()).VISIBILITY; @PsiModifier.ModifierConstant String newVisibility; if(VisibilityUtil.ESCALATE_VISIBILITY.equals(visibility)) { PsiClass aClass = member instanceof PsiClass ? (PsiClass) member : member.getContainingClass(); newVisibility = PsiUtil.getMaximumModifierForMember(aClass, false); } else { //noinspection MagicConstant newVisibility = visibility; } VisibilityUtil.setVisibility(prototype.getModifierList(), newVisibility); return prototype; }
private static PsiMethodCallExpression createSetterCall(FieldDescriptor fieldDescriptor, PsiExpression setterArgument, PsiReferenceExpression expr, PsiClass aClass, PsiMethod setter) throws IncorrectOperationException { PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); final String setterName = fieldDescriptor.getSetterName(); @NonNls String text = setterName + "(a)"; PsiExpression qualifier = expr.getQualifierExpression(); if (qualifier != null){ text = "q." + text; } PsiMethodCallExpression methodCall = (PsiMethodCallExpression)factory.createExpressionFromText(text, expr); methodCall.getArgumentList().getExpressions()[0].replace(setterArgument); if (qualifier != null){ methodCall.getMethodExpression().getQualifierExpression().replace(qualifier); } methodCall = checkMethodResolvable(methodCall, setter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
@Nullable private static PsiMethodCallExpression createGetterCall(FieldDescriptor fieldDescriptor, PsiReferenceExpression expr, PsiClass aClass, PsiMethod getter) throws IncorrectOperationException { PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); final String getterName = fieldDescriptor.getGetterName(); @NonNls String text = getterName + "()"; PsiExpression qualifier = expr.getQualifierExpression(); if (qualifier != null) { text = "q." + text; } PsiMethodCallExpression methodCall = (PsiMethodCallExpression)factory.createExpressionFromText(text, expr); if (qualifier != null) { methodCall.getMethodExpression().getQualifierExpression().replace(qualifier); } methodCall = checkMethodResolvable(methodCall, getter, expr, aClass); if (methodCall == null) { VisibilityUtil.escalateVisibility(fieldDescriptor.getField(), expr); } return methodCall; }
private PsiMethod getPatternMethod() throws IncorrectOperationException { final PsiMethod methodCopy = (PsiMethod) myMethod.copy(); String name = myTargetClass.isInterface() ? PsiModifier.PUBLIC : !Comparing.strEqual(myNewVisibility, VisibilityUtil.ESCALATE_VISIBILITY) ? myNewVisibility : null; if(name != null) { PsiUtil.setModifierProperty(methodCopy, name, true); } if(myTargetVariable instanceof PsiParameter) { final int index = myMethod.getParameterList().getParameterIndex((PsiParameter) myTargetVariable); methodCopy.getParameterList().getParameters()[index].delete(); } addParameters(JavaPsiFacade.getInstance(myProject).getElementFactory(), methodCopy, myTargetClass.isInterface()); return methodCopy; }