java.lang.reflect.InvocationHandler#invoke ( )源码实例Demo

下面列出了java.lang.reflect.InvocationHandler#invoke ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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 ) ;
}
 
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 ) ;
}
 
源代码3 项目: jdk8u60   文件: CompositeInvocationHandlerImpl.java
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 ) ;
}
 
源代码4 项目: gadtry   文件: CutModeImpl.java
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);
}
 
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 ) ;
}
 
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 ) ;
}
 
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 ) ;
}
 
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 ) ;
}
 
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 ) ;
}
 
源代码10 项目: openjdk-8   文件: CompositeInvocationHandlerImpl.java
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 ) ;
}
 
源代码11 项目: carbon-device-mgt   文件: 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;
        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;
}
 
源代码12 项目: attic-polygene-java   文件: MockPlayerMixin.java
/**
 * 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 );
}
 
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);
        }
    }
}
 
源代码14 项目: attic-polygene-java   文件: SideEffectsInstance.java
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 );
    }
}
 
源代码15 项目: carbon-device-mgt   文件: AnnotationProcessor.java
/**
 * 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;
    }
}
 
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);
        }
    }
}
 
源代码17 项目: carbon-device-mgt   文件: 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;
    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;
}
 
源代码18 项目: carbon-device-mgt   文件: AnnotationProcessor.java
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;
}
 
源代码19 项目: carbon-device-mgt   文件: AnnotationProcessor.java
/**
 * 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;
    }
}
 
源代码20 项目: carbon-device-mgt   文件: 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
 */
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;
}
 
 同类方法