com.intellij.psi.PsiAnnotationMemberValue#textMatches ( )源码实例Demo

下面列出了com.intellij.psi.PsiAnnotationMemberValue#textMatches ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dagger-intellij-plugin   文件: Decider.java
@Override public boolean shouldShow(UsageTarget target, Usage usage) {
  PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();
  PsiMethod psimethod = PsiConsultantImpl.findMethod(element);

  PsiAnnotationMemberValue attribValue = PsiConsultantImpl
      .findTypeAttributeOfProvidesAnnotation(psimethod);

  // Is it a @Provides method?
  return psimethod != null
      // Ensure it has an @Provides.
      && PsiConsultantImpl.hasAnnotation(psimethod, CLASS_PROVIDES)
      // Check for Qualifier annotations.
      && PsiConsultantImpl.hasQuailifierAnnotations(psimethod, qualifierAnnotations)
      // Right return type.
      && PsiConsultantImpl.getReturnClassFromMethod(psimethod, false)
      .getName()
      .equals(target.getName())
      // Right type parameters.
      && PsiConsultantImpl.hasTypeParameters(psimethod, typeParameters)
      // @Provides(type=SET)
      && attribValue != null
      && attribValue.textMatches(SET_TYPE);
}
 
/**
 * Return the appropriate return class for a given method element.
 *
 * @param psiMethod the method to get the return class from.
 * @param expandType set this to true if return types annotated with @Provides(type=?)
 * should be expanded to the appropriate collection type.
 * @return the appropriate return class for the provided method element.
 */
public static PsiClass getReturnClassFromMethod(PsiMethod psiMethod, boolean expandType) {
  if (psiMethod.isConstructor()) {
    return psiMethod.getContainingClass();
  }

  PsiClassType returnType = ((PsiClassType) psiMethod.getReturnType());
  if (returnType != null) {
    // Check if has @Provides annotation and specified type
    if (expandType) {
      PsiAnnotationMemberValue attribValue = findTypeAttributeOfProvidesAnnotation(psiMethod);
      if (attribValue != null) {
        if (attribValue.textMatches(SET_TYPE)) {
          String typeName = "java.util.Set<" + returnType.getCanonicalText() + ">";
          returnType =
              ((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject())
                  .createTypeFromText(typeName, psiMethod));
        } else if (attribValue.textMatches(MAP_TYPE)) {
          // TODO(radford): Supporting map will require fetching the key type and also validating
          // the qualifier for the provided key.
          //
          // String typeName = "java.util.Map<String, " + returnType.getCanonicalText() + ">";
          // returnType = ((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject())
          //    .createTypeFromText(typeName, psiMethod));
        }
      }
    }

    return returnType.resolve();
  }
  return null;
}
 
public static List<PsiType> getTypeParameters(PsiElement psiElement) {
  PsiClassType psiClassType = getPsiClassType(psiElement);
  if (psiClassType == null) {
    return new ArrayList<PsiType>();
  }

  // Check if @Provides(type=?) pattern (annotation with specified type).
  PsiAnnotationMemberValue attribValue = findTypeAttributeOfProvidesAnnotation(psiElement);
  if (attribValue != null) {
    if (attribValue.textMatches(SET_TYPE)) {
      // type = SET. Transform the type parameter to the element type.
      ArrayList<PsiType> result = new ArrayList<PsiType>();
      result.add(psiClassType);
      return result;
    } else if (attribValue.textMatches(MAP_TYPE)) {
      // TODO(radford): Need to figure out key type for maps.
      // type = SET or type = MAP. Transform the type parameter to the element type.
      //ArrayList<PsiType> result = new ArrayList<PsiType>();
      //result.add(psiKeyType):
      //result.add(psiClassType);
      //return result;
    }
  }

  if (PsiConsultantImpl.isLazyOrProvider(getClass(psiClassType))) {
    psiClassType = extractFirstTypeParameter(psiClassType);
  }

  Collection<PsiType> typeParameters =
      psiClassType.resolveGenerics().getSubstitutor().getSubstitutionMap().values();
  return new ArrayList<PsiType>(typeParameters);
}