[疑难] 有办法遍历命名枚举吗?
zozoh
2009-03-17
package com.zzh.castor.castor; import java.lang.reflect.Field; import com.zzh.castor.Castor; import com.zzh.castor.FailToCastObjectException; import com.zzh.lang.Lang; @SuppressWarnings("unchecked") public class Number2Enum extends Castor<Number, Enum> { @Override protected Enum cast(Number src, Class<?> toType, String... args) throws FailToCastObjectException { try { for (Field field : toType.getFields()) { if (field.getType() == toType) { Enum em = (Enum) field.get(null); if (em.ordinal() == src.intValue()) return em; } } throw Lang.makeThrow(FailToCastObjectException.class, "Can NO find enum value in [%s] by int value '%d'", toType.getName(), src .intValue()); } catch (Exception e) { throw Lang.wrapThrow(e, FailToCastObjectException.class); } } } |
|
tomqyp
2009-03-18
有个库可以在运行时取到类名,变量名等信息,不过编译时要打开调试信息开关.
具体什么名字我也不记得了,可以翻翻以前的贴子 |