Java 类com.intellij.psi.util.PsiClassUtil 实例源码

项目:intellij-ce-playground    文件:AppletConfigurationProducer.java   
private static boolean isAppletClass(final PsiClass aClass, final PsiManager manager) {
  if (!PsiClassUtil.isRunnableClass(aClass, true)) return false;

  final Module module = JavaExecutionUtil.findModule(aClass);
  final GlobalSearchScope scope = module != null
                            ? GlobalSearchScope.moduleWithLibrariesScope(module)
                            : GlobalSearchScope.projectScope(manager.getProject());
  PsiClass appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.applet.Applet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("javax.swing.JApplet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  return false;
}
项目:tools-idea    文件:AppletConfigurationProducer.java   
private static boolean isAppletClass(final PsiClass aClass, final PsiManager manager) {
  if (!PsiClassUtil.isRunnableClass(aClass, true)) return false;

  final Module module = JavaExecutionUtil.findModule(aClass);
  final GlobalSearchScope scope = module != null
                            ? GlobalSearchScope.moduleWithLibrariesScope(module)
                            : GlobalSearchScope.projectScope(manager.getProject());
  PsiClass appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.applet.Applet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("javax.swing.JApplet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  return false;
}
项目:consulo-java    文件:AppletConfigurationProducer.java   
private static boolean isAppletClass(final PsiClass aClass, final PsiManager manager) {
  if (!PsiClassUtil.isRunnableClass(aClass, true)) return false;

  final Module module = JavaExecutionUtil.findModule(aClass);
  final GlobalSearchScope scope = module != null
                                  ? GlobalSearchScope.moduleWithLibrariesScope(module)
                                  : GlobalSearchScope.projectScope(manager.getProject());
  PsiClass appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.applet.Applet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  appletClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("javax.swing.JApplet", scope);
  if (appletClass != null) {
    if (aClass.isInheritor(appletClass, true)) return true;
  }
  return false;
}
项目:intellij-ce-playground    文件:JUnitUtil.java   
public static boolean isTestClass(@NotNull PsiClass psiClass, boolean checkAbstract, boolean checkForTestCaseInheritance) {
  if (psiClass.getQualifiedName() == null) return false;
  final PsiClass topLevelClass = PsiTreeUtil.getTopmostParentOfType(psiClass, PsiClass.class);
  if (topLevelClass != null) {
    final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(topLevelClass, Collections.singleton(RUN_WITH));
    if (annotation != null) {
      final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value");
      if (attributeValue instanceof PsiClassObjectAccessExpression) {
        final String runnerName = ((PsiClassObjectAccessExpression)attributeValue).getOperand().getType().getCanonicalText();
        if (!(PARAMETERIZED_CLASS_NAME.equals(runnerName) || SUITE_CLASS_NAME.equals(runnerName))) {
          return true;
        }
      }
    }
  }
  if (!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract)) return false;
  if (checkForTestCaseInheritance && isTestCaseInheritor(psiClass)) return true;
  final PsiModifierList modifierList = psiClass.getModifierList();
  if (modifierList == null) return false;
  if (AnnotationUtil.isAnnotated(psiClass, RUN_WITH, true)) return true;

  for (final PsiMethod method : psiClass.getAllMethods()) {
    ProgressManager.checkCanceled();
    if (isSuiteMethod(method)) return true;
    if (isTestAnnotated(method)) return true;
  }

  return false;
}
项目:intellij-ce-playground    文件:JUnitUtil.java   
private static boolean isJUnit4TestClass(final PsiClass psiClass, boolean checkAbstract) {
  if (!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract)) return false;

  final PsiModifierList modifierList = psiClass.getModifierList();
  if (modifierList == null) return false;
  if (AnnotationUtil.isAnnotated(psiClass, RUN_WITH, true)) return true;
  for (final PsiMethod method : psiClass.getAllMethods()) {
    ProgressManager.checkCanceled();
    if (isTestAnnotated(method)) return true;
  }

  return false;
}
项目:intellij-ce-playground    文件:TemplateParameterStep2.java   
@Override
public Visibility isDeclarationVisible(PsiElement declaration, @Nullable PsiElement place) {
  if (declaration instanceof PsiClass) {
    PsiClass classDecl = (PsiClass)declaration;
    if (PsiClassUtil.isRunnableClass(classDecl, true, true) &&
        isActivitySubclass(classDecl) && isOnClasspath(classDecl)) {
      return Visibility.VISIBLE;
    }
  }
  return Visibility.NOT_VISIBLE;
}
项目:intellij-ce-playground    文件:JUnitEntryPoint.java   
@Override
public boolean isEntryPoint(@NotNull PsiElement psiElement) {
  if (ADD_JUNIT_TO_ENTRIES) {
    if (psiElement instanceof PsiClass) {
      final PsiClass aClass = (PsiClass)psiElement;
      if (JUnitUtil.isTestClass(aClass, false, true)) {
        if (!PsiClassUtil.isRunnableClass(aClass, true, true)) {
          final PsiClass topLevelClass = PsiTreeUtil.getTopmostParentOfType(aClass, PsiClass.class);
          if (topLevelClass != null && PsiClassUtil.isRunnableClass(topLevelClass, true, true)) {
            return true;
          }
          final CommonProcessors.FindProcessor<PsiClass> findProcessor = new CommonProcessors.FindProcessor<PsiClass>() {
            @Override
            protected boolean accept(PsiClass psiClass) {
              return !psiClass.hasModifierProperty(PsiModifier.ABSTRACT);
            }
          };
          return !ClassInheritorsSearch.search(aClass).forEach(findProcessor) && findProcessor.isFound();
        }
        return true;
      }
    }
    else if (psiElement instanceof PsiMethod) {
      final PsiMethod method = (PsiMethod)psiElement;
      if (method.isConstructor() && method.getParameterList().getParametersCount() == 0) {
        return JUnitUtil.isTestClass(method.getContainingClass());
      }
      if (JUnitUtil.isTestMethodOrConfig(method)) return true;
    }
  }
  return false;
}
项目:tools-idea    文件:JUnitUtil.java   
public static boolean isTestClass(@NotNull PsiClass psiClass, boolean checkAbstract, boolean checkForTestCaseInheritance) {
  if (!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract)) return false;
  if (checkForTestCaseInheritance && isTestCaseInheritor(psiClass)) return true;
  final PsiModifierList modifierList = psiClass.getModifierList();
  if (modifierList == null) return false;
  if (AnnotationUtil.isAnnotated(psiClass, RUN_WITH, true)) return true;

  for (final PsiMethod method : psiClass.getAllMethods()) {
    ProgressManager.checkCanceled();
    if (isSuiteMethod(method)) return true;
    if (isTestAnnotated(method)) return true;
  }

  return false;
}
项目:tools-idea    文件:JUnitUtil.java   
private static boolean isJUnit4TestClass(final PsiClass psiClass, boolean checkAbstract) {
  if (!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract)) return false;

  final PsiModifierList modifierList = psiClass.getModifierList();
  if (modifierList == null) return false;
  if (AnnotationUtil.isAnnotated(psiClass, RUN_WITH, true)) return true;
  for (final PsiMethod method : psiClass.getAllMethods()) {
    ProgressManager.checkCanceled();
    if (isTestAnnotated(method)) return true;
  }

  return false;
}
项目:tools-idea    文件:JUnitEntryPoint.java   
@Override
public boolean isEntryPoint(PsiElement psiElement) {
  if (ADD_JUNIT_TO_ENTRIES) {
    if (psiElement instanceof PsiClass) {
      final PsiClass aClass = (PsiClass)psiElement;
      if (JUnitUtil.isTestClass(aClass, false, true)) {
        if (!PsiClassUtil.isRunnableClass(aClass, true, true)) {
          final CommonProcessors.FindProcessor<PsiClass> findProcessor = new CommonProcessors.FindProcessor<PsiClass>() {
            @Override
            protected boolean accept(PsiClass psiClass) {
              return !psiClass.hasModifierProperty(PsiModifier.ABSTRACT);
            }
          };
          return !ClassInheritorsSearch.search(aClass).forEach(findProcessor) && findProcessor.isFound();
        }
        return true;
      }
    }
    else if (psiElement instanceof PsiMethod) {
      final PsiMethod method = (PsiMethod)psiElement;
      if (method.isConstructor() && method.getParameterList().getParametersCount() == 0) {
        return JUnitUtil.isTestClass(method.getContainingClass());
      }
      if (JUnitUtil.isTestMethodOrConfig(method)) return true;
    }
  }
  return false;
}
项目:intellij-ce-playground    文件:TestNGRelatedFilesProvider.java   
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) {
  PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false);
  if (psiClass != null) {
    final Project project = psiClass.getProject();
    while (psiClass != null && TestNGUtil.hasTest(psiClass) && PsiClassUtil.isRunnableClass(psiClass, true)) {
      final String qName = psiClass.getQualifiedName();
      if (qName != null) {
        final String packageQName = ((PsiJavaFile)psiClass.getContainingFile()).getPackageName();
        final String packageName = StringUtil.getShortName(packageQName);
        final String[] names;
        if (packageQName.length() > 0) {
          final String pName = packageName.length() > 0 ? packageName : packageQName;
          names = new String[]{qName, pName};
        }
        else {
          names = new String[]{qName};
        }
        final List<PsiElement> tags = new ArrayList<PsiElement>();
        for (final String name : names) {
          PsiSearchHelper.SERVICE.getInstance(project)
            .processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {
              public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
                final PsiReference referenceAt = file.findReferenceAt(startOffset);
                if (referenceAt != null) {
                  if (packageQName.endsWith(name)) { //special package tag required
                    final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                    if (tag == null || !tag.getName().equals("package")) {
                      return true;
                    }
                    final XmlAttribute attribute = tag.getAttribute("name");
                    if (attribute == null) return true;
                    final String value = attribute.getValue();
                    if (value == null) return true;
                    if (!(value.equals(StringUtil.getQualifiedName(packageQName, "*")) || value.equals(packageQName))) return true;
                  }
                  tags.add(referenceAt.getElement());
                }
                return true;
              }
            }, new TestNGSearchScope(project));
        }

        if (!tags.isEmpty()) {
          return GotoRelatedItem.createItems(tags, "TestNG");
        }
      }
      psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class);
    }
  }
  return Collections.emptyList();
}
项目:intellij-ce-playground    文件:AbstractTestNGInClassConfigurationProducer.java   
private static boolean isTestNGClass(PsiClass psiClass) {
  return psiClass != null && PsiClassUtil.isRunnableClass(psiClass, true, false) && TestNGUtil.hasTest(psiClass);
}
项目:intellij-ce-playground    文件:UndeclaredTestInspection.java   
@Nullable
public ProblemDescriptor[] checkClass(@NotNull final PsiClass aClass,
                                      @NotNull final InspectionManager manager,
                                      final boolean isOnTheFly) {
  if (TestNGUtil.hasTest(aClass) && PsiClassUtil.isRunnableClass(aClass, true)) {
    final Project project = aClass.getProject();
    final String qName = aClass.getQualifiedName();
    if (qName == null) return null;
    final String packageQName = StringUtil.getPackageName(qName);
    final List<String> names = new ArrayList<String>();
    for(int i = 0; i < qName.length(); i++) {
      if (qName.charAt(i) == '.') {
        names.add(qName.substring(0, i));
      }
    }
    names.add(qName);
    Collections.reverse(names);

    for (final String name : names) {
      final boolean isFullName = qName.equals(name);
      final boolean[] found = new boolean[]{false};
      PsiSearchHelper.SERVICE.getInstance(project)
        .processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {
          public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
            if (file.findReferenceAt(startOffset) != null) {
              if (!isFullName) { //special package tag required
                final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                if (tag == null || !tag.getName().equals("package")) {
                  return true;
                }
                final XmlAttribute attribute = tag.getAttribute("name");
                if (attribute == null) return true;
                final String value = attribute.getValue();
                if (value == null) return true;
                if (!value.endsWith(".*") && !value.equals(packageQName)) return true;
              }
              found[0] = true;
              return false;
            }
            return true;
          }
        }, new TestNGSearchScope(project));
      if (found[0]) return null;
    }
    final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
    LOG.assertTrue(nameIdentifier != null);
    return new ProblemDescriptor[]{manager.createProblemDescriptor(nameIdentifier, "Undeclared test \'" + aClass.getName() + "\'",
                                                                   isOnTheFly, new LocalQuickFix[]{new RegisterClassFix(aClass),
                                                                     new CreateTestngFix()},
                                                                   ProblemHighlightType.GENERIC_ERROR_OR_WARNING)};
  }
  return null;
}
项目:tools-idea    文件:JavaExecutionUtil.java   
public static boolean isRunnableClass(final PsiClass aClass) {
  return PsiClassUtil.isRunnableClass(aClass, true);
}
项目:tools-idea    文件:TestNGRelatedFilesProvider.java   
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) {
  PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false);
  if (psiClass != null) {
    final Project project = psiClass.getProject();
    while (psiClass != null && TestNGUtil.hasTest(psiClass) && PsiClassUtil.isRunnableClass(psiClass, true)) {
      final String qName = psiClass.getQualifiedName();
      if (qName != null) {
        final String packageQName = ((PsiJavaFile)psiClass.getContainingFile()).getPackageName();
        final String packageName = StringUtil.getShortName(packageQName);
        final String[] names;
        if (packageQName.length() > 0) {
          final String pName = packageName.length() > 0 ? packageName : packageQName;
          names = new String[]{qName, pName};
        }
        else {
          names = new String[]{qName};
        }
        final List<PsiElement> tags = new ArrayList<PsiElement>();
        for (final String name : names) {
          PsiSearchHelper.SERVICE.getInstance(project)
            .processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {
              public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
                final PsiReference referenceAt = file.findReferenceAt(startOffset);
                if (referenceAt != null) {
                  if (packageQName.endsWith(name)) { //special package tag required
                    final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                    if (tag == null || !tag.getName().equals("package")) {
                      return true;
                    }
                    final XmlAttribute attribute = tag.getAttribute("name");
                    if (attribute == null) return true;
                    final String value = attribute.getValue();
                    if (value == null) return true;
                    if (!(value.equals(StringUtil.getQualifiedName(packageQName, "*")) || value.equals(packageQName))) return true;
                  }
                  tags.add(referenceAt.getElement());
                }
                return true;
              }
            }, new TestNGSearchScope(project));
        }

        if (!tags.isEmpty()) {
          return GotoRelatedItem.createItems(tags, "TestNG");
        }
      }
      psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class);
    }
  }
  return Collections.emptyList();
}
项目:tools-idea    文件:TestNGInClassConfigurationProducer.java   
private static boolean isTestNGClass(PsiClass psiClass) {
  return psiClass != null && PsiClassUtil.isRunnableClass(psiClass, true, false) && TestNGUtil.hasTest(psiClass);
}
项目:tools-idea    文件:UndeclaredTestInspection.java   
@Nullable
public ProblemDescriptor[] checkClass(@NotNull final PsiClass aClass,
                                      @NotNull final InspectionManager manager,
                                      final boolean isOnTheFly) {
  if (TestNGUtil.hasTest(aClass) && PsiClassUtil.isRunnableClass(aClass, true)) {
    final Project project = aClass.getProject();
    final String qName = aClass.getQualifiedName();
    if (qName == null) return null;

    final List<String> names = new ArrayList<String>();
    for(int i = 0; i < qName.length(); i++) {
      if (qName.charAt(i) == '.') {
        names.add(qName.substring(0, i));
      }
    }
    names.add(qName);
    Collections.reverse(names);

    for (final String name : names) {
      final boolean isFullName = qName.equals(name);
      final boolean[] found = new boolean[]{false};
      PsiSearchHelper.SERVICE.getInstance(project)
        .processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {
          public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
            if (file.findReferenceAt(startOffset) != null) {
              if (!isFullName) { //special package tag required
                final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                if (tag == null || !tag.getName().equals("package")) {
                  return true;
                }
                final XmlAttribute attribute = tag.getAttribute("name");
                if (attribute == null) return true;
                final String value = attribute.getValue();
                if (value == null) return true;
                if (!value.endsWith(".*")) return true;
              }
              found[0] = true;
              return false;
            }
            return true;
          }
        }, new TestNGSearchScope(project));
      if (found[0]) return null;
    }
    final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
    LOG.assertTrue(nameIdentifier != null);
    return new ProblemDescriptor[]{manager.createProblemDescriptor(nameIdentifier, "Undeclared test \'" + aClass.getName() + "\'",
                                                                   isOnTheFly, new LocalQuickFix[]{new RegisterClassFix(aClass),
                                                                     new CreateTestngFix()},
                                                                   ProblemHighlightType.GENERIC_ERROR_OR_WARNING)};
  }
  return null;
}
项目:consulo-java    文件:JUnitUtil.java   
public static boolean isTestClass(@NotNull PsiClass psiClass, boolean checkAbstract, boolean checkForTestCaseInheritance)
{
    if(psiClass.getQualifiedName() == null)
    {
        return false;
    }
    if(isJUnit5(psiClass) && isJUnit5TestClass(psiClass, checkAbstract))
    {
        return true;
    }
    final PsiClass topLevelClass = PsiTreeUtil.getTopmostParentOfType(psiClass, PsiClass.class);
    if(topLevelClass != null)
    {
        final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(topLevelClass, Collections.singleton(RUN_WITH));
        if(annotation != null)
        {
            final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value");
            if(attributeValue instanceof PsiClassObjectAccessExpression)
            {
                final String runnerName = ((PsiClassObjectAccessExpression) attributeValue).getOperand().getType().getCanonicalText();
                if(!(PARAMETERIZED_CLASS_NAME.equals(runnerName) || SUITE_CLASS_NAME.equals(runnerName)))
                {
                    return true;
                }
            }
        }
    }

    if(!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract))
    {
        return false;
    }

    if(AnnotationUtil.isAnnotated(psiClass, RUN_WITH, CHECK_HIERARCHY))
    {
        return true;
    }

    if(checkForTestCaseInheritance && isTestCaseInheritor(psiClass))
    {
        return true;
    }

    return CachedValuesManager.getCachedValue(psiClass, () -> CachedValueProvider.Result.create(hasTestOrSuiteMethods(psiClass), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT));
}
项目:consulo-java    文件:JUnitUtil.java   
public static boolean isJUnit4TestClass(final PsiClass psiClass, boolean checkAbstract)
{
    final PsiModifierList modifierList = psiClass.getModifierList();
    if(modifierList == null)
    {
        return false;
    }
    final PsiClass topLevelClass = PsiTreeUtil.getTopmostParentOfType(modifierList, PsiClass.class);
    if(topLevelClass != null)
    {
        if(AnnotationUtil.isAnnotated(topLevelClass, RUN_WITH, CHECK_HIERARCHY))
        {
            PsiAnnotation annotation = getRunWithAnnotation(topLevelClass);
            if(topLevelClass == psiClass)
            {
                return true;
            }

            //default runners do not implicitly run inner classes
            if(annotation != null && !isInheritorOrSelfRunner(annotation, RUNNERS_UNAWARE_OF_INNER_CLASSES))
            {
                return true;
            }
        }
    }

    if(!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract))
    {
        return false;
    }

    for(final PsiMethod method : psiClass.getAllMethods())
    {
        ProgressManager.checkCanceled();
        if(isTestAnnotated(method))
        {
            return true;
        }
    }

    return false;
}
项目:consulo-java    文件:JUnitUtil.java   
public static boolean isJUnit5TestClass(@NotNull final PsiClass psiClass, boolean checkAbstract)
{
    final PsiModifierList modifierList = psiClass.getModifierList();
    if(modifierList == null)
    {
        return false;
    }

    if(psiClass.getContainingClass() != null && AnnotationUtil.isAnnotated(psiClass, JUNIT5_NESTED, 0))
    {
        return true;
    }

    if(MetaAnnotationUtil.isMetaAnnotated(psiClass, Collections.singleton(CUSTOM_TESTABLE_ANNOTATION)))
    {
        return true;
    }

    if(!PsiClassUtil.isRunnableClass(psiClass, false, checkAbstract))
    {
        return false;
    }

    Module module = ModuleUtilCore.findModuleForPsiElement(psiClass);
    if(module != null)
    {
        return CachedValuesManager.getCachedValue(psiClass, () ->
        {
            boolean hasAnnotation = false;
            for(final PsiMethod method : psiClass.getAllMethods())
            {
                ProgressManager.checkCanceled();
                if(MetaAnnotationUtil.isMetaAnnotated(method, TEST5_ANNOTATIONS))
                {
                    hasAnnotation = true;
                    break;
                }
            }

            if(!hasAnnotation)
            {
                for(PsiClass aClass : psiClass.getAllInnerClasses())
                {
                    if(MetaAnnotationUtil.isMetaAnnotated(aClass, Collections.singleton(JUNIT5_NESTED)))
                    {
                        hasAnnotation = true;
                        break;
                    }
                }
            }
            return CachedValueProvider.Result.create(hasAnnotation, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT);
        });
    }

    return false;
}
项目:consulo-java    文件:JavaExecutionUtil.java   
public static boolean isRunnableClass(final PsiClass aClass)
{
    return PsiClassUtil.isRunnableClass(aClass, true);
}