Skip to content

Commit fa5a114

Browse files
Added Dumper classes
Created a Dumper interface, with the methods #dump(T) and #printDump(T). Implemented the Dumper interface when creating the following classes: * ClassDumper * FieldDumper * ConstructorDumper * MethodDumper * AllFieldsDumper * AllConstructorsDumper * AllMethodsDumper Created the TypeDisplayNameFormat enum, which is used to specify the name formats of return types, parameter types and field types. Written unit tests for the new Dumper classes Fixed missing generic in BetterReflectionClass#isAssignableFrom(BetterReflectionClass) method Deprecated BetterReflectionUtils#dumpMethodHeader() and BetterReflectionUtils#generateMethodHeader()
1 parent 09d0790 commit fa5a114

File tree

16 files changed

+1092
-4
lines changed

16 files changed

+1092
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<groupId>top.wavelength</groupId>
3131
<artifactId>Java-BetterReflection</artifactId>
32-
<version>1.1</version>
32+
<version>1.2</version>
3333

3434
<properties>
3535
<maven.compiler.source>1.8</maven.compiler.source>

src/main/java/top/wavelength/betterreflection/BetterReflection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static FutureTask<Boolean> isUpToDate() {
155155
* @since 0.4
156156
*/
157157
public static String getVersion() {
158-
return "1.1";
158+
return "1.2";
159159
}
160160

161161
/**

src/main/java/top/wavelength/betterreflection/BetterReflectionClass.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package top.wavelength.betterreflection;
22

3+
import top.wavelength.betterreflection.dumper.TypeDisplayNameFormat;
4+
import top.wavelength.betterreflection.dumper.implementation.MethodDumper;
5+
36
import java.io.File;
47
import java.lang.reflect.*;
58
import java.security.ProtectionDomain;
@@ -406,7 +409,7 @@ public boolean isAssignableFrom(Class<?> clasz) {
406409
return this.clasz.isAssignableFrom(clasz);
407410
}
408411

409-
public boolean isAssignableFrom(BetterReflectionClass clasz) {
412+
public boolean isAssignableFrom(BetterReflectionClass<?> clasz) {
410413
return isAssignableFrom(clasz.getClasz());
411414
}
412415

@@ -450,7 +453,7 @@ public void dumpMethodHeaders(boolean includeModifiers, boolean includeReturnTyp
450453
*/
451454
public void dumpMethodHeaders(boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames) {
452455
for (Method method : getDeclaredMethods())
453-
BetterReflectionUtils.dumpMethodHeader(method, includeModifiers, includeReturnType, includeParameterNames);
456+
new MethodDumper().setIncludeModifiers(includeModifiers).setReturnTypeDisplay(includeReturnType ? TypeDisplayNameFormat.FULL_NAME : TypeDisplayNameFormat.NONE).setReturnTypeDisplay(includeParameterNames ? TypeDisplayNameFormat.FULL_NAME : TypeDisplayNameFormat.NONE).dump(method);
454457
}
455458

456459
/**

src/main/java/top/wavelength/betterreflection/BetterReflectionUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ public static void setFinal(Field field, boolean setFinal) throws Exception {
355355
* @return the method's header (modifiers name(parameters), e.g. public String
356356
* generateMethodHeader(Method method)
357357
* @since 0.7
358+
* @deprecated see {@link top.wavelength.betterreflection.dumper.implementation.MethodDumper}
358359
*/
360+
@Deprecated
359361
public static String generateMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames) {
360362
StringBuilder parameters = new StringBuilder();
361363
for (Parameter parameter : method.getParameters()) {
@@ -374,7 +376,9 @@ public static String generateMethodHeader(Method method, boolean includeModifier
374376
* @param includeReturnType whether the method's return type should be dumped
375377
* @param includeParameterNames whether the method's parameter names should be dumped
376378
* @since 0.7 Writes {@link #generateMethodHeader(Method, boolean, boolean, boolean)}} to stout
379+
* @deprecated see {@link top.wavelength.betterreflection.dumper.implementation.MethodDumper}
377380
*/
381+
@Deprecated
378382
public static void dumpMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames) {
379383
System.out.println(generateMethodHeader(method, includeModifiers, includeReturnType, includeParameterNames));
380384
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.wavelength.betterreflection.dumper;
2+
3+
/**
4+
* The Dumper interface is responsible for dumping the information related to an object of type T.
5+
*
6+
* @param <T> the type of object to be dumped
7+
* @since 1.2
8+
*/
9+
public interface Dumper<T> {
10+
/**
11+
* Dumps the information related to an object of type T.
12+
*
13+
* @param object the object to be dumped
14+
* @return a string containing the dumped information
15+
* @since 1.2
16+
*/
17+
String dump(T object);
18+
19+
/**
20+
* Prints the dumped information of an object.
21+
*
22+
* @param object the object to be dumped
23+
* @since 1.2
24+
*/
25+
default void printDump(T object) {
26+
System.out.println(dump(object));
27+
}
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package top.wavelength.betterreflection.dumper;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Parameter;
6+
7+
/**
8+
* TypeDisplayNameFormat is an enumeration that represents different formats for displaying the names of types.
9+
* It provides methods to get the formatted names of classes, methods, fields, and parameters.
10+
*
11+
* @since 1.2
12+
*/
13+
public enum TypeDisplayNameFormat {
14+
15+
FULL_NAME,
16+
CANONICAL_NAME,
17+
TYPE_NAME,
18+
SIMPLE_NAME,
19+
NONE;
20+
21+
/**
22+
* Returns the name of the given type based on the specified TypeDisplayNameFormat.
23+
*
24+
* @param type The type for which the name will be returned
25+
* @return The formatted name of the type
26+
* @since 1.2
27+
*/
28+
public String getName(Class<?> type) {
29+
switch (this) {
30+
case FULL_NAME:
31+
return type.getName();
32+
case CANONICAL_NAME:
33+
return type.getCanonicalName();
34+
case TYPE_NAME:
35+
return type.getTypeName();
36+
case SIMPLE_NAME:
37+
return type.getSimpleName();
38+
case NONE:
39+
default:
40+
return "";
41+
}
42+
}
43+
44+
/**
45+
* Returns the name of the given method's return type based on the specified TypeDisplayNameFormat.
46+
*
47+
* @param method The Method object for which the return type name will be returned
48+
* @return The formatted name of the return type of the method
49+
* @since 1.2
50+
*/
51+
public String getName(Method method) {
52+
return getName(method.getReturnType());
53+
}
54+
55+
/**
56+
* Returns the name of the given field's type.
57+
*
58+
* @param field The field for which the type name will be returned
59+
* @return The name of the field's type
60+
* @since 1.2
61+
*/
62+
public String getName(Field field) {
63+
return getName(field.getType());
64+
}
65+
66+
/**
67+
* Returns the name of the given parameter's type based on the specified TypeDisplayNameFormat.
68+
*
69+
* @param parameter The parameter for which the type name will be returned
70+
* @return The formatted name of the parameter's type
71+
* @since 1.2
72+
*/
73+
public String getName(Parameter parameter) {
74+
return getName(parameter.getType());
75+
}
76+
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package top.wavelength.betterreflection.dumper.all;
2+
3+
import top.wavelength.betterreflection.BetterReflectionClass;
4+
import top.wavelength.betterreflection.dumper.Dumper;
5+
import top.wavelength.betterreflection.dumper.implementation.ConstructorDumper;
6+
7+
import java.lang.reflect.Constructor;
8+
9+
/**
10+
* The AllConstructorsDumper class is responsible for dumping the constructors of a class.
11+
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
12+
*
13+
* @since 1.2
14+
*/
15+
public class AllConstructorsDumper implements Dumper<BetterReflectionClass<?>> {
16+
17+
private final ConstructorDumper constructorDumper;
18+
19+
/**
20+
* Constructs an AllConstructorsDumper with a specified ConstructorDumper.
21+
*
22+
* @param constructorDumper the ConstructorDumper to be used for dumping.
23+
* @since 1.2
24+
*/
25+
public AllConstructorsDumper(ConstructorDumper constructorDumper) {
26+
this.constructorDumper = constructorDumper;
27+
}
28+
29+
/**
30+
* Default constructor for AllConstructorsDumper.
31+
* Initializes a new ConstructorDumper.
32+
*
33+
* @since 1.2
34+
*/
35+
public AllConstructorsDumper() {
36+
this(new ConstructorDumper());
37+
}
38+
39+
/**
40+
* Dumps all the constructors of a given BetterReflectionClass.
41+
*
42+
* @param clasz the class to be dumped
43+
* @return a string representation of the dumped constructors
44+
* @since 1.2
45+
*/
46+
@Override
47+
public String dump(BetterReflectionClass<?> clasz) {
48+
StringBuilder result = new StringBuilder();
49+
for (Constructor<?> constructor : clasz.getConstructors())
50+
result.append(constructorDumper.dump(constructor)).append("\n");
51+
return result.toString();
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package top.wavelength.betterreflection.dumper.all;
2+
3+
import top.wavelength.betterreflection.BetterReflectionClass;
4+
import top.wavelength.betterreflection.dumper.Dumper;
5+
import top.wavelength.betterreflection.dumper.implementation.FieldDumper;
6+
7+
import java.lang.reflect.Field;
8+
9+
/**
10+
* The AllFieldsDumper class is responsible for dumping the fields of a class.
11+
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
12+
*
13+
* @since 1.2
14+
*/
15+
public class AllFieldsDumper implements Dumper<BetterReflectionClass<?>> {
16+
17+
private final FieldDumper fieldDumper;
18+
19+
/**
20+
* Constructs an AllFieldsDumper with a specified FieldDumper.
21+
*
22+
* @param fieldDumper the FieldDumper to be used for dumping.
23+
* @since 1.2
24+
*/
25+
public AllFieldsDumper(FieldDumper fieldDumper) {
26+
this.fieldDumper = fieldDumper;
27+
}
28+
29+
/**
30+
* Default constructor for AllFieldsDumper.
31+
* Initializes a new FieldDumper.
32+
*
33+
* @since 1.2
34+
*/
35+
public AllFieldsDumper() {
36+
this(new FieldDumper());
37+
}
38+
39+
/**
40+
* Dumps all the fields of a given BetterReflectionClass.
41+
*
42+
* @param clasz the class to be dumped
43+
* @return a string representation of the dumped fields
44+
* @since 1.2
45+
*/
46+
@Override
47+
public String dump(BetterReflectionClass<?> clasz) {
48+
StringBuilder result = new StringBuilder();
49+
for (Field field : clasz.getFields())
50+
result.append(fieldDumper.dump(field)).append("\n");
51+
return result.toString();
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package top.wavelength.betterreflection.dumper.all;
2+
3+
import top.wavelength.betterreflection.BetterReflectionClass;
4+
import top.wavelength.betterreflection.dumper.Dumper;
5+
import top.wavelength.betterreflection.dumper.implementation.MethodDumper;
6+
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* The AllMethodsDumper class is responsible for dumping the methods of a class.
11+
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
12+
*
13+
* @since 1.2
14+
*/
15+
public class AllMethodsDumper implements Dumper<BetterReflectionClass<?>> {
16+
17+
private final MethodDumper methodDumper;
18+
19+
/**
20+
* Constructs an AllMethodsDumper with a specified MethodDumper.
21+
*
22+
* @param methodDumper the MethodDumper to be used for dumping.
23+
* @since 1.2
24+
*/
25+
public AllMethodsDumper(MethodDumper methodDumper) {
26+
this.methodDumper = methodDumper;
27+
}
28+
29+
/**
30+
* Default constructor for AllMethodsDumper.
31+
* Initializes a new MethodDumper.
32+
*
33+
* @since 1.2
34+
*/
35+
public AllMethodsDumper() {
36+
this(new MethodDumper());
37+
}
38+
39+
/**
40+
* Dumps all the methods of a given BetterReflectionClass.
41+
*
42+
* @param clasz the class to be dumped
43+
* @return a string representation of the dumped methods
44+
* @since 1.2
45+
*/
46+
@Override
47+
public String dump(BetterReflectionClass<?> clasz) {
48+
StringBuilder result = new StringBuilder();
49+
for (Method method : clasz.getMethods())
50+
result.append(methodDumper.dump(method)).append("\n");
51+
return result.toString();
52+
}
53+
}

0 commit comments

Comments
 (0)