我没有经验,但看起来它的打字比java弱得多。您的问题的第一部分非常简单。
//this checks for null objects
public static boolean isEmpty(Object o) {
return o == null;
}
但是,在您的示例代码中,您正在检查具有特定名称的特定字段。这在java中要复杂得多。您可以使用反射,但您需要检查方法和字段。然后,您需要检查适当的返回类型:Number、int、long、short、float、double。所以,这是可能的,但需要大量的工作并且会很慢。
一个更简单但更具限制性的机制是只检查常见类型:(
更新以修复枚举错误并添加数组;由于数组在 java 中的工作方式,必须为每个基元分解数组)
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
}
else if (o instanceof Object[]) {
return ((Object[]) o).length <= 0;
}
else if (o instanceof boolean[]) {
return ((boolean[]) o).length <= 0;
}
else if (o instanceof byte[]) {
return ((byte[]) o).length <= 0;
}
else if (o instanceof short[]) {
return ((short[]) o).length <= 0;
}
else if (o instanceof char[]) {
return ((char[]) o).length <= 0;
}
else if (o instanceof int[]) {
return ((int[]) o).length <= 0;
}
else if (o instanceof long[]) {
return ((long[]) o).length <= 0;
}
else if (o instanceof float[]) {
return ((float[]) o).length <= 0;
}
else if (o instanceof double[]) {
return ((double[]) o).length <= 0;
}
else if (o instanceof CharSequence) {
return ((CharSequence) o).length() <= 0;
}
else if (o instanceof Collection) {
return ((Collection) o).isEmpty();
}
else if (o instanceof Map) {
return ((Map) o).isEmpty();
}
else if (o instanceof Enumeration) {
return !((Enumeration) o).hasMoreElements();
}
else if (o instanceof Dictionary) {
return ((Dictionary) o).isEmpty();
}
else if (o instanceof Iterable) {
// NOTE: may not be efficient because an iterator is created
return !((Iterable) o).iterator().hasNext();
}
return false;
}
更新:以下是以前版本的反射检查方法,代码可以扩展以支持字段。处理返回类型并不像我最初想象的那么困难。即使使用反射,它也会使自动装箱工作。我还检查了对象是否具有 isEmpty() 布尔方法。
public static boolean isEmpty(final Object o) {
if (o == null) {
return true;
}
else if (o instanceof Object[]) {
return ((Object[]) o).length <= 0;
}
else if (o instanceof boolean[]) {
return ((boolean[]) o).length <= 0;
}
else if (o instanceof byte[]) {
return ((byte[]) o).length <= 0;
}
else if (o instanceof short[]) {
return ((short[]) o).length <= 0;
}
else if (o instanceof char[]) {
return ((char[]) o).length <= 0;
}
else if (o instanceof int[]) {
return ((int[]) o).length <= 0;
}
else if (o instanceof long[]) {
return ((long[]) o).length <= 0;
}
else if (o instanceof float[]) {
return ((float[]) o).length <= 0;
}
else if (o instanceof double[]) {
return ((double[]) o).length <= 0;
}
else if (o instanceof CharSequence) {
return ((CharSequence) o).length() <= 0;
}
else if (o instanceof Collection) {
return ((Collection) o).isEmpty();
}
else if (o instanceof Map) {
return ((Map) o).isEmpty();
}
else if (o instanceof Enumeration) {
return !((Enumeration) o).hasMoreElements();
}
else if (o instanceof Dictionary) {
return ((Dictionary) o).isEmpty();
}
else if (o instanceof Iterable) {
// NOTE: may not be efficient because an iterator is created
return !((Iterable) o).iterator().hasNext();
}
// reflection code
final Number length = retrieveNumberFromMethod(o, "length");
if (length != null) {
return length.shortValue() <= 0;
}
final Number size = retrieveNumberFromMethod(o, "size");
if (size != null) {
return size.shortValue() <= 0;
}
final Boolean isEmpty = retrieveBooleanFromMethod(o, "isEmpty");
if (isEmpty != null) {
return isEmpty;
}
return false;
}
static Number retrieveNumberFromMethod(final Object o, final String methodName) {
try {
final Number number = (Number) o.getClass().getMethod(methodName).invoke(o);
return number;
}
catch (final IllegalArgumentException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final SecurityException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final InvocationTargetException e) {
throw new IllegalStateException("Unable to retrieve number from " + methodName + " on " + o, e);
}
catch (final IllegalAccessException e) {
return null;
}
catch (final NoSuchMethodException e) {
return null;
}
}
static Boolean retrieveBooleanFromMethod(final Object o, final String methodName) {
try {
final Boolean bool = (Boolean) o.getClass().getMethod(methodName).invoke(o);
return bool;
}
catch (final IllegalArgumentException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final SecurityException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final InvocationTargetException e) {
throw new IllegalStateException("Unable to retrieve boolean from " + methodName + " on " + o, e);
}
catch (final IllegalAccessException e) {
return null;
}
catch (final NoSuchMethodException e) {
return null;
}
}