Example usage for java.lang.reflect Proxy getInvocationHandler

List of usage examples for java.lang.reflect Proxy getInvocationHandler

Introduction

In this page you can find the example usage for java.lang.reflect Proxy getInvocationHandler.

Prototype

@CallerSensitive
public static InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException 

Source Link

Document

Returns the invocation handler for the specified proxy instance.

Usage

From source file:org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.AnnotationProcessor.java

private ApiScope getScope(Annotation currentMethod) throws Throwable {
    InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod);
    Annotation[] extensions = (Annotation[]) methodHandler.invoke(currentMethod,
            apiOperation.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null);
    if (extensions != null) {
        methodHandler = Proxy.getInvocationHandler(extensions[0]);
        Annotation[] properties = (Annotation[]) methodHandler.invoke(extensions[0],
                extensionClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES, null), null);
        String scopeKey;/*from  ww  w .  java  2 s. c  o  m*/
        String propertyName;
        for (Annotation property : properties) {
            methodHandler = Proxy.getInvocationHandler(property);
            propertyName = (String) methodHandler.invoke(property,
                    extensionPropertyClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null);
            if (ANNOTATIONS_SCOPE.equals(propertyName)) {
                scopeKey = (String) methodHandler.invoke(property,
                        extensionPropertyClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null), null);
                if (scopeKey.isEmpty()) {
                    return null;
                }
                return apiScopes.get(scopeKey);
            }
        }
    }
    return null;
}

From source file:org.diorite.config.impl.proxy.ConfigInvocationHandler.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private Config cloneImpl() {
    Config copy = ProxyImplementationProvider.getInstance().createImplementation(this.template.getConfigType(),
            (ConfigTemplate) this.template);
    ConfigInvocationHandler invocationHandler = (ConfigInvocationHandler) Proxy.getInvocationHandler(copy);

    invocationHandler.charsetDecoder = this.charsetDecoder;
    invocationHandler.charsetEncoder = this.charsetEncoder;
    invocationHandler.bindFile = this.bindFile;

    StringBuilderWriter writer = new StringBuilderWriter((this.simpleDynamicValues.size() * 100)
            + ((this.dynamicValues.size() + this.predefinedValues.size()) * 1000));
    this.saveImpl(writer);
    invocationHandler.loadImpl(new StringReader(writer.toString()));

    //        for (Entry<String, ConfigPropertyValueImpl<Object>> valueEntry : this.predefinedValues.entrySet())
    //        {/*  www  . j  a v  a  2s. c o  m*/
    //            String key = valueEntry.getKey();
    //            ConfigPropertyValueImpl<Object> value = valueEntry.getValue();
    //            ConfigPropertyValueImpl<Object> copyValue = invocationHandler.getOrCreatePredefinedValue(value.getProperty());
    //            copyValue.setRawValue(value.getRawValue());
    //        }
    //
    //        for (Entry<String, ConfigNode> entry : this.dynamicValues.entrySet())
    //        {
    //            invocationHandler.dynamicValues.put(entry.getKey(), (ConfigNode) entry.getValue().clone());
    //        }
    //
    //        for (Entry<String, Node> entry : this.simpleDynamicValues.entrySet())
    //        {
    //            // should be safe to use this same node instance, as it will be deserialized and get/set anyway.
    //            invocationHandler.simpleDynamicValues.put(entry.getKey(), entry.getValue());
    //        }

    return copy;
}

From source file:io.coala.json.DynaBean.java

/**
 * @param annot the {@link BeanProxy} instance for the type of wrapper of
 *            {@link DynaBean}s containing the {@link Comparable} value type
 *            in the annotated property key
 * @return a (cached) comparator/*from  ww  w  . j a v  a 2  s.  co m*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <S extends Comparable> Comparator<S> getComparator(final BeanProxy annot) {
    if (annot.comparableOn().isEmpty())
        return null;
    synchronized (COMPARATOR_CACHE) {
        Comparator<S> result = (Comparator<S>) COMPARATOR_CACHE.get(annot);
        if (result == null) {
            result = new Comparator<S>() {
                @Override
                public int compare(final S o1, final S o2) {
                    final S key1 = (S) ((DynaBeanInvocationHandler) Proxy.getInvocationHandler(o1)).bean.any()
                            .get(annot.comparableOn());
                    final S key2 = (S) ((DynaBeanInvocationHandler) Proxy.getInvocationHandler(o2)).bean.any()
                            .get(annot.comparableOn());
                    return key1.compareTo(key2);
                }
            };
            LOG.trace("Created comparator for " + annot);
            COMPARATOR_CACHE.put(annot, result);
        }
        return result;
    }
}

From source file:org.apache.olingo.ext.proxy.commons.AbstractStructuredInvocationHandler.java

protected AbstractStructuredInvocationHandler getStructuredInvocationHandler(final Object value) {
    if (value != null && Proxy.isProxyClass(value.getClass())) {
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(value);
        if (invocationHandler instanceof AbstractStructuredInvocationHandler) {
            return (AbstractStructuredInvocationHandler) invocationHandler;
        }//from  w ww . j  a  v  a 2 s .  co  m
    }

    return null;
}

From source file:org.apache.olingo.fit.proxy.APIBasicDesignTestITCase.java

@Test
public void issueOLINGO398() {
    AbstractCollectionInvocationHandler<?, ?> handler = AbstractCollectionInvocationHandler.class
            .cast(Proxy.getInvocationHandler(container.getCustomers().getByKey(1).getOrders()
                    .select("OrderID", "CustomerForOrder").expand("CustomerForOrder").top(1).skip(2)));

    assertEquals(/*from   w  w w  .j  a  v a 2  s .c  o m*/
            "http://localhost:9080/stub/StaticService/V40/Static.svc/Customers(1)/Orders?"
                    + "%24select=OrderID%2CCustomerForOrder&%24expand=CustomerForOrder&%24top=1&%24skip=2",
            handler.getRequestURI().toASCIIString());

    handler = AbstractCollectionInvocationHandler.class
            .cast(Proxy.getInvocationHandler(container.getCustomers().getByKey(1).getOrders()
                    .select("OrderID", "CustomerForOrder").expand("CustomerForOrder").top(1).skip(2)));

    assertEquals(
            "http://localhost:9080/stub/StaticService/V40/Static.svc/Customers(1)/Orders?%24"
                    + "select=OrderID%2CCustomerForOrder&%24expand=CustomerForOrder&%24top=1&%24skip=2",
            handler.getRequestURI().toASCIIString());
}

