Java Code Examples for com.android.resources.ResourceType#fromClassName()

The following examples show how to use com.android.resources.ResourceType#fromClassName() . 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: KotlinResourcePsiElementFinder.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Checks if `expression` matches an expected R.abc.xyz pattern. */
private static boolean isResourceExpression(PsiElement expression) {
  if (!(expression instanceof KtQualifiedExpression)) {
    return false;
  }

  KtQualifiedExpression qualifiedExpression = (KtQualifiedExpression) expression;
  // qualifier should be `R.abc` which is also a `KtQualifiedExpression`
  PsiElement qualifier = qualifiedExpression.getReceiverExpression();
  if (!(qualifier instanceof KtQualifiedExpression)) {
    return false;
  }

  KtQualifiedExpression qualifierExpression = (KtQualifiedExpression) qualifier;
  // rClassExpression should be `R`
  PsiElement rClassExpression = qualifierExpression.getReceiverExpression();
  // rTypeExpression should be `abc`
  PsiElement rTypeExpression = qualifierExpression.getSelectorExpression();

  return rTypeExpression != null
      && SdkConstants.R_CLASS.equals(rClassExpression.getText())
      && ResourceType.fromClassName(rTypeExpression.getText()) != null;
}
 
Example 2
Source File: JavaResourcePsiElementFinder.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Checks if `expression` matches an expected R.abc.xyz pattern. */
private static boolean isResourceExpression(PsiElement expression) {
  if (!(expression instanceof PsiQualifiedReferenceElement)) {
    return false;
  }

  PsiQualifiedReferenceElement qualifiedExpression = (PsiQualifiedReferenceElement) expression;
  // qualifier should be `R.abc` which is also a `PsiReferenceExpression`
  PsiElement qualifier = qualifiedExpression.getQualifier();
  if (!(qualifier instanceof PsiReferenceExpression)) {
    return false;
  }

  PsiReferenceExpression qualifierExpression = (PsiReferenceExpression) qualifier;
  // rClassExpression should be `R`
  PsiElement rClassExpression = qualifierExpression.getQualifier();
  // rTypeExpression should be `abc`
  String rType = qualifierExpression.getReferenceName();

  return rClassExpression != null
      && rType != null
      && SdkConstants.R_CLASS.equals(rClassExpression.getText())
      && ResourceType.fromClassName(rType) != null;
}