use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class MethodsChainLookupRangingHelper method chainToWeightableLookupElement.
@SuppressWarnings("ConstantConditions")
@Nullable
private static LookupElement chainToWeightableLookupElement(final MethodsChain chain, final ChainCompletionContext context, final CachedRelevantStaticMethodSearcher staticMethodSearcher) {
final int chainSize = chain.size();
assert chainSize != 0;
final int lastMethodWeight = chain.getChainWeight();
int unreachableParametersCount = 0;
int notMatchedStringVars = 0;
int matchedParametersInContext = 0;
Boolean isFirstMethodStatic = null;
Boolean hasCallingVariableInContext = null;
LookupElement chainLookupElement = null;
PsiClass newVariableClass = null;
final NullableNotNullManager nullableNotNullManager = NullableNotNullManager.getInstance(context.getProject());
for (final PsiMethod[] psiMethods : chain.getPath()) {
final PsiMethod method = MethodChainsSearchUtil.getMethodWithMinNotPrimitiveParameters(psiMethods, Collections.singleton(context.getTarget().getClassQName()));
if (method == null) {
return null;
}
if (isFirstMethodStatic == null) {
isFirstMethodStatic = psiMethods[0].hasModifierProperty(PsiModifier.STATIC);
}
final PsiClass qualifierClass;
final boolean isHead = chainLookupElement == null;
if (isHead) {
final String qualifierClassName = chain.getQualifierClassName();
qualifierClass = JavaPsiFacade.getInstance(context.getProject()).findClass(qualifierClassName, context.getResolveScope());
} else {
qualifierClass = null;
}
final MethodProcResult procResult = processMethod(method, qualifierClass, isHead, lastMethodWeight, context, staticMethodSearcher, nullableNotNullManager);
if (procResult == null) {
return null;
}
if (hasCallingVariableInContext == null) {
hasCallingVariableInContext = procResult.hasCallingVariableInContext();
}
if (isHead && procResult.isIntroduceNewVariable()) {
newVariableClass = qualifierClass;
}
matchedParametersInContext += procResult.getMatchedParametersInContext();
unreachableParametersCount += procResult.getUnreachableParametersCount();
notMatchedStringVars += procResult.getNotMatchedStringVars();
chainLookupElement = isHead ? procResult.getLookupElement() : new JavaChainLookupElement(chainLookupElement, procResult.getLookupElement());
}
if (newVariableClass != null) {
chainLookupElement = ChainCompletionNewVariableLookupElement.create(newVariableClass, chainLookupElement);
}
final ChainRelevance relevance = new ChainRelevance(chainSize, lastMethodWeight, unreachableParametersCount, notMatchedStringVars, hasCallingVariableInContext, isFirstMethodStatic, matchedParametersInContext);
return new WeightableChainLookupElement(chainLookupElement, relevance);
}
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 OverrideImplementsAnnotationsHandlerImpl method getAnnotations.
@Override
public String[] getAnnotations(Project project) {
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
final Collection<String> anns = new ArrayList<>(manager.getNotNulls());
anns.addAll(manager.getNullables());
anns.add(AnnotationUtil.NLS);
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
anns.addAll(settings.getRepeatAnnotations());
return ArrayUtil.toStringArray(anns);
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class ExtractMethodProcessor method initNullness.
private Nullness initNullness() {
if (!PsiUtil.isLanguageLevel5OrHigher(myElements[0]) || PsiUtil.resolveClassInType(myReturnType) == null)
return null;
final NullableNotNullManager manager = NullableNotNullManager.getInstance(myProject);
final PsiClass nullableAnnotationClass = JavaPsiFacade.getInstance(myProject).findClass(manager.getDefaultNullable(), myElements[0].getResolveScope());
if (nullableAnnotationClass != null) {
final PsiElement elementInCopy = myTargetClass.getContainingFile().copy().findElementAt(myTargetClass.getTextOffset());
final PsiClass classCopy = PsiTreeUtil.getParentOfType(elementInCopy, PsiClass.class);
if (classCopy == null) {
return null;
}
final PsiMethod emptyMethod = (PsiMethod) classCopy.addAfter(generateEmptyMethod("name"), classCopy.getLBrace());
prepareMethodBody(emptyMethod, false);
if (myNotNullConditionalCheck || myNullConditionalCheck) {
return Nullness.NULLABLE;
}
return DfaUtil.inferMethodNullity(emptyMethod);
}
return null;
}
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;
}
Aggregations