org.jetbrains.uast.UCallExpression Java Examples

The following examples show how to use org.jetbrains.uast.UCallExpression. 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: DebugLintIssue.java    From Debug with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public UElementHandler createUastHandler(@NotNull final JavaContext context) {
    return new UElementHandler() {
        @Override
        public void visitCallExpression(@NotNull UCallExpression node) {

            final PsiMethod psiMethod = node.resolve();
            if (psiMethod != null
                    && context.getEvaluator().isMemberInClass(psiMethod, "io.noties.debug.Debug")) {

                final String name = node.getMethodName();
                if (name != null && METHODS.contains(name)) {
                    process(context, node);
                }
            }
        }
    };
}
 
Example #2
Source File: InstalledBeforeSuperDetector.java    From Folivora with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visitCallExpression(UCallExpression node) {
  if (node == target || node.getPsi() != null && node.getPsi() == target.getPsi()) {
    if (onCreateFound) {
      ok = true;
      return true;
    }
  } else {
    if ("onCreate".equals(LintUtils.getMethodName(node))
      && node.getReceiver() != null
      && "super".equals(node.getReceiver().toString())) {
      onCreateFound = true;
    }
  }
  return super.visitCallExpression(node);
}
 
Example #3
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 6 votes vote down vote up
/**
 * Generate method identifier from method information.
 *
 * @param node UCallExpression
 * @return className + methodName + parametersType
 */
@Nullable
private static String methodIdentifier(@NotNull UCallExpression node) {
    UElement element = node.getUastParent();
    while (element != null) {
        if (element instanceof UClass) {
            break;
        }
        element = element.getUastParent();
    }
    UClass uClass = (UClass) element;
    if (uClass == null || node.getMethodName() == null) {
        return null;
    }
    return uClass.getName() + COLON + node.getMethodName();
}
 
Example #4
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 6 votes vote down vote up
private boolean isWithPermissionCheckCalled(@NotNull UCallExpression node, @NotNull UIdentifier methodIdentifier) {
    if (!(needPermissionMethodName + "WithPermissionCheck").equalsIgnoreCase(methodIdentifier.getName())) {
        return false;
    }

    UExpression receiver = node.getReceiver();
    if (isKotlin) {
        return receiver == null;
    } else {
        String qualifiedName = uClass.getQualifiedName();
        if (node.getValueArgumentCount() < 1) return false;
        PsiType expressionType = node.getValueArguments().get(0).getExpressionType();
        return receiver != null
                && qualifiedName != null
                && expressionType != null
                && qualifiedName.equalsIgnoreCase(expressionType.getCanonicalText())
                && (uClass.getName() + "PermissionsDispatcher").equalsIgnoreCase(receiver.asSourceString());
    }
}
 
Example #5
Source File: AirConUsageDetector.java    From aircon with MIT License 5 votes vote down vote up
@Override
public boolean visitCallExpression(@NotNull final UCallExpression node) {
	for (IssueDetector issueDetector : mIssueDetectors) {
		issueDetector.visit(node);
	}
	return super.visitCallExpression(node);
}
 
Example #6
Source File: SignalLogDetector.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private LintFix quickFixIssueLog(@NotNull UCallExpression logCall) {
  List<UExpression> arguments  = logCall.getValueArguments();
  String            methodName = logCall.getMethodName();
  UExpression       tag        = arguments.get(0);

  String fixSource = "org.thoughtcrime.securesms.logging.Log.";

  switch (arguments.size()) {
    case 2:
      UExpression msgOrThrowable = arguments.get(1);
      fixSource += String.format("%s(%s, %s)", methodName, tag, msgOrThrowable.asSourceString());
      break;

    case 3:
      UExpression msg = arguments.get(1);
      UExpression throwable = arguments.get(2);
      fixSource += String.format("%s(%s, %s, %s)", methodName, tag, msg.asSourceString(), throwable.asSourceString());
      break;

    default:
      throw new IllegalStateException("Log overloads should have 2 or 3 arguments");
  }

  String logCallSource = logCall.asSourceString();
  LintFix.GroupBuilder fixGrouper = fix().group();
  fixGrouper.add(fix().replace().text(logCallSource).shortenNames().reformat(true).with(fixSource).build());
  return fixGrouper.build();
}
 
