aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
blob: e60f241c9a17e3d90b6424668bc46697fcfee14b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.shaking;

import com.android.tools.r8.errors.Unreachable;
import com.android.tools.r8.graph.DexAnnotation;
import com.android.tools.r8.graph.DexAnnotationSet;
import com.android.tools.r8.graph.DexAnnotationSetRefList;
import com.android.tools.r8.graph.DexEncodedField;
import com.android.tools.r8.graph.DexEncodedMethod;
import com.android.tools.r8.graph.DexProgramClass;
import com.android.tools.r8.shaking.Enqueuer.AppInfoWithLiveness;
import com.android.tools.r8.utils.InternalOptions;
import com.android.tools.r8.utils.InternalOptions.AttributeRemovalOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;

public class AnnotationRemover {

  private final AppInfoWithLiveness appInfo;
  private final boolean minificationEnabled;
  private final AttributeRemovalOptions keep;

  public AnnotationRemover(AppInfoWithLiveness appInfo, InternalOptions options) {
    this(appInfo, !options.skipMinification, options.attributeRemoval);
  }

  public AnnotationRemover(AppInfoWithLiveness appInfo, boolean minificationEnabled,
      AttributeRemovalOptions keep) {
    this.appInfo = appInfo;
    this.minificationEnabled = minificationEnabled;
    this.keep = keep;
  }

  /**
   * Used to filter annotations on classes, methods and fields.
   */
  private boolean filterAnnotations(DexAnnotation annotation) {
    switch (annotation.visibility) {
      case DexAnnotation.VISIBILITY_SYSTEM:
        // EnclosingClass and InnerClass are used in Dalvik to represent the InnerClasses
        // attribute section from class files.
        if (keep.innerClasses &&
            (DexAnnotation.isInnerClassesAnnotation(annotation) ||
                DexAnnotation.isEnclosingClassAnnotation(annotation))) {
          return true;
        }
        if (keep.enclosingMethod && DexAnnotation.isEnclosingMethodAnnotation(annotation)) {
          return true;
        }
        if (keep.exceptions && DexAnnotation.isThrowingAnnotation(annotation)) {
          return true;
        }
        if (keep.signature && DexAnnotation.isSignatureAnnotation(annotation)) {
          return true;
        }
        if (keep.sourceDebugExtension && DexAnnotation.isSourceDebugExtension(annotation)) {
          return true;
        }
        return false;
      case DexAnnotation.VISIBILITY_RUNTIME:
        if (!keep.runtimeVisibleAnnotations) {
          return false;
        }
        break;
      case DexAnnotation.VISIBILITY_BUILD:
        if (!keep.runtimeInvisibleAnnotations) {
          return false;
        }
        break;
      default:
        throw new Unreachable("Unexpected annotation visiility.");
    }
    return appInfo.liveTypes.contains(annotation.annotation.type);
  }

  /**
   * Used to filter annotations on parameters.
   */
  private boolean filterParameterAnnotations(DexAnnotation annotation) {
    switch (annotation.visibility) {
      case DexAnnotation.VISIBILITY_SYSTEM:
        return false;
      case DexAnnotation.VISIBILITY_RUNTIME:
        if (!keep.runtimeVisibleParameterAnnotations) {
          return false;
        }
        break;
      case DexAnnotation.VISIBILITY_BUILD:
        if (!keep.runtimeInvisibleParamterAnnotations) {
          return false;
        }
        break;
      default:
        throw new Unreachable("Unexpected annotation visibility.");
    }
    return appInfo.liveTypes.contains(annotation.annotation.type);
  }

  public void run() {
    keep.ensureValid(minificationEnabled);
    for (DexProgramClass clazz : appInfo.classes()) {
      clazz.annotations = stripAnnotations(clazz.annotations, this::filterAnnotations);
      processMethods(clazz.virtualMethods());
      processMethods(clazz.directMethods());
      processFields(clazz.staticFields());
      processFields(clazz.instanceFields());
    }
  }

  private void processMethods(DexEncodedMethod[] methods) {
    for (DexEncodedMethod method : methods) {
      method.annotations = stripAnnotations(method.annotations, this::filterAnnotations);
      method.parameterAnnotations = stripAnnotations(method.parameterAnnotations,
          this::filterParameterAnnotations);
    }
  }

  private void processFields(DexEncodedField[] fields) {
    for (DexEncodedField field : fields) {
      field.annotations = stripAnnotations(field.annotations, this::filterAnnotations);
    }
  }

  private DexAnnotationSetRefList stripAnnotations(DexAnnotationSetRefList annotations,
      Predicate<DexAnnotation> filter) {
    DexAnnotationSet[] filtered = null;
    for (int i = 0; i < annotations.values.length; i++) {
      DexAnnotationSet updated = stripAnnotations(annotations.values[i], filter);
      if (updated != annotations.values[i]) {
        if (filtered == null) {
          filtered = annotations.values.clone();
          filtered[i] = updated;
        }
      }
    }
    if (filtered == null) {
      return annotations;
    } else {
      if (Arrays.stream(filtered).allMatch(DexAnnotationSet::isEmpty)) {
        return DexAnnotationSetRefList.empty();
      }
      return new DexAnnotationSetRefList(filtered);
    }
  }

  private DexAnnotationSet stripAnnotations(DexAnnotationSet annotations,
      Predicate<DexAnnotation> filter) {
    ArrayList<DexAnnotation> filtered = null;
    for (int i = 0; i < annotations.annotations.length; i++) {
      DexAnnotation annotation = annotations.annotations[i];
      if (filter.test(annotation)) {
        if (filtered != null) {
          filtered.add(annotation);
        }
      } else {
        if (filtered == null) {
          filtered = new ArrayList<>(annotations.annotations.length);
          for (int j = 0; j < i; j++) {
            filtered.add(annotations.annotations[j]);
          }
        }
      }
    }
    if (filtered == null) {
      return annotations;
    } else if (filtered.isEmpty()) {
      return DexAnnotationSet.empty();
    } else {
      return new DexAnnotationSet(filtered.toArray(new DexAnnotation[filtered.size()]));
    }
  }
}