diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/FieldFromParameterUtils.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/FieldFromParameterUtils.java index 09fa70cfdd5a..cb330b1d0151 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/FieldFromParameterUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/FieldFromParameterUtils.java @@ -194,7 +194,10 @@ public final class FieldFromParameterUtils { modifierList.setModifierProperty(PsiModifier.STATIC, isStatic); modifierList.setModifierProperty(PsiModifier.FINAL, isFinal); - NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(parameter, field); + NullableNotNullManager manager = NullableNotNullManager.getInstance(project); + if (manager.copyNullableAnnotation(parameter, field) == null && isFinal) { + manager.copyNotNullAnnotation(parameter, field); + } PsiCodeBlock methodBody = method.getBody(); if (methodBody == null) return; diff --git a/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java b/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java index f2b43f58d0fd..846d8f5e6637 100644 --- a/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java +++ b/java/java-psi-api/src/com/intellij/codeInsight/NullableNotNullManager.java @@ -142,6 +142,11 @@ public abstract class NullableNotNullManager implements PersistentStateComponent return copyAnnotation(getNotNullAnnotation(original, false), generated); } + @Nullable + public PsiAnnotation copyNullableAnnotation(@NotNull PsiModifierListOwner original, @NotNull PsiModifierListOwner generated) { + return copyAnnotation(getNullableAnnotation(original, false), generated); + } + @Nullable public PsiAnnotation copyNullableOrNotNullAnnotation(@NotNull PsiModifierListOwner original, @NotNull PsiModifierListOwner generated) { PsiAnnotation annotation = getNullableAnnotation(original, false); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/afterNotNullNotFinal.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/afterNotNullNotFinal.java new file mode 100644 index 000000000000..f738f0a06255 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/afterNotNullNotFinal.java @@ -0,0 +1,11 @@ +// "Create field for parameter 'name'" "true" + +import org.jetbrains.annotations.NotNull; + +class Test { + private String myName; + + void f(@NotNull String name) { + myName = name; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/beforeNotNullNotFinal.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/beforeNotNullNotFinal.java new file mode 100644 index 000000000000..5fa9f6bcfa0e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter/beforeNotNullNotFinal.java @@ -0,0 +1,8 @@ +// "Create field for parameter 'name'" "true" + +import org.jetbrains.annotations.NotNull; + +class Test { + void f(@NotNull String name) { + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateFieldFromParameterTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateFieldFromParameterTest.java index f715e99539ec..88dc71d5d60c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateFieldFromParameterTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateFieldFromParameterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -16,7 +16,6 @@ package com.intellij.codeInsight.daemon.quickFix; import com.intellij.codeInsight.daemon.LightIntentionActionTestCase; -import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; /** @@ -26,21 +25,22 @@ public class CreateFieldFromParameterTest extends LightIntentionActionTestCase { @Override protected void setUp() throws Exception { super.setUp(); - CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); - settings.FIELD_NAME_PREFIX = "my"; + CodeStyleSettingsManager.getSettings(getProject()).FIELD_NAME_PREFIX = "my"; } @Override protected void tearDown() throws Exception { - CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); - settings.FIELD_NAME_PREFIX = ""; + CodeStyleSettingsManager.getSettings(getProject()).FIELD_NAME_PREFIX = ""; + //noinspection SuperTearDownInFinally super.tearDown(); } - public void test() throws Exception { doAllTests(); } + public void test() { + doAllTests(); + } @Override protected String getBasePath() { return "/codeInsight/daemonCodeAnalyzer/quickFix/createFieldFromParameter"; } -} +} \ No newline at end of file