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
|
/*
* Copyright (C) 2012 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.apigenerator.enumfix;
import com.android.apigenerator.ApiClass;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* This codes looks at all the android.jar in an SDK and use ASM to figure out when enums
* where introduced. This is a one time thing that creates the file
* /com/android/apichecker/generator/enums.xml which is then used to create the final API file.
*
*/
public class AndroidJarReader {
// this the last API until we switched to a new API format that included enum values.
private final static int MAX_API = 13;
private static final byte[] BUFFER = new byte[65535];
private final String mSdkFolder;
public AndroidJarReader(String sdkFolder) {
mSdkFolder = sdkFolder;
}
public Map<String, ApiClass> getEnumClasses() {
HashMap<String, ApiClass> map = new HashMap<String, ApiClass>();
// Get all the android.jar. They are in platforms-#
for (int apiLevel = 1 ; apiLevel <= MAX_API ; apiLevel++) {
try {
File jar = new File(mSdkFolder, "platforms/android-" + apiLevel + "/android.jar");
if (jar.exists() == false) {
System.err.println("Missing android.jar for API level " + apiLevel);
continue;
}
FileInputStream fis = new FileInputStream(jar);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
String name = entry.getName();
if (name.endsWith(".class")) {
int index = 0;
do {
int size = zis.read(BUFFER, index, BUFFER.length - index);
if (size >= 0) {
index += size;
} else {
break;
}
} while (true);
byte[] b = new byte[index];
System.arraycopy(BUFFER, 0, b, 0, index);
ClassReader reader = new ClassReader(b);
ClassNode classNode = new ClassNode();
reader.accept(classNode, 0 /*flags*/);
if (classNode != null && classNode.superName != null &&
classNode.superName.equals("java/lang/Enum")) {
ApiClass theClass = addClass(map, classNode.name, apiLevel);
theClass.addSuperClass("java/lang/Enum", apiLevel);
List fields = classNode.fields;
for (Object f : fields) {
FieldNode fnode = (FieldNode) f;
if (fnode.desc.substring(1, fnode.desc.length() - 1).equals(classNode.name)) {
theClass.addField(fnode.name, apiLevel);
}
}
}
}
entry = zis.getNextEntry();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
return map;
}
private ApiClass addClass(HashMap<String, ApiClass> classes, String name, int apiLevel) {
ApiClass theClass = classes.get(name);
if (theClass == null) {
theClass = new ApiClass(name, apiLevel);
classes.put(name, theClass);
}
return theClass;
}
}
|