use of java.lang.reflect.InvocationHandler in project MLib by DaoBillTang.
the class Reflect method as.
/**
* 创建一个动态代理根据传入的类型.
* 如果我们正在维护的是一个Map,那么当调用出现异常时我们将从Map中取值.
*
* @param proxyType 需要动态代理的类型
* @return 动态代理生成的对象
*/
@SuppressWarnings("unchecked")
public <P> P as(Class<P> proxyType) {
final boolean isMap = (object instanceof Map);
final InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
try {
return on(object).call(name, args).get();
} catch (ReflectException e) {
if (isMap) {
Map<String, Object> map = (Map<String, Object>) object;
int length = (args == null ? 0 : args.length);
if (length == 0 && name.startsWith("get")) {
return map.get(property(name.substring(3)));
} else if (length == 0 && name.startsWith("is")) {
return map.get(property(name.substring(2)));
} else if (length == 1 && name.startsWith("set")) {
map.put(property(name.substring(3)), args[0]);
return null;
}
}
throw e;
}
}
};
return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[] { proxyType }, handler);
}
use of java.lang.reflect.InvocationHandler in project omegat by omegat-org.
the class OSXIntegration method setPreferencesHandler.
public static void setPreferencesHandler(ActionListener listener) {
try {
// Handler must implement com.apple.eawt.PreferencesHandler interface.
Class<?> preferencesHandlerClass = Class.forName("com.apple.eawt.PreferencesHandler");
InvocationHandler ih = (proxy, method, args) -> {
if (method.getName().equals("handlePreferences")) {
// Respond to
// handlePreferences(com.apple.eawt.AppEvent.PreferencesHandler)
listener.actionPerformed(null);
}
return null;
};
Object handler = Proxy.newProxyInstance(OSXIntegration.class.getClassLoader(), new Class<?>[] { preferencesHandlerClass }, ih);
// Set handler:
// app.setPreferencesHandler(handler);
Method setAboutHandler = getAppClass().getDeclaredMethod("setPreferencesHandler", preferencesHandlerClass);
setAboutHandler.invoke(getApp(), handler);
} catch (Exception ex) {
Log.log(ex);
}
}
use of java.lang.reflect.InvocationHandler in project coprhd-controller by CoprHD.
the class ExceptionMessagesProxy method create.
@SuppressWarnings("unchecked")
public static synchronized <T> T create(final Class<T> interfaze) {
if (!proxyMap.containsKey(interfaze)) {
final ClassLoader loader = currentThread().getContextClassLoader();
final Class<?>[] interfaces = new Class<?>[] { interfaze };
final InvocationHandler handler = new ExceptionMessagesProxy();
final T instance = (T) newProxyInstance(loader, interfaces, handler);
proxyMap.put(interfaze, instance);
}
return (T) proxyMap.get(interfaze);
}
use of java.lang.reflect.InvocationHandler in project jdk8u_jdk by JetBrains.
the class TestUtils method getObjectName.
/**
* Returns the ObjectName of the MBean that a proxy object
* is proxying.
**/
public static ObjectName getObjectName(Object proxy) {
if (!(proxy instanceof Proxy))
throw new IllegalArgumentException("not a " + Proxy.class.getName());
final Proxy p = (Proxy) proxy;
final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
if (handler instanceof MBeanServerInvocationHandler)
return ((MBeanServerInvocationHandler) handler).getObjectName();
throw new IllegalArgumentException("not a JMX Proxy");
}
use of java.lang.reflect.InvocationHandler in project jdk8u_jdk by JetBrains.
the class ActivationID method writeObject.
/**
* <code>writeObject</code> for custom serialization.
*
* <p>This method writes this object's serialized form for
* this class as follows:
*
* <p>The <code>writeObject</code> method is invoked on
* <code>out</code> passing this object's unique identifier
* (a {@link java.rmi.server.UID UID} instance) as the argument.
*
* <p>Next, the {@link
* java.rmi.server.RemoteRef#getRefClass(java.io.ObjectOutput)
* getRefClass} method is invoked on the activator's
* <code>RemoteRef</code> instance to obtain its external ref
* type name. Next, the <code>writeUTF</code> method is
* invoked on <code>out</code> with the value returned by
* <code>getRefClass</code>, and then the
* <code>writeExternal</code> method is invoked on the
* <code>RemoteRef</code> instance passing <code>out</code>
* as the argument.
*
* @serialData The serialized data for this class comprises a
* <code>java.rmi.server.UID</code> (written with
* <code>ObjectOutput.writeObject</code>) followed by the
* external ref type name of the activator's
* <code>RemoteRef</code> instance (a string written with
* <code>ObjectOutput.writeUTF</code>), followed by the
* external form of the <code>RemoteRef</code> instance as
* written by its <code>writeExternal</code> method.
*
* <p>The external ref type name of the
* <code>RemoteRef</Code> instance is
* determined using the definitions of external ref type
* names specified in the {@link java.rmi.server.RemoteObject
* RemoteObject} <code>writeObject</code> method
* <b>serialData</b> specification. Similarly, the data
* written by the <code>writeExternal</code> method and read
* by the <code>readExternal</code> method of
* <code>RemoteRef</code> implementation classes
* corresponding to each of the defined external ref type
* names is specified in the {@link
* java.rmi.server.RemoteObject RemoteObject}
* <code>writeObject</code> method <b>serialData</b>
* specification.
**/
private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException {
out.writeObject(uid);
RemoteRef ref;
if (activator instanceof RemoteObject) {
ref = ((RemoteObject) activator).getRef();
} else if (Proxy.isProxyClass(activator.getClass())) {
InvocationHandler handler = Proxy.getInvocationHandler(activator);
if (!(handler instanceof RemoteObjectInvocationHandler)) {
throw new InvalidObjectException("unexpected invocation handler");
}
ref = ((RemoteObjectInvocationHandler) handler).getRef();
} else {
throw new InvalidObjectException("unexpected activator type");
}
out.writeUTF(ref.getRefClass(out));
ref.writeExternal(out);
}
Aggregations