use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceNameConverter method getErrorMessage.
@Override
public String getErrorMessage(@Nullable String s, ConvertContext context) {
ResourceType type = null;
XmlTag tag = context.getTag();
if (tag != null && SdkConstants.TAG_ATTR.equals(tag.getName())) {
// The resource name validator needs to know if we're dealing with
// an <attr>
type = ResourceType.ATTR;
}
return s == null ? null : ValueResourceNameValidator.getErrorText(s, type);
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class ViewLoader method parseClass.
private static boolean parseClass(@NotNull Class<?> rClass, @NotNull TIntObjectHashMap<Pair<ResourceType, String>> id2res, @NotNull Map<IntArrayWrapper, String> styleableId2Res, @NotNull Map<ResourceType, TObjectIntHashMap<String>> res2id) throws ClassNotFoundException {
try {
final Class<?>[] nestedClasses;
try {
nestedClasses = rClass.getDeclaredClasses();
} catch (LinkageError e) {
final Throwable cause = e.getCause();
LOG.debug(e);
if (cause instanceof ClassNotFoundException) {
throw (ClassNotFoundException) cause;
}
throw e;
}
for (Class<?> resClass : nestedClasses) {
final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
if (resType == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" '%s' is not a valid resource type", anonymizeClassName(resClass.getSimpleName())));
}
continue;
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" Defining resource type '%s'", anonymizeClassName(resClass.getSimpleName())));
}
final TObjectIntHashMap<String> resName2Id = new TObjectIntHashMap<>();
res2id.put(resType, resName2Id);
for (Field field : resClass.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
// May not be final in library projects
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" '%s' field is not static, skipping", field.getName()));
}
continue;
}
final Class<?> type = field.getType();
if (type.isArray() && type.getComponentType() == int.class) {
styleableId2Res.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" '%s' defined as int[]", field.getName()));
}
} else if (type == int.class) {
final Integer value = (Integer) field.get(null);
id2res.put(value, Pair.of(resType, field.getName()));
resName2Id.put(field.getName(), value);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" '%s' defined as int", field.getName()));
}
} else {
LOG.error("Unknown field type in R class: " + type);
}
}
}
} catch (IllegalAccessException e) {
LOG.info(e);
return false;
}
return true;
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class AndroidPsiAugmentProvider method getAugments.
@SuppressWarnings("unchecked")
@NotNull
@Override
public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
if ((type != PsiClass.class && type != PsiField.class) || !(element instanceof PsiExtensibleClass)) {
return Collections.emptyList();
}
final PsiExtensibleClass aClass = (PsiExtensibleClass) element;
final String className = aClass.getName();
final boolean rClassAugment = AndroidUtils.R_CLASS_NAME.equals(className) && type == PsiClass.class;
if (DumbService.isDumb(element.getProject())) {
if (rClassAugment) {
LOG.debug("R_CLASS_AUGMENT: empty because of dumb mode");
}
return Collections.emptyList();
}
final AndroidFacet facet = AndroidFacet.getInstance(element);
if (facet == null) {
if (rClassAugment) {
LOG.debug("R_CLASS_AUGMENT: empty because no facet");
}
return Collections.emptyList();
}
final PsiFile containingFile = element.getContainingFile();
if (containingFile == null) {
if (rClassAugment) {
LOG.debug("R_CLASS_AUGMENT: empty because of no containing file");
}
return Collections.emptyList();
}
if (type == PsiClass.class) {
if (AndroidUtils.R_CLASS_NAME.equals(className) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
final Set<String> existingInnerClasses = getOwnInnerClasses(aClass);
final Set<ResourceType> types = ResourceReferenceConverter.getResourceTypesInCurrentModule(facet);
final List<Psi> result = new ArrayList<Psi>();
for (ResourceType resType : types) {
if (!existingInnerClasses.contains(resType.getName())) {
final AndroidLightClass resClass = new ResourceTypeClass(facet, resType.getName(), aClass);
result.add((Psi) resClass);
}
}
if (rClassAugment) {
LOG.debug("R_CLASS_AUGMENT: " + result.size() + " classes added");
}
return result;
} else if (AndroidUtils.MANIFEST_CLASS_NAME.equals(className) && AndroidResourceUtil.isManifestJavaFile(facet, containingFile)) {
return Arrays.asList((Psi) new PermissionClass(facet, aClass), (Psi) new PermissionGroupClass(facet, aClass));
}
if (rClassAugment) {
LOG.debug("R_CLASS_AUGMENT: empty because containing file is not actual R.java file");
}
} else if (type == PsiField.class && !(aClass instanceof AndroidLightClass)) {
// extend existing inner classes, not provided by this augment (ex. they can be generated by maven)
final PsiClass parentClass = aClass.getContainingClass();
if (parentClass != null && AndroidUtils.R_CLASS_NAME.equals(parentClass.getName()) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
final String resClassName = aClass.getName();
if (resClassName != null && ResourceType.getEnum(resClassName) != null) {
final Set<String> existingFields = getOwnFields(aClass);
final PsiField[] newFields = ResourceTypeClass.buildLocalResourceFields(facet, resClassName, aClass);
final List<Psi> result = new ArrayList<Psi>();
for (PsiField field : newFields) {
if (!existingFields.contains(field.getName())) {
result.add((Psi) field);
}
}
return result;
}
}
}
return Collections.emptyList();
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class AndroidColorAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
String tagName = tag.getName();
if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
String value = tag.getValue().getText().trim();
annotateXml(element, holder, value);
}
} else if (TAG_ITEM.equals(tagName)) {
XmlTagValue value = tag.getValue();
String text = value.getText();
annotateXml(element, holder, text);
}
} else if (element instanceof XmlAttributeValue) {
XmlAttributeValue v = (XmlAttributeValue) element;
String value = v.getValue();
if (value == null || value.isEmpty()) {
return;
}
annotateXml(element, holder, value);
} else if (element instanceof PsiReferenceExpression) {
ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
if (referenceType != ResourceReferenceType.NONE) {
// (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
// same expression, which would result in both elements getting annotated and the icon showing up
// in the gutter twice. Instead we only count the outer one.
ResourceType type = AndroidPsiUtils.getResourceType(element);
if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
String name = AndroidPsiUtils.getResourceName(element);
annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
}
}
}
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class AndroidJavaCompletionContributor method filterPrivateResources.
public void filterPrivateResources(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet, AndroidFacet facet) {
final ResourceVisibilityLookup lookup = AppResourceRepository.getAppResources(facet, true).getResourceVisibility(facet);
if (lookup.isEmpty()) {
return;
}
resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {
@Override
public void consume(CompletionResult result) {
final Object obj = result.getLookupElement().getObject();
if (obj instanceof PsiField) {
PsiField field = (PsiField) obj;
PsiClass containingClass = field.getContainingClass();
if (containingClass != null) {
PsiClass rClass = containingClass.getContainingClass();
if (rClass != null && rClass.getName().equals(R_CLASS)) {
ResourceType type = ResourceType.getEnum(containingClass.getName());
if (type != null && lookup.isPrivate(type, field.getName())) {
return;
}
}
}
}
resultSet.passResult(result);
}
});
}
Aggregations