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.geoserver.catalog.impl.DefaultCatalogFacade.java

public void save(NamespaceInfo namespace) {
    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);
        }//from   w  w w.j  av a  2 s  .c  o m
    }

    saved(namespace);
}

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

/**
 * Iterate API annotation and build API Configuration
 *
 * @param annotation reading @SwaggerDefinition annotation
 * @return APIResourceConfiguration which compose with an API information which has its name, context,version,and tags
 * @throws Throwable//from   w w w  . ja v a2 s .c om
 */
private APIResourceConfiguration processAPIAnnotation(Annotation annotation) throws Throwable {
    InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
    Annotation info = (Annotation) methodHandler.invoke(annotation,
            apiClazz.getMethod(SWAGGER_ANNOTATIONS_INFO, null), null);
    Annotation[] tags = (Annotation[]) methodHandler.invoke(annotation,
            apiClazz.getMethod(SWAGGER_ANNOTATIONS_TAGS, null), null);
    String[] tagNames = new String[tags.length];
    for (int i = 0; i < tags.length; i++) {
        methodHandler = Proxy.getInvocationHandler(tags[i]);
        tagNames[i] = (String) methodHandler.invoke(tags[i],
                tagClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null);
    }
    methodHandler = Proxy.getInvocationHandler(info);
    String version = (String) methodHandler.invoke(info,
            infoClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VERSION, null), null);
    if ("".equals(version))
        return null;
    Annotation[] apiInfo = (Annotation[]) methodHandler.invoke(info,
            infoClass.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null);
    methodHandler = Proxy.getInvocationHandler(apiInfo[0]);
    Annotation[] properties = (Annotation[]) methodHandler.invoke(apiInfo[0],
            extensionClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES, null), null);
    APIResourceConfiguration apiResourceConfig = new APIResourceConfiguration();
    for (Annotation property : properties) {
        methodHandler = Proxy.getInvocationHandler(property);
        String key = (String) methodHandler.invoke(property,
                extensionPropertyClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null);
        String value = (String) methodHandler.invoke(property,
                extensionPropertyClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null), null);
        if ("".equals(key))
            return null;
        switch (key) {
        case SWAGGER_ANNOTATIONS_PROPERTIES_NAME:
            if ("".equals(value))
                return null;
            apiResourceConfig.setName(value);
            break;
        case SWAGGER_ANNOTATIONS_PROPERTIES_CONTEXT:
            if ("".equals(value))
                return null;
            apiResourceConfig.setContext(value);
            break;
        default:
            break;
        }
    }
    apiResourceConfig.setVersion(version);
    apiResourceConfig.setTags(tagNames);
    return apiResourceConfig;
}

From source file:org.apache.olingo.ext.proxy.utils.CoreUtils.java

private static void populate(final EdmEnabledODataClient client, final EntityInvocationHandler typeHandler,
        final Object bean, final Class<? extends Annotation> getterAnn,
        final Iterator<? extends ClientProperty> propItor) {

    if (bean != null) {
        final Class<?> typeRef;
        if (bean instanceof Proxy) {
            final InvocationHandler handler = Proxy.getInvocationHandler(bean);
            if (handler instanceof AbstractStructuredInvocationHandler) {
                typeRef = ((ComplexInvocationHandler) handler).getTypeRef();
            } else {
                throw new IllegalStateException("Invalid bean " + bean);
            }//ww  w  .  j av  a 2  s. com
        } else {
            typeRef = bean.getClass();
        }
        populate(client, typeHandler, bean, typeRef, getterAnn, propItor);
    }
}

From source file:rpc.TestRPC.java

@Test
public void testStopProxy() throws IOException {
    StoppedProtocol proxy = RPC.getProxy(StoppedProtocol.class, StoppedProtocol.versionID, null, conf);
    StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler) Proxy.getInvocationHandler(proxy);
    assertEquals(0, invocationHandler.getCloseCalled());
    RPC.stopProxy(proxy);/* w w  w .  j a  v  a  2s . com*/
    assertEquals(1, invocationHandler.getCloseCalled());
}

From source file:org.wso2.carbon.device.mgt.core.config.permission.AnnotationProcessor.java

