Java Code Examples for com.intellij.psi.PsiAnnotationMemberValue#textMatches()

The following examples show how to use com.intellij.psi.PsiAnnotationMemberValue#textMatches() . 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: Decider.java    From dagger-intellij-plugin with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: PsiConsultantImpl.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: PsiConsultantImpl.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
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);
}