public static Class<?> childClassOf(Class<?> parentClass,
            Object instance) {
        if (instance == null || instance == Object.class)
            return null;
        if (parentClass != null) {
            if (parentClass.isInterface()) {
                // child of an interface class is not injective (ie one-to-one)
                return null;
            }
        }
        Class<?> childClass = instance.getClass();
        while (true) {
            Class<?> parent = childClass.getSuperclass();
            if (parent == parentClass)
                return childClass;
            if (parent == null)
                return null;
            childClass = parent;
        }
    }
Returns the class of the immediate subclass of the given parent class for
the given object instance; or null if such immediate subclass cannot be
uniquely identified for the given object instance.

@param parentClass
           the parent class. The child class is {@link Object} if and
           only if the parent class is null.
@param instance
           the given object instance
aws-java-sdk-core/src/main/java/com/amazonaws/util/Classes.java