在代碼:java.lang.reflect.Proxy.getProxyClass(ClassLoader, Class<?>...) 中,能夠發現這一段代碼:java
String proxyPkg = null; // package to define proxy class inspa
/*get
* Record the package of a non-public proxy interface so that thestring
* proxy class will be defined in the same package. Verify thatit
* all non-public proxy interfaces are in the same package.io
*/ast
for (int i = 0; i < interfaces.length; i++) {class
int flags = interfaces[i].getModifiers();next
if (!Modifier.isPublic(flags)) {static
String name = interfaces[i].getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) { // if no non-public proxy interfaces,
proxyPkg = ""; // use the unnamed package
}
{
/*
* Choose a name for the proxy class to generate.
*/
long num;
synchronized (nextUniqueNumberLock) {
num = nextUniqueNumber++;
}
String proxyName = proxyPkg + proxyClassNamePrefix + num;
其中:
private static long nextUniqueNumber = 0;
private static Object nextUniqueNumberLock = new Object();
能夠發現,主要是nextUniqueNumberLock 這個字段是一直在增加的。因此纔會有"$Proxy7"、"java.lang.ClassCastException: $Proxy19"、"java.lang.NoSuchMethodException: $Proxy84"...等等的輸出了。