|
| 1 | +package jadx.core.dex.visitors.gradle; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import jadx.api.plugins.input.data.annotations.AnnotationVisibility; |
| 6 | +import jadx.api.plugins.input.data.annotations.EncodedValue; |
| 7 | +import jadx.api.plugins.input.data.annotations.IAnnotation; |
| 8 | +import jadx.api.plugins.input.data.attributes.JadxAttrType; |
| 9 | +import jadx.api.plugins.input.data.attributes.types.AnnotationsAttr; |
| 10 | +import jadx.core.dex.attributes.nodes.CodeFeaturesAttr; |
| 11 | +import jadx.core.dex.info.ClassInfo; |
| 12 | +import jadx.core.dex.nodes.ClassNode; |
| 13 | +import jadx.core.dex.nodes.FieldNode; |
| 14 | +import jadx.core.dex.nodes.IFieldInfoRef; |
| 15 | +import jadx.core.dex.nodes.IRegion; |
| 16 | +import jadx.core.dex.nodes.MethodNode; |
| 17 | +import jadx.core.dex.nodes.RootNode; |
| 18 | +import jadx.core.dex.regions.SwitchRegion; |
| 19 | +import jadx.core.dex.visitors.AbstractVisitor; |
| 20 | +import jadx.core.dex.visitors.FixSwitchOverEnum; |
| 21 | +import jadx.core.dex.visitors.JadxVisitor; |
| 22 | +import jadx.core.dex.visitors.regions.DepthRegionTraversal; |
| 23 | +import jadx.core.dex.visitors.regions.IRegionIterativeVisitor; |
| 24 | +import jadx.core.export.GradleInfoStorage; |
| 25 | +import jadx.core.utils.android.AndroidResourcesUtils; |
| 26 | +import jadx.core.utils.exceptions.JadxException; |
| 27 | + |
| 28 | +@JadxVisitor( |
| 29 | + name = "NonFinalResIdsVisitor", |
| 30 | + desc = "Detect usage of android resource constants in cases where constant expressions are required.", |
| 31 | + runAfter = FixSwitchOverEnum.class |
| 32 | +) |
| 33 | +public class NonFinalResIdsVisitor extends AbstractVisitor implements IRegionIterativeVisitor { |
| 34 | + |
| 35 | + private boolean nonFinalResIdsFlagRequired = false; |
| 36 | + |
| 37 | + private GradleInfoStorage gradleInfoStorage; |
| 38 | + |
| 39 | + public void init(RootNode root) throws JadxException { |
| 40 | + gradleInfoStorage = root.getGradleInfoStorage(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean visit(ClassNode cls) throws JadxException { |
| 45 | + if (nonFinalResIdsFlagRequired) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + AnnotationsAttr annotationsList = cls.get(JadxAttrType.ANNOTATION_LIST); |
| 49 | + if (visitAnnotationList(annotationsList)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + return super.visit(cls); |
| 53 | + } |
| 54 | + |
| 55 | + private static boolean isCustomResourceClass(ClassInfo cls) { |
| 56 | + ClassInfo parentClass = cls.getParentClass(); |
| 57 | + return parentClass != null && parentClass.getShortName().equals("R") && !parentClass.getFullName().equals("android.R"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void visit(MethodNode mth) throws JadxException { |
| 62 | + AnnotationsAttr annotationsList = mth.get(JadxAttrType.ANNOTATION_LIST); |
| 63 | + if (visitAnnotationList(annotationsList)) { |
| 64 | + nonFinalResIdsFlagRequired = true; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (nonFinalResIdsFlagRequired || !CodeFeaturesAttr.contains(mth, CodeFeaturesAttr.CodeFeature.SWITCH)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + DepthRegionTraversal.traverseIterative(mth, this); |
| 72 | + } |
| 73 | + |
| 74 | + private boolean visitAnnotationList(AnnotationsAttr annotationsList) { |
| 75 | + if (annotationsList != null) { |
| 76 | + for (IAnnotation annotation : annotationsList.getAll()) { |
| 77 | + if (annotation.getVisibility() == AnnotationVisibility.SYSTEM) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + for (Map.Entry<String, EncodedValue> entry : annotation.getValues().entrySet()) { |
| 81 | + Object value = entry.getValue().getValue(); |
| 82 | + if (value instanceof IFieldInfoRef && isCustomResourceClass(((IFieldInfoRef) value).getFieldInfo().getDeclClass())) { |
| 83 | + gradleInfoStorage.setNonFinalResIds(true); |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean visitRegion(MethodNode mth, IRegion region) { |
| 94 | + if (nonFinalResIdsFlagRequired) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + if (region instanceof SwitchRegion) { |
| 98 | + return detectSwitchOverResIds((SwitchRegion) region); |
| 99 | + } |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + private boolean detectSwitchOverResIds(SwitchRegion switchRegion) { |
| 104 | + for (SwitchRegion.CaseInfo caseInfo : switchRegion.getCases()) { |
| 105 | + for (Object key : caseInfo.getKeys()) { |
| 106 | + if (key instanceof FieldNode) { |
| 107 | + ClassNode topParentClass = ((FieldNode) key).getTopParentClass(); |
| 108 | + if (AndroidResourcesUtils.isResourceClass(topParentClass) && !"android.R".equals(topParentClass.getFullName())) { |
| 109 | + this.nonFinalResIdsFlagRequired = true; |
| 110 | + gradleInfoStorage.setNonFinalResIds(true); |
| 111 | + return false; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | +} |
0 commit comments