类com.intellij.psi.PsiNameValuePair源码实例Demo

下面列出了怎么用com.intellij.psi.PsiNameValuePair的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: litho   文件: PsiMethodExtractorUtils.java
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;
}
 
@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];
        }
    });
}
 
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());
}
 
源代码4 项目: litho   文件: PsiWorkingRangesMethodExtractor.java
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();
}
 
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());
}
 
private static PsiNameValuePair findContainingNameValuePair(PsiElement highlightedElement) {
  PsiElement nameValuePair = highlightedElement;
  while (!(nameValuePair == null || nameValuePair instanceof PsiNameValuePair)) {
    nameValuePair = nameValuePair.getContext();
  }

  return (PsiNameValuePair) nameValuePair;
}
 
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));
}
 
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);
}
 
@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);
      }
    );
  }
}
 
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;
}
 
源代码11 项目: litho   文件: PsiTriggerMethodExtractor.java
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);
}
 
 类所在包
 类方法
 同包方法