Java Code Examples for com.intellij.psi.PsiMethodCallExpression#getArgumentList()

The following examples show how to use com.intellij.psi.PsiMethodCallExpression#getArgumentList() . 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: AddArgumentFix.java    From litho with Apache License 2.0 5 votes vote down vote up
static PsiExpressionList createArgumentList(
    PsiElement context, String clsName, String methodName, PsiElementFactory elementFactory) {
  final PsiMethodCallExpression stub =
      (PsiMethodCallExpression)
          elementFactory.createExpressionFromText(
              "methodName(" + clsName + "." + methodName + "())", context);
  return stub.getArgumentList();
}
 
Example 2
Source File: RequiredPropAnnotator.java    From litho with Apache License 2.0 5 votes vote down vote up
private static void handleMethodCall(
    PsiMethodCallExpression currentMethodCall,
    Set<String> methodNamesCalled,
    BiConsumer<Collection<String>, PsiReferenceExpression> errorHandler,
    Function<PsiMethodCallExpression, PsiClass> generatedClassResolver) {
  PsiReferenceExpression methodExpression = currentMethodCall.getMethodExpression();
  methodNamesCalled.add(methodExpression.getReferenceName());

  // Assumption to find next method in a call chain
  PsiMethodCallExpression nextMethodCall =
      PsiTreeUtil.getChildOfType(methodExpression, PsiMethodCallExpression.class);
  if (nextMethodCall != null) {
    handleMethodCall(nextMethodCall, methodNamesCalled, errorHandler, generatedClassResolver);
  } else if ("create".equals(methodExpression.getReferenceName())) {
    // Finish call chain
    // TODO T47712852: allow setting required prop in another statement
    Optional.ofNullable(generatedClassResolver.apply(currentMethodCall))
        .map(generatedCls -> collectMissingRequiredProps(generatedCls, methodNamesCalled))
        .filter(result -> !result.isEmpty())
        .ifPresent(
            missingRequiredProps -> errorHandler.accept(missingRequiredProps, methodExpression));
  }

  PsiExpressionList argumentList = currentMethodCall.getArgumentList();
  for (PsiExpression argument : argumentList.getExpressions()) {
    handleIfMethodCall(argument, errorHandler, generatedClassResolver);
  }
}
 
Example 3
Source File: EventHandlerAnnotator.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public void annotate(PsiElement element, AnnotationHolder holder) {
  DEBUG_LOGGER.logStep("start " + element);
  // Implementation similar to {@link HighlightMethodUtil#checkMethodCall}
  if (!(element instanceof PsiMethodCallExpression)) {
    return;
  }
  // MethodCall == method usage
  final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element;
  final PsiExpressionList list = eventHandlerSetter.getArgumentList();
  if (!list.isEmpty()) {
    return;
  }
  final String eventQualifiedName = resolveEventName(eventHandlerSetter);
  if (eventQualifiedName == null) {
    return;
  }
  final PsiClass parentCls =
      (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance);
  if (parentCls == null) {
    return;
  }
  final LayoutSpecModel parentLayoutModel = ComponentGenerateUtils.createLayoutModel(parentCls);
  if (parentLayoutModel == null) {
    return;
  }
  final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>>
      implementedEventHandlers = parentLayoutModel.getEventMethods();
  final String componentQualifiedName = parentLayoutModel.getComponentTypeName().toString();
  final Project project = eventHandlerSetter.getProject();
  final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
  final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter);
  final SpecModelValidationError error = new SpecModelValidationError(list, message);

  final List<IntentionAction> fixes =
      implementedEventHandlers.stream()
          .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName()))
          .map(handler -> handler.name.toString())
          .distinct()
          .map(
              methodName ->
                  AddArgumentFix.createAddMethodCallFix(
                      eventHandlerSetter, componentQualifiedName, methodName, elementFactory))
          .collect(Collectors.toList());

  final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName);
  if (event != null) {
    fixes.add(
        AddArgumentFix.createNewMethodCallFix(
            eventHandlerSetter, componentQualifiedName, event, parentCls));
  }
  AnnotatorUtils.addError(holder, error, fixes);
  DEBUG_LOGGER.logStep("end " + element);
}