All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.util.MockInvocationHandler Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, 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.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.util;

import java.lang.annotation.*;
import java.lang.reflect.*;
import javax.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(); private static final Class[] CONSTRUCTOR_PARAMETERS_FOR_PROXY_CLASS = {InvocationHandler.class}; @Nonnull public static Object newMockedInstance(@Nonnull Class proxyClass) { Constructor publicConstructor; try { publicConstructor = proxyClass.getConstructor(CONSTRUCTOR_PARAMETERS_FOR_PROXY_CLASS); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } return ConstructorReflection.invoke(publicConstructor, INSTANCE); } @Nullable @Override public Object invoke(@Nonnull Object proxy, @Nonnull Method method, @Nonnull 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 { // "toString" return ObjectMethods.objectIdentity(proxy); } } if (declaringClass == Annotation.class) { return proxy.getClass().getInterfaces()[0]; } Class retType = method.getReturnType(); return DefaultValues.computeForType(retType); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy