Couple<Set<String>> () { Set<String> set1 = myClassesSet; Set<String> set2 = myVariablesSet; boolean wasConflict = myConflict; if (set1 == null || set2 == null) { final Set<String> localsSet = new HashSet<>(); final Set<String> classesSet = new HashSet<>(); final Ref<Boolean> conflict = new Ref<>(Boolean.FALSE); PsiScopesUtil.walkChildrenScopes(this, new PsiScopeProcessor() { @Override public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { if (element instanceof PsiLocalVariable || element instanceof PsiPatternVariable) { final PsiVariable variable = (PsiVariable)element; final String name = variable.getName(); if (!localsSet.add(name)) { conflict.set(Boolean.TRUE); localsSet.clear(); classesSet.clear(); } } else if (element instanceof PsiClass) { final PsiClass psiClass = (PsiClass)element; final String name = psiClass.getName(); if (!classesSet.add(name)) { conflict.set(Boolean.TRUE); localsSet.clear(); classesSet.clear(); } } return !conflict.get(); } }, PatternResolveState.WHEN_BOTH.putInto(ResolveState.initial()), this, this); myClassesSet = set1 = classesSet.isEmpty() ? Collections.emptySet() : classesSet; myVariablesSet = set2 = localsSet.isEmpty() ? Collections.emptySet() : localsSet; myConflict = wasConflict = conflict.get(); } return wasConflict ? null : Couple.of(set1, set2); }