com.intellij.psi.PsiExpressionList Java Examples

The following examples show how to use com.intellij.psi.PsiExpressionList. 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
/** Creates new fix, that adds static method call as an argument to the originalMethodCall. */
static IntentionAction createAddMethodCallFix(
    PsiMethodCallExpression originalMethodCall,
    String clsName,
    String methodName,
    PsiElementFactory elementFactory) {
  PsiExpressionList newArgumentList =
      createArgumentList(originalMethodCall.getContext(), clsName, methodName, elementFactory);
  String fixDescription =
      "Add ." + methodName + "() " + getCapitalizedMethoName(originalMethodCall);
  return new AddArgumentFix(originalMethodCall, newArgumentList, fixDescription);
}
 
Example #2
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 #3
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 #4
Source File: ThinrDetector.java    From thinr with Apache License 2.0 5 votes vote down vote up
@Override
public JavaElementVisitor createPsiVisitor(@NonNull final JavaContext context) {
    return new JavaElementVisitor() {
        @Override
        public void visitLambdaExpression(PsiLambdaExpression expression) {

            if (!(expression.getParent() instanceof PsiExpressionList)) {
                return;
            }

            PsiExpressionList exprList = (PsiExpressionList) expression.getParent();
            if (!(exprList.getParent() instanceof PsiMethodCallExpression)) {
                return;
            }
            PsiMethodCallExpression call = (PsiMethodCallExpression) exprList.getParent();

            if (call.getType() == null) {
                return;
            }

            String callType = call.getType().getCanonicalText();

            if (!callType.startsWith("de.mobilej.thinr.Thinr")) {
                return;
            }

            markLeakSuspects(expression, expression, context);
        }
    };
}
 
Example #5
Source File: CamelDocumentationProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    if (ServiceManager.getService(element.getProject(), CamelService.class).isCamelPresent()) {
        PsiExpressionList exps = PsiTreeUtil.getNextSiblingOfType(originalElement, PsiExpressionList.class);
        if (exps != null) {
            if (exps.getExpressions().length >= 1) {
                // grab first string parameter (as the string would contain the camel endpoint uri
                final PsiClassType stringType = PsiType.getJavaLangString(element.getManager(), element.getResolveScope());
                PsiExpression exp = Arrays.stream(exps.getExpressions()).filter(
                    e -> e.getType() != null && stringType.isAssignableFrom(e.getType()))
                    .findFirst().orElse(null);
                if (exp instanceof PsiLiteralExpression) {
                    Object o = ((PsiLiteralExpression) exp).getValue();
                    String val = o != null ? o.toString() : null;
                    // okay only allow this popup to work when its from a RouteBuilder class
                    PsiClass clazz = PsiTreeUtil.getParentOfType(originalElement, PsiClass.class);
                    if (clazz != null) {
                        PsiClassType[] types = clazz.getExtendsListTypes();
                        boolean found = Arrays.stream(types).anyMatch(p -> p.getClassName().equals("RouteBuilder"));
                        if (found) {
                            String componentName = asComponentName(val);
                            if (componentName != null) {
                                // the quick info cannot be so wide so wrap at 120 chars
                                return generateCamelComponentDocumentation(componentName, val, 120, element.getProject());
                            }
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example #6
Source File: JavaCamelIdeaUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement getPsiElementForCamelBeanMethod(PsiElement element) {
    if (element instanceof PsiLiteral || element.getParent() instanceof PsiLiteralExpression) {
        final PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(element, PsiExpressionList.class);
        if (expressionList != null) {
            final PsiIdentifier identifier = PsiTreeUtil.getChildOfType(expressionList.getPrevSibling(), PsiIdentifier.class);
            if (identifier != null && identifier.getNextSibling() == null && ("method" .equals(identifier.getText()) || "bean" .equals(identifier.getText()))) {
                return expressionList;
            }
        }
    }
    return null;
}
 
Example #7
Source File: ExpressionListTranslator.java    From java2typescript with Apache License 2.0 5 votes vote down vote up
public static void translate(PsiExpressionList element, TranslationContext ctx) {
    PsiExpression[] arguments = element.getExpressions();
    for (int i = 0; i < arguments.length; i++) {
        ExpressionTranslator.translate(arguments[i], ctx);
        if (i != arguments.length - 1) {
            ctx.append(", ");
        }
    }
}
 
Example #8
Source File: AddArgumentFix.java    From litho with Apache License 2.0 4 votes vote down vote up
private AddArgumentFix(
    PsiCall originalCall, PsiExpressionList newArgumentList, String description) {
  this.originalCall = originalCall;
  this.newArgumentList = newArgumentList;
  setText(description);
}
 
Example #9
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);
}
 
Example #10
Source File: LombokEnumConstantBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
@Override
public PsiExpressionList getArgumentList() {
  return null;
}