Skip to content

Commit

Permalink
Added Dumper classes
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
OxideWaveLength committed Mar 2, 2024
1 parent 09d0790 commit fa5a114
Show file tree
Hide file tree
Showing 16 changed files with 1,092 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<groupId>top.wavelength</groupId>
<artifactId>Java-BetterReflection</artifactId>
<version>1.1</version>
<version>1.2</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static FutureTask<Boolean> isUpToDate() {
* @since 0.4
*/
public static String getVersion() {
return "1.1";
return "1.2";
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package top.wavelength.betterreflection;

import top.wavelength.betterreflection.dumper.TypeDisplayNameFormat;
import top.wavelength.betterreflection.dumper.implementation.MethodDumper;

import java.io.File;
import java.lang.reflect.*;
import java.security.ProtectionDomain;
Expand Down Expand Up @@ -406,7 +409,7 @@ public boolean isAssignableFrom(Class<?> clasz) {
return this.clasz.isAssignableFrom(clasz);
}

public boolean isAssignableFrom(BetterReflectionClass clasz) {
public boolean isAssignableFrom(BetterReflectionClass<?> clasz) {
return isAssignableFrom(clasz.getClasz());
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ public static void setFinal(Field field, boolean setFinal) throws Exception {
* @return the method's header (modifiers name(parameters), e.g. public String
* generateMethodHeader(Method method)
* @since 0.7
* @deprecated see {@link top.wavelength.betterreflection.dumper.implementation.MethodDumper}
*/
@Deprecated
public static String generateMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames) {
StringBuilder parameters = new StringBuilder();
for (Parameter parameter : method.getParameters()) {
Expand All @@ -374,7 +376,9 @@ public static String generateMethodHeader(Method method, boolean includeModifier
* @param includeReturnType whether the method's return type should be dumped
* @param includeParameterNames whether the method's parameter names should be dumped
* @since 0.7 Writes {@link #generateMethodHeader(Method, boolean, boolean, boolean)}} to stout
* @deprecated see {@link top.wavelength.betterreflection.dumper.implementation.MethodDumper}
*/
@Deprecated
public static void dumpMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames) {
System.out.println(generateMethodHeader(method, includeModifiers, includeReturnType, includeParameterNames));
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/top/wavelength/betterreflection/dumper/Dumper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package top.wavelength.betterreflection.dumper;

/**
* The Dumper interface is responsible for dumping the information related to an object of type T.
*
* @param <T> the type of object to be dumped
* @since 1.2
*/
public interface Dumper<T> {
/**
* Dumps the information related to an object of type T.
*
* @param object the object to be dumped
* @return a string containing the dumped information
* @since 1.2
*/
String dump(T object);

/**
* Prints the dumped information of an object.
*
* @param object the object to be dumped
* @since 1.2
*/
default void printDump(T object) {
System.out.println(dump(object));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package top.wavelength.betterreflection.dumper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
* TypeDisplayNameFormat is an enumeration that represents different formats for displaying the names of types.
* It provides methods to get the formatted names of classes, methods, fields, and parameters.
*
* @since 1.2
*/
public enum TypeDisplayNameFormat {

FULL_NAME,
CANONICAL_NAME,
TYPE_NAME,
SIMPLE_NAME,
NONE;

/**
* Returns the name of the given type based on the specified TypeDisplayNameFormat.
*
* @param type The type for which the name will be returned
* @return The formatted name of the type
* @since 1.2
*/
public String getName(Class<?> type) {
switch (this) {
case FULL_NAME:
return type.getName();
case CANONICAL_NAME:
return type.getCanonicalName();
case TYPE_NAME:
return type.getTypeName();
case SIMPLE_NAME:
return type.getSimpleName();
case NONE:
default:
return "";
}
}

/**
* Returns the name of the given method's return type based on the specified TypeDisplayNameFormat.
*
* @param method The Method object for which the return type name will be returned
* @return The formatted name of the return type of the method
* @since 1.2
*/
public String getName(Method method) {
return getName(method.getReturnType());
}

/**
* Returns the name of the given field's type.
*
* @param field The field for which the type name will be returned
* @return The name of the field's type
* @since 1.2
*/
public String getName(Field field) {
return getName(field.getType());
}

/**
* Returns the name of the given parameter's type based on the specified TypeDisplayNameFormat.
*
* @param parameter The parameter for which the type name will be returned
* @return The formatted name of the parameter's type
* @since 1.2
*/
public String getName(Parameter parameter) {
return getName(parameter.getType());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package top.wavelength.betterreflection.dumper.all;

import top.wavelength.betterreflection.BetterReflectionClass;
import top.wavelength.betterreflection.dumper.Dumper;
import top.wavelength.betterreflection.dumper.implementation.ConstructorDumper;

import java.lang.reflect.Constructor;

/**
* The AllConstructorsDumper class is responsible for dumping the constructors of a class.
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
*
* @since 1.2
*/
public class AllConstructorsDumper implements Dumper<BetterReflectionClass<?>> {

private final ConstructorDumper constructorDumper;

/**
* Constructs an AllConstructorsDumper with a specified ConstructorDumper.
*
* @param constructorDumper the ConstructorDumper to be used for dumping.
* @since 1.2
*/
public AllConstructorsDumper(ConstructorDumper constructorDumper) {
this.constructorDumper = constructorDumper;
}

/**
* Default constructor for AllConstructorsDumper.
* Initializes a new ConstructorDumper.
*
* @since 1.2
*/
public AllConstructorsDumper() {
this(new ConstructorDumper());
}

/**
* Dumps all the constructors of a given BetterReflectionClass.
*
* @param clasz the class to be dumped
* @return a string representation of the dumped constructors
* @since 1.2
*/
@Override
public String dump(BetterReflectionClass<?> clasz) {
StringBuilder result = new StringBuilder();
for (Constructor<?> constructor : clasz.getConstructors())
result.append(constructorDumper.dump(constructor)).append("\n");
return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package top.wavelength.betterreflection.dumper.all;

import top.wavelength.betterreflection.BetterReflectionClass;
import top.wavelength.betterreflection.dumper.Dumper;
import top.wavelength.betterreflection.dumper.implementation.FieldDumper;

import java.lang.reflect.Field;

/**
* The AllFieldsDumper class is responsible for dumping the fields of a class.
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
*
* @since 1.2
*/
public class AllFieldsDumper implements Dumper<BetterReflectionClass<?>> {

private final FieldDumper fieldDumper;

/**
* Constructs an AllFieldsDumper with a specified FieldDumper.
*
* @param fieldDumper the FieldDumper to be used for dumping.
* @since 1.2
*/
public AllFieldsDumper(FieldDumper fieldDumper) {
this.fieldDumper = fieldDumper;
}

/**
* Default constructor for AllFieldsDumper.
* Initializes a new FieldDumper.
*
* @since 1.2
*/
public AllFieldsDumper() {
this(new FieldDumper());
}

/**
* Dumps all the fields of a given BetterReflectionClass.
*
* @param clasz the class to be dumped
* @return a string representation of the dumped fields
* @since 1.2
*/
@Override
public String dump(BetterReflectionClass<?> clasz) {
StringBuilder result = new StringBuilder();
for (Field field : clasz.getFields())
result.append(fieldDumper.dump(field)).append("\n");
return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package top.wavelength.betterreflection.dumper.all;

import top.wavelength.betterreflection.BetterReflectionClass;
import top.wavelength.betterreflection.dumper.Dumper;
import top.wavelength.betterreflection.dumper.implementation.MethodDumper;

import java.lang.reflect.Method;

/**
* The AllMethodsDumper class is responsible for dumping the methods of a class.
* It implements the Dumper interface and operates on an object of type {@link BetterReflectionClass}.
*
* @since 1.2
*/
public class AllMethodsDumper implements Dumper<BetterReflectionClass<?>> {

private final MethodDumper methodDumper;

/**
* Constructs an AllMethodsDumper with a specified MethodDumper.
*
* @param methodDumper the MethodDumper to be used for dumping.
* @since 1.2
*/
public AllMethodsDumper(MethodDumper methodDumper) {
this.methodDumper = methodDumper;
}

/**
* Default constructor for AllMethodsDumper.
* Initializes a new MethodDumper.
*
* @since 1.2
*/
public AllMethodsDumper() {
this(new MethodDumper());
}

/**
* Dumps all the methods of a given BetterReflectionClass.
*
* @param clasz the class to be dumped
* @return a string representation of the dumped methods
* @since 1.2
*/
@Override
public String dump(BetterReflectionClass<?> clasz) {
StringBuilder result = new StringBuilder();
for (Method method : clasz.getMethods())
result.append(methodDumper.dump(method)).append("\n");
return result.toString();
}
}
Loading

0 comments on commit fa5a114

Please sign in to comment.