Skip to content

Instantly share code, notes, and snippets.

@ufo22940268
Created January 5, 2013 04:03
Show Gist options
  • Save ufo22940268/4459682 to your computer and use it in GitHub Desktop.
Save ufo22940268/4459682 to your computer and use it in GitHub Desktop.
PathClassLoader.java
/**
* Finds a class. This method is called by {@code loadClass()} after the
* parent ClassLoader has failed to find a loaded class of the same name.
*
* @param name
* The "binary name" of the class to search for, in a
* human-readable form like "java.lang.String" or
* "java.net.URLClassLoader$3$1".
* @return the {@link Class} object representing the class
* @throws ClassNotFoundException
* if the class cannot be found
*/
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
//System.out.println("PathClassLoader " + this + ": findClass '" + name + "'");
byte[] data = null;
int length = mPaths.length;
for (int i = 0; i < length; i++) {
//System.out.println("My path is: " + mPaths[i]);
if (mDexs[i] != null) {
Class clazz = mDexs[i].loadClassBinaryName(name, this);
if (clazz != null)
return clazz;
} else if (mZips[i] != null) {
String fileName = name.replace('.', '/') + ".class";
data = loadFromArchive(mZips[i], fileName);
} else {
File pathFile = mFiles[i];
if (pathFile.isDirectory()) {
String fileName =
mPaths[i] + "/" + name.replace('.', '/') + ".class";
data = loadFromDirectory(fileName);
} else {
//System.out.println("PathClassLoader: can't find '"
// + mPaths[i] + "'");
}
}
/* --this doesn't work in current version of Dalvik--
if (data != null) {
System.out.println("--- Found class " + name
+ " in zip[" + i + "] '" + mZips[i].getName() + "'");
int dotIndex = name.lastIndexOf('.');
if (dotIndex != -1) {
String packageName = name.substring(0, dotIndex);
synchronized (this) {
Package packageObj = getPackage(packageName);
if (packageObj == null) {
definePackage(packageName, null, null,
null, null, null, null, null);
}
}
}
return defineClass(name, data, 0, data.length);
}
*/
}
throw new ClassNotFoundException(name + " in loader " + this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment