月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信)
二.Package的类图:a).一个AnnotatedElement,这个接口允许以反射方式读取注释,此接口中的方法返回的所有注释都是不可变和可序列化的
......(假装这个是链接,以后补充)
三.成员变量: // The map of loaded system packages
//加载的系统包图
private static Map pkgs = new HashMap<>(31);
// Maps each directory or zip file name to its corresponding url
//将每个目录或 zip 文件名映射到其对应的 url
private static Map urls = new HashMap<>(10);
// Maps each code source url for a jar file to its manifest
//将 jar 文件的每个代码源 url 映射到其清单
private static Map mans = new HashMap<>(10);
private final String pkgName;
private final String specTitle;
private final String specVersion;
private final String specVendor;
private final String implTitle;
private final String implVersion;
private final String implVendor;
private final URL sealbase;
private transient final ClassLoader loader;
private transient Class> packageInfo;
四.构造方法:
Package(String name,
String spectitle, String specversion, String specvendor,
String impltitle, String implversion, String implvendor,
URL sealbase, ClassLoader loader)
{
pkgName = name;
implTitle = impltitle;
implVersion = implversion;
implVendor = implvendor;
specTitle = spectitle;
specVersion = specversion;
specVendor = specvendor;
sealbase = sealbase;
this.loader = loader;
}
private Package(String name, Manifest man, URL url, ClassLoader loader) {
String path = name.replace('.', '/').concat("/");
String sealed = null;
String specTitle= null;
String specVersion= null;
String specVendor= null;
String implTitle= null;
String implVersion= null;
String implVendor= null;
URL sealbase= null;
Attributes attr = man.getAttributes(path);
if (attr != null) {
specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
sealed = attr.getValue(Name.SEALED);
}
attr = man.getMainAttributes();
if (attr != null) {
if (specTitle == null) {
specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
}
if (specVersion == null) {
specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
}
if (specVendor == null) {
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
}
if (implTitle == null) {
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
}
if (implVersion == null) {
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
}
if (implVendor == null) {
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
}
if (sealed == null) {
sealed = attr.getValue(Name.SEALED);
}
}
if ("true".equalsIgnoreCase(sealed)) {
sealbase = url;
}
pkgName = name;
this.specTitle = specTitle;
this.specVersion = specVersion;
this.specVendor = specVendor;
this.implTitle = implTitle;
this.implVersion = implVersion;
this.implVendor = implVendor;
this.sealbase = sealbase;
this.loader = loader;
}
五.内部方法
获取包相关信息
public String getName() {
return pkgName;
}
public String getSpecificationTitle() {
return specTitle;
}
public String getSpecificationVersion() {
return specVersion;
}
public String getSpecificationVendor() {
return specVendor;
}
public String getImplementationTitle() {
return implTitle;
}
public String getImplementationVersion() {
return implVersion;
}
public String getImplementationVendor() {
return implVendor;
}
isSealed
public boolean isSealed() {
return sealbase != null;
}
public boolean isSealed(URL url) {
return url.equals(sealbase);
}
isCompatibleWith
public boolean isCompatibleWith(String desired)
throws NumberFormatException
{
if (specVersion == null || specVersion.length() < 1) {
throw new NumberFormatException("Empty version string");
}
String [] sa = specVersion.split("\.", -1);
int [] si = new int[sa.length];
for (int i = 0; i < sa.length; i++) {
si[i] = Integer.parseInt(sa[i]);
if (si[i] < 0)
throw NumberFormatException.forInputString("" + si[i]);
}
String [] da = desired.split("\.", -1);
int [] di = new int[da.length];
for (int i = 0; i < da.length; i++) {
di[i] = Integer.parseInt(da[i]);
if (di[i] < 0)
throw NumberFormatException.forInputString("" + di[i]);
}
int len = Math.max(di.length, si.length);
for (int i = 0; i < len; i++) {
int d = (i < di.length ? di[i] : 0);
int s = (i < si.length ? si[i] : 0);
if (s < d)
return false;
if (s > d)
return true;
}
return true;
}
获取包
@CallerSensitive
public static Package getPackage(String name) {
ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass());
if (l != null) {
return l.getPackage(name);
} else {
return getSystemPackage(name);
}
}
@CallerSensitive
public static Package[] getPackages() {
ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass());
if (l != null) {
return l.getPackages();
} else {
return getSystemPackages();
}
}
static Package getSystemPackage(String name) {
synchronized (pkgs) {
Package pkg = pkgs.get(name);
if (pkg == null) {
name = name.replace('.', '/').concat("/");
String fn = getSystemPackage0(name);
if (fn != null) {
pkg = defineSystemPackage(name, fn);
}
}
return pkg;
}
}
static Package[] getSystemPackages() {
// First, update the system package map with new package names
//首先,使用新的包名更新系统包映射
String[] names = getSystemPackages0();
synchronized (pkgs) {
for (int i = 0; i < names.length; i++) {
defineSystemPackage(names[i], getSystemPackage0(names[i]));
}
return pkgs.values().toArray(new Package[pkgs.size()]);
}
}
hashCode
public int hashCode(){
return pkgName.hashCode();
}
toString
public String toString() {
String spec = specTitle;
String ver = specVersion;
if (spec != null && spec.length() > 0)
spec = ", " + spec;
else
spec = "";
if (ver != null && ver.length() > 0)
ver = ", version " + ver;
else
ver = "";
return "package " + pkgName + spec + ver;
}
六.总结
这个类貌似用的非常少....



