protected final PsiElement getParentByStub() {
    final StubElement<?> stub = getStub();
    if (stub != null) {
      return stub.getParentStub().getPsi();
    }

    return SharedImplUtil.getParent(getNode());
  }
  @Override
  public boolean isValid() {
    T stub = myStub;
    if (stub != null) {
      StubElement parent = stub.getParentStub();
      if (parent == null) {
        LOG.error("No parent for stub " + stub + " of class " + stub.getClass());
        return false;
      }
      PsiElement psi = parent.getPsi();
      return psi != null && psi.isValid();
    }

    return super.isValid();
  }
  private static IElementType stubType(final StubElement<?> stub) {
    if (stub instanceof PsiFileStub) {
      return ((PsiFileStub) stub).getType();
    }

    return stub.getStubType();
  }
 @Nullable
 public <Psi extends PsiElement> Psi getStubOrPsiChild(
     final IStubElementType<? extends StubElement, Psi> elementType) {
   T stub = myStub;
   if (stub != null) {
     final StubElement<Psi> element = stub.findChildStubByType(elementType);
     if (element != null) {
       return element.getPsi();
     }
   } else {
     final ASTNode childNode = getNode().findChildByType(elementType);
     if (childNode != null) {
       return (Psi) childNode.getPsi();
     }
   }
   return null;
 }
  @Override
  @NotNull
  public PsiFile getContainingFile() {
    StubElement stub = myStub;
    if (stub != null) {
      while (!(stub instanceof PsiFileStub)) {
        stub = stub.getParentStub();
      }
      PsiFile psi = (PsiFile) stub.getPsi();
      if (psi == null) {
        throw new PsiInvalidElementAccessException(this, "no psi for file stub " + stub, null);
      }
      return psi;
    }

    PsiFile file = super.getContainingFile();
    if (file == null) {
      throw new PsiInvalidElementAccessException(this);
    }

    return file;
  }