Example usage for org.springframework.aop.support AopUtils isEqualsMethod

List of usage examples for org.springframework.aop.support AopUtils isEqualsMethod

Introduction

In this page you can find the example usage for org.springframework.aop.support AopUtils isEqualsMethod.

Prototype

public static boolean isEqualsMethod(@Nullable Method method) 

Source Link

Document

Determine whether the given method is an "equals" method.

Usage

From source file:com.mystudy.source.spring.aop.JdkDynamicAopProxy.java

/**
 * Finds any {@link #equals} or {@link #hashCode} method that may be defined
 * on the supplied set of interfaces.//from  www.j  a v a2  s  .  c  o m
 * @param proxiedInterfaces the interfaces to introspect
 */
private void findDefinedEqualsAndHashCodeMethods(Class<?>[] proxiedInterfaces) {
    for (Class<?> proxiedInterface : proxiedInterfaces) {
        Method[] methods = proxiedInterface.getDeclaredMethods();
        for (Method method : methods) {
            if (AopUtils.isEqualsMethod(method)) {
                this.equalsDefined = true;
            }
            if (AopUtils.isHashCodeMethod(method)) {
                this.hashCodeDefined = true;
            }
            if (this.equalsDefined && this.hashCodeDefined) {
                return;
            }
        }
    }
}

From source file:com.mystudy.source.spring.aop.JdkDynamicAopProxy.java

/**
 * // w w w  .  j a v  a2s .c om
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodInvocation invocation;
    Object oldProxy = null;
    boolean setProxyContext = false;

    TargetSource targetSource = this.advised.targetSource;
    Class<?> targetClass = null;
    Object target = null;

    try {//eqal
        if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
            // The target does not implement the equals(Object) method itself.
            return equals(args[0]);
        }
        //hashcode
        if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
            // The target does not implement the hashCode() method itself.
            return hashCode();
        }

        if (!this.advised.opaque && method.getDeclaringClass().isInterface()
                && method.getDeclaringClass().isAssignableFrom(Advised.class)) {
            // Service invocations on ProxyConfig with the proxy config...
            return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
        }

        Object retVal;

        if (this.advised.exposeProxy) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            setProxyContext = true;
        }

        // May be null. Get as late as possible to minimize the time we "own" the target,
        // in case it comes from a pool.
        target = targetSource.getTarget();
        if (target != null) {
            targetClass = target.getClass();
        }

        // Get the interception chain for this method.
        List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

        // Check whether we have any advice. If we don't, we can fallback on direct
        // reflective invocation of the target, and avoid creating a MethodInvocation.
        if (chain.isEmpty()) {
            // We can skip creating a MethodInvocation: just invoke the target directly
            // Note that the final invoker must be an InvokerInterceptor so we know it does
            // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
            retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
        } else {
            //chain????
            // We need to create a method invocation...
            invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
            // Proceed to the joinpoint through the interceptor chain.
            retVal = invocation.proceed();
        }

        // Massage return value if necessary.
        Class<?> returnType = method.getReturnType();
        if (retVal != null && retVal == target && returnType.isInstance(proxy)
                && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
            // Special case: it returned "this" and the return type of the method
            // is type-compatible. Note that we can't help if the target sets
            // a reference to itself in another returned object.
            retVal = proxy;
        } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
            throw new AopInvocationException(
                    "Null return value from advice does not match primitive return type for: " + method);
        }
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            // Must have come from TargetSource.
            targetSource.releaseTarget(target);
        }
        if (setProxyContext) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}

From source file:org.springframework.aop.framework.JdkDynamicAopProxy.java

/**
 * Implementation of {@code InvocationHandler.invoke}.
 * <p>Callers will see exactly the exception thrown by the target,
 * unless a hook method throws an exception.
 *///w  w  w  .java  2 s  .co m
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodInvocation invocation;
    Object oldProxy = null;
    boolean setProxyContext = false;

    TargetSource targetSource = this.advised.targetSource;
    Object target = null;

    try {
        if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
            // The target does not implement the equals(Object) method itself.
            return equals(args[0]);
        } else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
            // The target does not implement the hashCode() method itself.
            return hashCode();
        } else if (method.getDeclaringClass() == DecoratingProxy.class) {
            // There is only getDecoratedClass() declared -> dispatch to proxy config.
            return AopProxyUtils.ultimateTargetClass(this.advised);
        } else if (!this.advised.opaque && method.getDeclaringClass().isInterface()
                && method.getDeclaringClass().isAssignableFrom(Advised.class)) {
            // Service invocations on ProxyConfig with the proxy config...
            return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
        }

        Object retVal;

        if (this.advised.exposeProxy) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            setProxyContext = true;
        }

        // Get as late as possible to minimize the time we "own" the target,
        // in case it comes from a pool.
        target = targetSource.getTarget();
        Class<?> targetClass = (target != null ? target.getClass() : null);

        // Get the interception chain for this method.
        List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

        // Check whether we have any advice. If we don't, we can fallback on direct
        // reflective invocation of the target, and avoid creating a MethodInvocation.
        if (chain.isEmpty()) {
            // We can skip creating a MethodInvocation: just invoke the target directly
            // Note that the final invoker must be an InvokerInterceptor so we know it does
            // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
            Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
            retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
        } else {
            // We need to create a method invocation...
            invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
            // Proceed to the joinpoint through the interceptor chain.
            retVal = invocation.proceed();
        }

        // Massage return value if necessary.
        Class<?> returnType = method.getReturnType();
        if (retVal != null && retVal == target && returnType != Object.class && returnType.isInstance(proxy)
                && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
            // Special case: it returned "this" and the return type of the method
            // is type-compatible. Note that we can't help if the target sets
            // a reference to itself in another returned object.
            retVal = proxy;
        } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
            throw new AopInvocationException(
                    "Null return value from advice does not match primitive return type for: " + method);
        }
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            // Must have come from TargetSource.
            targetSource.releaseTarget(target);
        }
        if (setProxyContext) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}

