Commit e2016cbb authored by Lubomir Bulej's avatar Lubomir Bulej

ObjectShadowClass: initial implementation of isInstance() and isAssignableFrom().

ObjectShadowClassTest: enable tests for reflection-like behavior of isInstance() and isAssignableFrom() on self.
parent 48201afa
......@@ -33,26 +33,84 @@ final class ObjectShadowClass extends ShadowClass {
*/
@Override
public boolean isInstance (final ShadowObject object) {
// return equals(obj.getShadowClass());
throw new UnsupportedOperationException ("not yet implemented");
if (object == null) {
return false;
}
if (object instanceof ShadowClass) {
//
// ShadowClass instances have no shadow class. If this shadow class
// represents the Object or Class types, then the other shadow class
// can be considered an instance of this type.
//
return __isObjectClass () || __isClassClass ();
} else {
//
// For all other cases, check whether the type represented by the
// given class is assignable to the type represented by this class.
//
return this.isAssignableFrom (object.getShadowClass ());
}
}
private boolean __isObjectClass () {
return this.__superClass == null;
}
private boolean __isClassClass () {
return this == ShadowClassTable.JAVA_LANG_CLASS.get ();
}
/**
* @see Class#isAssignableFrom(Class)
*/
@Override
public boolean isAssignableFrom (final ShadowClass other) {
// while (other != null) {
// if (other.equals(this)) {
// return true;
// }
//
// other = other.getSuperclass();
// }
// A reference class type is not assignable from a primitive type.
// This check also ensures that we throw a NullPointerException if
// the other reference is null.
//
// return false;
throw new UnsupportedOperationException ("not yet implemented");
if (other.isPrimitive ()) {
return false;
}
// Classes loaded by different class loaders are not assignable.
if (this.getClassLoader () != other.getClassLoader ()) {
return false;
}
// If this class represents Object, then anything is assignable to it.
if (__isObjectClass ()) {
return true;
}
// In all other cases, check along the inheritance hierarchy.
return __isAssignableFrom (other);
}
private boolean __isAssignableFrom (final ShadowClass other) {
if (other == null) {
// We reached the root of the inheritance hierarchy.
return false;
}
if (this.equals (other)) {
// We found a matching class in the inheritance hierarchy.
return true;
}
for (final ShadowClass otherIface : other.getInterfaces ()) {
if (this.isAssignableFrom (otherIface)) {
// The other class implements an assignable interface.
return true;
}
}
return __isAssignableFrom (other.getSuperclass ());
}
//
......
......@@ -5,7 +5,6 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.ParametersSuppliedBy;
......@@ -152,16 +151,14 @@ public class ObjectShadowClassTest extends ShadowClassTestBase {
//
@Override @Theory @Ignore
@Override @Theory
public void isInstanceOnSelfMatchesReflection (@ParametersSuppliedBy (ReferenceTypeSupplier.class) final Class <?> type) {
// TODO Enable ObjectShadowClassTest.isInstanceOnSelfMatchesReflection when implemented.
super.isInstanceOnSelfMatchesReflection (type);
}
@Override @Theory @Ignore
@Override @Theory
public void isAssignableOnSelfMatchesReflection (@ParametersSuppliedBy (ReferenceTypeSupplier.class) final Class <?> type) {
// TODO Enable ObjectShadowClassTest.isAssignableOnSelfMatchesReflection when implemented.
super.isAssignableOnSelfMatchesReflection (type);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment