private static boolean processField(
      @NotNull GrTypeDefinition grType,
      @NotNull PsiScopeProcessor processor,
      @NotNull ResolveState state,
      @NotNull PsiElement place,
      boolean processInstanceMethods,
      @NotNull PsiSubstitutor substitutor,
      @NotNull PsiElementFactory factory,
      @NotNull LanguageLevel level,
      CandidateInfo fieldInfo) {
    final PsiField field = (PsiField) fieldInfo.getElement();
    if (!processInstanceMember(processInstanceMethods, field) || isSameDeclaration(place, field)) {
      return true;
    }
    LOG.assertTrue(field.getContainingClass() != null);
    final PsiSubstitutor finalSubstitutor =
        PsiClassImplUtil.obtainFinalSubstitutor(
            field.getContainingClass(),
            fieldInfo.getSubstitutor(),
            grType,
            substitutor,
            factory,
            level);

    return processor.execute(field, state.put(PsiSubstitutor.KEY, finalSubstitutor));
  }
Esempio n. 2
0
 public static void checkFieldConflicts(
     @Nullable PsiClass aClass, String newName, final Map<PsiElement, String> conflicts) {
   PsiField existingField = aClass != null ? aClass.findFieldByName(newName, true) : null;
   if (existingField != null) {
     if (aClass.equals(existingField.getContainingClass())) {
       String className =
           aClass instanceof PsiAnonymousClass
               ? RefactoringBundle.message("current.class")
               : RefactoringUIUtil.getDescription(aClass, false);
       final String conflict =
           RefactoringBundle.message(
               "field.0.is.already.defined.in.the.1", existingField.getName(), className);
       conflicts.put(existingField, conflict);
     } else { // method somewhere in base class
       if (!existingField.hasModifierProperty(PsiModifier.PRIVATE)) {
         String fieldInfo =
             PsiFormatUtil.formatVariable(
                 existingField,
                 PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE | PsiFormatUtil.TYPE_AFTER,
                 PsiSubstitutor.EMPTY);
         String className =
             RefactoringUIUtil.getDescription(existingField.getContainingClass(), false);
         final String descr =
             RefactoringBundle.message(
                 "field.0.will.hide.field.1.of.the.base.class", newName, fieldInfo, className);
         conflicts.put(existingField, descr);
       }
     }
   }
 }
  protected void generatePsiElements(
      @NotNull PsiField psiField,
      @NotNull PsiAnnotation psiAnnotation,
      @NotNull List<? super PsiElement> target) {
    final String fieldName = psiField.getName();
    final PsiType psiFieldType = psiField.getType();

    final String methodName = getFindByName(psiField);

    PsiClass psiClass = psiField.getContainingClass();
    assert psiClass != null;

    UserMapKeys.addWriteUsageFor(psiField);

    LombokLightMethodBuilder method =
        new LombokLightMethodBuilder(psiField.getManager(), methodName)
            .withMethodReturnType(PsiTypesUtil.getClassType(psiClass))
            .withContainingClass(psiClass)
            .withParameter(fieldName, psiFieldType)
            .withNavigationElement(psiField);
    method.withModifier(PsiModifier.STATIC);
    method.withModifier(PsiModifier.PUBLIC);

    target.add(method);
  }
 public static void registerQuickFix(
     PsiMember refElement,
     PsiJavaCodeReferenceElement place,
     PsiClass accessObjectClass,
     HighlightInfo error) {
   if (refElement instanceof PsiField && place instanceof PsiReferenceExpression) {
     final PsiField psiField = (PsiField) refElement;
     final PsiClass containingClass = psiField.getContainingClass();
     if (containingClass != null) {
       if (PsiUtil.isOnAssignmentLeftHand((PsiExpression) place)) {
         final PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(psiField);
         final PsiMethod setter = containingClass.findMethodBySignature(setterPrototype, true);
         if (setter != null && PsiUtil.isAccessible(setter, place, accessObjectClass)) {
           QuickFixAction.registerQuickFixAction(
               error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, setter, true));
         }
       } else if (PsiUtil.isAccessedForReading((PsiExpression) place)) {
         final PsiMethod getterPrototype = PropertyUtil.generateGetterPrototype(psiField);
         final PsiMethod getter = containingClass.findMethodBySignature(getterPrototype, true);
         if (getter != null && PsiUtil.isAccessible(getter, place, accessObjectClass)) {
           QuickFixAction.registerQuickFixAction(
               error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, getter, false));
         }
       }
     }
   }
 }
  private GenerationInfo generateWithMethodFor(PsiField field) {
    PsiMethod templateMethod = generateMethodPrototype(field);
    PsiMethod existingMethod =
        field.getContainingClass().findMethodBySignature(templateMethod, false);

    return existingMethod == null ? new PsiGenerationInfo(templateMethod) : null;
  }
