proxy

generateProxyClass方法可以查看http://www.docjar.com/html/api/sun/misc/ProxyGenerator.java.html

public static Class<?> getProxyClass(ClassLoader loader,
			Class<?>... interfaces) throws IllegalArgumentException {
		if (interfaces.length > 65535) {
			throw new IllegalArgumentException("interface limit exceeded");
		}

		Class proxyClass = null;

		/* collect interface names to use as key for proxy class cache */
		String[] interfaceNames = new String[interfaces.length];

		Set interfaceSet = new HashSet(); // for detecting duplicates

		for (int i = 0; i < interfaces.length; i++) {
			/*
			 * Verify that the class loader resolves the name of this interface
			 * to the same Class object.
			 */
			String interfaceName = interfaces[i].getName();
			Class interfaceClass = null;
			try {
				interfaceClass = Class.forName(interfaceName, false, loader);
			} catch (ClassNotFoundException e) {
			}
			if (interfaceClass != interfaces[i]) {
				throw new IllegalArgumentException(interfaces[i]
						+ " is not visible from class loader");
			}

			/*
			 * Verify that the Class object actually represents an interface.
			 */
			if (!interfaceClass.isInterface()) {
				throw new IllegalArgumentException(interfaceClass.getName()
						+ " is not an interface");
			}

			/*
			 * Verify that this interface is not a duplicate.
			 */
			if (interfaceSet.contains(interfaceClass)) {
				throw new IllegalArgumentException("repeated interface: "
						+ interfaceClass.getName());
			}
			interfaceSet.add(interfaceClass);

			interfaceNames[i] = interfaceName;
		}

		Object key = Arrays.asList(interfaceNames);

		Map cache;
		synchronized (loaderToCache) {
			cache = (Map) loaderToCache.get(loader);
			if (cache == null) {
				cache = new HashMap();
				loaderToCache.put(loader, cache);
			}

		}

		synchronized (cache) {

			do {
				Object value = cache.get(key);
				if (value instanceof Reference) {
					proxyClass = (Class) ((Reference) value).get();
				}
				if (proxyClass != null) {
					// proxy class already generated: return it
					return proxyClass;
				} else if (value == pendingGenerationMarker) {
					// proxy class being generated: wait for it
					try {
						cache.wait();
					} catch (InterruptedException e) {

					}
					continue;
				} else {

					cache.put(key, pendingGenerationMarker);
					break;
				}
			} while (true);
		}

		try {
			String proxyPkg = null; // package to define proxy class in

			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
			}

			{

				long num;
				synchronized (nextUniqueNumberLock) {
					num = nextUniqueNumber++;
				}
				String proxyName = proxyPkg + proxyClassNamePrefix + num;
				
				byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
						proxyName, interfaces);
				try {
					proxyClass = defineClass0(loader, proxyName,
							proxyClassFile, 0, proxyClassFile.length);
				} catch (ClassFormatError e) {

					throw new IllegalArgumentException(e.toString());
				}
			}
			// add to set of all generated proxy classes, for isProxyClass
			proxyClasses.put(proxyClass, null);

		} finally {

			synchronized (cache) {
				if (proxyClass != null) {
					cache.put(key, new WeakReference(proxyClass));
				} else {
					cache.remove(key);
				}
				cache.notifyAll();
			}
		}
		return proxyClass;
	}

你可能感兴趣的:(proxy)