use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class ExtractMethodObjectProcessor method createField.
private PsiField createField(PsiParameter parameter, PsiMethod constructor, boolean isFinal) {
final String parameterName = parameter.getName();
PsiType type = parameter.getType();
if (type instanceof PsiEllipsisType)
type = ((PsiEllipsisType) type).toArrayType();
try {
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(getMethod().getProject());
final String fieldName = styleManager.suggestVariableName(VariableKind.FIELD, styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER), null, type).names[0];
PsiField field = myElementFactory.createField(fieldName, type);
final PsiModifierList modifierList = field.getModifierList();
LOG.assertTrue(modifierList != null);
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
if (manager.isNullable(parameter, false)) {
modifierList.addAfter(myElementFactory.createAnnotationFromText("@" + manager.getDefaultNullable(), field), null);
}
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
final PsiCodeBlock methodBody = constructor.getBody();
LOG.assertTrue(methodBody != null);
@NonNls final String stmtText;
if (Comparing.strEqual(parameterName, fieldName)) {
stmtText = "this." + fieldName + " = " + parameterName + ";";
} else {
stmtText = fieldName + " = " + parameterName + ";";
}
PsiStatement assignmentStmt = myElementFactory.createStatementFromText(stmtText, methodBody);
assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(constructor.getProject()).reformat(assignmentStmt);
methodBody.add(assignmentStmt);
field = (PsiField) myInnerClass.add(field);
return field;
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class AssignmentToNullInspection method buildFix.
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
final Object info = infos[0];
if (!(info instanceof PsiReferenceExpression)) {
return null;
}
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) info;
if (TypeUtils.isOptional(referenceExpression.getType())) {
return null;
}
final PsiElement target = referenceExpression.resolve();
if (!(target instanceof PsiVariable)) {
return null;
}
final PsiVariable variable = (PsiVariable) target;
if (NullableNotNullManager.isNotNull(variable)) {
return null;
}
final NullableNotNullManager manager = NullableNotNullManager.getInstance(target.getProject());
return new DelegatingFix(new AddAnnotationPsiFix(manager.getDefaultNullable(), variable, PsiNameValuePair.EMPTY_ARRAY));
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class FieldFromParameterUtils method createFieldAndAddAssignment.
public static void createFieldAndAddAssignment(@NotNull final Project project, @NotNull final PsiClass targetClass, @NotNull final PsiMethod method, @NotNull final PsiParameter parameter, @NotNull final PsiType fieldType, @NotNull final String fieldName, final boolean isStatic, final boolean isFinal) throws IncorrectOperationException {
PsiManager psiManager = PsiManager.getInstance(project);
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiManager.getProject());
PsiElementFactory factory = psiFacade.getElementFactory();
PsiField field = factory.createField(fieldName, fieldType);
PsiModifierList modifierList = field.getModifierList();
if (modifierList == null)
return;
modifierList.setModifierProperty(PsiModifier.STATIC, isStatic);
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
if (manager.copyNullableAnnotation(parameter, field) == null && isFinal) {
manager.copyNotNullAnnotation(parameter, field);
}
PsiCodeBlock methodBody = method.getBody();
if (methodBody == null)
return;
PsiStatement[] statements = methodBody.getStatements();
Ref<Pair<PsiField, Boolean>> anchorRef = new Ref<>();
int i = findFieldAssignmentAnchor(statements, anchorRef, targetClass, parameter);
Pair<PsiField, Boolean> fieldAnchor = anchorRef.get();
String stmtText = fieldName + " = " + parameter.getName() + ";";
final PsiVariable variable = psiFacade.getResolveHelper().resolveReferencedVariable(fieldName, methodBody);
if (variable != null && !(variable instanceof PsiField)) {
String prefix = isStatic ? targetClass.getName() == null ? "" : targetClass.getName() + "." : "this.";
stmtText = prefix + stmtText;
}
PsiStatement assignmentStmt = factory.createStatementFromText(stmtText, methodBody);
assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(project).reformat(assignmentStmt);
if (i == statements.length) {
methodBody.add(assignmentStmt);
} else {
methodBody.addAfter(assignmentStmt, i > 0 ? statements[i - 1] : null);
}
if (fieldAnchor != null) {
PsiVariable psiVariable = fieldAnchor.getFirst();
psiVariable.normalizeDeclaration();
}
if (targetClass.findFieldByName(fieldName, false) == null) {
if (fieldAnchor != null) {
Boolean insertBefore = fieldAnchor.getSecond();
PsiField inField = fieldAnchor.getFirst();
if (insertBefore.booleanValue()) {
targetClass.addBefore(field, inField);
} else {
targetClass.addAfter(field, inField);
}
} else {
targetClass.add(field);
}
}
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class NullableStuffInspectionTest method testOverrideCustomDefault.
public void testOverrideCustomDefault() {
DataFlowInspectionTest.addJavaxNullabilityAnnotations(myFixture);
myFixture.addClass("package custom;" + "public @interface CheckForNull {}");
final NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(getProject());
nnnManager.setNullables("custom.CheckForNull");
Disposer.register(myFixture.getTestRootDisposable(), new Disposable() {
@Override
public void dispose() {
nnnManager.setNullables();
}
});
myFixture.addClass("package foo;" + "import static java.lang.annotation.ElementType.*;" + "@javax.annotation.meta.TypeQualifierDefault(METHOD) " + "@javax.annotation.Nonnull " + "public @interface ReturnValuesAreNonnullByDefault {}");
myFixture.addFileToProject("foo/package-info.java", "@ReturnValuesAreNonnullByDefault package foo;");
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject(getTestName(false) + ".java", "foo/Classes.java"));
myFixture.enableInspections(myInspection);
myFixture.checkHighlighting(true, false, true);
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class DataFlowInspection8Test method setCustomAnnotations.
static void setCustomAnnotations(Project project, Disposable parentDisposable, String notNull, String nullable) {
NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(project);
nnnManager.setNotNulls(notNull);
nnnManager.setNullables(nullable);
Disposer.register(parentDisposable, () -> {
nnnManager.setNotNulls();
nnnManager.setNullables();
});
}
Aggregations