/*
 * 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());
  }
}

// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi;

import com.intellij.openapi.diagnostic.Attachment;
import com.intellij.openapi.diagnostic.Logger;
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 {

  private static final Logger LOG = Logger.getInstance(PsiReferenceService.class);

  @NotNull
  @Override
  public List<PsiReference> getReferences(@NotNull PsiElement element, @NotNull Hints hints) {
    List<PsiReference> references = doGetReferences(element, hints);
    assertReferencesHaveSameElement(element, references);
    return references;
  }

  @NotNull
  private static List<PsiReference> doGetReferences(@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());
  }

  private static void assertReferencesHaveSameElement(@NotNull PsiElement element, @NotNull List<PsiReference> references) {
    for (PsiReference reference : references) {
      PsiElement referenceElement = reference.getElement();
      if (referenceElement != element) {
        LOG.error(
          "Reference element is not the same element for which references were queried",
          new RuntimeException(
            "Element: " + element.getClass().getName() + "; " +
            "reference: " + reference.getClass().getName() + "; " +
            "reference element: " + referenceElement.getClass().getName()
          ),
          new Attachment(
            "info.txt",
            "Element text: `" + element.getText() + "`\n" +
            "Reference range: " + reference.getRangeInElement()
          )
        );
      }
    }
  }
}

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