Search in sources :

Example 51 with InvocationHandler

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);
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 52 with InvocationHandler

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);
    }
}
Also used : OConsts(org.omegat.util.OConsts) JRootPane(javax.swing.JRootPane) ActionListener(java.awt.event.ActionListener) Proxy(java.lang.reflect.Proxy) Image(java.awt.Image) IProjectEventListener(org.omegat.core.events.IProjectEventListener) Window(java.awt.Window) Log(org.omegat.util.Log) ProjectUICommands(org.omegat.gui.main.ProjectUICommands) StaticUtils(org.omegat.util.StaticUtils) IApplicationEventListener(org.omegat.core.events.IApplicationEventListener) File(java.io.File) CoreEvents(org.omegat.core.CoreEvents) ArrayList(java.util.ArrayList) List(java.util.List) SwingUtilities(javax.swing.SwingUtilities) Core(org.omegat.core.Core) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) Preferences(org.omegat.util.Preferences) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 53 with InvocationHandler

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);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Example 54 with InvocationHandler

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");
}
Also used : Proxy(java.lang.reflect.Proxy) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 55 with InvocationHandler

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);
}
Also used : RemoteObject(java.rmi.server.RemoteObject) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) RemoteRef(java.rmi.server.RemoteRef) InvalidObjectException(java.io.InvalidObjectException) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7