Example #7
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 #8
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 #9
Source File: CallNeedsPermissionDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitCallExpression(@NotNull UCallExpression node) {
    if (isGeneratedFiles(context) || !hasRuntimePermissionAnnotation) {
        return true;
    }
    if (node.getReceiver() == null && annotatedMethods.contains(methodIdentifier(node))) {
        context.report(ISSUE, node, context.getLocation(node), "Trying to access permission-protected method directly");
    }
    return true;
}
 
Example #10
Source File: FromMethodVisitor.java    From HighLite with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitCallExpression(UCallExpression node) {
    if (!METHOD_NAME.equals(node.getMethodName())) return false;

    final JavaUCompositeQualifiedExpression exp =
            (JavaUCompositeQualifiedExpression) node.getUastParent();
    if (exp == null
            || !CLASS_SQLITE_OPERATOR.equals(exp.receiver.toString())
            || node.getValueArgumentCount() != 2) return false;

    final JavaUClassLiteralExpression classLiteral =
            (JavaUClassLiteralExpression) node.getValueArguments().get(PARAM_INDEX);

    if (classLiteral == null) return false;

    final PsiClass psiClass = mContext.getEvaluator().findClass(classLiteral.toString());

    if (psiClass == null
            || psiClass.getModifierList() == null) return false;

    boolean found = false;
    for (final PsiAnnotation annotation : psiClass.getModifierList().getAnnotations()) {
        if (!ANNOTATION_TYPE_LONG.equals(annotation.getQualifiedName())) continue;

        found = true;
        break;
    }

    if (!found) {
        mContext.report(ISSUE, mContext.getLocation(classLiteral),
                String.format(ISSUE.getExplanation(TextFormat.TEXT), classLiteral));
        return true;
    }

    return false;
}
 
Example #11
Source File: NoDelegateOnResumeDetector.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitCallExpression(UCallExpression node) {
    super.visitCallExpression(node);
    UIdentifier methodIdentifier = node.getMethodIdentifier();
    if (methodIdentifier != null && isWithPermissionCheckCalled(node, methodIdentifier)) {
        context.report(ISSUE, context.getLocation(node), "Asking permission inside onResume()");
    }
    return true;
}
 
Example #12
Source File: DebugLintIssue.java    From Debug with Apache License 2.0 4 votes vote down vote up
private static void process(@NonNull JavaContext context, @NonNull UCallExpression expression) {

        // to be able to mutate (we remove first Throwable if present)
        final List<UExpression> arguments = new ArrayList<>(expression.getValueArguments());
        if (arguments.isEmpty()) {
            // if there are no arguments -> no check
            return;
        }

        // remove throwable (comes first0
        if (isSubclassOf(context, arguments.get(0), Throwable.class)) {
            arguments.remove(0);
        }

        // still check for empty arguments (method can be called with just a throwable)
        // if first argument is not a string, then also nothing to do here
        if (arguments.isEmpty()
                || !isSubclassOf(context, arguments.get(0), String.class)) {
            return;
        }

        // now, first arg is string, check if it matches the pattern
        final String pattern = (String) arguments.get(0).evaluate();
        if (pattern == null
                || pattern.length() == 0) {
            // if no pattern is available -> return
            return;
        }

        final Matcher matcher = STRING_FORMAT_PATTERN.matcher(pattern);

        // we must _find_, not _matches_
        if (matcher.find()) {
            // okay, first argument is string
            // evaluate other arguments (actually create them)

            // remove pattern
            arguments.remove(0);

            // what else can we do -> count actual placeholders and arguments
            // (if mismatch... no, we can have positioned)
            final Object[] mock = mockArguments(arguments);

            try {
                //noinspection ResultOfMethodCallIgnored
                String.format(pattern, mock);
            } catch (Throwable t) {
                context.report(
                        ISSUE,
                        expression,
                        context.getLocation(expression),
                        t.getMessage());
            }
        }
    }
 
