mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-07 05:09:37 +07:00
insert same notnull annotation as exist on the existing field (IDEA-79436)
This commit is contained in:
@@ -270,12 +270,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(field.getProject());
|
notNull(project, field, parameter);
|
||||||
if (nullableManager.isNotNull(field, false)) {
|
|
||||||
final PsiAnnotation annotation = JavaPsiFacade.getElementFactory(project).createAnnotationFromText(
|
|
||||||
"@" + nullableManager.getDefaultNotNull(), field);
|
|
||||||
parameter.getModifierList().addBefore(annotation, null);
|
|
||||||
}
|
|
||||||
AssignFieldFromParameterAction.addFieldAssignmentStatement(project, field, parameter, editor);
|
AssignFieldFromParameterAction.addFieldAssignmentStatement(project, field, parameter, editor);
|
||||||
created = true;
|
created = true;
|
||||||
}
|
}
|
||||||
@@ -283,6 +278,14 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
|
|||||||
return created;
|
return created;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void notNull(Project project, PsiField field, PsiParameter parameter) {
|
||||||
|
final String notNull = NullableNotNullManager.getInstance(field.getProject()).getNotNull(field);
|
||||||
|
if (notNull != null) {
|
||||||
|
final PsiAnnotation annotation = JavaPsiFacade.getElementFactory(project).createAnnotationFromText("@" + notNull, field);
|
||||||
|
parameter.getModifierList().addBefore(annotation, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static PsiParameter findParamByName(String newName, PsiParameter[] newParameters) {
|
private static PsiParameter findParamByName(String newName, PsiParameter[] newParameters) {
|
||||||
PsiParameter parameter = null;
|
PsiParameter parameter = null;
|
||||||
|
|||||||
@@ -274,9 +274,9 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
|
|||||||
PsiParameter parm = factory.createParameter(parmName, field.getType());
|
PsiParameter parm = factory.createParameter(parmName, field.getType());
|
||||||
|
|
||||||
final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(field.getProject());
|
final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(field.getProject());
|
||||||
if (nullableManager.isNotNull(field, false)) {
|
final String notNull = nullableManager.getNotNull(field);
|
||||||
final PsiAnnotation annotation = factory.createAnnotationFromText("@" + nullableManager.getDefaultNotNull(), field);
|
if (notNull != null) {
|
||||||
parm.getModifierList().addAfter(annotation, null);
|
parm.getModifierList().addAfter(factory.createAnnotationFromText("@" + notNull, field), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor.getParameterList().add(parm);
|
constructor.getParameterList().add(parm);
|
||||||
|
|||||||
@@ -216,10 +216,14 @@ public class CreateFieldFromParameterAction implements IntentionAction {
|
|||||||
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
|
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
|
||||||
|
|
||||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
|
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
|
||||||
if (manager.isNullable(myParameter, false)) {
|
final String nullable = manager.getNullable(myParameter);
|
||||||
modifierList.addAfter(factory.createAnnotationFromText("@" + manager.getDefaultNullable(), field), null);
|
if (nullable != null) {
|
||||||
} else if (isFinal && manager.isNotNull(myParameter, false)) {
|
modifierList.addAfter(factory.createAnnotationFromText("@" + nullable, field), null);
|
||||||
modifierList.addAfter(factory.createAnnotationFromText("@" + manager.getDefaultNotNull(), field), null);
|
} else if (isFinal) {
|
||||||
|
final String notNull = manager.getNotNull(myParameter);
|
||||||
|
if (notNull != null) {
|
||||||
|
modifierList.addAfter(factory.createAnnotationFromText("@" + notNull, field), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PsiCodeBlock methodBody = method.getBody();
|
PsiCodeBlock methodBody = method.getBody();
|
||||||
|
|||||||
@@ -517,11 +517,15 @@ public class InheritanceToDelegationProcessor extends BaseRefactoringProcessor {
|
|||||||
final PsiModifierList modifierList = methodToAdd.getModifierList();
|
final PsiModifierList modifierList = methodToAdd.getModifierList();
|
||||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
|
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
|
||||||
modifierList.setModifierProperty(PsiModifier.ABSTRACT, false);
|
modifierList.setModifierProperty(PsiModifier.ABSTRACT, false);
|
||||||
if (manager.isNullable(method, false)) {
|
final String nullable = manager.getNullable(method);
|
||||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + manager.getDefaultNullable(), methodToAdd), null);
|
if (nullable != null) {
|
||||||
|
modifierList.addAfter(myFactory.createAnnotationFromText("@" + nullable, methodToAdd), null);
|
||||||
}
|
}
|
||||||
else if (manager.isNotNull(method, false)) {
|
else {
|
||||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + manager.getDefaultNotNull(), methodToAdd), null);
|
final String notNull = manager.getNotNull(method);
|
||||||
|
if (notNull != null) {
|
||||||
|
modifierList.addAfter(myFactory.createAnnotationFromText("@" + notNull, methodToAdd), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final String delegationBody = getDelegationBody(methodToAdd, delegationTarget);
|
final String delegationBody = getDelegationBody(methodToAdd, delegationTarget);
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Add constructor parameter" "true"
|
||||||
|
class A {
|
||||||
|
@javax.annotation.Nonnull private final Object field;
|
||||||
|
|
||||||
|
A(@javax.annotation.Nonnull Object field, String... strs) {
|
||||||
|
this.field = field;<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Add constructor parameter" "true"
|
||||||
|
class A {
|
||||||
|
@javax.annotation.Nonnull private final Object <caret>field;
|
||||||
|
|
||||||
|
A(String... strs) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import com.intellij.openapi.util.WriteExternalException;
|
|||||||
import com.intellij.psi.PsiModifierListOwner;
|
import com.intellij.psi.PsiModifierListOwner;
|
||||||
import org.jdom.Element;
|
import org.jdom.Element;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -88,6 +89,14 @@ public class NullableNotNullManager implements PersistentStateComponent<Element>
|
|||||||
public String getDefaultNullable() {
|
public String getDefaultNullable() {
|
||||||
return myDefaultNullable;
|
return myDefaultNullable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getNullable(PsiModifierListOwner owner) {
|
||||||
|
for (String nullable : myNullables) {
|
||||||
|
if (AnnotationUtil.isAnnotated(owner, nullable, false)) return nullable;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDefaultNullable(@NotNull String defaultNullable) {
|
public void setDefaultNullable(@NotNull String defaultNullable) {
|
||||||
LOG.assertTrue(getNullables().contains(defaultNullable));
|
LOG.assertTrue(getNullables().contains(defaultNullable));
|
||||||
@@ -97,6 +106,14 @@ public class NullableNotNullManager implements PersistentStateComponent<Element>
|
|||||||
public String getDefaultNotNull() {
|
public String getDefaultNotNull() {
|
||||||
return myDefaultNotNull;
|
return myDefaultNotNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getNotNull(PsiModifierListOwner owner) {
|
||||||
|
for (String notNull : myNotNulls) {
|
||||||
|
if (AnnotationUtil.isAnnotated(owner, notNull, false)) return notNull;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDefaultNotNull(@NotNull String defaultNotNull) {
|
public void setDefaultNotNull(@NotNull String defaultNotNull) {
|
||||||
LOG.assertTrue(getNotNulls().contains(defaultNotNull));
|
LOG.assertTrue(getNotNulls().contains(defaultNotNull));
|
||||||
|
|||||||
@@ -478,11 +478,15 @@ public class PropertyUtil {
|
|||||||
private static void annotateWithNullableStuff(final PsiModifierListOwner field, final PsiElementFactory factory, final PsiModifierListOwner listOwner)
|
private static void annotateWithNullableStuff(final PsiModifierListOwner field, final PsiElementFactory factory, final PsiModifierListOwner listOwner)
|
||||||
throws IncorrectOperationException {
|
throws IncorrectOperationException {
|
||||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
||||||
if (manager.isNotNull(field, false)) {
|
final String notNull = manager.getNotNull(field);
|
||||||
annotate(factory, listOwner, manager.getDefaultNotNull());
|
if (notNull != null) {
|
||||||
|
annotate(factory, listOwner, notNull);
|
||||||
}
|
}
|
||||||
else if (manager.isNullable(field, false)) {
|
else {
|
||||||
annotate(factory, listOwner, manager.getDefaultNullable());
|
final String nullable = manager.getNullable(field);
|
||||||
|
if (nullable != null) {
|
||||||
|
annotate(factory, listOwner, nullable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -552,11 +552,15 @@ public class GroovyPropertyUtils {
|
|||||||
private static void annotateWithNullableStuff(final PsiModifierListOwner field, final PsiModifierListOwner listOwner)
|
private static void annotateWithNullableStuff(final PsiModifierListOwner field, final PsiModifierListOwner listOwner)
|
||||||
throws IncorrectOperationException {
|
throws IncorrectOperationException {
|
||||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
||||||
if (manager.isNotNull(field, false)) {
|
final String notNull = manager.getNotNull(field);
|
||||||
annotate(listOwner, manager.getDefaultNotNull());
|
if (notNull != null) {
|
||||||
|
annotate(listOwner, notNull);
|
||||||
}
|
}
|
||||||
else if (manager.isNullable(field, false)) {
|
else {
|
||||||
annotate(listOwner, manager.getDefaultNullable());
|
final String nullable = manager.getNullable(field);
|
||||||
|
if (nullable != null) {
|
||||||
|
annotate(listOwner, nullable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final PsiModifierList modifierList = listOwner.getModifierList();
|
final PsiModifierList modifierList = listOwner.getModifierList();
|
||||||
|
|||||||
Reference in New Issue
Block a user