summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/shrob/shrink/KeeperBrush.java
blob: b323f3d7dbf3004fc8e56e52c38881dcca482350 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.shrob.shrink;

import com.android.jack.Jack;
import com.android.jack.JackAbortException;
import com.android.jack.analysis.tracer.AbstractTracerBrush;
import com.android.jack.analysis.tracer.Tracer;
import com.android.jack.ir.ast.JClass;
import com.android.jack.ir.ast.JDefinedClass;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JDefinedInterface;
import com.android.jack.ir.ast.JField;
import com.android.jack.ir.ast.JInterface;
import com.android.jack.ir.ast.JMethod;
import com.android.jack.ir.ast.JNode;
import com.android.jack.ir.ast.JPhantomClass;
import com.android.jack.ir.ast.JPhantomClassOrInterface;
import com.android.jack.ir.ast.JPhantomInterface;
import com.android.jack.ir.formatter.UserFriendlyFormatter;
import com.android.jack.reporting.Reporter.Severity;
import com.android.jack.shrob.seed.SeedMarker;
import com.android.jack.shrob.spec.KeepModifier;
import com.android.sched.item.Description;
import com.android.sched.schedulable.Constraint;
import com.android.sched.schedulable.Transform;
import com.android.sched.util.config.HasKeyId;
import com.android.sched.util.config.ThreadConfig;
import com.android.sched.util.config.id.BooleanPropertyId;

import javax.annotation.Nonnull;

/**
 * A {@link Tracer} configuration that marks all classes and members that will be kept when
 * shrinking.
 */
@Description("Marks all classes and members that will be kept when shrinking.")
@Transform(add = KeepMarker.class)
@Constraint(need = SeedMarker.class)
@HasKeyId
public class KeeperBrush extends AbstractTracerBrush<KeepMarker> {

  private static final class UnknownReferencedTypeException extends Exception {

    private static final long serialVersionUID = 1L;

    @Nonnull
    private final JPhantomClassOrInterface phantomClOrI;

    private UnknownReferencedTypeException(@Nonnull JPhantomClassOrInterface phantomClOrI) {
      this.phantomClOrI = phantomClOrI;
    }

    @Override
    @Nonnull
    public String getMessage() {
      return "Unknown referenced type '"
          + UserFriendlyFormatter.getFormatter().getName(phantomClOrI)
          + "'. Add type into your classpath.";
    }
  }

  @Nonnull
  public static final BooleanPropertyId KEEP_ENCLOSING_METHOD = BooleanPropertyId.create(
  "jack.shrink.keep.enclosing.method",
  "Keep the enclosing method of anonymous classes").addDefaultValue(Boolean.FALSE);

  public KeeperBrush() {
    super(ThreadConfig.get(KeeperBrush.KEEP_ENCLOSING_METHOD).booleanValue(), KeepMarker.class,
        SeedMarker.class);
  }

  @Override
  protected boolean mustTraceOverridingMethod(@Nonnull JMethod method) {
    if (method.getEnclosingType().isExternal()) {
      return true;
    } else {
      return super.mustTraceOverridingMethod(method);
    }
  }

  @Nonnull
  @Override
  protected KeepMarker createMarkerFor(@Nonnull JNode node) {
    return new KeepMarker();
  }

  @Override
  protected boolean isMarked(@Nonnull JNode node) {
    if (node instanceof JDefinedClassOrInterface
        && ((JDefinedClassOrInterface) node).isExternal()) {
      return true;
    } else if (node instanceof JMethod
        && ((JMethod) node).getEnclosingType().isExternal()) {
      return true;
    } else {
      return super.isMarked(node);
    }
  }
  @Override
  public void setMustTraceOverridingMethods(@Nonnull JMethod method) {
    if (!method.getEnclosingType().isExternal()) {
      super.setMustTraceOverridingMethods(method);
    }
  }

  @Override
  protected boolean markIfNecessary(@Nonnull JNode node) {
    if (node instanceof JDefinedClassOrInterface
        && ((JDefinedClassOrInterface) node).isExternal()) {
      return false;
    } else if (node.getParent(JDefinedClassOrInterface.class).isExternal()) {
      return false;
    } else {
      return super.markIfNecessary(node);
    }
  }

  @Override
  public boolean startTraceSeed(@Nonnull JDefinedClassOrInterface type) {
    SeedMarker marker = type.getMarker(SeedMarker.class);
    return marker != null && marker.getModifier() != KeepModifier.ALLOW_SHRINKING;
  }

  @Override
  public boolean startTraceSeed(@Nonnull JMethod method) {
    SeedMarker marker = method.getMarker(SeedMarker.class);
    return marker != null && marker.getModifier() != KeepModifier.ALLOW_SHRINKING;
  }

  @Override
  public boolean startTraceSeed(@Nonnull JField field) {
    SeedMarker marker = field.getMarker(SeedMarker.class);
    return marker != null && marker.getModifier() != KeepModifier.ALLOW_SHRINKING;
  }

  @Override
  public boolean startTrace(@Nonnull JDefinedClassOrInterface type) {
    boolean traceType = markIfNecessary(type);
    if (traceType) {
      try {
        if (type instanceof JDefinedClass) {
          verifyHierarchy((JDefinedClass) type);
        } else {
          assert type instanceof JDefinedInterface;
          verifyImplementedInterfaces(type);
        }
      } catch (UnknownReferencedTypeException e) {
        ShrinkingException reportable = new ShrinkingException(e);
        Jack.getSession().getReporter().report(Severity.FATAL, reportable);
        throw new JackAbortException(reportable);
      }
    }
    return traceType;
  }

  private void verifyHierarchy(@Nonnull JDefinedClass t) throws UnknownReferencedTypeException {
    JClass superClass = t.getSuperClass();
    if (superClass instanceof JPhantomClass) {
      throw new UnknownReferencedTypeException((JPhantomClass) superClass);
    } else if (superClass != null) {
      verifyHierarchy((JDefinedClass) superClass);
    }
    verifyImplementedInterfaces(t);
  }

  private void verifyImplementedInterfaces(@Nonnull JDefinedClassOrInterface t)
      throws UnknownReferencedTypeException {
    for (JInterface jInterface : t.getImplements()) {
      if (jInterface instanceof JPhantomInterface) {
        throw new UnknownReferencedTypeException((JPhantomInterface) jInterface);
      } else {
        assert jInterface instanceof JDefinedInterface;
        verifyImplementedInterfaces((JDefinedInterface) jInterface);
      }
    }
  }
}