summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/jayce/v0002/io/JayceInternalReaderImpl.java
blob: ce098f112b78fb0f5eea9cad9bf91481c32ba2ed (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2013 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.jayce.v0002.io;

import com.android.jack.ir.ast.FieldKind;
import com.android.jack.ir.ast.JMethodCall.DispatchKind;
import com.android.jack.ir.ast.JRetentionPolicy;
import com.android.jack.ir.ast.MethodKind;
import com.android.jack.jayce.DeclaredTypeNode;
import com.android.jack.jayce.JayceFormatException;
import com.android.jack.jayce.JayceInternalReader;
import com.android.jack.jayce.NodeLevel;
import com.android.jack.jayce.v0002.NNode;
import com.android.jack.jayce.v0002.Version;
import com.android.jack.jayce.v0002.nodes.HasCatchBlockIds;
import com.android.jack.jayce.v0002.nodes.HasSourceInfo;
import com.android.jack.jayce.v0002.nodes.NDeclaredType;
import com.android.jack.jayce.v0002.nodes.NMethod;
import com.android.jack.jayce.v0002.nodes.NMethodCall.ReceiverKind;
import com.android.jack.jayce.v0002.nodes.NSourceInfo;
import com.android.jack.jayce.v0002.util.DispatchKindIdHelper;
import com.android.jack.jayce.v0002.util.FieldRefKindIdHelper;
import com.android.jack.jayce.v0002.util.MethodKindIdHelper;
import com.android.jack.jayce.v0002.util.ReceiverKindIdHelper;
import com.android.jack.jayce.v0002.util.RetentionPolicyIdHelper;
import com.android.sched.util.log.Tracer;
import com.android.sched.util.log.TracerFactory;
import com.android.sched.util.log.stats.Percent;
import com.android.sched.util.log.stats.PercentImpl;
import com.android.sched.util.log.stats.StatisticId;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

/**
 * Jayce internal reader implementation.
 */
public class JayceInternalReaderImpl implements JayceInternalReader {
  @Nonnull
  public static final StatisticId<Percent> SKIPED_NDECLARED_TYPE = new StatisticId<Percent>(
      "jayce.ndeclaredtype.skiped", "NDeclaredType loading that skiped by the reader",
      PercentImpl.class, Percent.class);
  @Nonnull
  public static final StatisticId<Percent> SKIPED_BODY = new StatisticId<Percent>(
      "jayce.body.skiped", "Body loading skiped by the reader",
      PercentImpl.class, Percent.class);

  @Nonnull
  private final Tokenizer tokenizer;

  @Nonnull
  private NodeLevel nodeLevel = NodeLevel.FULL;

  @CheckForNull
  private NDeclaredType type;

  @CheckForNull
  private String currentFileName;

  @Nonnegative
  private int currentLine;

  @Nonnull
  private final List<String> currentCatchBlockList = new ArrayList<String>();

  @Nonnull
  private final Tracer tracer = TracerFactory.getTracer();

  public JayceInternalReaderImpl(@Nonnull InputStream in) {
    this.tokenizer = new Tokenizer(in);
  }

  @Nonnull
  public NodeLevel getNodeLevel() {
    return nodeLevel;
  }

  @CheckForNull
  public String readId() throws IOException {
    return readString();
  }

  @CheckForNull
  public String readCurrentFileName() throws IOException {
    if (tokenizer.readOpenFileName()) {
      currentFileName = readString();
      tokenizer.readCloseFileName();
    }
    return currentFileName;
  }

  @Nonnegative
  public int readCurrentLine() throws IOException {
    if (tokenizer.readOpenLineInfo()) {
      currentLine = readInt();
      tokenizer.readCloseLineInfo();
    }
    return currentLine;
  }

  @Nonnull
  public JRetentionPolicy readRetentionPolicyEnum() throws IOException {
    return RetentionPolicyIdHelper.getValue(readByte());
  }

  @Nonnull
  public FieldKind readFieldRefKindEnum() throws IOException {
    return FieldRefKindIdHelper.getValue(readByte());
  }

  @Nonnull
  public MethodKind readMethodKindEnum() throws IOException {
    return MethodKindIdHelper.getValue(readByte());
  }

  @Nonnull
  public ReceiverKind readReceiverKindEnum() throws IOException {
    return ReceiverKindIdHelper.getValue(readByte());
  }

  @Nonnull
  public DispatchKind readDispatchKindEnum() throws IOException {
    return DispatchKindIdHelper.getValue(readByte());
  }

  @CheckForNull
  public String readString() throws IOException {
    return tokenizer.readString();
  }

  @Nonnull
  public List<String> readIds() throws IOException {
    tokenizer.readOpen();
    int length = readInt();
    List<String> ids = new ArrayList<String>(length);
    for (int i = 0; i < length; i++) {
      ids.add(readId());
    }
    tokenizer.readClose();
    return ids;
  }

  public void readCatchBlockIds() throws IOException {
    if (tokenizer.readOpenCatchBlockIdAdd()) {
      int length = tokenizer.readInt();
      for (int i = 0; i < length; i++) {
        String id = readId();
        currentCatchBlockList.add(id);
        assert currentCatchBlockList.indexOf(id) == currentCatchBlockList.lastIndexOf(id);
      }
      tokenizer.readCloseCatchBlockId();
    }
    if (tokenizer.readOpenCatchBlockIdRemove()) {
      int length = tokenizer.readInt();
      for (int i = 0; i < length; i++) {
        String id = readId();
        currentCatchBlockList.remove(id);
        assert !currentCatchBlockList.contains(id);
      }
      tokenizer.readCloseCatchBlockId();
    }
  }

  @SuppressWarnings("unchecked")
  @CheckForNull
  public <T extends NNode> T readNode(@Nonnull Class<T> nodeClass) throws IOException,
      JayceFormatException {
    String fileName = readCurrentFileName();
    int startLine = readCurrentLine();

    readCatchBlockIds();

    Token token = tokenizer.next();

    if (token == Token.NULL) {
      return null;
    }



    tokenizer.readOpen();
    NNode node;
    try {
      node = token.newNode();
    } catch (InvalidTokenException e) {
      throw new ParseException("Unexpected token " + token + " while expecting node.", e);
    }
    Percent statistic = null;
    if (token == Token.METHOD_BODY) {
      statistic = tracer.getStatistic(SKIPED_BODY);
    } else if (node instanceof NDeclaredType) {
      statistic = tracer.getStatistic(SKIPED_NDECLARED_TYPE);
    }

    if (!nodeClass.isAssignableFrom(node.getClass())) {
      throw new JayceFormatException("Unexpected node " + node.getClass().getSimpleName() + ", "
          + nodeClass.getSimpleName() + " was expected.");
    }

    if (nodeLevel != NodeLevel.TYPES && node instanceof HasSourceInfo) {
      NSourceInfo sourceInfo = new NSourceInfo();
      sourceInfo.fileName = fileName;
      sourceInfo.startLine = startLine;
      ((HasSourceInfo) node).setSourceInfos(sourceInfo);
    }
    if (node instanceof HasCatchBlockIds) {
      ((HasCatchBlockIds) node).setCatchBlockIds(new ArrayList<String>(currentCatchBlockList));
    }
    /* readContent can stop in the middle of the node data when nodeLevel is NodeLevel.TYPES
     * meaning we can't read anything after and we have to skip source info.
     */
    node.readContent(this);
    if (nodeLevel != NodeLevel.TYPES) {
      readSourceInfoEnd(node);
      assert !(node instanceof NMethod) || currentCatchBlockList.isEmpty();
      tokenizer.readClose();
    }
    if (nodeLevel.keep(token.getNodeLevel())) {
      if (statistic != null) {
        statistic.addFalse();
      }
      return (T) node;
    } else {
      if (statistic != null) {
        statistic.addTrue();
      }
      return null;
    }
  }

  private void readSourceInfoEnd(@Nonnull NNode node)
      throws IOException {
    if (node instanceof HasSourceInfo) {
      NSourceInfo sourceInfo = ((HasSourceInfo) node).getSourceInfos();
      sourceInfo.endLine = readCurrentLine();
      if (sourceInfo.startLine == 0
              && sourceInfo.endLine == 0
              && !(node instanceof NDeclaredType)) {
        ((HasSourceInfo) node).setSourceInfos(NSourceInfo.UNKNOWN);
      }
    }
  }

  @Nonnull
  public <T extends NNode> List<T> readNodes(@Nonnull Class<T> nodeClass) throws IOException,
      JayceFormatException {
    tokenizer.readOpen();
    int length = readInt();
    List<T> nodes = new ArrayList<T>(length);
    for (int i = 0; i < length; i++) {
      T node = readNode(nodeClass);
      if (node != null) {
        nodes.add(node);
      }
    }
    tokenizer.readClose();
    return nodes;

  }

  public int readInt() throws IOException {
    return tokenizer.readInt();
  }

  public byte readByte() throws IOException {
    return tokenizer.readByte();
  }

  public boolean readBoolean() throws IOException {
    return tokenizer.readBoolean();
  }

  public long readLong() throws IOException {
    return tokenizer.readLong();
  }

  public short readShort() throws IOException {
    return tokenizer.readShort();
  }

  public char readChar() throws IOException {
    return tokenizer.readChar();
  }

  public float readFloat() throws IOException {
    return tokenizer.readFloat();
  }

  public double readDouble() throws IOException {
    return tokenizer.readDouble();
  }

  @Override
  @Nonnull
  public DeclaredTypeNode readType(@Nonnull NodeLevel nodeLevel) throws IOException,
      JayceFormatException {
    if (type == null) {
      this.nodeLevel = nodeLevel;
      type = readNode(NDeclaredType.class);
    }
    assert type != null;
    return type;
  }

  @Override
  public int getCurrentMinor() {
    return Version.CURRENT_MINOR;
  }

  @Override
  public int getMinorMin() {
    return Version.MINOR_MIN;
  }
}