use of com.intellij.codeInspection.dataFlow.Nullness in project intellij-community by JetBrains.
the class SliceNullnessAnalyzer method calcNullableLeaves.
@NotNull
static NullAnalysisResult calcNullableLeaves(@NotNull final SliceNode root, @NotNull AbstractTreeStructure treeStructure, @NotNull final Map<SliceNode, NullAnalysisResult> map) {
final SliceLeafAnalyzer.SliceNodeGuide guide = new SliceLeafAnalyzer.SliceNodeGuide(treeStructure);
WalkingState<SliceNode> walkingState = new WalkingState<SliceNode>(guide) {
@Override
public void visit(@NotNull final SliceNode element) {
element.calculateDupNode();
node(element, map).clear();
SliceNode duplicate = element.getDuplicate();
if (duplicate != null) {
node(element, map).add(node(duplicate, map));
} else {
final PsiElement value = ApplicationManager.getApplication().runReadAction(new Computable<PsiElement>() {
@Override
public PsiElement compute() {
return element.getValue().getElement();
}
});
Nullness nullness = ApplicationManager.getApplication().runReadAction(new Computable<Nullness>() {
@Override
public Nullness compute() {
return checkNullness(value);
}
});
if (nullness == Nullness.NULLABLE) {
group(element, map, NullAnalysisResult.NULLS).add(value);
} else if (nullness == Nullness.NOT_NULL) {
group(element, map, NullAnalysisResult.NOT_NULLS).add(value);
} else {
Collection<? extends AbstractTreeNode> children = ApplicationManager.getApplication().runReadAction(new Computable<Collection<? extends AbstractTreeNode>>() {
@Override
public Collection<? extends AbstractTreeNode> compute() {
return element.getChildren();
}
});
if (children.isEmpty()) {
group(element, map, NullAnalysisResult.UNKNOWNS).add(value);
}
super.visit(element);
}
}
}
@Override
public void elementFinished(@NotNull SliceNode element) {
SliceNode parent = guide.getParent(element);
if (parent != null) {
node(parent, map).add(node(element, map));
}
}
};
walkingState.visit(root);
return node(root, map);
}
use of com.intellij.codeInspection.dataFlow.Nullness in project intellij-community by JetBrains.
the class SliceNullnessAnalyzer method checkNullness.
@NotNull
private static Nullness checkNullness(final PsiElement element) {
// null
PsiElement value = element;
if (value instanceof PsiExpression) {
value = PsiUtil.deparenthesizeExpression((PsiExpression) value);
}
if (value instanceof PsiLiteralExpression) {
return ((PsiLiteralExpression) value).getValue() == null ? Nullness.NULLABLE : Nullness.NOT_NULL;
}
// not null
if (value instanceof PsiNewExpression)
return Nullness.NOT_NULL;
if (value instanceof PsiThisExpression)
return Nullness.NOT_NULL;
if (value instanceof PsiMethodCallExpression) {
PsiMethod method = ((PsiMethodCallExpression) value).resolveMethod();
if (method != null && NullableNotNullManager.isNotNull(method))
return Nullness.NOT_NULL;
if (method != null && NullableNotNullManager.isNullable(method))
return Nullness.NULLABLE;
}
if (value instanceof PsiPolyadicExpression && ((PsiPolyadicExpression) value).getOperationTokenType() == JavaTokenType.PLUS) {
// "xxx" + var
return Nullness.NOT_NULL;
}
// unfortunately have to resolve here, since there can be no subnodes
PsiElement context = value;
if (value instanceof PsiReference) {
PsiElement resolved = ((PsiReference) value).resolve();
if (resolved instanceof PsiCompiledElement) {
resolved = resolved.getNavigationElement();
}
value = resolved;
}
if (value instanceof PsiParameter && ((PsiParameter) value).getDeclarationScope() instanceof PsiCatchSection) {
// exception thrown is always not null
return Nullness.NOT_NULL;
}
if (value instanceof PsiLocalVariable || value instanceof PsiParameter) {
Nullness result = DfaUtil.checkNullness((PsiVariable) value, context);
if (result != Nullness.UNKNOWN) {
return result;
}
}
if (value instanceof PsiModifierListOwner) {
if (NullableNotNullManager.isNotNull((PsiModifierListOwner) value))
return Nullness.NOT_NULL;
if (NullableNotNullManager.isNullable((PsiModifierListOwner) value))
return Nullness.NULLABLE;
}
if (value instanceof PsiEnumConstant)
return Nullness.NOT_NULL;
return Nullness.UNKNOWN;
}
use of com.intellij.codeInspection.dataFlow.Nullness in project intellij-community by JetBrains.
the class WrapObjectWithOptionalOfNullableFix method getModifiedExpression.
@NotNull
private static PsiExpression getModifiedExpression(PsiExpression expression) {
final Project project = expression.getProject();
PsiModifierListOwner toCheckNullability = null;
if (expression instanceof PsiMethodCallExpression) {
toCheckNullability = ((PsiMethodCallExpression) expression).resolveMethod();
} else if (expression instanceof PsiReferenceExpression) {
final PsiElement resolved = ((PsiReferenceExpression) expression).resolve();
if (resolved instanceof PsiModifierListOwner) {
toCheckNullability = (PsiModifierListOwner) resolved;
}
}
final Nullness nullability = toCheckNullability == null ? Nullness.NOT_NULL : DfaPsiUtil.getElementNullability(expression.getType(), toCheckNullability);
String methodName = nullability == Nullness.NOT_NULL ? "of" : "ofNullable";
final String newExpressionText = CommonClassNames.JAVA_UTIL_OPTIONAL + "." + methodName + "(" + expression.getText() + ")";
return JavaPsiFacade.getElementFactory(project).createExpressionFromText(newExpressionText, expression);
}
use of com.intellij.codeInspection.dataFlow.Nullness in project intellij-community by JetBrains.
the class DfaVariableValue method calcInherentNullability.
@NotNull
private Nullness calcInherentNullability() {
PsiModifierListOwner var = getPsiVariable();
Nullness nullability = DfaPsiUtil.getElementNullability(getVariableType(), var);
if (nullability != Nullness.UNKNOWN) {
return nullability;
}
Nullness defaultNullability = myFactory.isUnknownMembersAreNullable() && MEMBER_OR_METHOD_PARAMETER.accepts(var) ? Nullness.NULLABLE : Nullness.UNKNOWN;
if (var instanceof PsiParameter && var.getParent() instanceof PsiForeachStatement) {
PsiExpression iteratedValue = ((PsiForeachStatement) var.getParent()).getIteratedValue();
if (iteratedValue != null) {
PsiType itemType = JavaGenericsUtil.getCollectionItemType(iteratedValue);
if (itemType != null) {
return DfaPsiUtil.getElementNullability(itemType, var);
}
}
}
if (var instanceof PsiField && myFactory.isHonorFieldInitializers()) {
return getNullabilityFromFieldInitializers((PsiField) var, defaultNullability);
}
return defaultNullability;
}
Aggregations