From source file:org.springmodules.xt.model.generator.factory.FactoryGeneratorInterceptor.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (this.isConstructorArg(method)) {
        return this.putConstructorArg(args, method);
    } else if (this.isPropertySetter(method)) {
        return this.putProperty(args, method);
    } else if (this.isValueSetter(method)) {
        return this.putValue(args, method);
    } else if (this.isGetter(method)) {
        return this.readProperty(args, method);
    } else if (this.isFactoryMethod(method)) {
        Class returnType = method.getReturnType();
        if (!returnType.isAssignableFrom(this.productClass)) {
            throw new ReturnTypeMismatchException("Return type mismatch. Expected assignable from: "
                    + this.productClass + ", found: " + returnType);
        } else {//from w  w w . jav  a  2 s .c  o m
            Object product = this.make();
            // Fill properties:
            for (Map.Entry<String, PropertyPair> entry : this.properties.entrySet()) {
                String propertyName = entry.getKey();
                Object propertyValue = entry.getValue().getValue();
                Property.AccessType propertyAccess = entry.getValue().getAccess();
                if (propertyAccess.equals(Property.AccessType.FIELD)) {
                    Field field = this.productClass.getDeclaredField(propertyName);
                    field.setAccessible(true);
                    field.set(product, propertyValue);
                } else {
                    String methodName = new StringBuilder("set").append(StringUtils.capitalize(propertyName))
                            .toString();
                    Method methodObj = this.productClass.getMethod(methodName, propertyValue.getClass());
                    methodObj.invoke(product, propertyValue);
                }
            }
            // Return product:
            return product;
        }
    } else {
        if (AopUtils.isEqualsMethod(method)) {
            return this.doEquals(proxy, args[0]);
        } else if (AopUtils.isHashCodeMethod(method)) {
            return this.doHashCode(proxy);
        } else if (AopUtils.isToStringMethod(method)) {
            return this.doToString(proxy);
        } else {
            // Fail fast:
            throw new UnsupportedOperationException("Unsupported method called: " + method.getName());
        }
    }
}