Enum源码解析
类定义
1 2
| public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable
|
Enum是一个抽象类,其泛型是其子类,也就是自己定义的枚举类型
主要变量
1 2 3 4 5 6
| private final String name;
private final int ordinal;
|
构造方法
1 2 3 4 5 6 7 8
|
protected Enum(String name, int ordinal) { this.name = name; this.ordinal = ordinal; }
|
主要方法
name 和 ordinal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
public final String name() { return name; }
public final int ordinal() { return ordinal; }
|
toString、equals和hashCode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public String toString() { return name; }
public final boolean equals(Object other) { return this==other; }
public final int hashCode() { return super.hashCode(); }
|
clone
1 2 3 4 5 6 7
|
protected final Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); }
|
compareTo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public final int compareTo(E o) { Enum<?> other = (Enum<?>)o; Enum<E> self = this; if (self.getClass() != other.getClass() && self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; }
|
getDeclaringClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@SuppressWarnings("unchecked") public final Class<E> getDeclaringClass() { Class<?> clazz = getClass(); Class<?> zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper; }
|
valueOf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) { T result = enumType.enumConstantDirectory().get(name); if (result != null) return result; if (name == null) throw new NullPointerException("Name is null"); throw new IllegalArgumentException("No enum constant " + enumType.getCanonicalName() + "." + name); }
Map<String, T> enumConstantDirectory() { if (enumConstantDirectory == null) { T[] universe = getEnumConstantsShared(); if (universe == null) throw new IllegalArgumentException( getName() + " is not an enum type"); Map<String, T> m = new HashMap<>(2 * universe.length); for (T constant : universe) m.put(((Enum<?>)constant).name(), constant); enumConstantDirectory = m; } return enumConstantDirectory; }
枚举类型的Class时如何获得保存的枚举值的? ;
T[] getEnumConstantsShared() { if (enumConstants == null) { if (!isEnum()) return null; try { final Method values = getMethod("values"); java.security.AccessController.doPrivileged( new java.security.PrivilegedAction<Void>() { public Void run() { values.setAccessible(true); return null; } }); @SuppressWarnings("unchecked") T[] temporaryConstants = (T[])values.invoke(null); enumConstants = temporaryConstants; } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException ex) { return null; } } return enumConstants; }
通过枚举类型生成的values方法,可以获得所有的枚举值。 查看反编译代码可以看到枚举类中会生成一个values方法。静态方法invoke参数为null
|
finalize和反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
protected final void finalize() { }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { throw new InvalidObjectException("can't deserialize enum"); }
private void readObjectNoData() throws ObjectStreamException { throw new InvalidObjectException("can't deserialize enum"); }
|