From source file:com.taobao.adfs.distributed.rpc.RPC.java

/**
 * Stop this proxy and release its invoker's resource
 * //www  .j  a  v  a2  s . c  o  m
 * @param proxy
 *          the proxy to be stopped
 */
public static void stopProxy(VersionedProtocol proxy) {
    if (proxy != null) {
        ((Invoker) Proxy.getInvocationHandler(proxy)).close();
    }
}

From source file:org.diorite.config.impl.proxy.ConfigInvocationHandler.java

private Map<String, Object> toMap() {
    Map<String, Object> result = new LinkedHashMap<>(20);

    for (Entry<String, ConfigPropertyValueImpl<Object>> entry : this.predefinedValues.entrySet()) {
        result.put(entry.getKey(), entry.getValue().getPropertyValue());
    }//from  www  . j  ava  2s  .co m
    for (Entry<String, Node> entry : this.simpleDynamicValues.entrySet()) {
        result.put(entry.getKey(), Serialization.getGlobal().fromYamlNode(entry.getValue()));
    }
    for (Entry<String, SimpleConfig> entry : this.dynamicValues.entrySet()) {
        result.put(entry.getKey(),
                ((ConfigInvocationHandler) Proxy.getInvocationHandler(entry.getValue())).toMap());
    }

    return result;
}

From source file:ca.oson.json.util.ObjectUtil.java

/**
 * Changes the annotation value for the given key of the given annotation to newValue and returns
 * the previous value./*w w w  . jav a 2 s  . co m*/
 * @author: Balder
 * @param annotation the annotation to change its value
 * @param key the key in the value map
 * @param newValue the new value to change to
 */
@SuppressWarnings("unchecked")
public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue) {
    Object handler = Proxy.getInvocationHandler(annotation);
    Field f;
    try {
        f = handler.getClass().getDeclaredField("memberValues");
    } catch (NoSuchFieldException | SecurityException e) {
        throw new IllegalStateException(e);
    }
    f.setAccessible(true);
    Map<String, Object> memberValues;
    try {
        memberValues = (Map<String, Object>) f.get(handler);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    Object oldValue = memberValues.get(key);
    if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
        throw new IllegalArgumentException();
    }
    memberValues.put(key, newValue);
    return oldValue;
}

From source file:org.geoserver.catalog.impl.CatalogImpl.java

public void save(NamespaceInfo namespace) {
    validate(namespace, false);/*from  w ww .  j a  v  a2  s . c  o m*/

    ModificationProxy h = (ModificationProxy) Proxy.getInvocationHandler(namespace);

    NamespaceInfo ns = (NamespaceInfo) h.getProxyObject();
    if (!namespace.getPrefix().equals(ns.getPrefix())) {
        synchronized (namespaces) {
            namespaces.remove(ns.getPrefix());
            namespaces.put(namespace.getPrefix(), ns);
        }
    }

    saved(namespace);
}

From source file:org.apache.brooklyn.entity.brooklynnode.BrooklynNodeIntegrationTest.java

private BrooklynNodeSshDriver getDriver(BrooklynNode brooklynNode) {
    try {//from www . j  av  a  2s.c om
        EntityProxyImpl entityProxy = (EntityProxyImpl) Proxy.getInvocationHandler(brooklynNode);
        Method getDriver = BrooklynNodeImpl.class.getMethod("getDriver");
        return (BrooklynNodeSshDriver) entityProxy.invoke(brooklynNode, getDriver, new Object[] {});
    } catch (Throwable e) {
        throw Exceptions.propagate(e);
    }
}