目录
一、Java枚举类源代码
二、编译
三、简单反编译
1.命令
2.反编译结果
四、复杂反编译
1.命令
2.结果
五、Enum 源码
一、Java枚举类源代码
package com.fanzs.enums;
public enum DateTypeEnum {
YEAR, MONTH, DAY;
}
二、编译
javac DateTypeEnum.java
三、简单反编译
1.命令
javap -p DateTypeEnum.class
2.反编译结果
public final class com.fanzs.enums.DateTypeEnum extends java.lang.Enum<com.fanzs.enums.DateTypeEnum> {
public static final com.fanzs.enums.DateTypeEnum YEAR;
public static final com.fanzs.enums.DateTypeEnum MONTH;
public static final com.fanzs.enums.DateTypeEnum DAY;
private static final com.fanzs.enums.DateTypeEnum[] $VALUES;
public static com.fanzs.enums.DateTypeEnum[] values();
public static com.fanzs.enums.DateTypeEnum valueOf(java.lang.String);
private com.fanzs.enums.DateTypeEnum();
static {};
}
四、复杂反编译
1.命令
javap -c DateTypeEnum
2.结果
Compiled from "DateTypeEnum.java"
public final class com.fanzs.enums.DateTypeEnum extends java.lang.Enum<com.fanzs.enums.DateTypeEnum> {
public static final com.fanzs.enums.DateTypeEnum YEAR;
public static final com.fanzs.enums.DateTypeEnum MONTH;
public static final com.fanzs.enums.DateTypeEnum DAY;
public static com.fanzs.enums.DateTypeEnum[] values();
Code:
0: getstatic #1 // Field $VALUES:[Lcom/fanzs/enums/DateTypeEnum;
3: invokevirtual #2 // Method "[Lcom/fanzs/enums/DateTypeEnum;".clone:()Ljava/lang/Object;
6: checkcast #3 // class "[Lcom/fanzs/enums/DateTypeEnum;"
9: areturn
public static com.fanzs.enums.DateTypeEnum valueOf(java.lang.String);
Code:
0: ldc #4 // class com/fanzs/enums/DateTypeEnum
2: aload_0
3: invokestatic #5 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
6: checkcast #4 // class com/fanzs/enums/DateTypeEnum
9: areturn
static {};
Code:
0: new #4 // class com/fanzs/enums/DateTypeEnum
3: dup
4: ldc #7 // String YEAR
6: iconst_0
7: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V
10: putstatic #9 // Field YEAR:Lcom/fanzs/enums/DateTypeEnum;
13: new #4 // class com/fanzs/enums/DateTypeEnum
16: dup
17: ldc #10 // String MONTH
19: iconst_1
20: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V
23: putstatic #11 // Field MONTH:Lcom/fanzs/enums/DateTypeEnum;
26: new #4 // class com/fanzs/enums/DateTypeEnum
29: dup
30: ldc #12 // String DAY
32: iconst_2
33: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V
36: putstatic #13 // Field DAY:Lcom/fanzs/enums/DateTypeEnum;
39: iconst_3
40: anewarray #4 // class com/fanzs/enums/DateTypeEnum
43: dup
44: iconst_0
45: getstatic #9 // Field YEAR:Lcom/fanzs/enums/DateTypeEnum;
48: aastore
49: dup
50: iconst_1
51: getstatic #11 // Field MONTH:Lcom/fanzs/enums/DateTypeEnum;
54: aastore
55: dup
56: iconst_2
57: getstatic #13 // Field DAY:Lcom/fanzs/enums/DateTypeEnum;
60: aastore
61: putstatic #1 // Field $VALUES:[Lcom/fanzs/enums/DateTypeEnum;
64: return
}
五、Enum 源码
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
private final String name;
public final String name() {
return name;
}
private final int ordinal;
public final int ordinal() {
return ordinal;
}
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
public String toString() {
return name;
}
public final boolean equals(Object other) {
return this==other;
}
public final int hashCode() {
return super.hashCode();
}
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public final int compareTo(E o) {
Enum<?> other = (Enum<?>)o;
Enum<E> self = this;
if (self.getClass() != other.getClass() && // optimization
self.getDeclaringClass() != other.getDeclaringClass())
throw new ClassCastException();
return self.ordinal - other.ordinal;
}
@SuppressWarnings("unchecked")
public final Class<E> getDeclaringClass() {
Class<?> clazz = getClass();
Class<?> zuper = clazz.getSuperclass();
return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
}
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);
}
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");
}
}