本文整理汇总了Java中java.lang.reflect.InvocationHandler类的典型用法代码示例。如果您正苦于以下问题:Java InvocationHandler类的具体用法?Java InvocationHandler怎么用?Java InvocationHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InvocationHandler类属于java.lang.reflect包,在下文中一共展示了InvocationHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invoke
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public Object invoke(Object target, Method method, Object[] params) throws Throwable {
if (EQUALS_METHOD.equals(method)) {
Object param = params[0];
if (param == null || !Proxy.isProxyClass(param.getClass())) {
return false;
}
InvocationHandler other = Proxy.getInvocationHandler(param);
return equals(other);
} else if (HASHCODE_METHOD.equals(method)) {
return hashCode();
}
MethodInvocation invocation = new MethodInvocation(method.getName(), method.getReturnType(), method.getGenericReturnType(), method.getParameterTypes(), target, targetType, sourceObject, params);
invoker.invoke(invocation);
if (!invocation.found()) {
String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()";
throw Exceptions.unsupportedMethod(methodName);
}
return invocation.getResult();
}
示例2: enableFullScreen
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
private static void enableFullScreen(Window window) {
try {
Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities");
Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class);
setWindowCanFullScreen.invoke(fullScreenUtilities, window, true);
Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener");
Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "windowEnteringFullScreen":
windowEnteringFullScreen = true;
break;
case "windowEnteredFullScreen":
windowEnteredFullScreen = true;
break;
}
return null;
}
});
Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener);
addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject);
} catch (Exception e) {
throw new RuntimeException("FullScreen utilities not available", e);
}
}
示例3: defineClass
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public Class<?> defineClass(Class clazz, InvocationHandler invocationHandler) throws ClassNotFoundException {
String clazzName = replaceClassName(clazz) + ".class";
try {
ClassWriter cw = new ClassWriter(0);
//读取 被代理类
InputStream is = clazz.getClassLoader().getResourceAsStream(clazzName);
ClassReader reader = new ClassReader(is);
reader.accept(new AopClassAdapter(ASM5, cw, invocationHandler), ClassReader.SKIP_DEBUG);
byte[] code = cw.toByteArray();
FileOutputStream fos = new FileOutputStream(AopClassLoader.class.getResource("").getPath().toString() + "/Asm_Tmp.class");
fos.write(code);
fos.flush();
fos.close();
return super.defineClass(clazz.getName() + "$simplify", code, 0, code.length);
} catch (Throwable e) {
System.out.println(e);
throw new ClassNotFoundException();
}
}
示例4: connect
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public Connection connect(String url, Properties info) throws SQLException {
return (Connection)Proxy.newProxyInstance(DriverImpl.class.getClassLoader(), new Class[] { ConnectionEx.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method m, Object[] args) {
String methodName = m.getName();
if (methodName.equals("getDriver")) {
return DriverImpl.this;
} else if (methodName.equals("hashCode")) {
Integer i = new Integer(System.identityHashCode(proxy));
return i;
} else if (methodName.equals("equals")) {
return Boolean.valueOf(proxy == args[0]);
}
return null;
}
});
}
示例5: generateProxyType
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
private static Object generateProxyType(final int i) {
class DummyH implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("equals")) {
return proxy == args[0];
}
if (method.getName().equals("toString")) {
return "DummyH[" + i + "]";
}
return null;
}
}
return Proxy.newProxyInstance(
MetaInfCache.class.getClassLoader(),
findTypes(i),
new DummyH()
);
}
示例6: afterPropertiesSet
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// 加载Iface接口
objectClass = classLoader.loadClass(serverAddressProvider.getService() + "$Iface");
// 加载Client.Factory类
Class<TServiceClientFactory<TServiceClient>> fi =
(Class<TServiceClientFactory<TServiceClient>>) classLoader.
loadClass(serverAddressProvider.getService() + "$Client$Factory");
TServiceClientFactory<TServiceClient> clientFactory = fi.newInstance();
ThriftClientPoolFactory clientPool = new ThriftClientPoolFactory(serverAddressProvider,
clientFactory, callback);
pool = new GenericObjectPool<TServiceClient>(clientPool, makePoolConfig());
// InvocationHandler handler = makeProxyHandler();//方式1
InvocationHandler handler = makeProxyHandler2();//方式2
proxyClient = Proxy.newProxyInstance(classLoader, new Class[] { objectClass }, handler);
}
示例7: getObject
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public Object getObject(final String command) throws Exception {
Object templatesImpl = Gadgets.createTemplatesImpl(command);
// inert chain for setup
final Transformer transformerChain = new ChainedTransformer(
new Transformer[]{ new ConstantTransformer(1) });
// real chain for after setup
final Transformer[] transformers = new Transformer[] {
new ConstantTransformer(TrAXFilter.class),
new InstantiateTransformer(
new Class[] { Templates.class },
new Object[] { templatesImpl } )};
final Map innerMap = new HashMap();
final Map lazyMap = LazyMap.decorate(innerMap, transformerChain);
final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class);
final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy);
Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain
return handler;
}
示例8: getInterfaceProxyHelper
import java.lang.reflect.InvocationHandler; //导入依赖的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;
}
示例9: HookHandler
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public HookHandler(IBinder base, Class<?> stubClass,
InvocationHandler InvocationHandler) {
mInvocationHandler = InvocationHandler;
try {
Method asInterface = stubClass.getDeclaredMethod("asInterface", IBinder.class);
this.mBase = asInterface.invoke(null, base);
Class clazz = mBase.getClass();
Field mRemote = clazz.getDeclaredField("mRemote");
mRemote.setAccessible(true);
//新建一个 BinderProxy 的代理对象
Object binderProxy = Proxy.newProxyInstance(mBase.getClass().getClassLoader(),
new Class[] {IBinder.class}, new TransactionWatcherHook((IBinder) mRemote.get(mBase), (IInterface) mBase));
mRemote.set(mBase, binderProxy);
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: create
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
public <T> T create(final Class<T> service) {
if(!service.isInterface()){
throw new IllegalArgumentException("service must be an interface");
}
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
AbstractRemoteHandler handler = handlerFor(method);
if(handler == null){
return null;
}
return handler.invoke(args[0]);
}
});
}
示例11: testProvideInvocationHandlerFactory
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
@Test
public void testProvideInvocationHandlerFactory() throws Exception {
server.enqueue(new MockResponse().setBody("response data"));
String url = "http://localhost:" + server.getPort();
final AtomicInteger callCount = new AtomicInteger();
InvocationHandlerFactory factory = new InvocationHandlerFactory() {
private final InvocationHandlerFactory delegate = new Default();
@Override
public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) {
callCount.incrementAndGet();
return delegate.create(target, dispatch);
}
};
TestInterface api =
Feign.builder().invocationHandlerFactory(factory).target(TestInterface.class, url);
Response response = api.codecPost("request data");
assertEquals("response data", Util.toString(response.body().asReader()));
assertEquals(1, callCount.get());
assertThat(server.takeRequest())
.hasBody("request data");
}
示例12: getService
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
@Override
public IBinder getService(final Context context, ClassLoader classLoader, IBinder binder) {
return new StubBinder(classLoader, binder) {
@Override
public InvocationHandler createHandler(Class<?> interfaceClass, final IInterface base) {
return new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(base, args);
} catch (InvocationTargetException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
};
}
};
}
示例13: genArrays
import java.lang.reflect.InvocationHandler; //导入依赖的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;
}
示例14: newInstanceFromConstructor
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
private void newInstanceFromConstructor(Class<?> proxyClass)
throws Exception
{
// expect newInstance to succeed if it's in the same runtime package
boolean isSamePackage = proxyClass.getName().lastIndexOf('.') == -1;
try {
Constructor cons = proxyClass.getConstructor(InvocationHandler.class);
cons.newInstance(newInvocationHandler());
if (!isSamePackage) {
throw new RuntimeException("ERROR: Constructor.newInstance should not succeed");
}
} catch (IllegalAccessException e) {
if (isSamePackage) {
throw e;
}
}
}
示例15: createInvocationHandler
import java.lang.reflect.InvocationHandler; //导入依赖的package包/类
private InvocationHandler createInvocationHandler(final Object instance) {
return new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(!shouldTrace(method)) {
return method.invoke(instance, args);
}
SpanBuilder span = tracer.createSpan();
try {
return method.invoke(instance, args);
}
catch (InvocationTargetException e) {
final Throwable cause = e.getCause();
span.exception(cause);
throw cause;
}
finally {
span.resource(method.getDeclaringClass().getCanonicalName()).operation(method.getName());
tracer.closeSpan(span);
}
}
};
}