1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package dexfuzz.rawdex;
18
19import java.io.IOException;
20
21public class AnnotationsDirectoryItem implements RawDexObject {
22  public Offset classAnnotationsOff;
23  public int fieldsSize;
24  public int annotatedMethodsSize;
25  public int annotatedParametersSize;
26  public FieldAnnotation[] fieldAnnotations;
27  public MethodAnnotation[] methodAnnotations;
28  public ParameterAnnotation[] parameterAnnotations;
29
30  @Override
31  public void read(DexRandomAccessFile file) throws IOException {
32    file.alignForwards(4);
33    file.getOffsetTracker().getNewOffsettable(file, this);
34    classAnnotationsOff = file.getOffsetTracker().getNewOffset(file.readUInt());
35    fieldsSize = file.readUInt();
36    annotatedMethodsSize = file.readUInt();
37    annotatedParametersSize = file.readUInt();
38    if (fieldsSize != 0) {
39      fieldAnnotations = new FieldAnnotation[fieldsSize];
40      for (int i = 0; i < fieldsSize; i++) {
41        (fieldAnnotations[i] = new FieldAnnotation()).read(file);
42      }
43    }
44    if (annotatedMethodsSize != 0) {
45      methodAnnotations = new MethodAnnotation[annotatedMethodsSize];
46      for (int i = 0; i < annotatedMethodsSize; i++) {
47        (methodAnnotations[i] = new MethodAnnotation()).read(file);
48      }
49    }
50    if (annotatedParametersSize != 0) {
51      parameterAnnotations = new ParameterAnnotation[annotatedParametersSize];
52      for (int i = 0; i < annotatedParametersSize; i++) {
53        (parameterAnnotations[i] = new ParameterAnnotation()).read(file);
54      }
55    }
56  }
57
58  @Override
59  public void write(DexRandomAccessFile file) throws IOException {
60    file.alignForwards(4);
61    file.getOffsetTracker().updatePositionOfNextOffsettable(file);
62    file.getOffsetTracker().tryToWriteOffset(classAnnotationsOff, file, false /* ULEB128 */);
63    file.writeUInt(fieldsSize);
64    file.writeUInt(annotatedMethodsSize);
65    file.writeUInt(annotatedParametersSize);
66    if (fieldAnnotations != null) {
67      for (FieldAnnotation fieldAnnotation : fieldAnnotations) {
68        fieldAnnotation.write(file);
69      }
70    }
71    if (methodAnnotations != null) {
72      for (MethodAnnotation methodAnnotation : methodAnnotations) {
73        methodAnnotation.write(file);
74      }
75    }
76    if (parameterAnnotations != null) {
77      for (ParameterAnnotation parameterAnnotation : parameterAnnotations) {
78        parameterAnnotation.write(file);
79      }
80    }
81  }
82
83  @Override
84  public void incrementIndex(IndexUpdateKind kind, int insertedIdx) {
85    if (fieldAnnotations != null) {
86      for (FieldAnnotation fieldAnnotation : fieldAnnotations) {
87        fieldAnnotation.incrementIndex(kind, insertedIdx);
88      }
89    }
90    if (methodAnnotations != null) {
91      for (MethodAnnotation methodAnnotation : methodAnnotations) {
92        methodAnnotation.incrementIndex(kind, insertedIdx);
93      }
94    }
95    if (parameterAnnotations != null) {
96      for (ParameterAnnotation parameterAnnotation : parameterAnnotations) {
97        parameterAnnotation.incrementIndex(kind, insertedIdx);
98      }
99    }
100  }
101}
102