2015-09-07 11:44:36 -06:00
|
|
|
// enums/Reflection.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Analyzing enums using reflection
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.lang.reflect.*;
|
|
|
|
import java.util.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
import onjava.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
enum Explore { HERE, THERE }
|
|
|
|
|
|
|
|
public class Reflection {
|
2017-05-10 11:45:39 -06:00
|
|
|
public static
|
|
|
|
Set<String> analyze(Class<?> enumClass) {
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
2017-05-10 11:45:39 -06:00
|
|
|
"_____ Analyzing " + enumClass + " _____");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Interfaces:");
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Type t : enumClass.getGenericInterfaces())
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(t);
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"Base: " + enumClass.getSuperclass());
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Methods: ");
|
2015-06-15 17:47:35 -07:00
|
|
|
Set<String> methods = new TreeSet<>();
|
|
|
|
for(Method m : enumClass.getMethods())
|
|
|
|
methods.add(m.getName());
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(methods);
|
2015-06-15 17:47:35 -07:00
|
|
|
return methods;
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
2017-01-20 21:30:44 -08:00
|
|
|
Set<String> exploreMethods =
|
|
|
|
analyze(Explore.class);
|
2015-06-15 17:47:35 -07:00
|
|
|
Set<String> enumMethods = analyze(Enum.class);
|
2017-01-20 21:30:44 -08:00
|
|
|
System.out.println(
|
|
|
|
"Explore.containsAll(Enum)? " +
|
2015-06-15 17:47:35 -07:00
|
|
|
exploreMethods.containsAll(enumMethods));
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.print("Explore.removeAll(Enum): ");
|
2015-06-15 17:47:35 -07:00
|
|
|
exploreMethods.removeAll(enumMethods);
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(exploreMethods);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Decompile the code for the enum:
|
2017-01-20 21:30:44 -08:00
|
|
|
OSExecute.command(
|
2020-10-07 13:28:11 -06:00
|
|
|
"javap -cp build/classes/java/main Explore");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
_____ Analyzing class Explore _____
|
2015-06-15 17:47:35 -07:00
|
|
|
Interfaces:
|
|
|
|
Base: class java.lang.Enum
|
|
|
|
Methods:
|
2017-05-10 11:45:39 -06:00
|
|
|
[compareTo, equals, getClass, getDeclaringClass,
|
|
|
|
hashCode, name, notify, notifyAll, ordinal, toString,
|
|
|
|
valueOf, values, wait]
|
|
|
|
_____ Analyzing class java.lang.Enum _____
|
2015-06-15 17:47:35 -07:00
|
|
|
Interfaces:
|
|
|
|
java.lang.Comparable<E>
|
|
|
|
interface java.io.Serializable
|
|
|
|
Base: class java.lang.Object
|
|
|
|
Methods:
|
2017-05-10 11:45:39 -06:00
|
|
|
[compareTo, equals, getClass, getDeclaringClass,
|
|
|
|
hashCode, name, notify, notifyAll, ordinal, toString,
|
|
|
|
valueOf, wait]
|
2015-06-15 17:47:35 -07:00
|
|
|
Explore.containsAll(Enum)? true
|
|
|
|
Explore.removeAll(Enum): [values]
|
|
|
|
Compiled from "Reflection.java"
|
|
|
|
final class Explore extends java.lang.Enum<Explore> {
|
|
|
|
public static final Explore HERE;
|
|
|
|
public static final Explore THERE;
|
|
|
|
public static Explore[] values();
|
|
|
|
public static Explore valueOf(java.lang.String);
|
|
|
|
static {};
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|