59 lines
1.9 KiB
Java
59 lines
1.9 KiB
Java
|
//: enumerated/Reflection.java
|
||
|
// Analyzing enums using reflection.
|
||
|
import java.lang.reflect.*;
|
||
|
import java.util.*;
|
||
|
import net.mindview.util.*;
|
||
|
import static net.mindview.util.Print.*;
|
||
|
|
||
|
enum Explore { HERE, THERE }
|
||
|
|
||
|
public class Reflection {
|
||
|
public static Set<String> analyze(Class<?> enumClass) {
|
||
|
print("----- Analyzing " + enumClass + " -----");
|
||
|
print("Interfaces:");
|
||
|
for(Type t : enumClass.getGenericInterfaces())
|
||
|
print(t);
|
||
|
print("Base: " + enumClass.getSuperclass());
|
||
|
print("Methods: ");
|
||
|
Set<String> methods = new TreeSet<String>();
|
||
|
for(Method m : enumClass.getMethods())
|
||
|
methods.add(m.getName());
|
||
|
print(methods);
|
||
|
return methods;
|
||
|
}
|
||
|
public static void main(String[] args) {
|
||
|
Set<String> exploreMethods = analyze(Explore.class);
|
||
|
Set<String> enumMethods = analyze(Enum.class);
|
||
|
print("Explore.containsAll(Enum)? " +
|
||
|
exploreMethods.containsAll(enumMethods));
|
||
|
printnb("Explore.removeAll(Enum): ");
|
||
|
exploreMethods.removeAll(enumMethods);
|
||
|
print(exploreMethods);
|
||
|
// Decompile the code for the enum:
|
||
|
OSExecute.command("javap Explore");
|
||
|
}
|
||
|
} /* Output:
|
||
|
----- Analyzing class Explore -----
|
||
|
Interfaces:
|
||
|
Base: class java.lang.Enum
|
||
|
Methods:
|
||
|
[compareTo, equals, getClass, getDeclaringClass, hashCode, name, notify, notifyAll, ordinal, toString, valueOf, values, wait]
|
||
|
----- Analyzing class java.lang.Enum -----
|
||
|
Interfaces:
|
||
|
java.lang.Comparable<E>
|
||
|
interface java.io.Serializable
|
||
|
Base: class java.lang.Object
|
||
|
Methods:
|
||
|
[compareTo, equals, getClass, getDeclaringClass, hashCode, name, notify, notifyAll, ordinal, toString, valueOf, wait]
|
||
|
Explore.containsAll(Enum)? true
|
||
|
Explore.removeAll(Enum): [values]
|
||
|
Compiled from "Reflection.java"
|
||
|
final class Explore extends java.lang.Enum{
|
||
|
public static final Explore HERE;
|
||
|
public static final Explore THERE;
|
||
|
public static final Explore[] values();
|
||
|
public static Explore valueOf(java.lang.String);
|
||
|
static {};
|
||
|
}
|
||
|
*///:~
|