Java Code Examples for com.intellij.psi.PsiClass#isInheritor()

The following examples show how to use com.intellij.psi.PsiClass#isInheritor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstalledBeforeSuperDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(JavaContext context,
                        UCallExpression call,
                        PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.installViewFactory() call
  String methodName = method.getName();
  if (!methodName.equals(INSTALL_METHOD) || !evaluator.isMemberInClass(method, TYPE_FOLIVORA))
    return;

  //check current class is decent of AppCompatActivity
  PsiClass legacyCompatActClass = evaluator.findClass(LEGACY_COMPAT_ACTIVITY);
  PsiClass compatActClass = evaluator.findClass(COMPAT_ACTIVITY);
  PsiClass c = UastUtils.getContainingClass(call);
  boolean isAppCompatActivity = false;
  if (c != null) {
    isAppCompatActivity = (legacyCompatActClass != null && c.isInheritor(legacyCompatActClass,
      true/*deep check*/))
      || compatActClass != null && c.isInheritor(compatActClass, true/*deep check*/);
  }
  if (!isAppCompatActivity) return;

  //check current method is onCreate
  @SuppressWarnings("unchecked")
  UMethod uMethod = UastUtils.getParentOfType(call, true, UMethod.class);
  if (uMethod == null || !"onCreate".equals(uMethod.getName())) return;

  SuperOnCreateFinder finder = new SuperOnCreateFinder(call);
  uMethod.accept(finder);
  if (!finder.isSuperOnCreateCalled()) {
    context.report(ISSUE, call, context.getLocation(call),
      "calling `Folivora.installViewFactory()` before super.onCreate can cause AppCompatViews unavailable");
  }
}
 
Example 2
Source File: InternalFolivoraApiDetector.java    From Folivora with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(@NotNull JavaContext context,
                        @NotNull UCallExpression call,
                        @NotNull PsiMethod method) {
  JavaEvaluator evaluator = context.getEvaluator();

  //check Folivora.applyDrawableToView() call
  String methodName = method.getName();
  if (!methodName.equals(APPLY_METHOD) || !evaluator.isMemberInClass(method, FOLIVORA_CLASS)) return;

  PsiClass viewClass = evaluator.findClass(VIEW_CLASS);
  PsiClass currentClass = UastUtils.getContainingClass(call);
  if(currentClass == null || viewClass == null) return;
  if (!currentClass.isInheritor(viewClass, true/*deep check*/)) {
    report(context, call);
    return;
  }

  UMethod uMethod = UastUtils.getParentOfType(call, UMethod.class, false);
  if(uMethod == null) return;

  //check it is a view's constructor
  if (!uMethod.isConstructor()) {
    report(context, call);
    return;
  }

  MethodSignature signature = uMethod.getSignature(PsiSubstitutor.EMPTY);
  PsiType[] types = signature.getParameterTypes();
  if (types.length != 2) {
    report(context, call);
    return;
  }

  if (!types[0].equalsToText(CONTEXT_CLASS)
    || !types[1].equalsToText(ATTRS_CLASS)) {
    report(context, call);
  }
}
 
Example 3
Source File: ProducerUtils.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static boolean isTestCaseInheritor(PsiClass psiClass) {
  // unlike JUnitUtil#isTestCaseInheritor, works even if the class isn't in the project
  PsiClass testCaseClass =
      JavaPsiFacade.getInstance(psiClass.getProject())
          .findClass(
              JUnitUtil.TEST_CASE_CLASS, GlobalSearchScope.allScope(psiClass.getProject()));
  if (testCaseClass != null) {
    return psiClass.isInheritor(testCaseClass, true);
  }
  // TestCase isn't indexed, instead use heuristics to check
  return extendsTestCase(psiClass, new HashSet<>());
}
 
Example 4
Source File: MixinImplementsMixinType.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private boolean isImplementValidMixinType( PsiClass mixinClass, Set<PsiClass> validMixinsType )
{
    for( PsiClass validMixinTypeClass : validMixinsType )
    {
        if( mixinClass.isInheritor( validMixinTypeClass, true ) )
        {
            return true;
        }
    }

    return false;
}