Example usage for org.springframework.aop ProxyMethodInvocation getProxy

List of usage examples for org.springframework.aop ProxyMethodInvocation getProxy

Introduction

In this page you can find the example usage for org.springframework.aop ProxyMethodInvocation getProxy.

Prototype

Object getProxy();

Source Link

Document

Return the proxy that this method invocation was made through.

Usage

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.aop.ScriptableBaseAdapterInterceptor.java

/**
 * {@inheritDoc}/*from  w w  w.  ja v a 2s  .  c o m*/
 */
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    final Object result;
    final Method method = invocation.getMethod();
    final Class<?> declaringClass = method.getDeclaringClass();
    if (Scriptable.class.equals(declaringClass) && invocation instanceof ProxyMethodInvocation) {
        final ProxyMethodInvocation pInvocation = (ProxyMethodInvocation) invocation;
        final Object proxy = pInvocation.getProxy();

        final String methodName = method.getName();
        final Object[] arguments = invocation.getArguments();

        // String-switch not supported in Java < 8
        switch (ScriptableMethodName.methodLiteralOf(methodName)) {
        case GETPARENTSCOPE:
            result = this.parentScope.get(proxy);
            break;
        case SETPARENTSCOPE:
            if (arguments.length > 0 && arguments[0] instanceof Scriptable) {
                this.parentScope.put(proxy, (Scriptable) arguments[0]);
            }
            // void return type
            result = null;
            break;
        case GETPROTOTYPE:
            result = this.prototype.get(proxy);
            break;
        case SETPROTOTYPE:
            if (arguments.length > 0 && arguments[0] instanceof Scriptable) {
                this.prototype.put(proxy, (Scriptable) arguments[0]);
            }
            // void return type
            result = null;
            break;
        case HASINSTANCE:
            // proxies can never be used for hasInstance checks
            result = Boolean.FALSE;
            break;
        case GETDEFAULVALUE:
            if (arguments.length > 0) {
                if (arguments[0] == null || ScriptRuntime.StringClass == arguments[0]) {
                    result = proxy.toString();
                } else {
                    result = null;
                }
            } else {
                result = null;
            }
            break;
        default:
            // simply delegate further
            result = invocation.proceed();
            break;
        }
    } else {
        result = invocation.proceed();
    }

    return result;
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.aop.ScriptableListLikeMapAdapterInterceptor.java

/**
 * {@inheritDoc}//from  w ww. j av a2s .co m
 */
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    final Object result;
    final Method method = invocation.getMethod();
    final Class<?> declaringClass = method.getDeclaringClass();
    if (Scriptable.class.equals(declaringClass) && invocation instanceof ProxyMethodInvocation) {
        final ProxyMethodInvocation pInvocation = (ProxyMethodInvocation) invocation;
        final Object proxy = pInvocation.getProxy();

        final String methodName = method.getName();
        final Object[] arguments = invocation.getArguments();

        // String-switch not supported in Java < 8
        switch (ScriptableMethodName.methodLiteralOf(methodName)) {
        case GET:
            if (proxy instanceof Map<?, ?> && arguments.length > 0 && arguments[0] instanceof Integer) {
                if (!((Map<?, ?>) proxy).containsKey(arguments[0])) {
                    final Object[] keys = ((Map<?, ?>) proxy).keySet().toArray(new Object[0]);
                    if (((Integer) arguments[0]).intValue() >= 0
                            && keys.length > ((Integer) arguments[0]).intValue()) {
                        result = ((Map<?, ?>) proxy).get(keys[((Integer) arguments[0]).intValue()]);
                    } else {
                        // simply delegate further
                        result = invocation.proceed();
                    }
                } else {
                    // simply delegate further
                    result = invocation.proceed();
                }
            } else {
                // simply delegate further
                result = invocation.proceed();
            }
            break;
        case HAS:
            if (proxy instanceof Map<?, ?> && arguments.length > 0 && arguments[0] instanceof Integer) {
                if (!((Map<?, ?>) proxy).containsKey(arguments[0])) {
                    final Object[] keys = ((Map<?, ?>) proxy).keySet().toArray(new Object[0]);
                    result = Boolean.valueOf(((Integer) arguments[0]).intValue() >= 0
                            && keys.length > ((Integer) arguments[0]).intValue());
                } else {
                    // simply delegate further
                    result = invocation.proceed();
                }
            } else {
                // simply delegate further
                result = invocation.proceed();
            }
            break;
        case PUT:
            if (proxy instanceof Map<?, ?> && arguments.length > 2 && arguments[0] instanceof Integer) {
                if (!((Map<?, ?>) proxy).containsKey(arguments[0])) {
                    // we only support indexed-put for existing keys
                    final Object[] keys = ((Map<?, ?>) proxy).keySet().toArray(new Object[0]);
                    if (((Integer) arguments[0]).intValue() >= 0
                            && keys.length > ((Integer) arguments[0]).intValue()) {
                        @SuppressWarnings("unchecked")
                        final Map<Object, Object> map = (Map<Object, Object>) proxy;
                        map.put(keys[((Integer) arguments[0]).intValue()], arguments[2]);
                        // void return type
                        result = null;
                    } else {
                        // simply delegate further
                        result = invocation.proceed();
                    }
                } else {
                    // simply delegate further
                    result = invocation.proceed();
                }
            } else {
                // simply delegate further
                result = invocation.proceed();
            }
            break;
        case DELETE:
            if (proxy instanceof Map<?, ?> && arguments.length > 0 && arguments[0] instanceof Integer) {
                if (!((Map<?, ?>) proxy).containsKey(arguments[0])) {
                    final Object[] keys = ((Map<?, ?>) proxy).keySet().toArray(new Object[0]);
                    if (((Integer) arguments[0]).intValue() >= 0
                            && keys.length > ((Integer) arguments[0]).intValue()) {
                        ((Map<?, ?>) proxy).remove(keys[((Integer) arguments[0]).intValue()]);
                        // void return type
                        result = null;
                    } else {
                        // simply delegate further
                        result = invocation.proceed();
                    }
                } else {
                    // simply delegate further
                    result = invocation.proceed();
                }
            } else {
                // simply delegate further
                result = invocation.proceed();
            }
            break;
        default:
            // simply delegate further
            result = invocation.proceed();
            break;
        }

    } else {
        result = invocation.proceed();
    }

    return result;
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.aop.ScriptableMapListAdapterInterceptor.java

