refine PsiField.getName() notnullness to avoid a lot of yellow code and fix redundant code in clients

GitOrigin-RevId: b9c9437b83e7ac8266b87b635757b9ca067041b4
This commit is contained in:
Alexey Kudravtsev
2019-10-14 12:48:21 +03:00
committed by intellij-monorepo-bot
parent e5837e0268
commit 97102f4588
37 changed files with 27 additions and 59 deletions

View File

@@ -41,7 +41,7 @@ public class JavaCompilerRefAdapter implements LanguageCompilerRefAdapter {
if (aClass == null || aClass instanceof PsiAnonymousClass) return null;
final String jvmOwnerName = ClassUtil.getJVMClassName(aClass);
final String name = field.getName();
if (name == null || jvmOwnerName == null) return null;
if (jvmOwnerName == null) return null;
final int ownerId = names.tryEnumerate(jvmOwnerName);
if (ownerId == 0) return null;
final int nameId = names.tryEnumerate(name);

View File

@@ -727,7 +727,6 @@ public class AnnotationsHighlightUtil {
if (field instanceof PsiEnumConstant) {
String name = ((PsiEnumConstant)field).getName();
try {
//noinspection ConstantConditions
return Enum.valueOf(RetentionPolicy.class, name);
}
catch (Exception e) {

View File

@@ -103,7 +103,7 @@ public class NullabilityUtil {
private static boolean weAreSureThereAreNoExplicitWrites(PsiField field) {
String name = field.getName();
if (name == null || field.getInitializer() != null) return false;
if (field.getInitializer() != null) return false;
if (!isCheapEnoughToSearch(field, name)) return false;

View File

@@ -344,7 +344,7 @@ public class JavaKeywordCompletion {
TailType tailType = TailTypes.forSwitchLabel(switchBlock);
for (PsiField field : switchType.getAllFields()) {
String name = field.getName();
if (!(field instanceof PsiEnumConstant) || used.contains(CompletionUtil.getOriginalOrSelf(field)) || name == null) {
if (!(field instanceof PsiEnumConstant) || used.contains(CompletionUtil.getOriginalOrSelf(field))) {
continue;
}
String prefix = "case ";

View File

@@ -203,7 +203,7 @@ public class JavaMemberNameCompletionContributor extends CompletionContributor {
for (PsiField field : psiClass.getFields()) {
String name = field.getName();
if (field.getType().isAssignableFrom(var.getType()) && name != null) {
if (field.getType().isAssignableFrom(var.getType())) {
String prop = codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD);
addLookupItems(set, null, matcher, project, codeStyleManager.propertyNameToVariableName(prop, VariableKind.PARAMETER));
}

View File

@@ -24,7 +24,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.intellij.util.ObjectUtils.tryCast;
@@ -214,7 +213,6 @@ public class VariableAccessFromInnerClassJava10Fix extends BaseIntentionAction {
if (!TypeUtils.isJavaLangObject(anonymousClass.getBaseClassType())) return null;
if (Arrays.stream(anonymousClass.getFields())
.map(field -> field.getName())
.filter(Objects::nonNull)
.anyMatch(name -> name.equals(variableName))) {
return null;
}

View File

@@ -253,7 +253,6 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
List<PsiParameter> fieldParams = new ArrayList<>();
for (PsiField field : fields) {
String fieldName = field.getName();
assert fieldName != null : field;
String name = javaStyle.variableNameToPropertyName(fieldName, VariableKind.FIELD);
String parmName = javaStyle.propertyNameToVariableName(name, VariableKind.PARAMETER);
parmName = javaStyle.suggestUniqueVariableName(parmName, dummyConstructor, true);

View File

@@ -290,7 +290,6 @@ public class ExtractSetFromComparisonChainAction extends PsiElementBaseIntention
PsiClass containingClass = field.getContainingClass();
if (containingClass == null) return null;
String name = field.getName();
if (name == null) return null;
PsiExpression expression = myExpressionPtr.getElement();
PsiExpression firstComparison = myFirstComparisonPtr.getElement();
PsiExpression lastComparison = myLastComparisonPtr.getElement();
@@ -365,7 +364,7 @@ public class ExtractSetFromComparisonChainAction extends PsiElementBaseIntention
PsiReferenceExpression ref = tryCast(PsiUtil.skipParenthesizedExprDown(constant), PsiReferenceExpression.class);
if (ref != null) {
PsiEnumConstant enumConstant = tryCast(ref.resolve(), PsiEnumConstant.class);
if (enumConstant != null && enumConstant.getName() != null) {
if (enumConstant != null) {
return new ExpressionToConstantComparison(candidate, nonConstant, ref, enumConstant.getName());
}
}

View File

@@ -20,7 +20,6 @@ import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiSubstitutor;
@@ -51,7 +50,7 @@ public class PsiFieldNode extends BasePsiMemberNode<PsiField>{
PsiSubstitutor.EMPTY);
}
catch (IndexNotReadyException e) {
name = StringUtil.notNullize(field.getName());
name = field.getName();
}
int c = name.indexOf('\n');
if (c > -1) {

View File

@@ -135,7 +135,7 @@ public class AtomicReferenceImplicitUsageProvider implements ImplicitUsageProvid
Project project = field.getProject();
PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
if (scope instanceof GlobalSearchScope && name != null &&
if (scope instanceof GlobalSearchScope &&
searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)scope, null, null) == FEW_OCCURRENCES) {
return scope;
}

View File

@@ -107,7 +107,6 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
case GET_DECLARED_FIELD:
return Arrays.stream(ownerClass.getPsiClass().getFields())
.filter(field -> field.getName() != null)
.sorted(Comparator.comparing(PsiField::getName))
.map(field -> JavaLookupElementBuilder.forField(field))
.toArray();
@@ -115,7 +114,7 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
case GET_FIELD: {
final Set<String> uniqueNames = new THashSet<>();
return Arrays.stream(ownerClass.getPsiClass().getAllFields())
.filter(field -> isPotentiallyAccessible(field, ownerClass) && field.getName() != null && uniqueNames.add(field.getName()))
.filter(field -> isPotentiallyAccessible(field, ownerClass) && uniqueNames.add(field.getName()))
.sorted(Comparator.comparingInt((PsiField field) -> isPublic(field) ? 0 : 1).thenComparing(PsiField::getName))
.map(field -> withPriority(JavaLookupElementBuilder.forField(field), isPublic(field)))
.toArray();
@@ -142,7 +141,6 @@ public class JavaLangClassMemberReference extends PsiReferenceBase<PsiLiteralExp
case NEW_UPDATER: {
return Arrays.stream(ownerClass.getPsiClass().getFields())
.filter(field -> field.getName() != null)
.sorted(Comparator.comparingInt((PsiField field) -> isAtomicallyUpdateable(field) ? 0 : 1).thenComparing(PsiField::getName))
.map(field -> withPriority(JavaLookupElementBuilder.forField(field), isAtomicallyUpdateable(field)))
.toArray();

View File

@@ -158,7 +158,7 @@ public class JavaLangInvokeHandleReference extends PsiReferenceBase<PsiLiteralEx
return Arrays.stream(ownerClass.getPsiClass().getAllFields())
.filter(field -> field != null &&
(field.getContainingClass() == ownerClass.getPsiClass() || !field.hasModifierProperty(PsiModifier.PRIVATE)) &&
field.getName() != null && uniqueNames.add(field.getName()))
uniqueNames.add(field.getName()))
.filter(filter)
.sorted(Comparator.comparing((PsiField field) -> isPublic(field) ? 0 : 1).thenComparing(PsiField::getName))
.map(field -> withPriority(JavaLookupElementBuilder.forField(field).withInsertHandler(this), isPublic(field)))

View File

@@ -21,7 +21,7 @@ public class FieldDeclarationDescriptor implements ItemToReplaceDescriptor {
@Nullable
public static ItemToReplaceDescriptor createIfInaccessible(@NotNull PsiField field) {
String fieldName = field.getName();
if (!PsiReflectionAccessUtil.isAccessibleType(field.getType()) && fieldName != null) {
if (!PsiReflectionAccessUtil.isAccessibleType(field.getType())) {
return new FieldDeclarationDescriptor(field, fieldName);
}

View File

@@ -105,7 +105,7 @@ public class FieldDescriptor implements ItemToReplaceDescriptor {
PsiClass containingClass = myField.getContainingClass();
String className = containingClass == null ? null : ClassUtil.getJVMClassName(containingClass);
String fieldName = myField.getName();
if (className == null || fieldName == null) {
if (className == null) {
LOG.warn("Code is incomplete. Class name or field name not found");
return null;
}

View File

@@ -496,7 +496,6 @@ public class MoveInstanceMethodProcessor extends BaseRefactoringProcessor{
if (ExpressionUtils.isReferenceTo(qualifier, myTargetVariable)) {
if (resolved instanceof PsiField) {
String fieldName = ((PsiField)resolved).getName();
LOG.assertTrue(fieldName != null);
for (PsiParameter parameter : myMethod.getParameterList().getParameters()) {
if (Comparing.strEqual(parameter.getName(), fieldName) ||
facade.getResolveHelper().resolveReferencedVariable(fieldName, expression) != null) {

View File

@@ -36,4 +36,8 @@ public interface PsiField extends PsiJvmMember, PsiVariable, PsiDocCommentOwner,
@NotNull
@Override
PsiType getType();
@NotNull
@Override
String getName();
}

View File

@@ -545,7 +545,6 @@ class ConstantExpressionVisitor extends JavaElementVisitor implements PsiConstan
PsiElement resolvedExpression = expression.resolve();
if (resolvedExpression instanceof PsiEnumConstant) {
String constant = ((PsiEnumConstant)resolvedExpression).getName();
if (constant == null) return;
PsiReferenceExpression qualifier = (PsiReferenceExpression)expression.getQualifier();
if (qualifier == null) return;
PsiElement element = qualifier.resolve();

View File

@@ -1171,7 +1171,6 @@ public class PsiClassImplUtil {
public static boolean isFieldEquivalentTo(@NotNull PsiField field, PsiElement another) {
if (!(another instanceof PsiField)) return false;
String name1 = field.getName();
if (name1 == null) return false;
if (!another.isValid()) return false;
String name2 = ((PsiField)another).getName();

View File

@@ -41,6 +41,7 @@ public class LightField extends LightElement implements PsiField {
}
@Override
@NotNull
public String getName() {
return myField.getName();
}

View File

@@ -61,10 +61,7 @@ public class PsiFieldTreeElement extends JavaClassTreeElementBase<PsiField> impl
public String getAlphaSortKey() {
final PsiField field = getElement();
if (field != null) {
String name = field.getName();
if (name != null) {
return name;
}
return field.getName();
}
return "";
}

View File

@@ -104,9 +104,6 @@ public class SuspiciousGetterSetterInspection extends BaseInspection {
else {
return;
}
if (fieldName == null) {
return;
}
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
final String computedFieldName = codeStyleManager.propertyNameToVariableName(extractedFieldName, VariableKind.FIELD);
final String computedStaticFieldName = codeStyleManager.propertyNameToVariableName(extractedFieldName, VariableKind.STATIC_FINAL_FIELD);

View File

@@ -265,7 +265,7 @@ public class LoggingConditionDisagreesWithLogStatementInspection extends BaseIns
}
final PsiField field = (PsiField)target;
final String fieldName = field.getName();
return fieldName != null && !StringUtil.toLowerCase(fieldName).equals(priority);
return !StringUtil.toLowerCase(fieldName).equals(priority);
}
else if ("isEnabledFor".equals(methodName)) {
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
@@ -290,7 +290,7 @@ public class LoggingConditionDisagreesWithLogStatementInspection extends BaseIns
}
final PsiField field = (PsiField)argumentTarget;
final String fieldName = field.getName();
return fieldName != null && !StringUtil.toLowerCase(fieldName).equals(priority);
return !StringUtil.toLowerCase(fieldName).equals(priority);
}
}
return false;

View File

@@ -58,7 +58,6 @@ public class InlineGetterSetterCallFix extends InspectionGadgetsFix {
final PsiField field = myGetter ? PropertyUtil.getFieldOfGetter(method) : PropertyUtil.getFieldOfSetter(method);
if (field == null) return;
final String name = field.getName();
if (name == null) return;
final CommentTracker tracker = new CommentTracker();
final StringBuilder newText = new StringBuilder();
final PsiExpression qualifier = methodExpression.getQualifierExpression();

View File

@@ -135,7 +135,6 @@ public class NonThreadSafeLazyInitializationInspection extends BaseInspection {
}
final PsiField field = (PsiField)resolved;
final String fieldName = field.getName();
assert fieldName != null;
@NonNls final String holderName = StringUtil.capitalize(fieldName) + "Holder";
final PsiElement expressionParent = expression.getParent();
if (!(expressionParent instanceof PsiAssignmentExpression)) {

View File

@@ -38,9 +38,7 @@ public class FieldNamingConventionInspection extends AbstractNamingConventionIns
@Override
public void visitField(PsiField field) {
String name = field.getName();
if (name != null) {
checkName(field, name, holder);
}
checkName(field, name, holder);
}
};
}

View File

@@ -73,9 +73,6 @@ public class UpperCaseFieldNameNotConstantInspection extends BaseInspection {
return;
}
final String fieldName = field.getName();
if (fieldName == null) {
return;
}
if (!fieldName.equals(StringUtil.toUpperCase(fieldName))) {
return;
}

View File

@@ -192,7 +192,6 @@ public class CreateMissingSwitchBranchesAction extends PsiElementBaseIntentionAc
@Nullable
static Value fromField(@NotNull PsiField field) {
String name = field.getName();
if (name == null) return null;
PsiClass aClass = field.getContainingClass();
if (aClass == null) return null;
String className = aClass.getQualifiedName();

View File

@@ -108,7 +108,6 @@ public class ExtensionDomExtender extends DomExtender<Extension> {
}
final String fieldName = field.getName();
assert fieldName != null;
final PsiAnnotation attrAnno = PsiUtil.findAnnotation(Attribute.class, field, getter, setter);
if (attrAnno != null) {
final String attrName = getStringAttribute(attrAnno, "value", fieldName);

View File

@@ -66,7 +66,6 @@ public abstract class ExtensionPointImpl implements ExtensionPoint {
final List<PsiField> result = new SmartList<>();
for (PsiField field : beanClass.getAllFields()) {
final String fieldName = field.getName();
if (fieldName == null) continue;
if (Extension.isClassField(fieldName) &&
ExtensionDomExtender.findWithElement(getWithElements(), field) == null) {

View File

@@ -40,10 +40,8 @@ public class GrAliasedImportedElementSearcher extends QueryExecutorBase<PsiRefer
final PsiField field = GroovyPropertyUtils.findFieldForAccessor(method, true);
if (field != null) {
final String propertyName = field.getName();
if (propertyName != null) {
final MyProcessor processor = new MyProcessor(method, GroovyPropertyUtils.getAccessorPrefix(method), session);
collector.searchWord(propertyName, onlyGroovy, UsageSearchContext.IN_CODE, true, method, processor);
}
final MyProcessor processor = new MyProcessor(method, GroovyPropertyUtils.getAccessorPrefix(method), session);
collector.searchWord(propertyName, onlyGroovy, UsageSearchContext.IN_CODE, true, method, processor);
}
}
}

View File

@@ -60,7 +60,6 @@ public abstract class BuilderAnnotationContributor implements AstTransformationS
private static PsiField[] filterFields(Collection<? extends PsiField> collectedFields) {
return collectedFields.stream()
.filter(field -> field.getName() != null)
.filter(field -> !field.hasModifierProperty(PsiModifier.STATIC))
.filter(field -> {
PsiClass aClass = field.getContainingClass();

View File

@@ -449,7 +449,6 @@ public class GroovyGenerateEqualsHelper {
if (!(f1.getType() instanceof PsiPrimitiveType) && f2.getType() instanceof PsiPrimitiveType) return 1;
final String name1 = f1.getName();
final String name2 = f2.getName();
assert name1 != null && name2 != null;
return name1.compareTo(name2);
}
}

View File

@@ -179,7 +179,7 @@ public class JavaFxFieldToPropertyIntention extends PsiElementBaseIntentionActio
final String fieldName = field.getName();
final PsiClass containingClass = field.getContainingClass();
final PsiTypeElement typeElement = field.getTypeElement();
if (fieldName != null && containingClass != null && typeElement != null) {
if (containingClass != null && typeElement != null) {
final ObservableType observableType = ObservableType.createObservableType(field, project);
if (observableType != null) {
return new PropertyInfo(field, containingClass, typeElement, fieldName, observableType);

View File

@@ -56,9 +56,7 @@ public class JavaFxGetterSetterPrototypeProvider extends GetterSetterPrototypePr
getterBody.getStatements()[0].replace(factory.createStatementFromText("return " + fieldName + ".get();", field));
final PsiMethod propertyGetter = PropertyUtilBase.generateGetterPrototype(field);
if (propertyGetter != null && fieldName != null) {
propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(fieldName, VariableKind.FIELD) + JavaFxCommonNames.PROPERTY_METHOD_SUFFIX);
}
propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(fieldName, VariableKind.FIELD) + JavaFxCommonNames.PROPERTY_METHOD_SUFFIX);
return new PsiMethod[] {getter, GenerateMembersUtil.annotateOnOverrideImplement(field.getContainingClass(), propertyGetter)};
}

View File

@@ -70,7 +70,6 @@ public class JavaFxImplicitUsageProvider implements ImplicitUsageProvider {
final PsiField field = (PsiField)element;
if (!isImplicitFxmlAccess(field)) return false;
final String fieldName = field.getName();
if (fieldName == null) return false;
final PsiClass containingClass = field.getContainingClass();
if (containingClass == null) return false;
@@ -102,10 +101,8 @@ public class JavaFxImplicitUsageProvider implements ImplicitUsageProvider {
private static boolean isInjectedByFxmlLoader(@NotNull PsiField field) {
final String fieldName = field.getName();
final PsiType fieldType = field.getType();
return fieldName != null &&
("resources".equals(fieldName) && InheritanceUtil.isInheritor(fieldType, "java.util.ResourceBundle") ||
"location".equals(fieldName) && InheritanceUtil.isInheritor(fieldType, "java.net.URL")) &&
isDeclaredInControllerClass(field);
return ("resources".equals(fieldName) && InheritanceUtil.isInheritor(fieldType, "java.util.ResourceBundle") ||
"location".equals(fieldName) && InheritanceUtil.isInheritor(fieldType, "java.net.URL")) && isDeclaredInControllerClass(field);
}
private static boolean isDeclaredInControllerClass(@NotNull PsiMember member) {

View File

@@ -35,7 +35,6 @@ public class JavaFxControllerFieldSearcher implements QueryExecutor<PsiReference
for (final PsiFile file : fxmlWithController) {
ApplicationManager.getApplication().runReadAction(() -> {
final String fieldName = field.getName();
if (fieldName == null) return;
final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();
final SearchScope searchScope = queryParameters.getEffectiveSearchScope();
if (searchScope.contains(virtualFile)) {

View File

@@ -123,7 +123,7 @@ public class FormReferencesSearcher implements QueryExecutor<PsiReference, Refer
PsiManager psiManager, final PsiEnumConstant enumConstant,
GlobalSearchScope scope, final LocalSearchScope filterScope) {
String className = ReadAction.compute(() -> enumConstant.getName());
return className == null || processReferencesInUIFormsInner(className, enumConstant, processor, scope, psiManager, filterScope);
return processReferencesInUIFormsInner(className, enumConstant, processor, scope, psiManager, filterScope);
}
private static boolean processReferencesInUIFormsInner(String name,