private Map<String, Scope> processAPIScopes(Annotation annotation) throws Throwable {
    Map<String, Scope> scopes = new HashMap<>();

    InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
    Annotation[] annotatedScopes = (Annotation[]) methodHandler.invoke(annotation,
            scopesClass.getMethod(ANNOTATIONS_SCOPES, null), null);

    Scope scope;/*from   w ww. j a  va  2  s  .  c  om*/
    String permissions[];
    StringBuilder aggregatedPermissions;
    for (int i = 0; i < annotatedScopes.length; i++) {
        aggregatedPermissions = new StringBuilder();
        methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
        scope = new Scope();
        scope.setName(invokeMethod(scopeClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME),
                annotatedScopes[i], STRING));
        scope.setDescription(invokeMethod(scopeClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_DESCRIPTION),
                annotatedScopes[i], STRING));
        scope.setKey(invokeMethod(scopeClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_KEY), annotatedScopes[i],
                STRING));
        permissions = (String[]) methodHandler.invoke(annotatedScopes[i],
                scopeClass.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS, null), null);
        for (String permission : permissions) {
            aggregatedPermissions.append(PERMISSION_PREFIX);
            aggregatedPermissions.append(permission);
            aggregatedPermissions.append(" ");
        }
        scope.setRoles(aggregatedPermissions.toString());
        scopes.put(scope.getKey(), scope);
    }
    return scopes;
}

From source file:rpc.TestRPC.java

@Test
public void testWrappedStopProxy() throws IOException {
    StoppedProtocol wrappedProxy = RPC.getProxy(StoppedProtocol.class, StoppedProtocol.versionID, null, conf);
    StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler) Proxy
            .getInvocationHandler(wrappedProxy);

    StoppedProtocol proxy = (StoppedProtocol) RetryProxy.create(StoppedProtocol.class, wrappedProxy,
            RetryPolicies.RETRY_FOREVER);

    assertEquals(0, invocationHandler.getCloseCalled());
    RPC.stopProxy(proxy);/* w ww .  j  a v a 2 s  .  c o  m*/
    assertEquals(1, invocationHandler.getCloseCalled());
}

From source file:com.m4rc310.cb.builders.ComponentBuilder.java

public Object changeAnnotationValue(Annotation annotation, String key, Object newValue) {
    Object handler = Proxy.getInvocationHandler(annotation);
    Field f;//  w w  w  .j  a  v  a2s .  c o m
    try {
        f = handler.getClass().getDeclaredField("memberValues");
    } catch (NoSuchFieldException | SecurityException e) {
        infoError(e);
        throw new IllegalStateException(e);
    }
    f.setAccessible(true);
    Map<String, Object> memberValues;
    try {
        memberValues = (Map<String, Object>) f.get(handler);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        infoError(e);
        throw new IllegalStateException(e);
    }
    Object oldValue = memberValues.get(key);
    if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
        throw new IllegalArgumentException();
    }
    memberValues.put(key, newValue);

    LogServer.getInstance().debug(null, "O campo <{0}> foi alterado de [{1}] para [{2}]", key, oldValue,
            newValue);

    return oldValue;
}

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

public void save(WorkspaceInfo workspace) {
    ModificationProxy h = (ModificationProxy) Proxy.getInvocationHandler(workspace);

    WorkspaceInfo ws = (WorkspaceInfo) h.getProxyObject();
    if (!workspace.getName().equals(ws.getName())) {
        synchronized (workspaces) {
            workspaces.remove(ws.getName());
            workspaces.put(workspace.getName(), ws);
        }/* w  w w .  j  a  v  a  2 s  . com*/
    }

    saved(workspace);
}

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

/**
 * When an annotation and method is passed, this method invokes that executes said method against the annotation
 *
 * @param method// www  .  ja v a  2s. c o m
 * @param annotation
 * @param returnType
 * @return
 * @throws Throwable
 */
private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable {
    InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
    switch (returnType) {
    case STRING:
        return (String) methodHandler.invoke(annotation, method, null);
    case STRING_ARR:
        return ((String[]) methodHandler.invoke(annotation, method, null))[0];
    default:
        return null;
    }
}

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

private void loadImpl(@WillNotClose Reader reader) {
    Config fromYaml = Serialization.getGlobal().fromYaml(reader, this.template.getConfigType());
    ConfigInvocationHandler invocationHandler = (ConfigInvocationHandler) Proxy.getInvocationHandler(fromYaml);

    this.predefinedValues.putAll(invocationHandler.predefinedValues);
    this.dynamicValues.putAll(invocationHandler.dynamicValues);
    this.simpleDynamicValues.putAll(invocationHandler.simpleDynamicValues);
}