@Override
  public @NotNull PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    PsiFile containingFile = getContainingFile();
    CheckUtil.checkWritable(containingFile);
    if (isReferenceTo(element)) return this;

    Kind kind = getKindEnum(containingFile);
    switch (kind) {
      case CLASS_NAME_KIND:
      case CLASS_FQ_NAME_KIND:
        if (!(element instanceof PsiClass)) {
          throw cannotBindError(element, kind, element+ " is not a PsiClass but "+element.getClass());
        }
        return bindToClass((PsiClass)element, containingFile);

      case PACKAGE_NAME_KIND:
        if (!(element instanceof PsiPackage)) {
          throw cannotBindError(element, kind, "I, " + this +" ("+getClass()+") contained in "+containingFile+" in "+containingFile.getVirtualFile()+
                                               " am a package since my treeParent is " + getTreeParent()+" of "+getTreeParent().getElementType()+
                                               ", but "+element+ " is not a PsiPackage but "+element.getClass());
        }
        return bindToPackage((PsiPackage)element);

      case CLASS_OR_PACKAGE_NAME_KIND:
      case CLASS_FQ_OR_PACKAGE_NAME_KIND:
        if (element instanceof PsiClass) {
          return bindToClass((PsiClass)element, containingFile);
        }
        else if (element instanceof PsiPackage) {
          return bindToPackage((PsiPackage)element);
        }
        else {
          throw cannotBindError(element, kind, element+ " is not a PsiClass/PsiPackage but "+element.getClass());
        }
      case CLASS_IN_QUALIFIED_NEW_KIND:
        if (element instanceof PsiClass) {
          PsiClass aClass = (PsiClass)element;
          String name = aClass.getName();
          if (name == null) {
            throw new IncorrectOperationException(aClass.toString());
          }
          PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(containingFile.getProject()).getParserFacade();
          PsiJavaCodeReferenceElement ref = parserFacade.createReferenceFromText(name, getParent());
          getTreeParent().replaceChildInternal(this, (TreeElement)ref.getNode());
          return ref;
        }
        else {
          throw cannotBindError(element, kind, element+ " is not a PsiClass but "+element.getClass());
        }
      case MODULE_NAME_KIND:
        if (element instanceof PsiJavaModule) {
          return bindToModule((PsiJavaModule)element, containingFile);
        }
        else {
          throw cannotBindError(element, kind, element+ " is not a PsiJavaModule but "+element.getClass());
        }
      default:
        throw new IllegalArgumentException("Unexpected kind: " + kind);
    }
  }
@Override
  public @NotNull PsiElement bindToElement(@NotNull PsiElement element)