/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi;

import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * @author Gregory.Shrago
 */
public class PsiReferenceServiceImpl extends PsiReferenceService {
  @NotNull
  @Override
  public List<PsiReference> getReferences(@NotNull PsiElement element, @NotNull Hints hints) {
    if (element instanceof ContributedReferenceHost) {
      return Arrays.asList(ReferenceProvidersRegistry.getReferencesFromProviders(element, hints));
    }
    if (element instanceof HintedReferenceHost) {
      return Arrays.asList(((HintedReferenceHost)element).getReferences(hints));
    }
    return Arrays.asList(element.getReferences());
  }
}

@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -18,9 +18,9 @@
 -201
-4
+9
  Jet
@@ -36,593 +36,234 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.intellij.psi;%0A
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.%0Apackage com.intellij.psi;%0A%0Aimport com.intellij.openapi.diagnostic.Attachment;%0Aimport com.intellij.openapi.diagnostic.Logger;
 %0Aimp
@@ -534,16 +534,101 @@
 rvice %7B%0A
+%0A  private static final Logger LOG = Logger.getInstance(PsiReferenceService.class);%0A%0A
   @NotNu
@@ -733,24 +733,295 @@
 ts hints) %7B%0A
+    List%3CPsiReference%3E references = doGetReferences(element, hints);%0A    assertReferencesHaveSameElement(element, references);%0A    return references;%0A  %7D%0A%0A  @NotNull%0A  private static List%3CPsiReference%3E doGetReferences(@NotNull PsiElement element, @NotNull Hints hints) %7B%0A
     if (elem
@@ -1359,11 +1359,860 @@
 ());%0A  %7D
+%0A%0A  private static void assertReferencesHaveSameElement(@NotNull PsiElement element, @NotNull List%3CPsiReference%3E references) %7B%0A    for (PsiReference reference : references) %7B%0A      PsiElement referenceElement = reference.getElement();%0A      if (referenceElement != element) %7B%0A        LOG.error(%0A          %22Reference element is not the same element for which references were queried%22,%0A          new RuntimeException(%0A            %22Element: %22 + element.getClass().getName() + %22; %22 +%0A            %22reference: %22 + reference.getClass().getName() + %22; %22 +%0A            %22reference element: %22 + referenceElement.getClass().getName()%0A          ),%0A          new Attachment(%0A            %22info.txt%22,%0A            %22Element text: %60%22 + element.getText() + %22%60%5Cn%22 +%0A            %22Reference range: %22 + reference.getRangeInElement()%0A          )%0A        );%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

enforce `∀ref ∈ element.getReferences(), ref.getElement() == element` with assertion in PsiReferenceServiceImpl