insert same notnull annotation as exist on the existing field (IDEA-79436)

This commit is contained in:
anna
2011-12-29 13:07:29 +01:00
parent c81e2d4b9a
commit c3ce1ae00f
9 changed files with 78 additions and 25 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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();

View File

@@ -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 {
final String notNull = manager.getNotNull(method);
if (notNull != null) {
modifierList.addAfter(myFactory.createAnnotationFromText("@" + notNull, methodToAdd), null);
}
else if (manager.isNotNull(method, false)) {
modifierList.addAfter(myFactory.createAnnotationFromText("@" + manager.getDefaultNotNull(), methodToAdd), null);
}
final String delegationBody = getDelegationBody(methodToAdd, delegationTarget);

View File

@@ -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>
}
}

View File

@@ -0,0 +1,8 @@
// "Add constructor parameter" "true"
class A {
@javax.annotation.Nonnull private final Object <caret>field;
A(String... strs) {
}
}

View File

@@ -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;

View File

@@ -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 {
final String nullable = manager.getNullable(field);
if (nullable != null) {
annotate(factory, listOwner, nullable);
}
else if (manager.isNullable(field, false)) {
annotate(factory, listOwner, manager.getDefaultNullable());
}
}

View File

@@ -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 {
final String nullable = manager.getNullable(field);
if (nullable != null) {
annotate(listOwner, nullable);
}
else if (manager.isNullable(field, false)) {
annotate(listOwner, manager.getDefaultNullable());
}
final PsiModifierList modifierList = listOwner.getModifierList();