Example usage for java.lang.reflect InvocationHandler invoke

List of usage examples for java.lang.reflect InvocationHandler invoke

Introduction

In this page you can find the example usage for java.lang.reflect InvocationHandler invoke.

Prototype

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;

Source Link

Document

Processes a method invocation on a proxy instance and returns the result.

Usage

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

private void setPermission(Annotation currentMethod, Permission permission) 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);
        Scope scope;/* ww  w.  ja  v  a2s . c o  m*/
        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()) {
                    scope = apiScopes.get(scopeKey);
                    if (scope != null) {
                        permission.setName(scope.getName());
                        //TODO: currently permission tree supports only adding one permission per API point.
                        permission.setPath(scope.getRoles().split(" ")[0]);
                    } else {
                        log.warn("No Scope mapping is done for scope key: " + scopeKey);
                        permission.setName(DEFAULT_PERM_NAME);
                        permission.setPath(DEFAULT_PERM);
                    }
                }
            }
        }
    }
}

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;/* ww  w.j ava  2s  . c o m*/
    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;
}