mockit.internal.util.ObjectMethods 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 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.
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.util;
import javax.annotation.*;
public final class ObjectMethods
{
private ObjectMethods() {}
@Nonnull
public static String objectIdentity(@Nonnull Object obj) {
return obj.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(obj));
}
@Nullable
public static Object evaluateOverride(@Nonnull Object obj, @Nonnull String methodNameAndDesc, @Nonnull Object[] args) {
if ("equals(Ljava/lang/Object;)Z".equals(methodNameAndDesc)) {
return obj == args[0];
}
else if ("hashCode()I".equals(methodNameAndDesc)) {
return System.identityHashCode(obj);
}
else if ("toString()Ljava/lang/String;".equals(methodNameAndDesc)) {
return objectIdentity(obj);
}
else if (
args.length == 1 && methodNameAndDesc.startsWith("compareTo(L") && methodNameAndDesc.endsWith(";)I") &&
obj instanceof Comparable>
) {
Object arg = args[0];
if (obj == arg) {
return 0;
}
return System.identityHashCode(obj) > System.identityHashCode(arg) ? 1 : -1;
}
return null;
}
public static boolean isMethodFromObject(@Nonnull String name, @Nonnull String desc) {
return
"equals".equals(name) && "(Ljava/lang/Object;)Z".equals(desc) ||
"hashCode".equals(name) && "()I".equals(desc) ||
"toString".equals(name) && "()Ljava/lang/String;".equals(desc) ||
"finalize".equals(name) && "()V".equals(desc);
}
}