private boolean checkAccessibility(final PsiClass aClass) {
    // We don't care about accessibility in javadoc
    if (JavaResolveUtil.isInJavaDoc(myPlace)) {
      return true;
    }

    if (PsiImplUtil.isInServerPage(aClass.getContainingFile())) {
      PsiFile file = FileContextUtil.getContextFile(myPlace);
      if (PsiImplUtil.isInServerPage(file)) {
        return true;
      }
    }

    boolean accessible = true;
    if (aClass instanceof PsiTypeParameter) {
      accessible = !myStaticContext;
    }

    PsiManager manager = aClass.getManager();
    if (aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
      PsiElement parent = aClass.getParent();
      while (true) {
        PsiElement parentScope = parent.getParent();
        if (parentScope instanceof PsiJavaFile) break;
        parent = parentScope;
        if (!(parentScope instanceof PsiClass)) break;
      }
      if (parent instanceof PsiDeclarationStatement) {
        parent = parent.getParent();
      }
      accessible = false;
      for (PsiElement placeParent = myPlace;
          placeParent != null;
          placeParent = placeParent.getContext()) {
        if (manager.areElementsEquivalent(placeParent, parent)) accessible = true;
      }
    }
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    if (aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
      accessible = false;
      if (myPlace != null && facade.arePackagesTheSame(aClass, myPlace)) {
        accessible = true;
      } else {
        if (aClass.getContainingClass() != null) {
          accessible =
              myAccessClass == null
                  || myPlace != null
                      && facade.getResolveHelper().isAccessible(aClass, myPlace, myAccessClass);
        }
      }
    }
    if (aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
      if (myPlace == null || !facade.arePackagesTheSame(aClass, myPlace)) {
        accessible = false;
      }
    }
    return accessible;
  }
 @Override
 public PsiLanguageInjectionHost getInjectionHost(@NotNull PsiElement element) {
   final PsiFile file = element.getContainingFile();
   final VirtualFile virtualFile = file == null ? null : file.getVirtualFile();
   if (virtualFile instanceof VirtualFileWindow) {
     PsiElement host =
         FileContextUtil.getFileContext(
             file); // use utility method in case the file's overridden getContext()
     if (host instanceof PsiLanguageInjectionHost) {
       return (PsiLanguageInjectionHost) host;
     }
   }
   return null;
 }
  private boolean isOnDemand(PsiElement fileContext, PsiClass psiClass) {
    if (isImported(fileContext)) {
      return ((PsiImportStatementBase) fileContext).isOnDemand();
    }

    String fqn = psiClass.getQualifiedName();
    if (fqn == null) return false;

    PsiFile file = myPlace == null ? null : FileContextUtil.getContextFile(myPlace);

    String[] defaultPackages =
        file instanceof PsiJavaFile
            ? ((PsiJavaFile) file).getImplicitlyImportedPackages()
            : DEFAULT_PACKAGES;
    String packageName = StringUtil.getPackageName(fqn);
    for (String defaultPackage : defaultPackages) {
      if (defaultPackage.equals(packageName)) return true;
    }

    // class from my package imported implicitly
    return file instanceof PsiJavaFile && ((PsiJavaFile) file).getPackageName().equals(packageName);
  }
Ejemplo n.º 4
0
  private void getBindings() {
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
    if (containingClass != null) return;

    final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
    if (file instanceof GroovyFile) {
      ((GroovyFile) file)
          .accept(
              new GroovyRecursiveElementVisitor() {
                @Override
                public void visitAssignmentExpression(GrAssignmentExpression expression) {
                  super.visitAssignmentExpression(expression);

                  final GrExpression value = expression.getLValue();
                  if (value instanceof GrReferenceExpression
                      && !((GrReferenceExpression) value).isQualified()) {
                    final PsiElement resolved = ((GrReferenceExpression) value).resolve();
                    if (resolved instanceof GrBindingVariable) {
                      myProcessor.execute(resolved, ResolveState.initial());
                    } else if (resolved == null) {
                      myProcessor.execute(
                          new GrBindingVariable(
                              (GroovyFile) file,
                              ((GrReferenceExpression) value).getReferenceName(),
                              true),
                          ResolveState.initial());
                    }
                  }
                }

                @Override
                public void visitTypeDefinition(GrTypeDefinition typeDefinition) {
                  // don't go into classes
                }
              });
    }
  }
Ejemplo n.º 5
0
 @Override
 public PsiElement getContext() {
   return FileContextUtil.getFileContext(this);
 }