org.springframework.aop.TargetClassAware Java Examples

The following examples show how to use org.springframework.aop.TargetClassAware. 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
Source File: AopProxyUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well —
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		Object nested = null;
		if (current instanceof Advised) {
			TargetSource targetSource = ((Advised) current).getTargetSource();
			if (targetSource instanceof SingletonTargetSource) {
				nested = ((SingletonTargetSource) targetSource).getTarget();
			}
		}
		current = nested;
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #2
Source File: AopUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the target class of the given bean instance which might be an AOP proxy.
 * <p>Returns the target class for an AOP proxy or the plain class otherwise.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the target class (or the plain class of the given object as fallback;
 * never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see org.springframework.aop.framework.AopProxyUtils#ultimateTargetClass(Object)
 */
public static Class<?> getTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Class<?> result = null;
	if (candidate instanceof TargetClassAware) {
		result = ((TargetClassAware) candidate).getTargetClass();
	}
	if (result == null) {
		result = (isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #3
Source File: AopProxyUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #4
Source File: AopUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the target class of the given bean instance which might be an AOP proxy.
 * <p>Returns the target class for an AOP proxy or the plain class otherwise.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the target class (or the plain class of the given object as fallback;
 * never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see org.springframework.aop.framework.AopProxyUtils#ultimateTargetClass(Object)
 */
public static Class<?> getTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Class<?> result = null;
	if (candidate instanceof TargetClassAware) {
		result = ((TargetClassAware) candidate).getTargetClass();
	}
	if (result == null) {
		result = (isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #5
Source File: AopProxyUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #6
Source File: BeanUtils.java    From radar with Apache License 2.0 5 votes vote down vote up
public static Class<?> getImplClassFromBean(Object bean) {
	if (TargetClassAware.class.isInstance(bean)) {
		return ((TargetClassAware) bean).getTargetClass();
	}

	return bean.getClass();
}
 
Example #7
Source File: AopUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the target class of the given bean instance which might be an AOP proxy.
 * <p>Returns the target class for an AOP proxy or the plain class otherwise.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the target class (or the plain class of the given object as fallback;
 * never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see org.springframework.aop.framework.AopProxyUtils#ultimateTargetClass(Object)
 */
public static Class<?> getTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Class<?> result = null;
	if (candidate instanceof TargetClassAware) {
		result = ((TargetClassAware) candidate).getTargetClass();
	}
	if (result == null) {
		result = (isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #8
Source File: AopProxyUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #9
Source File: AopUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the target class of the given bean instance which might be an AOP proxy.
 * <p>Returns the target class for an AOP proxy or the plain class otherwise.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the target class (or the plain class of the given object as fallback;
 * never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see org.springframework.aop.framework.AopProxyUtils#ultimateTargetClass(Object)
 */
public static Class<?> getTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Class<?> result = null;
	if (candidate instanceof TargetClassAware) {
		result = ((TargetClassAware) candidate).getTargetClass();
	}
	if (result == null) {
		result = (isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example #10
Source File: SynchronizingAspectTest.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticDiscriminatorStaticIdEmptyConditionJdkProxy() throws Throwable {
    //Test to get coverage over code paths hit when a proxy is being used for the target class
    String expectedDiscriminator = "StaticDiscriminator";
    String expectedId = "staticId";
    String expectedResult = "testStaticDiscriminatorStaticIdEmptyCondition";
    InvocationHandler handler = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("getTargetClass")) {
                return DefaultTestService.class;
            } else if (method.getName().equals("testStaticDiscriminatorStaticIdEmptyCondition")) {
                return "testStaticDiscriminatorStaticIdEmptyCondition";
            } else {
                throw new RuntimeException();
            }
        }
    };

    TestService service = (TestService) Proxy.newProxyInstance(DefaultTestService.class.getClassLoader(),
            new Class[]{TestService.class, TargetClassAware.class}, handler);

    Method expectedMethod = service.getClass().getDeclaredMethod("testStaticDiscriminatorStaticIdEmptyCondition",
            TestObject.class);
    TestObject argument = new TestObject(1L, "Jesse");
    Object[] joinPointArgs = {argument};

    ProceedingJoinPoint joinPoint = prepareJoinPoint(expectedDiscriminator, expectedId, service,
            expectedMethod, argument, joinPointArgs);

    String result = (String) aspect.synchronizeInvocation(joinPoint);
    assertThat(result, is(expectedResult));
}
 
Example #11
Source File: JobResolverServiceImpl.java    From genie with Apache License 2.0 5 votes vote down vote up
private String getProxyObjectClassName(final Object possibleProxyObject) {
    final String className;
    if (possibleProxyObject instanceof TargetClassAware) {
        final Class<?> targetClass = ((TargetClassAware) possibleProxyObject).getTargetClass();
        if (targetClass != null) {
            className = targetClass.getCanonicalName();
        } else {
            className = possibleProxyObject.getClass().getCanonicalName();
        }
    } else {
        className = possibleProxyObject.getClass().getCanonicalName();
    }
    return className;
}