com.intellij.psi.PsiNameValuePair Java Examples

The following examples show how to use com.intellij.psi.PsiNameValuePair. 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: PsiMethodExtractorUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
private static List<AnnotationSpec> getExternalAnnotations(PsiParameter param) {
  PsiAnnotation[] annotationsOnParam = AnnotationUtil.getAllAnnotations(param, false, null);
  final List<AnnotationSpec> annotations = new ArrayList<>();

  for (PsiAnnotation annotationOnParam : annotationsOnParam) {
    if (annotationOnParam.getQualifiedName().startsWith(COMPONENTS_PACKAGE)) {
      continue;
    }

    final AnnotationSpec.Builder annotationSpec =
        AnnotationSpec.builder(PsiTypeUtils.guessClassName(annotationOnParam.getQualifiedName()));

    PsiNameValuePair[] paramAttributes = annotationOnParam.getParameterList().getAttributes();
    for (PsiNameValuePair attribute : paramAttributes) {
      annotationSpec.addMember(attribute.getName(), attribute.getDetachedValue().getText());
    }

    annotations.add(annotationSpec.build());
  }

  return annotations;
}
 
Example #2
Source File: CamelBeanInjectReferenceContributor.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    PsiJavaElementPattern.Capture<PsiLiteralExpression> pattern = PsiJavaPatterns
            .literalExpression()
            .insideAnnotationParam(CamelIdeaUtils.BEAN_INJECT_ANNOTATION);

    registrar.registerReferenceProvider(pattern, new CamelPsiReferenceProvider() {
        @Override
        protected PsiReference[] getCamelReferencesByElement(PsiElement element, ProcessingContext context) {
            PsiNameValuePair param = PsiTreeUtil.getParentOfType(element, PsiNameValuePair.class);
            if (param != null && param.getAttributeName().equals("value")) {
                String value = param.getLiteralValue();
                if (value != null) {
                    return new PsiReference[] {new BeanReference(element, value)};
                }
            }
            return new PsiReference[0];
        }
    });
}
 
Example #3
Source File: OnXAnnotationHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isOnXParameterAnnotation(HighlightInfo highlightInfo, PsiFile file) {
  final String description = StringUtil.notNullize(highlightInfo.getDescription());
  if (!(ANNOTATION_TYPE_EXPECTED.equals(description)
    || CANNOT_RESOLVE_SYMBOL_UNDERSCORES_MESSAGE.matcher(description).matches()
    || CANNOT_RESOLVE_METHOD_UNDERSCORES_MESSAGE.matcher(description).matches())) {
    return false;
  }

  PsiElement highlightedElement = file.findElementAt(highlightInfo.getStartOffset());

  PsiNameValuePair nameValuePair = findContainingNameValuePair(highlightedElement);
  if (nameValuePair == null || !(nameValuePair.getContext() instanceof PsiAnnotationParameterList)) {
    return false;
  }

  String parameterName = nameValuePair.getName();
  if (null != parameterName && parameterName.contains("_")) {
    parameterName = parameterName.substring(0, parameterName.indexOf('_'));
  }
  if (!ONX_PARAMETERS.contains(parameterName)) {
    return false;
  }

  PsiElement containingAnnotation = nameValuePair.getContext().getContext();
  return containingAnnotation instanceof PsiAnnotation && ONXABLE_ANNOTATIONS.contains(((PsiAnnotation) containingAnnotation).getQualifiedName());
}
 
Example #4
Source File: PsiWorkingRangesMethodExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
private static SpecMethodModel<EventMethod, WorkingRangeDeclarationModel>
    generateWorkingRangeMethod(
        PsiMethod psiMethod,
        List<Class<? extends Annotation>> permittedInterStageInputAnnotations,
        String annotationQualifiedName) {
  final List<MethodParamModel> methodParams =
      getMethodParams(
          psiMethod,
          getPermittedMethodParamAnnotations(permittedInterStageInputAnnotations),
          permittedInterStageInputAnnotations,
          ImmutableList.of());

  PsiAnnotation psiAnnotation = AnnotationUtil.findAnnotation(psiMethod, annotationQualifiedName);
  PsiNameValuePair valuePair = AnnotationUtil.findDeclaredAttribute(psiAnnotation, "name");

  return SpecMethodModel.<EventMethod, WorkingRangeDeclarationModel>builder()
      .annotations(ImmutableList.of())
      .modifiers(PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()))
      .name(psiMethod.getName())
      .returnTypeSpec(PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType()))
      .typeVariables(ImmutableList.copyOf(getTypeVariables(psiMethod)))
      .methodParams(ImmutableList.copyOf(methodParams))
      .representedObject(psiMethod)
      .typeModel(
          new WorkingRangeDeclarationModel(
              valuePair.getLiteralValue(), valuePair.getNameIdentifier()))
      .build();
}
 
Example #5
Source File: OnXAnnotationHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean isOnXParameterValue(HighlightInfo highlightInfo, PsiFile file) {
  if (!CANNOT_FIND_METHOD_VALUE_MESSAGE.equals(highlightInfo.getDescription())) {
    return false;
  }

  PsiElement highlightedElement = file.findElementAt(highlightInfo.getStartOffset());
  PsiNameValuePair nameValuePair = findContainingNameValuePair(highlightedElement);
  if (nameValuePair == null || !(nameValuePair.getContext() instanceof PsiAnnotationParameterList)) {
    return false;
  }

  PsiElement leftSibling = nameValuePair.getContext().getPrevSibling();
  return (leftSibling != null && UNDERSCORES.matcher(StringUtil.notNullize(leftSibling.getText())).matches());
}
 
