Look up ClassDefs in ClassPath using a hashmap

This commit is contained in:
Ben Gruver 2013-04-30 21:52:18 -07:00
parent 6893c660a3
commit 6fc32629c2

View File

@ -55,6 +55,7 @@ public class ClassPath {
@Nonnull private final TypeProto unknownClass; @Nonnull private final TypeProto unknownClass;
@Nonnull private DexFile[] dexFiles; @Nonnull private DexFile[] dexFiles;
@Nonnull private HashMap<String, TypeProto> loadedClasses = Maps.newHashMap(); @Nonnull private HashMap<String, TypeProto> loadedClasses = Maps.newHashMap();
@Nonnull private HashMap<String, ClassDef> availableClasses = Maps.newHashMap();
/** /**
* Creates a new ClassPath instance that can load classes from the given dex files * Creates a new ClassPath instance that can load classes from the given dex files
@ -96,6 +97,15 @@ public class ClassPath {
loadPrimitiveType("F"); loadPrimitiveType("F");
loadPrimitiveType("D"); loadPrimitiveType("D");
loadPrimitiveType("L"); loadPrimitiveType("L");
for (DexFile dexFile: dexFiles) {
for (ClassDef classDef: dexFile.getClasses()) {
ClassDef prev = availableClasses.get(classDef.getType());
if (prev == null) {
availableClasses.put(classDef.getType(), classDef);
}
}
}
} }
private void loadPrimitiveType(String type) { private void loadPrimitiveType(String type) {
@ -134,15 +144,11 @@ public class ClassPath {
@Nonnull @Nonnull
public ClassDef getClassDef(String type) { public ClassDef getClassDef(String type) {
// TODO: need a <= O(log) way to look up classes ClassDef ret = availableClasses.get(type);
for (DexFile dexFile: dexFiles) { if (ret == null) {
for (ClassDef classDef: dexFile.getClasses()) { throw new UnresolvedClassException("Could not resolve class %s", type);
if (classDef.getType().equals(type)) {
return classDef;
}
}
} }
throw new UnresolvedClassException("Could not resolve class %s", type); return ret;
} }
@Nonnull @Nonnull