mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Remove GeneralizeCatchFix as more general alternative exists
This commit is contained in:
@@ -153,9 +153,6 @@ public abstract class QuickFixFactory {
|
||||
@NotNull
|
||||
public abstract IntentionAction createAddExceptionToExistingCatch(@NotNull PsiElement element);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createGeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType type);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createChangeToAppendFix(@NotNull IElementType sign,
|
||||
@NotNull PsiType type,
|
||||
|
||||
-3
@@ -192,9 +192,6 @@ public class HighlightFixUtil {
|
||||
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddExceptionFromFieldInitializerToConstructorThrowsFix(element));
|
||||
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createSurroundWithTryCatchFix(element));
|
||||
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddExceptionToExistingCatch(element));
|
||||
if (unhandled.size() == 1) {
|
||||
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createGeneralizeCatchFix(element, unhandled.get(0)));
|
||||
}
|
||||
}
|
||||
|
||||
static void registerStaticProblemQuickFixAction(@NotNull PsiElement refElement, HighlightInfo errorResult, @NotNull PsiJavaCodeReferenceElement place) {
|
||||
|
||||
-99
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class GeneralizeCatchFix implements IntentionAction {
|
||||
private final PsiElement myElement;
|
||||
private final PsiClassType myUnhandledException;
|
||||
private PsiTryStatement myTryStatement;
|
||||
private PsiParameter myCatchParameter;
|
||||
|
||||
public GeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType unhandledException) {
|
||||
myElement = element;
|
||||
myUnhandledException = unhandledException;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("generalize.catch.text",
|
||||
JavaHighlightUtil.formatType(myCatchParameter == null ? null : myCatchParameter.getType()),
|
||||
JavaHighlightUtil.formatType(myUnhandledException));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return QuickFixBundle.message("generalize.catch.family");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (!(myElement.isValid()
|
||||
&& myUnhandledException.isValid()
|
||||
&& BaseIntentionAction.canModify(myElement))) return false;
|
||||
// find enclosing try
|
||||
PsiElement element = myElement;
|
||||
while (element != null) {
|
||||
if (PsiUtil.isTryBlock(element) || element instanceof PsiResourceList) {
|
||||
myTryStatement = (PsiTryStatement)element.getParent();
|
||||
break;
|
||||
}
|
||||
if (element instanceof PsiMethod || element instanceof PsiFunctionalExpression || (element instanceof PsiClass && !(element instanceof PsiAnonymousClass))) break;
|
||||
element = element.getParent();
|
||||
}
|
||||
if (myTryStatement == null) return false;
|
||||
// check we can generalize at least one catch
|
||||
PsiParameter[] catchBlockParameters = myTryStatement.getCatchBlockParameters();
|
||||
for (PsiParameter catchBlockParameter : catchBlockParameters) {
|
||||
PsiType type = catchBlockParameter.getType();
|
||||
if (myUnhandledException.isAssignableFrom(type)) {
|
||||
myCatchParameter = catchBlockParameter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return myCatchParameter != null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
|
||||
return myElement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(myElement.getProject());
|
||||
PsiTypeElement type = factory.createTypeElement(myUnhandledException);
|
||||
myCatchParameter.getTypeElement().replace(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
-6
@@ -230,12 +230,6 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
return new AddExceptionToExistingCatchFix(element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createGeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType type) {
|
||||
return new GeneralizeCatchFix(element, type);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createChangeToAppendFix(@NotNull IElementType sign,
|
||||
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// "Generalize catch for 'java.io.FileNotFoundException' to 'java.io.IOException'" "true"
|
||||
import java.io.*;
|
||||
|
||||
|
||||
public class c1 {
|
||||
void f() {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream("");
|
||||
DataInputStream dis = new DataInputStream(fis);
|
||||
dis.<caret>readInt();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
// "Generalize catch for 'C.E1' to 'C.E'" "true"
|
||||
class C {
|
||||
static class E extends Exception { }
|
||||
static class E1 extends E { }
|
||||
|
||||
static class MyResource implements AutoCloseable {
|
||||
public MyResource() throws E1 { }
|
||||
public void close() throws E { }
|
||||
}
|
||||
|
||||
void f() {
|
||||
try (MyResource r = new MyResource()) {
|
||||
} catch (E e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// "Generalize catch for 'java.io.FileNotFoundException' to 'java.io.IOException'" "true"
|
||||
import java.io.*;
|
||||
|
||||
|
||||
public class c1 {
|
||||
void f() {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream("");
|
||||
DataInputStream dis = new DataInputStream(fis);
|
||||
dis.<caret>readInt();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// "Generalize catch for 'java.io.FileNotFoundException' to 'java.lang.IllegalAccessException'" "false"
|
||||
import java.io.*;
|
||||
|
||||
|
||||
public class c1 {
|
||||
void f() {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream("");
|
||||
DataInputStream dis = new DataInputStream(fis);
|
||||
<caret>throw new IllegalAccessException();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
// "Generalize catch for 'C.E1' to 'C.E'" "true"
|
||||
class C {
|
||||
static class E extends Exception { }
|
||||
static class E1 extends E { }
|
||||
|
||||
static class MyResource implements AutoCloseable {
|
||||
public MyResource() throws E1 { }
|
||||
public void close() throws E { }
|
||||
}
|
||||
|
||||
void f() {
|
||||
try (<caret>MyResource r = new MyResource()) {
|
||||
} catch (E1 e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// "Generalize catch for 'java.lang.Exception' to 'java.lang.Exception'" "false"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class C {
|
||||
static Object get() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
void method() {
|
||||
try {
|
||||
Supplier<Object> lambda1 = () -> C.ge<caret>t();
|
||||
} catch( Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// "Generalize catch for 'java.lang.Exception' to 'java.lang.Exception'" "false"
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class C {
|
||||
static Object get() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
void method() {
|
||||
try {
|
||||
Supplier<Object> lambda1 = C::g<caret>et;
|
||||
} catch( Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
|
||||
public class GeneralizeCatchTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/generalizeCatch";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user