/**
 * {@inheritDoc}//from w ww.  ja v a 2  s.c o m
 */
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    final Object result;
    final Method method = invocation.getMethod();
    final Class<?> declaringClass = method.getDeclaringClass();
    if (Scriptable.class.equals(declaringClass) && invocation instanceof ProxyMethodInvocation) {
        final ProxyMethodInvocation pInvocation = (ProxyMethodInvocation) invocation;
        final Object proxy = pInvocation.getProxy();

        final String methodName = method.getName();
        final Object[] arguments = invocation.getArguments();
        final Class<?>[] parameterTypes = method.getParameterTypes();

        boolean redirect = false;
        String redirectMethod = null;
        Object[] redirectParams = null;
        @SuppressWarnings("rawtypes")
        Class[] redirectParamTypes = null;
        Class<?> redirectDeclaringClass = null;
        Object nonRedirectResult = null;

        // String-switch not supported in Java < 8
        switch (ScriptableMethodName.methodLiteralOf(methodName)) {
        case GET:
            if (proxy instanceof List<?> && int.class.equals(parameterTypes[0])) {
                redirect = true;
                redirectDeclaringClass = List.class;
                redirectMethod = "get";
                redirectParams = new Object[] { arguments[0] };
                redirectParamTypes = new Class[] { parameterTypes[0] };
            } else if (proxy instanceof Map<?, ?>) {
                redirect = true;
                redirectDeclaringClass = Map.class;
                redirectMethod = "get";
                redirectParams = new Object[] { arguments[0] };
                redirectParamTypes = new Class[] { Object.class };
            }
            break;
        case HAS:
            if (proxy instanceof List<?> && int.class.equals(parameterTypes[0])) {
                nonRedirectResult = Boolean
                        .valueOf(((Integer) arguments[0]).intValue() < ((List<?>) proxy).size());
            } else if (proxy instanceof Map<?, ?>) {
                redirect = true;
                redirectDeclaringClass = Map.class;
                redirectMethod = "containsKey";
                redirectParams = new Object[] { arguments[0] };
                redirectParamTypes = new Class[] { Object.class };
            }
            break;
        case PUT:
            if (proxy instanceof List<?> && int.class.equals(parameterTypes[0])) {
                redirect = true;
                redirectDeclaringClass = List.class;
                redirectMethod = ((List<?>) proxy).size() > ((Integer) arguments[0]).intValue() ? "set" : "add";
                redirectParams = new Object[] { arguments[0], arguments[2] };
                redirectParamTypes = new Class[] { int.class, Object.class };
            } else if (proxy instanceof Map<?, ?>) {
                redirect = true;
                redirectDeclaringClass = Map.class;
                redirectMethod = "put";
                redirectParams = new Object[] { arguments[0], arguments[2] };
                redirectParamTypes = new Class[] { Object.class, Object.class };
            }
            break;
        case DELETE:
            if (proxy instanceof List<?> && int.class.equals(parameterTypes[0])) {
                redirect = true;
                redirectDeclaringClass = List.class;
                redirectMethod = "remove";
                redirectParams = arguments;
                redirectParamTypes = parameterTypes;
            } else if (proxy instanceof Map<?, ?>) {
                redirect = true;
                redirectDeclaringClass = Map.class;
                redirectMethod = "remove";
                redirectParams = arguments;
                redirectParamTypes = new Class[] { Object.class };
            }
            break;
        case GETIDS:
            if (proxy instanceof Map<?, ?>) {
                // Note: we do not cover the case of Map + List for getIds, as List is typically only used for indexed access based on
                // known size and not retrieval of keys
                nonRedirectResult = ((Map<?, ?>) proxy).keySet().toArray(new Object[0]);
            } else if (proxy instanceof List<?>) {
                final int size = ((List<?>) proxy).size();
                final Object[] arr = new Object[size];
                for (int idx = 0; idx < size; idx++) {
                    arr[idx] = Integer.valueOf(idx);
                }
            }
            break;
        default:
            // NO-OP
            break;
        }

        final Class<?> returnType = method.getReturnType();
        if (redirect && redirectDeclaringClass != null) {
            final Method redirectMethodHandle = redirectDeclaringClass.getMethod(redirectMethod,
                    redirectParamTypes);
            result = redirectMethodHandle.invoke(proxy, redirectParams);
        } else if (void.class.equals(returnType)) {
            result = null;
        } else if (nonRedirectResult != null && returnType.isInstance(nonRedirectResult)) {
            result = nonRedirectResult;
        } else if (boolean.class.equals(returnType)) {
            result = nonRedirectResult instanceof Boolean ? (Boolean) nonRedirectResult : Boolean.FALSE;
        } else {
            // TODO: what else to do?
            result = null;
        }
    } else {
        result = invocation.proceed();
    }

    return result;
}

