本文整理汇总了Java中java.lang.reflect.Proxy.getProxyClass方法的典型用法代码示例。如果您正苦于以下问题:Java Proxy.getProxyClass方法的具体用法?Java Proxy.getProxyClass怎么用?Java Proxy.getProxyClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Proxy
的用法示例。
在下文中一共展示了Proxy.getProxyClass方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
public void run() throws Exception {
boolean hasAccess = loader != null || hasAccess();
try {
proxyClass = Proxy.getProxyClass(loader, interfaces);
if (!hasAccess) {
throw new RuntimeException("should have no permission to create proxy class");
}
} catch (AccessControlException e) {
if (hasAccess) {
throw e;
}
if (e.getPermission().getClass() != RuntimePermission.class ||
!e.getPermission().getName().equals("getClassLoader")) {
throw e;
}
return;
}
if (Modifier.isPublic(proxyClass.getModifiers())) {
throw new RuntimeException(proxyClass + " must be non-public");
}
newProxyInstance();
newInstanceFromConstructor(proxyClass);
}
示例2: genArrays
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Generate proxy arrays.
*/
Proxy[][] genArrays(int size, int narrays) throws Exception {
Class proxyClass =
Proxy.getProxyClass(DummyInterface.class.getClassLoader(),
new Class[] { DummyInterface.class });
Constructor proxyCons =
proxyClass.getConstructor(new Class[] { InvocationHandler.class });
Object[] consArgs = new Object[] { new DummyHandler() };
Proxy[][] arrays = new Proxy[narrays][size];
for (int i = 0; i < narrays; i++) {
for (int j = 0; j < size; j++) {
arrays[i][j] = (Proxy) proxyCons.newInstance(consArgs);
}
}
return arrays;
}
示例3: getInterfaceProxyHelper
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
@Override
protected Object getInterfaceProxyHelper(ContextFactory cf,
Class<?>[] interfaces)
{
// XXX: How to handle interfaces array withclasses from different
// class loaders? Using cf.getApplicationClassLoader() ?
ClassLoader loader = interfaces[0].getClassLoader();
Class<?> cl = Proxy.getProxyClass(loader, interfaces);
Constructor<?> c;
try {
c = cl.getConstructor(new Class[] { InvocationHandler.class });
} catch (NoSuchMethodException ex) {
// Should not happen
throw Kit.initCause(new IllegalStateException(), ex);
}
return c;
}
示例4: verifyProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
void verifyProxyClass() throws Exception {
Class<?> c = Proxy.getProxyClass(loader, interfaces);
Module m = c.getModule();
if (target != null && m != target) {
throw new RuntimeException(c.getModule() + " not expected: " + target);
}
// expect dynamic module
if (target == null && (!m.isNamed() || !m.getName().startsWith("jdk.proxy"))) {
throw new RuntimeException("Unexpected:" + m);
}
Module module = c.getModule();
try {
Constructor<?> cons = c.getConstructor(InvocationHandler.class);
cons.newInstance(ih);
if (module.isNamed()) {
throw new RuntimeException("expected IAE not thrown");
}
} catch (IllegalAccessException e) {
if (!module.isNamed()) {
throw e;
}
}
}
示例5: resolveProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Return a proxy class that implements the interfaces named in a proxy
* class descriptor. Do this using the class loader assigned to this
* Context.
*/
@Override
protected Class<?> resolveProxyClass(String[] interfaces)
throws IOException, ClassNotFoundException {
Class<?>[] cinterfaces = new Class[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
cinterfaces[i] = classLoader.loadClass(interfaces[i]);
}
try {
return Proxy.getProxyClass(classLoader, cinterfaces);
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(null, e);
}
}
示例6: resolveProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Resolve a proxy class that implements the given interfaces. First
* attempts to use the default resolution method provided by
* ObjectInputStream. If that fails with a ClassNotFoundException then
* tries to resolve the interfaces and create the proxy class using
* the GATE classloader instead.
*/
@Override
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException,
ClassNotFoundException {
try {
return super.resolveProxyClass(interfaces);
}
catch(ClassNotFoundException cnfe) {
// failed to load with the normal method, try the GATE ClassLoader
// instead
Class<?>[] interfaceClasses = new Class<?>[interfaces.length];
for(int i = 0; i < interfaces.length; i++) {
interfaceClasses[i] = Class.forName(interfaces[i], false, Gate
.getClassLoader());
}
return Proxy.getProxyClass(Gate.getClassLoader(), interfaceClasses);
}
}
示例7: resolveProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
protected Class<?> resolveProxyClass(String[] interfaces) throws
IOException, ClassNotFoundException {
ClassLoader nonPublicLoader = null;
boolean hasNonPublicInterface = false;
// define proxy in class loader of non-public interface(s), if any
Class<?>[] classObjs = new Class<?>[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
Class<?> cl = Class.forName(interfaces[i], false, classLoader);
if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
if (hasNonPublicInterface) {
if (nonPublicLoader != cl.getClassLoader()) {
throw new IllegalAccessError(
"conflicting non-public interface class loaders");
}
} else {
nonPublicLoader = cl.getClassLoader();
hasNonPublicInterface = true;
}
}
classObjs[i] = cl;
}
try {
@SuppressWarnings("deprecation")
Class<?> proxyClass = Proxy.getProxyClass(hasNonPublicInterface ?
nonPublicLoader : classLoader, classObjs);
return proxyClass;
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(null, e);
}
}
示例8: ProxyListener
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/** @param listener listener to delegate to
*/
ProxyListener(Class<?> c, Class<?> api, EventListener listener) {
super(api, listener);
try {
Reference<Constructor<?>> ref = constructors.get(c);
Constructor<?> proxyConstructor = ref == null ? null : ref.get();
if (proxyConstructor == null) {
Class<?> proxyClass = Proxy.getProxyClass(c.getClassLoader(), c);
proxyConstructor = proxyClass.getConstructor(InvocationHandler.class);
proxyConstructor.setAccessible(true);
constructors.put(c, new SoftReference<Constructor<?>>(proxyConstructor));
}
Object p;
try {
p = proxyConstructor.newInstance(this);
} catch (NoClassDefFoundError err) {
// if for some reason the actual creation of the instance
// from constructor fails, try it once more using regular
// method, see issue 30449
p = Proxy.newProxyInstance(c.getClassLoader(), new Class<?>[] { c }, this);
}
proxy = p;
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
示例9: getResultSetConstructor
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
protected Constructor<?> getResultSetConstructor() throws NoSuchMethodException {
if (resultSetConstructor == null) {
Class<?> proxyClass = Proxy.getProxyClass(StatementDecoratorInterceptor.class.getClassLoader(),
new Class[] { ResultSet.class });
resultSetConstructor = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
}
return resultSetConstructor;
}
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:9,代码来源:StatementDecoratorInterceptor.java
示例10: resolveProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Return a proxy class that implements the interfaces named in a proxy
* class descriptor. Do this using the class loader assigned to this
* Context.
*/
protected Class resolveProxyClass(String[] interfaces)
throws IOException, ClassNotFoundException {
Class[] cinterfaces = new Class[interfaces.length];
for (int i = 0; i < interfaces.length; i++)
cinterfaces[i] = classLoader.loadClass(interfaces[i]);
try {
return Proxy.getProxyClass(classLoader, cinterfaces);
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(null, e);
}
}
示例11: wrapDataSource
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
protected Object wrapDataSource(Object datasource, String username, String password) throws NamingException {
try {
Class<?> proxyClass = Proxy.getProxyClass(datasource.getClass().getClassLoader(),
datasource.getClass().getInterfaces());
Constructor<?> proxyConstructor = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
DataSourceHandler handler = new DataSourceHandler((DataSource) datasource, username, password);
return proxyConstructor.newInstance(handler);
} catch (Exception x) {
if (x instanceof InvocationTargetException) {
Throwable cause = x.getCause();
if (cause instanceof ThreadDeath) {
throw (ThreadDeath) cause;
}
if (cause instanceof VirtualMachineError) {
throw (VirtualMachineError) cause;
}
if (cause instanceof Exception) {
x = (Exception) cause;
}
}
if (x instanceof NamingException)
throw (NamingException) x;
else {
NamingException nx = new NamingException(x.getMessage());
nx.initCause(x);
throw nx;
}
}
}
示例12: wrapDataSource
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
protected Object wrapDataSource(Object datasource, String username, String password) throws NamingException {
try {
Class<?> proxyClass = Proxy.getProxyClass(datasource.getClass().getClassLoader(), datasource.getClass().getInterfaces());
Constructor<?> proxyConstructor = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
DataSourceHandler handler = new DataSourceHandler((DataSource)datasource, username, password);
return proxyConstructor.newInstance(handler);
}catch (Exception x) {
if (x instanceof InvocationTargetException) {
Throwable cause = x.getCause();
if (cause instanceof ThreadDeath) {
throw (ThreadDeath) cause;
}
if (cause instanceof VirtualMachineError) {
throw (VirtualMachineError) cause;
}
if (cause instanceof Exception) {
x = (Exception) cause;
}
}
if (x instanceof NamingException) throw (NamingException)x;
else {
NamingException nx = new NamingException(x.getMessage());
nx.initCause(x);
throw nx;
}
}
}
示例13: getConstructor
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Creates a constructor for a proxy class, if one doesn't already exist
* @param idx - the index of the constructor
* @param clazz - the interface that the proxy will implement
* @return - returns a constructor used to create new instances
* @throws NoSuchMethodException
*/
protected Constructor<?> getConstructor(int idx, Class<?> clazz) throws NoSuchMethodException {
if (constructors[idx]==null) {
Class<?> proxyClass = Proxy.getProxyClass(SlowQueryReport.class.getClassLoader(), new Class[] {clazz});
constructors[idx] = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
}
return constructors[idx];
}
示例14: loadProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Define a proxy class in the given class loader. The proxy
* class will implement the given interfaces Classes.
*/
private static Class<?> loadProxyClass(ClassLoader loader, Class<?>[] interfaces)
throws ClassNotFoundException
{
try {
return Proxy.getProxyClass(loader, interfaces);
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(
"error creating dynamic proxy class", e);
}
}
示例15: createProxyConstructor
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
private static <T> Constructor<T> createProxyConstructor(@Nullable LoadingCache<CacheKey, ClassLoader> cache, Class<T> interfaceClass,
Class<?>... otherInterfaceClasses) {
Class<?>[] interfaceClasses = new Class[1 + otherInterfaceClasses.length];
interfaceClasses[0] = interfaceClass;
ClassLoader classLoader;
if (otherInterfaceClasses.length == 0) {
classLoader = interfaceClass.getClassLoader();
} else {
System.arraycopy(otherInterfaceClasses, 0, interfaceClasses, 1, otherInterfaceClasses.length);
List<ClassLoader> classLoaders = extractClassloaderList(interfaceClasses);
if (classLoaders.size() == 1) {
classLoader = classLoaders.get(0);
} else if (cache != null) {
classLoader = cache.getUnchecked(new CacheKey(classLoaders));
} else {
classLoader = createClassLoader(classLoaders);
}
}
Class<?> uncheckedProxyClass = Proxy.getProxyClass(classLoader, interfaceClasses);
Class<T> proxyClass = interfaceClass.getClass().cast(uncheckedProxyClass);
try {
return proxyClass.getConstructor(InvocationHandler.class);
} catch (NoSuchMethodException e) {
throw new StoreException(e);
}
}