show setting with generate annotations right on extract method dialog

This commit is contained in:
Anna Kozlova
2014-11-28 15:22:31 +01:00
parent 1d6f2590bf
commit f0a876431a
18 changed files with 73 additions and 25 deletions
@@ -134,21 +134,6 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
};
analyzeDfaWithNestedClosures(scope, holder, dfaRunner, Arrays.asList(dfaRunner.createMemoryState()), onTheFly);
}
@Nullable
public static Collection<PsiElement> getNullableReturn(PsiMethod method) {
final StandardDataFlowRunner dfaRunner = new StandardDataFlowRunner();
final DataFlowInstructionVisitor visitor = new DataFlowInstructionVisitor(dfaRunner);
final PsiCodeBlock body = method.getBody();
if (body == null) {
return null;
}
final RunnerResult rc = dfaRunner.analyzeMethod(body, visitor);
if (rc == RunnerResult.OK) {
return visitor.getProblems(NullabilityProblem.nullableReturn);
}
return null;
}
private void analyzeDfaWithNestedClosures(PsiElement scope,
ProblemsHolder holder,
@@ -61,7 +61,8 @@ import java.awt.event.*;
*/
@SuppressWarnings("MethodMayBeStatic")
public class ExtractMethodDialog extends DialogWrapper implements AbstractExtractDialog {
public static final String EXTRACT_METHOD_DEFAULT_VISIBILITY = "extract.method.default.visibility";
private static final String EXTRACT_METHOD_DEFAULT_VISIBILITY = "extract.method.default.visibility";
public static final String EXTRACT_METHOD_GENERATE_ANNOTATIONS = "extractMethod.generateAnnotations";
private final Project myProject;
private final PsiType myReturnType;
private final PsiTypeParameterList myTypeParameterList;
@@ -75,6 +76,7 @@ public class ExtractMethodDialog extends DialogWrapper implements AbstractExtrac
private final MethodSignatureComponent mySignature;
private final JCheckBox myMakeStatic;
protected JCheckBox myMakeVarargs;
protected JCheckBox myGenerateAnnotations;
private JCheckBox myCbChainedConstructor;
private final InputVariables myVariableData;
@@ -196,6 +198,10 @@ public class ExtractMethodDialog extends DialogWrapper implements AbstractExtrac
if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.PUBLIC)) {
PropertiesComponent.getInstance(myProject).setValue(EXTRACT_METHOD_DEFAULT_VISIBILITY, getVisibility());
}
if (myGenerateAnnotations != null) {
PropertiesComponent.getInstance(myProject).setValue(EXTRACT_METHOD_GENERATE_ANNOTATIONS, String.valueOf(myGenerateAnnotations.isSelected()));
}
super.doOKAction();
}
@@ -287,6 +293,13 @@ public class ExtractMethodDialog extends DialogWrapper implements AbstractExtrac
optionsPanel.add(myMakeVarargs);
}
if (!(myReturnType instanceof PsiPrimitiveType) && PsiUtil.isLanguageLevel5OrHigher(myTargetClass)) {
final boolean isSelected = PropertiesComponent.getInstance(myProject).getBoolean(EXTRACT_METHOD_GENERATE_ANNOTATIONS, true);
myGenerateAnnotations = new JCheckBox("Generate annotations", isSelected);
//todo update signature?!
optionsPanel.add(myGenerateAnnotations);
}
if (myCbChainedConstructor != null) {
optionsPanel.add(myCbChainedConstructor);
myCbChainedConstructor.setBorder(emptyBorder);
@@ -31,6 +31,7 @@ import com.intellij.codeInspection.dataFlow.*;
import com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction;
import com.intellij.codeInspection.dataFlow.instructions.Instruction;
import com.intellij.ide.DataManager;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.PsiClassListCellRenderer;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
@@ -867,15 +868,25 @@ public class ExtractMethodProcessor implements MatchProvider {
}
}
if (isNullabilityCheckApplicable() && !(newMethod.getReturnType() instanceof PsiPrimitiveType) &&
PsiUtil.isLanguageLevel5OrHigher(newMethod) && Registry.is("annotate.extracted.method.nullable.when.applicable", true)) {
if (isNullabilityCheckApplicable() && !(newMethod.getReturnType() instanceof PsiPrimitiveType) && PsiUtil.isLanguageLevel5OrHigher(newMethod) &&
PropertiesComponent.getInstance(myProject).getBoolean(ExtractMethodDialog.EXTRACT_METHOD_GENERATE_ANNOTATIONS, true)) {
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
final PsiClass nullableAnnotationClass =
JavaPsiFacade.getInstance(myProject).findClass(manager.getDefaultNullable(), GlobalSearchScope.allScope(myProject));
if (nullableAnnotationClass != null) {
final Collection<PsiElement> nullableReturn = DataFlowInspectionBase.getNullableReturn(newMethod);
if (nullableReturn != null && !nullableReturn.isEmpty()) {
final AddNullableNotNullAnnotationFix annotationFix = new AddNullableAnnotationFix(newMethod);
final Nullness nullness = DfaUtil.inferMethodNullity(newMethod);
AddNullableNotNullAnnotationFix annotationFix;
switch (nullness) {
case NOT_NULL:
annotationFix = new AddNotNullAnnotationFix(newMethod);
break;
case NULLABLE:
annotationFix = new AddNullableAnnotationFix(newMethod);
break;
default:
annotationFix = null;
}
if (annotationFix != null) {
annotationFix.invoke(myProject, myTargetClass.getContainingFile(), newMethod, newMethod);
}
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
class Test {
void foo(Object x) {
@@ -5,6 +7,7 @@ class Test {
if (x instanceof String) x = newMethod((String) x);
}
@NotNull
private String newMethod(String x) {
return x.substring(1);
}
@@ -1,9 +1,12 @@
import org.jetbrains.annotations.NotNull;
class Test {
void foo() {
bar(newMethod());
baz(newMethod());
}
@NotNull
private String newMethod() {
return String.valueOf(1);
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
import java.util.*;
class C {
@@ -11,6 +13,7 @@ class C {
System.out.println("l1 = " + l1 + ", l2 = " + l2);
}
@NotNull
private List newMethod(Object[] array) {
List l1 = null;
l1 = new ArrayList(Arrays.asList(array));
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
import java.util.*;
class C {
@@ -11,6 +13,7 @@ class C {
System.out.println("l1 = " + l1 + ", l2 = " + l2);
}
@NotNull
private List newMethod(Object[] array) {
return new ArrayList(Arrays.asList(array));
}
@@ -1,9 +1,12 @@
import org.jetbrains.annotations.NotNull;
class C {
String method(Object o) {
System.out.println(o);
return newMethod(o);
}
@NotNull
private String newMethod(Object o) {
Integer i = new Integer(o.hashCode());
return i.toString();
@@ -1,9 +1,12 @@
import org.jetbrains.annotations.NotNull;
class C {
String method(Object o) {
System.out.println(o);
return newMethod(o);
}
@NotNull
private String newMethod(Object o) {
Integer i = new Integer(o.hashCode());
return i.toString();
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
import java.util.*;
class C {
@@ -8,6 +10,7 @@ class C {
List l1 = newMethod(new Object[0]);
}
@NotNull
private ArrayList newMethod(Object[] o) {
return new ArrayList(Arrays.asList(o));
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
public class S {
{
String s;
@@ -8,6 +10,7 @@ public class S {
System.out.print(s);
}
@NotNull
private String newMethod() {
String s;
s = "";
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
class Test {
interface I {
String foo();
@@ -8,6 +10,7 @@ class Test {
};
}
@NotNull
private String newMethod() {
return "42";
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
class Test {
void foo() {
final String str = newMethod();
@@ -9,6 +11,7 @@ class Test {
}
}
@NotNull
private String newMethod() {
final String str = "";
if (str == "") {
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
class A {
public String method() {
try {
@@ -9,6 +11,7 @@ class A {
return "";
}
@NotNull
private String newMethod() {
try {
return "";
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
public class Foo {
static Foo
f1 = new Foo(){
@@ -6,6 +8,7 @@ public class Foo {
}
};
@NotNull
private static String newMethod() {
return "a" + "b";
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
public class A {
A(String s) {
}
@@ -6,6 +8,7 @@ public class A {
this(newMethod());
}
@NotNull
private static String newMethod() {
return "a";
}
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;
class BasicLazyResolveTest {
@@ -9,6 +11,7 @@ class BasicLazyResolveTest {
}
@NotNull
private Object newMethod() {
return new Object() {
@@ -497,7 +497,4 @@ editor.config.stop.at.project.root=true
editor.config.stop.at.project.root.description=Stops searching for .editorconfig at project root (requires project reopening)
JDK8042508.bug.fixed=false
JDK8042508.bug.fixed.description=Disable check for type variable until javac bug is fixed
annotate.extracted.method.nullable.when.applicable=true
annotate.extracted.method.nullable.when.applicable.description=Enables @Nullable annotation on newly extracted method when applicable
JDK8042508.bug.fixed.description=Disable check for type variable until javac bug is fixed