1 /*
2  * Copyright (C) 2009 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 
17 package dex.reader;
18 
19 import dex.reader.DexFileReader.ClassDefItem;
20 import dex.reader.DexFileReader.FieldIdItem;
21 import dex.reader.DexFileReader.MethodsIdItem;
22 import dex.reader.DexFileReader.ProtIdItem;
23 import dex.structure.DexClass;
24 import dex.structure.DexFile;
25 
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 
30 /* package */final class DexFileImpl implements DexFile {
31 
32     private final String[] stringPool;
33     private final int[] typeIds;
34     private ProtIdItem[] protoIdItems;
35     private FieldIdItem[] fieldIdItems;
36     private MethodsIdItem[] methodIdItems;
37     private ClassDefItem[] classDefItems;
38     private final DexBuffer buffer;
39 
40     private List<DexClass> classes = null;
41 
DexFileImpl(DexBuffer buffer, String[] stringPool, int[] typeIds, ProtIdItem[] protoIds, FieldIdItem[] fieldIdItems, MethodsIdItem[] methodIdItems, ClassDefItem[] classDefItems)42     public DexFileImpl(DexBuffer buffer, String[] stringPool, int[] typeIds,
43             ProtIdItem[] protoIds, FieldIdItem[] fieldIdItems,
44             MethodsIdItem[] methodIdItems, ClassDefItem[] classDefItems) {
45         this.buffer = buffer;
46         this.stringPool = stringPool;
47         this.typeIds = typeIds;
48         this.protoIdItems = protoIds;
49         this.fieldIdItems = fieldIdItems;
50         this.methodIdItems = methodIdItems;
51         this.classDefItems = classDefItems;
52     }
53 
54     /*
55      * (non-Javadoc)
56      *
57      * @see dex.reader.DexFile#getDefinedClasses()
58      */
getDefinedClasses()59     public synchronized List<DexClass> getDefinedClasses() {
60         if (classes == null) {
61             classes = new ArrayList<DexClass>(classDefItems.length);
62             for (int i = 0; i < classDefItems.length; i++) {
63                 classes.add(new DexClassImpl(buffer.createCopy(),
64                         classDefItems[i], stringPool, typeIds, protoIdItems,
65                         fieldIdItems, methodIdItems));
66             }
67         }
68         return classes;
69     }
70 
71     @Override
toString()72     public String toString() {
73         StringBuilder b = new StringBuilder();
74         b.append("StringPool:\n").append(Arrays.toString(stringPool));
75         b.append("\nTypes:\n");
76         for (int i = 0; i < typeIds.length; i++) {
77             b.append(stringPool[typeIds[i]] + "\n");
78         }
79         b.append("\nProtos:\n").append(Arrays.toString(protoIdItems));
80         b.append("\nFields:\n").append(Arrays.toString(fieldIdItems));
81         b.append("\nMethods:\n").append(Arrays.toString(methodIdItems));
82         b.append("\nClasses:\n").append(Arrays.toString(classDefItems));
83         return b.toString();
84     }
85 
getName()86     public String getName() {
87         return "DexFile";
88     }
89 }
90