`

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;
	}
分享到:
评论

相关推荐

    foxyproxy_standard

    FoxyProxy 是一款高级代理服务器管理工具,是 Firefox 火狐浏览器的代理插件,相比比 SwitchProxy、ProxyButton、QuickProxy、xyzproxy、ProxyTex 等扩展提供更多的功能。 FoxyProxy 通过使用通配符、正则表达式和...

    Proxy SwitchOmega 2.5.15 Chrome离线插件(谷歌浏览器proxy插件)

    Google Chrome插件: Proxy SwitchOmega 2.5.15. 轻松快捷地管理和切换多个代理设置. 离线插件使用方法: 1. 打开Chrome -&gt; 自定义及控制按钮(右上角) -&gt; 更多工具 -&gt; 扩展程序 (有可能需要打开开发者模式) 2. 拖拽...

    ftpproxy-1.2.3.rar_Linux c proxy_ftpproxy_ftp代理_linux ftp_proxy-

    proxy源代码,linux下的ftp 代理的源代码,大家多多支持啊

    Nginx之proxy_redirect使用详解

    所以在这里用到了nginx的proxy_redirect指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值 以下是截取nginx的一小段配置文档 server { listen 80; server_name www.boke.com; l

    apache Proxy_Error

    apache Proxy Error apache Proxy Error apache Proxy Error apache Proxy Error

    ABAP 调用ABAP PROXY

    ABAP 调用ABAP PROXY

    火狐浏览器代理插件foxyproxy与SwitchyOmega.rar

    1、FoxyProxy是一个高级的代理管理工具,它完全替代了Firefox有限的代理功能。它提供比SwitchProxy、ProxyButton、 QuickProxy、xyzproxy、ProxyTex、TorButton等等更多的功能。 2、装完Firefox的Foxyproxy插件后,...

    proxy lab参考答案

    2014最新版proxy lab参考答案,小伙伴快来吧!

    Github Python ProxyPool(代理池)

    简易高效的代理池,提供如下功能: 1. 定时抓取免费代理网站,简易可扩展 2. 使用 Redis 对代理进行存储...Github 链接:https://github.com/Python3WebSpider/ProxyPool 详情阅读:README.md,使用方法及相关信息讲解

    MySQL Proxy 实现负载均衡测试

    MySQL Proxy 实现负载均衡测试 MySQL Proxy 实现负载均衡测试

    Elite Proxy Switcher Pro v1.27

    它是很棒的Proxy(代理服务)验证、切换工具,如需下载Proxy List需要购买订阅服务,或者从另一个工具Proxy Switcher PRO导出代理列表。这个注册版仅供学习、研究使用,如需商用或者个人有充足预算,请到官网购买...

    Elite Proxy Switcher Pro v1.24

    Elite Proxy Switcher Pro v1.24 是目前从国外论坛找到的零售注册版,官网的新版本只是免费版(只有会员购买后才可以下载专业版),免费版无法激活。它是很棒的Proxy(代理服务)验证、切换工具,如需下载Proxy List...

    tftp_proxy源码

    Tftp ProxyServer 代理服务器 源码 *=========================================================================== * * Project: tftp_proxy, a proxy for TFTP transfers through firewalls * File: tftp_...

    sharding-proxy实现分表

    使用mysql5.7+sharding-proxy实现分表,策略为每半年时间分一次表

    goproxy-android snail007/goproxy全能代理服务器安卓版

    goproxy-android snail007/goproxy全能代理服务器安卓版。项目使用androidstudio开发,基于snail007/goproxy官方安卓SDK开发

    proxy.jsp、proxy.ashx、proxy.php、proxy.config

    esri.config.defaults.io.proxyUrl is not set所缺文件 proxy.jsp、proxy.ashx、proxy.php、proxy.config

    kube-proxy-temp-1.16.8.yaml

    aws-kube-proxy1.16.8版本示例文件

    netty-handler-proxy-4.1.68.Final-API文档-中文版.zip

    赠送jar包:netty-handler-proxy-4.1.68.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.68.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-...

    ICS lab10 WebProxy

    ICS lab10 WebProxy 包含 proxy.c

Global site tag (gtag.js) - Google Analytics