diff options
Diffstat (limited to 'java/java-impl/src/com/intellij/refactoring')
12 files changed, 63 insertions, 159 deletions
diff --git a/java/java-impl/src/com/intellij/refactoring/changeSignature/JavaChangeSignatureUsageProcessor.java b/java/java-impl/src/com/intellij/refactoring/changeSignature/JavaChangeSignatureUsageProcessor.java index 8a9eca38a529..6a44f2529284 100644 --- a/java/java-impl/src/com/intellij/refactoring/changeSignature/JavaChangeSignatureUsageProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/changeSignature/JavaChangeSignatureUsageProcessor.java @@ -15,7 +15,9 @@ */ package com.intellij.refactoring.changeSignature; +import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.ExceptionUtil; +import com.intellij.codeInsight.InferredAnnotationsManager; import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil; import com.intellij.codeInspection.dataFlow.ControlFlowAnalyzer; import com.intellij.lang.StdLanguages; @@ -943,7 +945,8 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr } private static void checkContract(MultiMap<PsiElement, String> conflictDescriptions, PsiMethod method) { - if (ControlFlowAnalyzer.findContractAnnotation(method) != null) { + PsiAnnotation contract = ControlFlowAnalyzer.findContractAnnotation(method); + if (contract != null && !AnnotationUtil.isInferredAnnotation(contract)) { conflictDescriptions.putValue(method, "@Contract annotation will have to be changed manually"); } } diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodUtil.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodUtil.java deleted file mode 100644 index f20ff1ca9ded..000000000000 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodUtil.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2000-2009 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.refactoring.extractMethod; - -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.Key; -import com.intellij.psi.*; -import com.intellij.psi.search.SearchScope; -import com.intellij.psi.search.searches.ClassInheritorsSearch; -import com.intellij.psi.search.searches.ReferencesSearch; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.psi.util.RedundantCastUtil; -import com.intellij.util.IncorrectOperationException; -import com.intellij.util.Processor; -import com.intellij.util.containers.HashMap; - -import java.util.Map; - -/** - * @author ven - */ -public class ExtractMethodUtil { - private static final Key<PsiMethod> RESOLVE_TARGET_KEY = Key.create("RESOLVE_TARGET_KEY"); - private static final Logger LOG = Logger.getInstance("com.intellij.refactoring.extractMethod.ExtractMethodUtil"); - - private ExtractMethodUtil() { } - - static Map<PsiMethodCallExpression, PsiMethod> encodeOverloadTargets(final PsiClass targetClass, - final SearchScope processConflictsScope, - final String overloadName, - final PsiElement extractedFragment) { - final Map<PsiMethodCallExpression, PsiMethod> ret = new HashMap<PsiMethodCallExpression, PsiMethod>(); - encodeInClass(targetClass, overloadName, extractedFragment, ret); - - ClassInheritorsSearch.search(targetClass, processConflictsScope, true).forEach(new Processor<PsiClass>() { - public boolean process(PsiClass inheritor) { - encodeInClass(inheritor, overloadName, extractedFragment, ret); - return true; - } - }); - - return ret; - } - - private static void encodeInClass(final PsiClass aClass, - final String overloadName, - final PsiElement extractedFragment, - final Map<PsiMethodCallExpression, PsiMethod> ret) { - final PsiMethod[] overloads = aClass.findMethodsByName(overloadName, false); - for (final PsiMethod overload : overloads) { - for (final PsiReference ref : ReferencesSearch.search(overload)) { - final PsiElement element = ref.getElement(); - final PsiElement parent = element.getParent(); - if (parent instanceof PsiMethodCallExpression) { - final PsiMethodCallExpression call = (PsiMethodCallExpression)parent; - if (PsiTreeUtil.isAncestor(extractedFragment, element, false)) { - call.putCopyableUserData(RESOLVE_TARGET_KEY, overload); - } else { - //we assume element won't be invalidated as a result of extraction - ret.put(call, overload); - } - } - } - } - } - - public static void decodeOverloadTargets(Map<PsiMethodCallExpression, PsiMethod> oldResolves, final PsiMethod extracted, - final PsiElement oldFragment) { - final PsiCodeBlock body = extracted.getBody(); - assert body != null; - final JavaRecursiveElementVisitor visitor = new JavaRecursiveElementVisitor() { - - @Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { - super.visitMethodCallExpression(expression); - final PsiMethod target = expression.getCopyableUserData(RESOLVE_TARGET_KEY); - if (target != null) { - expression.putCopyableUserData(RESOLVE_TARGET_KEY, null); - try { - assertSameResolveTarget(target, expression, extracted); - } - catch (IncorrectOperationException e) { - LOG.error(e); - } - } - } - }; - body.accept(visitor); - oldFragment.accept(visitor); - - for (final Map.Entry<PsiMethodCallExpression, PsiMethod> entry : oldResolves.entrySet()) { - try { - assertSameResolveTarget(entry.getValue(), entry.getKey(), extracted); - } - catch (IncorrectOperationException e) { - LOG.error(e); - } - } - } - - private static void assertSameResolveTarget(final PsiMethod oldTarget, final PsiMethodCallExpression call, final PsiMethod extracted) - throws IncorrectOperationException { - final PsiMethod newTarget = call.resolveMethod(); - final PsiManager manager = extracted.getManager(); - final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); - if (!manager.areElementsEquivalent(oldTarget, newTarget)) { - final PsiParameter[] oldParameters = oldTarget.getParameterList().getParameters(); - if (oldParameters.length > 0) { - final PsiMethodCallExpression copy = (PsiMethodCallExpression)call.copy(); - final PsiExpression[] args = copy.getArgumentList().getExpressions(); - for (int i = 0; i < args.length; i++) { - PsiExpression arg = args[i]; - PsiType paramType = i < oldParameters.length ? oldParameters[i].getType() : oldParameters[oldParameters.length - 1].getType(); - final PsiTypeCastExpression cast = (PsiTypeCastExpression)factory.createExpressionFromText("(a)b", null); - final PsiTypeElement typeElement = cast.getCastType(); - assert typeElement != null; - typeElement.replace(factory.createTypeElement(paramType)); - final PsiExpression operand = cast.getOperand(); - assert operand != null; - operand.replace(arg); - arg.replace(cast); - } - - for (int i = 0; i < copy.getArgumentList().getExpressions().length; i++) { - PsiExpression oldarg = call.getArgumentList().getExpressions()[i]; - PsiTypeCastExpression cast = (PsiTypeCastExpression)copy.getArgumentList().getExpressions()[i]; - if (!RedundantCastUtil.isCastRedundant(cast)) { - oldarg.replace(cast); - } - } - } - } - } -} diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java index 7b68e338aa63..b29d02fad178 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java @@ -791,7 +791,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { String[] names = myJavaCodeStyle.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, thisType) .names; String thisVarName = names[0]; - thisVarName = myJavaCodeStyle.suggestUniqueVariableName(thisVarName, block.getFirstChild(), true); + thisVarName = myJavaCodeStyle.suggestUniqueVariableName(thisVarName, myMethod.getFirstChild(), true); PsiExpression initializer = myFactory.createExpressionFromText("null", null); PsiDeclarationStatement declaration = myFactory.createVariableDeclarationStatement(thisVarName, thisType, initializer); declaration = (PsiDeclarationStatement)block.addAfter(declaration, null); diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java index 731daff180c9..a751acea42df 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java @@ -405,7 +405,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory(); try { PsiField field = factory.createFieldFromText(pattern.toString(), null); - field.getTypeElement().replace(factory.createTypeElement(type)); + final PsiTypeElement typeElement = factory.createTypeElement(type); + field.getTypeElement().replace(typeElement); field = (PsiField)CodeStyleManager.getInstance(psiManager.getProject()).reformat(field); if (includeInitializer) { field.getInitializer().replace(initializerExpr); diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java index 4238c2181f0b..ceae8aed2e44 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java @@ -224,6 +224,11 @@ public class InplaceIntroduceConstantPopup extends AbstractInplaceIntroduceField } @Override + protected String getRefactoringId() { + return "refactoring.extractConstant"; + } + + @Override protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) { return super.startsOnTheSameElement(handler, element) && handler instanceof IntroduceConstantHandler; } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java index 4c1de5e39551..3998a93d1b54 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java @@ -148,6 +148,11 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop } } + @Override + protected String getRefactoringId() { + return "refactoring.extractField"; + } + public void setVisibility(String visibility) { myIntroduceFieldPanel.setVisibility(visibility); } @@ -203,6 +208,8 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop protected void performIntroduce() { ourLastInitializerPlace = myIntroduceFieldPanel.getInitializerPlace(); + final PsiType forcedType = getType(); + LOG.assertTrue(forcedType == null || forcedType.isValid(), forcedType); final BaseExpressionToFieldHandler.Settings settings = new BaseExpressionToFieldHandler.Settings(getInputName(), getExpr(), @@ -211,7 +218,7 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop myIntroduceFieldPanel.isDeclareFinal(), myIntroduceFieldPanel.getInitializerPlace(), myIntroduceFieldPanel.getFieldVisibility(), (PsiLocalVariable)getLocalVariable(), - getType(), + forcedType, myIntroduceFieldPanel.isDeleteVariable(), myParentClass, false, false); new WriteCommandAction(myProject, getCommandName(), getCommandName()){ diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java index dc1f1668d744..152172597d6e 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java @@ -348,6 +348,15 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem return data; } + @Nullable + @Override + protected RefactoringEventData getAfterData(UsageInfo[] usages) { + final PsiParameter parameter = JavaIntroduceParameterMethodUsagesProcessor.getAnchorParameter(myMethodToReplaceIn); + final RefactoringEventData afterData = new RefactoringEventData(); + afterData.addElement(parameter); + return afterData; + } + protected void performRefactoring(UsageInfo[] usages) { try { PsiElementFactory factory = JavaPsiFacade.getInstance(myManager.getProject()).getElementFactory(); diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 0e513d4d39d0..16fc097bc612 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -61,6 +61,8 @@ import com.intellij.refactoring.*; import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer; import com.intellij.refactoring.introduce.inplace.OccurrencesChooser; import com.intellij.refactoring.introduceField.ElementToWorkOn; +import com.intellij.refactoring.listeners.RefactoringEventData; +import com.intellij.refactoring.listeners.RefactoringEventListener; import com.intellij.refactoring.ui.TypeSelectorManagerImpl; import com.intellij.refactoring.util.CommonRefactoringUtil; import com.intellij.refactoring.util.FieldConflictsResolver; @@ -85,7 +87,8 @@ import java.util.*; public abstract class IntroduceVariableBase extends IntroduceHandlerBase { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceVariable.IntroduceVariableBase"); @NonNls private static final String PREFER_STATEMENTS_OPTION = "introduce.variable.prefer.statements"; - + @NonNls private static final String REFACTORING_ID = "refactoring.extractVariable"; + protected static final String REFACTORING_NAME = RefactoringBundle.message("introduce.variable.title"); public static final Key<Boolean> NEED_PARENTHESIS = Key.create("NEED_PARENTHESIS"); @@ -677,13 +680,27 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { occurrenceMarkers.add(topLevelEditor.getDocument().createRangeMarker(occurrence.getTextRange())); } } + final RefactoringEventData beforeData = new RefactoringEventData(); + beforeData.addElement(expr); + project.getMessageBus() + .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, beforeData); final String expressionText = expr.getText(); final Runnable runnable = introduce(project, expr, topLevelEditor, chosenAnchor, occurrences, settings, variable); CommandProcessor.getInstance().executeCommand( project, new Runnable() { public void run() { - ApplicationManager.getApplication().runWriteAction(runnable); + try { + ApplicationManager.getApplication().runWriteAction(runnable); + } + finally { + final RefactoringEventData afterData = new RefactoringEventData(); + final SmartPsiElementPointer<PsiVariable> pointer = variable.get(); + afterData.addElement(pointer != null ? pointer.getElement() : null); + project.getMessageBus() + .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, afterData); + } + if (isInplaceAvailableOnDataContext) { final PsiVariable elementToRename = variable.get().getElement(); if (elementToRename != null) { diff --git a/java/java-impl/src/com/intellij/refactoring/memberPullUp/JavaPullUpHelper.java b/java/java-impl/src/com/intellij/refactoring/memberPullUp/JavaPullUpHelper.java index 39913cdf2a60..ab86ee853b3b 100644 --- a/java/java-impl/src/com/intellij/refactoring/memberPullUp/JavaPullUpHelper.java +++ b/java/java-impl/src/com/intellij/refactoring/memberPullUp/JavaPullUpHelper.java @@ -219,7 +219,10 @@ public class JavaPullUpHelper implements PullUpHelper<MemberInfo> { } } PsiMethod methodCopy = (PsiMethod)method.copy(); - if (method.findSuperMethods(myTargetSuperClass).length == 0) { + Language language = myTargetSuperClass.getLanguage(); + final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false); + if (superClassMethod != null && superClassMethod.findDeepestSuperMethods().length == 0 || + method.findSuperMethods(myTargetSuperClass).length == 0) { deleteOverrideAnnotationIfFound(methodCopy); } boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT); @@ -238,7 +241,14 @@ public class JavaPullUpHelper implements PullUpHelper<MemberInfo> { myJavaDocPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), method.getDocComment(), isOriginalMethodAbstract); - final PsiMember movedElement = anchor != null ? (PsiMember)myTargetSuperClass.addBefore(methodCopy, anchor) : (PsiMember)myTargetSuperClass.add(methodCopy); + final PsiMember movedElement; + if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) { + movedElement = (PsiMember)superClassMethod.replace(convertMethodToLanguage(methodCopy, language)); + } + else { + movedElement = + anchor != null ? (PsiMember)myTargetSuperClass.addBefore(methodCopy, anchor) : (PsiMember)myTargetSuperClass.add(methodCopy); + } CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject()); if (styleSettings.INSERT_OVERRIDE_ANNOTATION) { if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myIsTargetInterface || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) { @@ -265,8 +275,6 @@ public class JavaPullUpHelper implements PullUpHelper<MemberInfo> { RefactoringUtil.replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory); fixReferencesToStatic(methodCopy); - Language language = myTargetSuperClass.getLanguage(); - final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false); if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) { superClassMethod.replace(convertMethodToLanguage(methodCopy, language)); } diff --git a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java index b92c5f52bfa9..3fd4e334a01f 100644 --- a/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java +++ b/java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpDialog.java @@ -196,7 +196,7 @@ public class PullUpDialog extends PullUpDialogBase<MemberInfoStorage, MemberInfo final PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(currentSuperClass, myClass, PsiSubstitutor.EMPTY); final MethodSignature signature = ((PsiMethod) element).getSignature(superSubstitutor); final PsiMethod superClassMethod = MethodSignatureUtil.findMethodBySignature(currentSuperClass, signature, false); - if (superClassMethod != null) return false; + if (superClassMethod != null && !PsiUtil.isLanguageLevel8OrHigher(currentSuperClass)) return false; return !((PsiModifierListOwner) element).hasModifierProperty(PsiModifier.STATIC) || PsiUtil.isLanguageLevel8OrHigher(currentSuperClass); } return true; diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/AutocreatingSingleSourceRootMoveDestination.java b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/AutocreatingSingleSourceRootMoveDestination.java index c1bc3bf60038..ce19d6bacaa5 100644 --- a/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/AutocreatingSingleSourceRootMoveDestination.java +++ b/java/java-impl/src/com/intellij/refactoring/move/moveClassesOrPackages/AutocreatingSingleSourceRootMoveDestination.java @@ -102,6 +102,6 @@ public class AutocreatingSingleSourceRootMoveDestination extends AutocreatingMov if (myTargetDirectory == null) { myTargetDirectory = RefactoringUtil.createPackageDirectoryInSourceRoot(myPackage, mySourceRoot); } - return RefactoringUtil.createPackageDirectoryInSourceRoot(myPackage, mySourceRoot); + return myTargetDirectory; } } diff --git a/java/java-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesImpl.java b/java/java-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesImpl.java index 895334d12abe..5e5b620bcd31 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesImpl.java +++ b/java/java-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesImpl.java @@ -131,7 +131,7 @@ public class DuplicatesImpl { } HighlightManager.getInstance(project).removeSegmentHighlighter(editor, highlighters.get(0)); - new WriteCommandAction(project, MethodDuplicatesHandler.REFACTORING_NAME) { + new WriteCommandAction(project, MethodDuplicatesHandler.REFACTORING_NAME, MethodDuplicatesHandler.REFACTORING_NAME) { @Override protected void run(Result result) throws Throwable { try { |