forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
111 lines (87 loc) · 4.74 KB
/
Copy pathMain.java
File metadata and controls
111 lines (87 loc) · 4.74 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package modern.challenge;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args)
throws ClassNotFoundException, NoSuchFieldException {
Pair pair = new Pair(1, 1);
System.out.println("Get the name of the Pair class via an instance:");
Class<?> clazz = pair.getClass();
// Class<Pair> clazz = Pair.class;
// Class<?> clazz = Class.forName("modern.challenge.Pair");
System.out.println("Name: " + clazz.getName());
System.out.println("Simple name: " + clazz.getSimpleName());
System.out.println("Canonical name: " + clazz.getCanonicalName());
System.out.println("\nGet the Pair class modifiers:");
int modifiers = clazz.getModifiers();
System.out.println("Is public? " + Modifier.isPublic(modifiers));
System.out.println("Is final? " + Modifier.isFinal(modifiers));
System.out.println("Is abstract? " + Modifier.isAbstract(modifiers));
System.out.println("\nGet the Pair class implemented interfaces:");
Class<?>[] interfaces = clazz.getInterfaces();
System.out.println("Interfaces: " + Arrays.toString(interfaces));
System.out.println("Interface simple name: " + interfaces[0].getSimpleName());
System.out.println("\nGet the Pair class constructors:");
Constructor<?>[] constructors = clazz.getConstructors();
System.out.println("Constructors: " + Arrays.toString(constructors));
System.out.println("\nGet the Pair class fields:");
Field[] fields = clazz.getDeclaredFields();
List<String> fieldsName = getFieldNames(fields);
System.out.println("Fields: " + Arrays.toString(fields));
System.out.println("Fields names: " + fieldsName);
System.out.println("\nGet the Pair class methods:");
Method[] methods = clazz.getMethods();
List<String> methodsName = getMethodNames(methods);
System.out.println("Methods: " + Arrays.toString(methods));
System.out.println("Methods names: " + methodsName);
System.out.println("\nGet the Pair class module:");
Module module = clazz.getModule();
System.out.println("Module: " + module.getName());
System.out.println("\nGet the Pair super-class:");
Class<?> superClass = clazz.getSuperclass();
System.out.println("Super-class: " + superClass.getName());
System.out.println("\nGet type name:");
System.out.println("Primitive type name: " + int.class.getTypeName());
System.out.println("Pair type name: " + clazz.getTypeName());
System.out.println("Inner class type name: " + Pair.Entry.class.getTypeName());
Thread thread = new Thread() {
public void run() {
System.out.println("Child Thread");
}
};
System.out.println("Anonymous class type name: " + thread.getClass().getTypeName());
Pair[] pairs = new Pair[10];
System.out.println("Array type name: " + pairs.getClass().getTypeName());
System.out.println("\nGet a string that describes the class:");
System.out.println("Description of Pair: " + clazz.toGenericString());
System.out.println("Description of Runnable: " + Runnable.class.toGenericString());
System.out.println("Description of Map: " + Map.class.toGenericString());
System.out.println("\nGet the type descriptor string for a class:");
System.out.println("Type descriptor of Pair: " + clazz.descriptorString());
System.out.println("Type descriptor of String: " + String.class.descriptorString());
System.out.println("\nGet the component type of an array:");
System.out.println("Component type of Pair[]: " + pairs.getClass().componentType());
String[] strings = new String[]{"1", "2", "3"};
System.out.println("Component type of String[]: " + strings.getClass().componentType());
System.out.println("\nGet a Class for an array type whose component type is described by Pair:");
Class<?> arrayClazz = clazz.arrayType();
System.out.println("Array type: " + arrayClazz.toGenericString());
}
private static List<String> getFieldNames(Field[] fields) {
return Arrays.stream(fields)
.map(Field::getName)
.collect(Collectors.toList());
}
private static List<String> getMethodNames(Method[] methods) {
return Arrays.stream(methods)
.map(Method::getName)
.collect(Collectors.toList());
}
}