Esempio n. 6
0
 @Override
 public LightRef asLightUsage(@NotNull PsiElement element, @NotNull ByteArrayEnumerator names) {
   if (mayBeVisibleOutsideOwnerFile(element)) {
     if (element instanceof PsiField) {
       final PsiField field = (PsiField) element;
       final PsiClass aClass = field.getContainingClass();
       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;
       return new LightRef.JavaLightFieldRef(id(jvmOwnerName, names), id(name, names));
     } else if (element instanceof PsiMethod) {
       final PsiClass aClass = ((PsiMethod) element).getContainingClass();
       if (aClass == null || aClass instanceof PsiAnonymousClass) return null;
       final String jvmOwnerName = ClassUtil.getJVMClassName(aClass);
       if (jvmOwnerName == null) return null;
       final PsiMethod method = (PsiMethod) element;
       final String name = method.isConstructor() ? "<init>" : method.getName();
       final int parametersCount = method.getParameterList().getParametersCount();
       return new LightRef.JavaLightMethodRef(
           id(jvmOwnerName, names), id(name, names), parametersCount);
     } else if (element instanceof PsiClass) {
       final String jvmClassName = ClassUtil.getJVMClassName((PsiClass) element);
       if (jvmClassName != null) {
         return new LightRef.JavaLightClassRef(id(jvmClassName, names));
       }
     }
   }
   return null;
 }
 public FieldConflictsResolver(String name, PsiCodeBlock scope) {
   myScope = scope;
   if (myScope == null) {
     myField = null;
     myReferenceExpressions = null;
     return;
   }
   JavaPsiFacade facade = JavaPsiFacade.getInstance(myScope.getProject());
   final PsiVariable oldVariable =
       facade.getResolveHelper().resolveAccessibleReferencedVariable(name, myScope);
   myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null;
   if (!(oldVariable instanceof PsiField)) {
     myReferenceExpressions = null;
     return;
   }
   myReferenceExpressions = new ArrayList<PsiReferenceExpression>();
   for (PsiReference reference :
       ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) {
     final PsiElement element = reference.getElement();
     if (element instanceof PsiReferenceExpression) {
       final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) element;
       if (referenceExpression.getQualifierExpression() == null) {
         myReferenceExpressions.add(referenceExpression);
       }
     }
   }
   if (myField.hasModifierProperty(PsiModifier.STATIC)) {
     myQualifyingClass = myField.getContainingClass();
   }
 }
 public VariableLookupItem(PsiField field, boolean shouldImport) {
   super(field, field.getName());
   myHelper = new MemberLookupHelper(field, field.getContainingClass(), shouldImport, false);
   if (!shouldImport) {
     forceQualify();
   }
 }
 @NonNls
 private static String computeConstantBooleanText(PsiExpression expression) {
   if (!(expression instanceof PsiReferenceExpression)) {
     return null;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression;
   final PsiElement target = referenceExpression.resolve();
   if (!(target instanceof PsiField)) {
     return null;
   }
   final PsiField field = (PsiField) target;
   final PsiClass containingClass = field.getContainingClass();
   if (containingClass == null) {
     return null;
   }
   final String qualifiedName = containingClass.getQualifiedName();
   if (!CommonClassNames.JAVA_LANG_BOOLEAN.equals(qualifiedName)) {
     return null;
   }
   @NonNls final String name = field.getName();
   if ("TRUE".equals(name)) {
     return "true";
   } else if ("FALSE".equals(name)) {
     return "false";
   } else {
     return null;
   }
 }
Esempio n. 10
0
  @NotNull
  public String elementToKotlin(@NotNull PsiElement element) {
    if (element instanceof PsiJavaFile) {
      return fileToFile((PsiJavaFile) element).toKotlin();
    }

    if (element instanceof PsiClass) {
      return classToClass((PsiClass) element).toKotlin();
    }

    if (element instanceof PsiMethod) {
      return methodToFunction((PsiMethod) element).toKotlin();
    }

    if (element instanceof PsiField) {
      PsiField field = (PsiField) element;
      return fieldToField(field, field.getContainingClass()).toKotlin();
    }

    if (element instanceof PsiStatement) {
      return statementToStatement((PsiStatement) element).toKotlin();
    }

    if (element instanceof PsiExpression) {
      return expressionToExpression((PsiExpression) element).toKotlin();
    }

    return "";
  }
  public FieldConflictsResolver(String name, GrCodeBlock scope) {
    myScope = scope;
    if (myScope == null) return;

    final GroovyPsiElement resolved = ResolveUtil.resolveProperty(myScope, name);
    if (resolved instanceof GrReferenceExpression || resolved == null) return;

    assert resolved instanceof PsiVariable;
    final PsiVariable oldVariable = (PsiVariable) resolved;
    myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null;
    if (!(oldVariable instanceof PsiField)) return;

    myReferenceExpressions = new ArrayList<>();
    for (PsiReference reference :
        ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) {
      final PsiElement element = reference.getElement();
      if (element instanceof GrReferenceExpression) {
        final GrReferenceExpression referenceExpression = (GrReferenceExpression) element;
        if (referenceExpression.getQualifier() == null) {
          myReferenceExpressions.add(referenceExpression);
        }
      }
    }
    if (myField.hasModifierProperty(PsiModifier.STATIC)) {
      myQualifyingClass = myField.getContainingClass();
    }
  }
 private static boolean canWriteToFinal(
     @NotNull PsiVariable variable,
     @NotNull PsiExpression expression,
     @NotNull PsiReferenceExpression reference,
     @NotNull PsiFile containingFile) {
   if (variable.hasInitializer()) return false;
   if (variable instanceof PsiParameter) return false;
   PsiElement innerClass = getInnerClassVariableReferencedFrom(variable, expression);
   if (variable instanceof PsiField) {
     // if inside some field initializer
     if (HighlightUtil.findEnclosingFieldInitializer(expression) != null) return true;
     // assignment from within inner class is illegal always
     PsiField field = (PsiField) variable;
     if (innerClass != null
         && !containingFile
             .getManager()
             .areElementsEquivalent(innerClass, field.getContainingClass())) return false;
     final PsiMember enclosingCtrOrInitializer =
         PsiUtil.findEnclosingConstructorOrInitializer(expression);
     return enclosingCtrOrInitializer != null
         && isSameField(enclosingCtrOrInitializer, field, reference, containingFile);
   }
   if (variable instanceof PsiLocalVariable) {
     boolean isAccessedFromOtherClass = innerClass != null;
     if (isAccessedFromOtherClass) {
       return false;
     }
   }
   return true;
 }
  @Nullable
  static HighlightInfo checkFinalFieldInitialized(@NotNull PsiField field) {
    if (!field.hasModifierProperty(PsiModifier.FINAL)) return null;
    if (isFieldInitializedAfterObjectConstruction(field)) return null;

    String description = JavaErrorMessages.message("variable.not.initialized", field.getName());
    TextRange range = HighlightNamesUtil.getFieldDeclarationTextRange(field);
    HighlightInfo highlightInfo =
        HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
            .range(range)
            .descriptionAndTooltip(description)
            .create();
    QuickFixAction.registerQuickFixAction(
        highlightInfo,
        HighlightMethodUtil.getFixRange(field),
        QUICK_FIX_FACTORY.createCreateConstructorParameterFromFieldFix(field));
    QuickFixAction.registerQuickFixAction(
        highlightInfo,
        HighlightMethodUtil.getFixRange(field),
        QUICK_FIX_FACTORY.createInitializeFinalFieldInConstructorFix(field));
    final PsiClass containingClass = field.getContainingClass();
    if (containingClass != null && !containingClass.isInterface()) {
      QuickFixAction.registerQuickFixAction(
          highlightInfo,
          QUICK_FIX_FACTORY.createModifierListFix(field, PsiModifier.FINAL, false, false));
    }
    QuickFixAction.registerQuickFixAction(
        highlightInfo, QUICK_FIX_FACTORY.createAddVariableInitializerFix(field));
    return highlightInfo;
  }
 @Nullable
 private static String getCannotRefactorMessage(PsiMember member) {
   if (member == null) {
     return RefactoringBundle.message("locate.caret.inside.a.method");
   }
   if (member instanceof PsiMethod) {
     if (((PsiMethod) member).isConstructor()) {
       return RefactoringBundle.message("replace.with.method.call.does.not.work.for.constructors");
     }
     final PsiCodeBlock body = ((PsiMethod) member).getBody();
     if (body == null) {
       return RefactoringBundle.message("method.does.not.have.a.body", member.getName());
     }
     final PsiStatement[] statements = body.getStatements();
     if (statements.length == 0) {
       return RefactoringBundle.message("method.has.an.empty.body", member.getName());
     }
   } else if (member instanceof PsiField) {
     final PsiField field = (PsiField) member;
     if (field.getInitializer() == null) {
       return "Field " + member.getName() + " doesn't have initializer";
     }
     final PsiClass containingClass = field.getContainingClass();
     if (!field.hasModifierProperty(PsiModifier.FINAL)
         || !field.hasModifierProperty(PsiModifier.STATIC)
         || containingClass == null
         || containingClass.getQualifiedName() == null) {
       return "Replace Duplicates works with constants only";
     }
   } else {
     return "Caret should be inside method or constant";
   }
   return null;
 }
 public CreateConstructorParameterFromFieldFix(@NotNull PsiField field) {
   myClass = field.getContainingClass();
   myField =
       SmartPointerManager.getInstance(field.getProject()).createSmartPsiElementPointer(field);
   if (myClass != null) {
     getFieldsToFix().add(myField);
   }
 }
  public Collection<PsiElement> getAdditionalElementsToDelete(
      @NotNull final PsiElement element,
      @NotNull final Collection<PsiElement> allElementsToDelete,
      final boolean askUser) {
    if (element instanceof PsiField) {
      PsiField field = (PsiField) element;
      final Project project = element.getProject();
      String propertyName =
          JavaCodeStyleManager.getInstance(project)
              .variableNameToPropertyName(field.getName(), VariableKind.FIELD);

      PsiClass aClass = field.getContainingClass();
      if (aClass != null) {
        boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
        PsiMethod[] getters =
            GetterSetterPrototypeProvider.findGetters(aClass, propertyName, isStatic);
        if (getters != null) {
          final List<PsiMethod> validGetters = new ArrayList<>(1);
          for (PsiMethod getter : getters) {
            if (!allElementsToDelete.contains(getter) && (getter != null && getter.isPhysical())) {
              validGetters.add(getter);
            }
          }
          getters =
              validGetters.isEmpty()
                  ? null
                  : validGetters.toArray(new PsiMethod[validGetters.size()]);
        }

        PsiMethod setter = PropertyUtil.findPropertySetter(aClass, propertyName, isStatic, false);
        if (allElementsToDelete.contains(setter) || setter != null && !setter.isPhysical())
          setter = null;
        if (askUser && (getters != null || setter != null)) {
          final String message =
              RefactoringMessageUtil.getGetterSetterMessage(
                  field.getName(),
                  RefactoringBundle.message("delete.title"),
                  getters != null ? getters[0] : null,
                  setter);
          if (!ApplicationManager.getApplication().isUnitTestMode()
              && Messages.showYesNoDialog(
                      project,
                      message,
                      RefactoringBundle.message("safe.delete.title"),
                      Messages.getQuestionIcon())
                  != Messages.YES) {
            getters = null;
            setter = null;
          }
        }
        List<PsiElement> elements = new ArrayList<>();
        if (setter != null) elements.add(setter);
        if (getters != null) Collections.addAll(elements, getters);
        return elements;
      }
    }
    return null;
  }
 private static boolean isAvailable(PsiField field) {
   PsiClass containingClass = field == null ? null : field.getContainingClass();
   return field != null
       && field.getManager().isInProject(field)
       && !field.hasModifierProperty(PsiModifier.STATIC)
       && containingClass != null
       && !(containingClass instanceof PsiSyntheticClass)
       && containingClass.getName() != null;
 }
 public RemoveMiddlemanProcessor(PsiField field, List<MemberInfo> memberInfos) {
   super(field.getProject());
   this.field = field;
   containingClass = field.getContainingClass();
   final String propertyName = PropertyUtil.suggestPropertyName(field);
   final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
   getter = PropertyUtil.findPropertyGetter(containingClass, propertyName, isStatic, false);
   myDelegateMethodInfos = memberInfos;
 }
  private static PsiStatement addInitializationToConstructors(
      PsiLocalVariable local,
      PsiField field,
      PsiMethod enclosingConstructor,
      PsiElementFactory factory)
      throws IncorrectOperationException {
    PsiClass aClass = field.getContainingClass();
    PsiMethod[] constructors = aClass.getConstructors();
    PsiStatement assignment = createAssignment(local, field.getName(), factory);
    boolean added = false;
    for (PsiMethod constructor : constructors) {
      if (constructor == enclosingConstructor) continue;
      PsiCodeBlock body = constructor.getBody();
      if (body == null) continue;
      PsiStatement[] statements = body.getStatements();
      if (statements.length > 0) {
        PsiStatement first = statements[0];
        if (first instanceof PsiExpressionStatement) {
          PsiExpression expression = ((PsiExpressionStatement) first).getExpression();
          if (expression instanceof PsiMethodCallExpression) {
            @NonNls
            String text = ((PsiMethodCallExpression) expression).getMethodExpression().getText();
            if ("this".equals(text)) {
              continue;
            }
            if ("super".equals(text)
                && enclosingConstructor == null
                && PsiTreeUtil.isAncestor(constructor, local, false)) {
              local.delete();
              return (PsiStatement) body.addAfter(assignment, first);
            }
          }
        }
        if (enclosingConstructor == null && PsiTreeUtil.isAncestor(constructor, local, false)) {
          local.delete();
          return (PsiStatement) body.addBefore(assignment, first);
        }
      }

      assignment = (PsiStatement) body.add(assignment);
      added = true;
    }
    if (!added && enclosingConstructor == null) {
      if (aClass instanceof PsiAnonymousClass) {
        final PsiClassInitializer classInitializer =
            (PsiClassInitializer) aClass.addAfter(factory.createClassInitializer(), field);
        assignment = (PsiStatement) classInitializer.getBody().add(assignment);
      } else {
        PsiMethod constructor = (PsiMethod) aClass.add(factory.createConstructor());
        assignment = (PsiStatement) constructor.getBody().add(assignment);
      }
    }

    if (enclosingConstructor == null) local.delete();
    return assignment;
  }
 @Override
 public void findExistingNameConflicts(
     PsiElement element, String newName, MultiMap<PsiElement, String> conflicts) {
   if (element instanceof PsiCompiledElement) return;
   if (element instanceof PsiField) {
     PsiField refactoredField = (PsiField) element;
     if (newName.equals(refactoredField.getName())) return;
     ConflictsUtil.checkFieldConflicts(refactoredField.getContainingClass(), newName, conflicts);
   }
 }
  private PsiMethod generateMethodFor(
      PsiField field, String methodName, String parameterName, PsiElementFactory elementFactory) {
    PsiMethod withMethod =
        elementFactory.createMethod(
            methodName, elementFactory.createType(field.getContainingClass()));
    PsiParameter parameter = elementFactory.createParameter(parameterName, field.getType());
    withMethod.getParameterList().add(parameter);
    PsiUtil.setModifierProperty(withMethod, PsiModifier.PUBLIC, true);

    return withMethod;
  }
Esempio n. 22
0
 @Nullable
 public static PsiField findPropertyFieldByMember(final PsiMember psiMember) {
   if (psiMember instanceof PsiField) {
     return (PsiField) psiMember;
   } else if (psiMember instanceof PsiMethod) {
     final PsiMethod psiMethod = (PsiMethod) psiMember;
     final PsiType returnType = psiMethod.getReturnType();
     if (returnType == null) return null;
     final PsiCodeBlock body = psiMethod.getBody();
     final PsiStatement[] statements = body == null ? null : body.getStatements();
     final PsiStatement statement =
         statements == null || statements.length != 1 ? null : statements[0];
     final PsiElement target;
     if (PsiType.VOID.equals(returnType)) {
       final PsiExpression expression =
           statement instanceof PsiExpressionStatement
               ? ((PsiExpressionStatement) statement).getExpression()
               : null;
       target =
           expression instanceof PsiAssignmentExpression
               ? ((PsiAssignmentExpression) expression).getLExpression()
               : null;
     } else {
       target =
           statement instanceof PsiReturnStatement
               ? ((PsiReturnStatement) statement).getReturnValue()
               : null;
     }
     final PsiElement resolved =
         target instanceof PsiReferenceExpression
             ? ((PsiReferenceExpression) target).resolve()
             : null;
     if (resolved instanceof PsiField) {
       final PsiField field = (PsiField) resolved;
       if (psiMember.getContainingClass() == field.getContainingClass()
           || psiMember.getContainingClass().isInheritor(field.getContainingClass(), true))
         return field;
     }
   }
   return null;
 }
  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();
    if (!name1.equals(name2)) return false;
    PsiClass aClass1 = field.getContainingClass();
    PsiClass aClass2 = ((PsiField) another).getContainingClass();
    return aClass1 != null
        && aClass2 != null
        && field.getManager().areElementsEquivalent(aClass1, aClass2);
  }
  private static boolean isSameField(
      @NotNull PsiMember enclosingCtrOrInitializer,
      @NotNull PsiField field,
      @NotNull PsiReferenceExpression reference,
      @NotNull PsiFile containingFile) {

    if (!containingFile
        .getManager()
        .areElementsEquivalent(
            enclosingCtrOrInitializer.getContainingClass(), field.getContainingClass()))
      return false;
    PsiExpression qualifierExpression = reference.getQualifierExpression();
    return qualifierExpression == null || qualifierExpression instanceof PsiThisExpression;
  }
    String getNewName(@NotNull PsiElement element) {
      String name = myNameMap.get(element);
      if (name != null) {
        return name;
      }

      if (element instanceof GrTraitField) {
        PsiField prototype = ((GrTraitField) element).getPrototype();
        String newPrototypeName = getNewName(prototype);
        return GrTraitUtil.getTraitFieldPrefix(prototype.getContainingClass()) + newPrototypeName;
      }

      return null;
    }
  @Override
  protected boolean validate(
      @NotNull PsiAnnotation psiAnnotation,
      @NotNull PsiField psiField,
      @NotNull ProblemBuilder builder) {
    boolean result = true;

    final PsiClass psiClass = psiField.getContainingClass();
    if (null == psiClass) {
      result = false;
    }
    result &= validateEnum(psiClass, builder);

    return result;
  }
 private static void findSubmemberHidesFieldCollisions(
     final PsiField field, final String newName, final List<UsageInfo> result) {
   if (field.getContainingClass() == null) return;
   if (field.hasModifierProperty(PsiModifier.PRIVATE)) return;
   final PsiClass containingClass = field.getContainingClass();
   Collection<PsiClass> inheritors = ClassInheritorsSearch.search(containingClass).findAll();
   for (PsiClass inheritor : inheritors) {
     PsiField conflictingField = inheritor.findFieldByName(newName, false);
     if (conflictingField != null) {
       result.add(new SubmemberHidesMemberUsageInfo(conflictingField, field));
     } else { // local class
       final PsiMember member = PsiTreeUtil.getParentOfType(inheritor, PsiMember.class);
       if (member != null) {
         final ArrayList<PsiVariable> variables = new ArrayList<>();
         ControlFlowUtil.collectOuterLocals(variables, inheritor, inheritor, member);
         for (PsiVariable variable : variables) {
           if (newName.equals(variable.getName())) {
             result.add(new FieldHidesLocalUsageInfo(variable, field));
           }
         }
       }
     }
   }
 }
 private static void addFieldsUsages(
     final PsiClass aClass,
     final Processor<UsageInfo> results,
     final JavaClassFindUsagesOptions options) {
   if (options.isIncludeInherited) {
     final PsiManager manager = aClass.getManager();
     PsiField[] fields = aClass.getAllFields();
     FieldsLoop:
     for (int i = 0; i < fields.length; i++) {
       final PsiField field = fields[i];
       // filter hidden fields
       for (int j = 0; j < i; j++) {
         if (Comparing.strEqual(field.getName(), fields[j].getName())) continue FieldsLoop;
       }
       final PsiClass fieldClass = field.getContainingClass();
       if (manager.areElementsEquivalent(fieldClass, aClass)) {
         addElementUsages(fields[i], results, options);
       } else {
         ReferencesSearch.search(
                 new ReferencesSearch.SearchParameters(
                     field, options.searchScope, false, options.fastTrack))
             .forEach(
                 new ReadActionProcessor<PsiReference>() {
                   @Override
                   public boolean processInReadAction(final PsiReference reference) {
                     addResultFromReference(
                         reference, fieldClass, manager, aClass, results, options);
                     return true;
                   }
                 });
       }
     }
   } else {
     PsiField[] fields =
         ApplicationManager.getApplication()
             .runReadAction(
                 new Computable<PsiField[]>() {
                   @Override
                   public PsiField[] compute() {
                     return aClass.getFields();
                   }
                 });
     for (PsiField field : fields) {
       addElementUsages(field, results, options);
     }
   }
 }
 private boolean isInitializedInInitializer(
     @NotNull PsiField field, UninitializedReadCollector uninitializedReadsCollector) {
   final PsiClass aClass = field.getContainingClass();
   if (aClass == null) {
     return false;
   }
   final PsiClassInitializer[] initializers = aClass.getInitializers();
   for (final PsiClassInitializer initializer : initializers) {
     if (!initializer.hasModifierProperty(PsiModifier.STATIC)) {
       final PsiCodeBlock body = initializer.getBody();
       if (uninitializedReadsCollector.blockAssignsVariable(body, field)) {
         return true;
       }
     }
   }
   return false;
 }
  private static void qualifyFieldReference(InsertionContext context, PsiField field) {
    context.commitDocument();
    PsiFile file = context.getFile();
    final PsiReference reference = file.findReferenceAt(context.getStartOffset());
    if (reference instanceof PsiJavaCodeReferenceElement
        && ((PsiJavaCodeReferenceElement) reference).isQualified()) {
      return;
    }

    PsiClass containingClass = field.getContainingClass();
    if (containingClass != null && containingClass.getName() != null) {
      OffsetKey oldStart = context.trackOffset(context.getStartOffset(), true);
      JavaCompletionUtil.insertClassReference(containingClass, file, context.getStartOffset());
      context.getDocument().insertString(context.getOffsetMap().getOffset(oldStart), ".");
      PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
    }
  }