Example #13
Source File: DebugLintIssue.java    From Debug with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public List<Class<? extends UElement>> getApplicableUastTypes() {
    //noinspection unchecked
    return (List) Collections.singletonList(UCallExpression.class);
}
 
Example #14
Source File: InternalFolivoraApiDetector.java    From Folivora with Apache License 2.0 4 votes vote down vote up
private void report(JavaContext context, UCallExpression call) {
  context.report(ISSUE, call, context.getLocation(call),
    "`Folivora.applyDrawableToView()` should only be called in preview stub view's (Context context, AttributeSet attrs) constructor");
}
 
Example #15
Source File: InstalledBeforeSuperDetector.java    From Folivora with Apache License 2.0 4 votes vote down vote up
private SuperOnCreateFinder(UCallExpression target) {
  this.target = target;
}
 
Example #16
Source File: IssueDetector.java    From aircon with MIT License 4 votes vote down vote up
public void visit(final UCallExpression node) {
	// No-op, to be overridden
}
 
Example #17
Source File: DialogMethodCallLintDetector.java    From SimpleDialogFragments with Apache License 2.0 3 votes vote down vote up
@Override
public void visitMethod(JavaContext context, UCallExpression node, PsiMethod method) {

    if (context.getEvaluator().isMemberInSubClassOf(method,
            "eltos.simpledialogfragment.SimpleDialog", false)) {

        PsiClass definingClass = method.getContainingClass();
        UExpression callingExpression = node.getReceiver();

        if (definingClass != null && callingExpression != null) {

            PsiType type = TypeEvaluator.evaluate(callingExpression);
            if (type instanceof PsiClassType) {
                // when called on instance of a class
                PsiClass callingClass = ((PsiClassType) type).resolve();

                if (callingClass != null && !Objects.equals(callingClass, definingClass)) {

                    context.report(BUILD_CALL, context.getLocation(node), String.format(
                            BUILD_CALL_MESSAGE, callingClass.getName(), definingClass.getName()));
                }

            } else {
                // when called as static reference
                if (!Objects.equals(definingClass.getName(), callingExpression.toString())) {
                    context.report(BUILD_CALL, context.getLocation(node), String.format(
                            BUILD_CALL_MESSAGE, callingExpression, definingClass.getName()));
                }
            }

        }



    }

}
 
Example #18
Source File: DialogMethodCallLintDetector.java    From SimpleDialogFragments with Apache License 2.0 3 votes vote down vote up
@Override
public void visitMethod(JavaContext context, UCallExpression node, PsiMethod method) {

    if (context.getEvaluator().isMemberInSubClassOf(method,
            "eltos.simpledialogfragment.SimpleDialog", false)) {

        PsiClass definingClass = method.getContainingClass();
        UExpression callingExpression = node.getReceiver();

        if (definingClass != null && callingExpression != null) {

            PsiType type = TypeEvaluator.evaluate(callingExpression);
            if (type instanceof PsiClassType) {
                // when called on instance of a class
                PsiClass callingClass = ((PsiClassType) type).resolve();

                if (callingClass != null && !Objects.equals(callingClass, definingClass)) {

                    context.report(BUILD_CALL, context.getLocation(node), String.format(
                            BUILD_CALL_MESSAGE, callingClass.getName(), definingClass.getName()));
                }

            } else {
                // when called as static reference
                if (!Objects.equals(definingClass.getName(), callingExpression.toString())) {
                    context.report(BUILD_CALL, context.getLocation(node), String.format(
                            BUILD_CALL_MESSAGE, callingExpression, definingClass.getName()));
                }
            }

        }



    }

}