Java Code Examples for java.lang.reflect.InvocationHandler#invoke()

The following examples show how to use java.lang.reflect.InvocationHandler#invoke() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 2
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 3
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 4
Source Project: gadtry   File: CutModeImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> T getProxyStatic(
        Proxy proxyFactory,
        ClassLoader loader,
        Class<?> interfaces,
        InvocationHandler handler,
        T instance,
        Function1<MethodInfo, Boolean> filter)
{
    InvocationHandler proxyHandler = filter != null ?
            (InvocationHandler & Serializable) (proxy, method, args) -> {
                if (filter.apply(MethodInfo.of(method))) {
                    return handler.invoke(proxy, method, args);
                }
                else {
                    return method.invoke(instance, args);
                }
            }
            : handler;

    return proxyFactory.getProxy(loader, proxyHandler, interfaces);
}
 
Example 5
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 6
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 7
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 8
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 9
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 10
public Object invoke( Object proxy, Method method, Object[] args )
    throws Throwable
{
    // Note that the declaring class in method is the interface
    // in which the method was defined, not the proxy class.
    Class cls = method.getDeclaringClass() ;
    InvocationHandler handler =
        (InvocationHandler)classToInvocationHandler.get( cls ) ;

    if (handler == null) {
        if (defaultHandler != null)
            handler = defaultHandler ;
        else {
            ORBUtilSystemException wrapper = ORBUtilSystemException.get(
                CORBALogDomains.UTIL ) ;
            throw wrapper.noInvocationHandler( "\"" + method.toString() +
                "\"" ) ;
        }
    }

    // handler should never be null here.

    return handler.invoke( proxy, method, args ) ;
}
 
Example 11
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;
        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;
}
 
Example 12
/**
 * Finds a registered mock that can handle the method invocation and delegate to it. If there is no such mock throws
 * IllegalStateException.
 *
 * @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])
 */
@Override
public Object invoke( final Object proxy, final Method method, final Object[] args )
    throws Throwable
{
    System.out.println( "Play mock for " + method );
    for( MockResolver mockResolver : mockRepository.getAll() )
    {
        InvocationHandler handler = mockResolver.getInvocationHandler( proxy, method, args );
        if( handler != null )
        {
            return handler.invoke( mockResolver, method, args );
        }
    }
    throw new IllegalStateException( "There is no mock registered that can handle " + method );
}
 
Example 13
public boolean isUseServerPrepare(Statement statement) {
    if(statement instanceof PGStatement) {
        PGStatement pgStatement = (PGStatement) statement;
        return pgStatement.isUseServerPrepare();
    } else {
        InvocationHandler handler = Proxy.getInvocationHandler(statement);
        try {
            return (boolean) handler.invoke(statement, PGStatement.class.getMethod("isUseServerPrepare"), null);
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
    }
}
 
Example 14
private void invokeSideEffects( Object proxy,
                                Method method,
                                Object[] params,
                                Object result,
                                Throwable originalThrowable
)
    throws Throwable
{
    proxyHandler.setProxy( proxy );
    resultInvocationHandler.setResult( result, originalThrowable );

    try
    {
        for( InvocationHandler sideEffect : sideEffects )
        {
            try
            {
                sideEffect.invoke( proxy, method, params );
            }
            catch( Throwable throwable )
            {
                if( throwable != originalThrowable )
                {
                    throwable.printStackTrace();
                }
            }
        }
    }
    finally
    {
        proxyHandler.clearProxy();
        resultInvocationHandler.setResult( null, null );
    }
}
 
Example 15
/**
 * When an annotation and method is passed, this method invokes that executes said method against the annotation
 *
 * @param method
 * @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;
    }
}
 
Example 16
public void setPrepareThreshold(Statement statement, int threshold) throws SQLException {
    if(statement instanceof PGStatement) {
        PGStatement pgStatement = (PGStatement) statement;
        pgStatement.setPrepareThreshold(threshold);
    } else {
        InvocationHandler handler = Proxy.getInvocationHandler(statement);
        try {
            handler.invoke(statement, PGStatement.class.getMethod("setPrepareThreshold", int.class), new Object[]{threshold});
        } catch (Throwable throwable) {
            throw new IllegalArgumentException(throwable);
        }
    }
}
 
Example 17
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;
    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;
}
 
Example 18
private Map<String,ApiScope> processAPIScopes(Annotation annotation) throws Throwable {
    Map<String, ApiScope> scopes = new HashMap<>();

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

    ApiScope scope;
    String permissions[];
    StringBuilder aggregatedPermissions;
    for(int i=0; i<annotatedScopes.length; i++){
        aggregatedPermissions = new StringBuilder();
        methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
        scope = new ApiScope();
        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().trim());
        scopes.put(scope.getKey(), scope);
    }
    return scopes;
}
 
Example 19
/**
 * When an annotation and method is passed, this method invokes that executes said method against the annotation
 *
 * @param method
 * @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;
    }
}
 
Example 20
/**
 * 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
 */
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;
}