Example #6
Source File: OnXAnnotationHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static PsiNameValuePair findContainingNameValuePair(PsiElement highlightedElement) {
  PsiElement nameValuePair = highlightedElement;
  while (!(nameValuePair == null || nameValuePair instanceof PsiNameValuePair)) {
    nameValuePair = nameValuePair.getContext();
  }

  return (PsiNameValuePair) nameValuePair;
}
 
Example #7
Source File: EqualsAndHashCodeCallSuperHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean isEqualsAndHashCodeCallSuperDefault(@NotNull PsiElement element) {
  PsiNameValuePair psiNameValuePair = PsiTreeUtil.getParentOfType(element, PsiNameValuePair.class);
  if (psiNameValuePair == null) {
    return false;
  }
  PsiAnnotation psiAnnotation = PsiTreeUtil.getParentOfType(psiNameValuePair, PsiAnnotation.class);
  if (psiAnnotation == null) {
    return false;
  }

  return "callSuper".equals(psiNameValuePair.getName()) && "EqualsAndHashCode".equals(PsiAnnotationSearchUtil.getSimpleNameOf(psiAnnotation));
}
 
Example #8
Source File: PsiQuickFixFactory.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static LocalQuickFix createAddAnnotationQuickFix(@NotNull PsiClass psiClass, @NotNull String annotationFQN, @Nullable String annotationParam) {
  PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
  PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + annotationFQN + "(" + StringUtil.notNullize(annotationParam) + ")", psiClass);
  final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();

  return new AddAnnotationFix(annotationFQN, psiClass, attributes);
}
 
Example #9
Source File: ChangeAnnotationParameterQuickFix.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
  final PsiAnnotation myAnnotation = (PsiAnnotation) startElement;
  final Editor editor = CodeInsightUtil.positionCursor(project, psiFile, myAnnotation);
  if (editor != null) {
    WriteCommandAction.writeCommandAction(project, psiFile).run(() ->
      {
        final PsiNameValuePair valuePair = selectAnnotationAttribute(myAnnotation);

        if (null != valuePair) {
          // delete this parameter
          valuePair.delete();
        }

        if (null != myNewValue) {
          //add new parameter
          final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myAnnotation.getProject());
          PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + myAnnotation.getQualifiedName() + "(" + myName + "=" + myNewValue + ")", myAnnotation.getContext());
          final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();

          myAnnotation.setDeclaredAttributeValue(attributes[0].getName(), attributes[0].getValue());
        }

        UndoUtil.markPsiFileForUndo(psiFile);
      }
    );
  }
}
 
Example #10
Source File: ChangeAnnotationParameterQuickFix.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PsiNameValuePair selectAnnotationAttribute(PsiAnnotation psiAnnotation) {
  PsiNameValuePair result = null;
  PsiNameValuePair[] attributes = psiAnnotation.getParameterList().getAttributes();
  for (PsiNameValuePair attribute : attributes) {
    @NonNls final String attributeName = attribute.getName();
    if (Objects.equals(myName, attributeName) || attributeName == null && myName.equals(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME)) {
      result = attribute;
      break;
    }
  }
  return result;
}
 
Example #11
Source File: PsiTriggerMethodExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
public static ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>>
    getOnTriggerMethods(
        PsiClass psiClass,
        List<Class<? extends Annotation>> permittedInterStageInputAnnotations) {
  final List<SpecMethodModel<EventMethod, EventDeclarationModel>> delegateMethods =
      new ArrayList<>();

  for (PsiMethod psiMethod : psiClass.getMethods()) {
    final OnTrigger onTriggerAnnotation =
        PsiAnnotationProxyUtils.findAnnotationInHierarchy(psiMethod, OnTrigger.class);
    if (onTriggerAnnotation != null) {
      final List<MethodParamModel> methodParams =
          getMethodParams(
              psiMethod,
              TriggerMethodExtractor.getPermittedMethodParamAnnotations(
                  permittedInterStageInputAnnotations),
              permittedInterStageInputAnnotations,
              ImmutableList.<Class<? extends Annotation>>of());

      PsiAnnotation psiOnTriggerAnnotation =
          AnnotationUtil.findAnnotation(psiMethod, OnTrigger.class.getName());
      PsiNameValuePair valuePair =
          AnnotationUtil.findDeclaredAttribute(psiOnTriggerAnnotation, "value");
      PsiClassObjectAccessExpression valueClassExpression =
          (PsiClassObjectAccessExpression) valuePair.getValue();

      // Reuse EventMethodModel and EventDeclarationModel because we are capturing the same info
      TypeSpec returnTypeSpec = PsiTypeUtils.generateTypeSpec(psiMethod.getReturnType());
      final SpecMethodModel<EventMethod, EventDeclarationModel> eventMethod =
          new SpecMethodModel<EventMethod, EventDeclarationModel>(
              ImmutableList.<Annotation>of(),
              PsiModifierExtractor.extractModifiers(psiMethod.getModifierList()),
              psiMethod.getName(),
              returnTypeSpec,
              ImmutableList.copyOf(getTypeVariables(psiMethod)),
              ImmutableList.copyOf(methodParams),
              psiMethod,
              PsiEventDeclarationsExtractor.getEventDeclarationModel(valueClassExpression));
      delegateMethods.add(eventMethod);
    }
  }

  return ImmutableList.copyOf(delegateMethods);
}