在代码:java.lang.reflect.Proxy.getProxyClass(ClassLoader, Class<?>...) 中,可以发现这一段代码:
String proxyPkg = null; // package to define proxy class in
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
for (int i = 0; i < interfaces.length; i++) {
int flags = interfaces[i].getModifiers();
if (!Modifier.isPublic(flags)) {
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"...等等的输出了。