ensure ClassLoader.getPackage works with all class libraries

There's more work to do to derive all the properties of a given class
from its code source (e.g. JAR file), but this at least ensures that
ClassLoader.getPackage will actually return something non-null when
appropriate.
This commit is contained in:
Joel Dice
2014-03-19 11:21:26 -06:00
parent e9e365d698
commit 8740d76154
10 changed files with 163 additions and 76 deletions

View File

@ -17,9 +17,12 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
public abstract class ClassLoader {
private final ClassLoader parent;
private Map<String, Package> packages;
protected ClassLoader(ClassLoader parent) {
if (parent == null) {
@ -33,6 +36,45 @@ public abstract class ClassLoader {
this(getSystemClassLoader());
}
private Map<String, Package> packages() {
if (packages == null) {
packages = new HashMap();
}
return packages;
}
protected Package getPackage(String name) {
synchronized (this) {
return packages().get(name);
}
}
protected Package[] getPackages() {
synchronized (this) {
return packages().values().toArray(new Package[packages().size()]);
}
}
protected Package definePackage(String name,
String specificationTitle,
String specificationVersion,
String specificationVendor,
String implementationTitle,
String implementationVersion,
String implementationVendor,
URL sealBase)
{
Package p = new Package
(name, implementationTitle, implementationVersion,
implementationVendor, specificationTitle, specificationVersion,
specificationVendor, sealBase, this);
synchronized (this) {
packages().put(name, p);
return p;
}
}
public static ClassLoader getSystemClassLoader() {
return ClassLoader.class.getClassLoader();
}