String (String initialVisibility, PsiExpression[] occurrences, PsiClass targetClass, Project project) { final ArrayList<String> visible = new ArrayList<>(); visible.add(PsiModifier.PRIVATE); visible.add(PsiModifier.PROTECTED); visible.add(PsiModifier.PACKAGE_LOCAL); visible.add(PsiModifier.PUBLIC); for (PsiExpression occurrence : occurrences) { final PsiManager psiManager = PsiManager.getInstance(project); for (Iterator<String> iterator = visible.iterator(); iterator.hasNext();) { String modifier = iterator.next(); try { final String modifierText = PsiModifier.PACKAGE_LOCAL.equals(modifier) ? "" : modifier + " "; final PsiField field = JavaPsiFacade.getElementFactory(psiManager.getProject()).createFieldFromText(modifierText + "int xxx;", targetClass); if (!JavaResolveUtil.isAccessible(field, targetClass, field.getModifierList(), occurrence, targetClass, null)) { iterator.remove(); } } catch (IncorrectOperationException e) { LOG.error(e); } } } if (!visible.isEmpty() && !visible.contains(initialVisibility)) { return visible.get(0); } return null; }