From source file:org.springframework.aop.aspectj.AspectJExpressionPointcut.java

@Override
public boolean matches(Method method, @Nullable Class<?> targetClass, Object... args) {
    obtainPointcutExpression();/*from w  w  w  .  ja va 2s  .c  o  m*/
    ShadowMatch shadowMatch = getShadowMatch(AopUtils.getMostSpecificMethod(method, targetClass), method);
    ShadowMatch originalShadowMatch = getShadowMatch(method, method);

    // Bind Spring AOP proxy to AspectJ "this" and Spring AOP target to AspectJ target,
    // consistent with return of MethodInvocationProceedingJoinPoint
    ProxyMethodInvocation pmi = null;
    Object targetObject = null;
    Object thisObject = null;
    try {
        MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
        targetObject = mi.getThis();
        if (!(mi instanceof ProxyMethodInvocation)) {
            throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
        }
        pmi = (ProxyMethodInvocation) mi;
        thisObject = pmi.getProxy();
    } catch (IllegalStateException ex) {
        // No current invocation...
        if (logger.isDebugEnabled()) {
            logger.debug("Could not access current invocation - matching with limited context: " + ex);
        }
    }

    try {
        JoinPointMatch joinPointMatch = shadowMatch.matchesJoinPoint(thisObject, targetObject, args);

        /*
         * Do a final check to see if any this(TYPE) kind of residue match. For
         * this purpose, we use the original method's (proxy method's) shadow to
         * ensure that 'this' is correctly checked against. Without this check,
         * we get incorrect match on this(TYPE) where TYPE matches the target
         * type but not 'this' (as would be the case of JDK dynamic proxies).
         * <p>See SPR-2979 for the original bug.
         */
        if (pmi != null && thisObject != null) { // there is a current invocation
            RuntimeTestWalker originalMethodResidueTest = getRuntimeTestWalker(originalShadowMatch);
            if (!originalMethodResidueTest.testThisInstanceOfResidue(thisObject.getClass())) {
                return false;
            }
            if (joinPointMatch.matches()) {
                bindParameters(pmi, joinPointMatch);
            }
        }

        return joinPointMatch.matches();
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate join point for arguments " + Arrays.asList(args)
                    + " - falling back to non-match", ex);
        }
        return false;
    }
}