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;
|
||||
}
|
||||
}
|
||||
final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(field.getProject());
|
||||
if (nullableManager.isNotNull(field, false)) {
|
||||
final PsiAnnotation annotation = JavaPsiFacade.getElementFactory(project).createAnnotationFromText(
|
||||
"@" + nullableManager.getDefaultNotNull(), field);
|
||||
parameter.getModifierList().addBefore(annotation, null);
|
||||
}
|
||||
notNull(project, field, parameter);
|
||||
AssignFieldFromParameterAction.addFieldAssignmentStatement(project, field, parameter, editor);
|
||||
created = true;
|
||||
}
|
||||
@@ -283,6 +278,14 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
|
||||
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
|
||||
private static PsiParameter findParamByName(String newName, PsiParameter[] newParameters) {
|
||||
PsiParameter parameter = null;
|
||||
|
||||
@@ -274,9 +274,9 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
|
||||
PsiParameter parm = factory.createParameter(parmName, field.getType());
|
||||
|
||||
final NullableNotNullManager nullableManager = NullableNotNullManager.getInstance(field.getProject());
|
||||
if (nullableManager.isNotNull(field, false)) {
|
||||
final PsiAnnotation annotation = factory.createAnnotationFromText("@" + nullableManager.getDefaultNotNull(), field);
|
||||
parm.getModifierList().addAfter(annotation, null);
|
||||
final String notNull = nullableManager.getNotNull(field);
|
||||
if (notNull != null) {
|
||||
parm.getModifierList().addAfter(factory.createAnnotationFromText("@" + notNull, field), null);
|
||||
}
|
||||
|
||||
constructor.getParameterList().add(parm);
|
||||
|
||||
@@ -216,10 +216,14 @@ public class CreateFieldFromParameterAction implements IntentionAction {
|
||||
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
|
||||
|
||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
|
||||
if (manager.isNullable(myParameter, false)) {
|
||||
modifierList.addAfter(factory.createAnnotationFromText("@" + manager.getDefaultNullable(), field), null);
|
||||
} else if (isFinal && manager.isNotNull(myParameter, false)) {
|
||||
modifierList.addAfter(factory.createAnnotationFromText("@" + manager.getDefaultNotNull(), field), null);
|
||||
final String nullable = manager.getNullable(myParameter);
|
||||
if (nullable != null) {
|
||||
modifierList.addAfter(factory.createAnnotationFromText("@" + nullable, 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();
|
||||
|
||||
@@ -517,11 +517,15 @@ public class InheritanceToDelegationProcessor extends BaseRefactoringProcessor {
|
||||
final PsiModifierList modifierList = methodToAdd.getModifierList();
|
||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
|
||||
modifierList.setModifierProperty(PsiModifier.ABSTRACT, false);
|
||||
if (manager.isNullable(method, false)) {
|
||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + manager.getDefaultNullable(), methodToAdd), null);
|
||||
final String nullable = manager.getNullable(method);
|
||||
if (nullable != null) {
|
||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + nullable, methodToAdd), null);
|
||||
}
|
||||
else if (manager.isNotNull(method, false)) {
|
||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + manager.getDefaultNotNull(), methodToAdd), null);
|
||||
else {
|
||||
final String notNull = manager.getNotNull(method);
|
||||
if (notNull != null) {
|
||||
modifierList.addAfter(myFactory.createAnnotationFromText("@" + notNull, methodToAdd), null);
|
||||
}
|
||||
}
|
||||
|
||||
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 org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -89,6 +90,14 @@ public class NullableNotNullManager implements PersistentStateComponent<Element>
|
||||
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) {
|
||||
LOG.assertTrue(getNullables().contains(defaultNullable));
|
||||
myDefaultNullable = defaultNullable;
|
||||
@@ -98,6 +107,14 @@ public class NullableNotNullManager implements PersistentStateComponent<Element>
|
||||
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) {
|
||||
LOG.assertTrue(getNotNulls().contains(defaultNotNull));
|
||||
myDefaultNotNull = defaultNotNull;
|
||||
|
||||
@@ -478,11 +478,15 @@ public class PropertyUtil {
|
||||
private static void annotateWithNullableStuff(final PsiModifierListOwner field, final PsiElementFactory factory, final PsiModifierListOwner listOwner)
|
||||
throws IncorrectOperationException {
|
||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
||||
if (manager.isNotNull(field, false)) {
|
||||
annotate(factory, listOwner, manager.getDefaultNotNull());
|
||||
final String notNull = manager.getNotNull(field);
|
||||
if (notNull != null) {
|
||||
annotate(factory, listOwner, notNull);
|
||||
}
|
||||
else if (manager.isNullable(field, false)) {
|
||||
annotate(factory, listOwner, manager.getDefaultNullable());
|
||||
else {
|
||||
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)
|
||||
throws IncorrectOperationException {
|
||||
final NullableNotNullManager manager = NullableNotNullManager.getInstance(field.getProject());
|
||||
if (manager.isNotNull(field, false)) {
|
||||
annotate(listOwner, manager.getDefaultNotNull());
|
||||
final String notNull = manager.getNotNull(field);
|
||||
if (notNull != null) {
|
||||
annotate(listOwner, notNull);
|
||||
}
|
||||
else if (manager.isNullable(field, false)) {
|
||||
annotate(listOwner, manager.getDefaultNullable());
|
||||
else {
|
||||
final String nullable = manager.getNullable(field);
|
||||
if (nullable != null) {
|
||||
annotate(listOwner, nullable);
|
||||
}
|
||||
}
|
||||
|
||||
final PsiModifierList modifierList = listOwner.getModifierList();
|
||||
|
||||
Reference in New Issue
Block a user