mockit.internal.util.MockInvocationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for developer (unit/integration) testing.
It contains mocking APIs and other tools, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006-2011 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.util;
import java.lang.reflect.*;
import java.lang.annotation.*;
/**
* Handles invocations to all kinds of mock implementations created for interfaces and annotation types through any of
* the mocking APIs.
*
* The {@code java.lang.Object} methods {@code equals}, {@code hashCode}, and {@code toString} are handled in a
* meaningful way, returning a value that makes sense for the proxy instance.
* The special {@linkplain Annotation} contracts for these three methods is not observed, though, since it
* would require making dynamic calls to the mocked annotation attributes.
*
* Any other method invocation is handled by simply returning the default value according to the method's return type
* (as defined in {@linkplain mockit.internal.util.DefaultValues}).
*/
public final class MockInvocationHandler implements InvocationHandler
{
public static final InvocationHandler INSTANCE = new MockInvocationHandler();
public Object invoke(Object proxy, Method method, Object[] args)
{
Class> declaringClass = method.getDeclaringClass();
String methodName = method.getName();
if (declaringClass == Object.class) {
if ("equals".equals(methodName)) {
return proxy == args[0];
}
else if ("hashCode".equals(methodName)) {
return System.identityHashCode(proxy);
}
else if ("toString".equals(methodName)) {
return
proxy.getClass().getName() + '@' +
Integer.toHexString(System.identityHashCode(proxy));
}
}
else if (declaringClass == Annotation.class) {
return proxy.getClass().getInterfaces()[0];
}
Class> retType = method.getReturnType();
return DefaultValues.computeForType(retType);
}
}