-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ClearXs/1.1.x
1.1.x
- Loading branch information
Showing
51 changed files
with
990 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
uno-core/src/main/java/cc/allio/uno/core/reflect/BinaryClassKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cc.allio.uno.core.reflect; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor(staticName = "of") | ||
public class BinaryClassKey { | ||
|
||
private final Class<?> cls1; | ||
private final Class<?> cls2; | ||
} |
57 changes: 57 additions & 0 deletions
57
uno-core/src/main/java/cc/allio/uno/core/reflect/DrawnClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cc.allio.uno.core.reflect; | ||
|
||
import cc.allio.uno.core.util.ObjectUtils; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
/** | ||
* Drawn to class | ||
* | ||
* @author j.x | ||
* @date 2024/4/4 17:59 | ||
* @since 1.1.8 | ||
*/ | ||
public class DrawnClass implements DrawnGeneric<Class<?>> { | ||
|
||
@Override | ||
public ParameterizedFinder drawn(Class<?> reflectType) { | ||
List<ParameterizedType> parameterizedTypes = drawnClass(reflectType); | ||
return new ParameterizedFinder(reflectType, parameterizedTypes); | ||
} | ||
|
||
/** | ||
* 从给定的Class对象中获取{@link ParameterizedType}类型,该方法将会递归查找所有范型父类以及范型接口 | ||
* | ||
* @param reflectType the reflection class | ||
* @return the {@link ParameterizedType} list | ||
*/ | ||
List<ParameterizedType> drawnClass(Class<?> reflectType) { | ||
List<ParameterizedType> types = Lists.newArrayList(); | ||
Type genericSuperclass = reflectType.getGenericSuperclass(); | ||
if (genericSuperclass != null) { | ||
if (genericSuperclass instanceof Class<?> superClass && !Object.class.isAssignableFrom(superClass)) { | ||
List<ParameterizedType> superParameterizedType = drawnClass(superClass); | ||
types.addAll(superParameterizedType); | ||
} | ||
if (genericSuperclass instanceof ParameterizedType parameterizedSuperclass) { | ||
types.add(parameterizedSuperclass); | ||
} | ||
} | ||
Type[] genericInterfaces = reflectType.getGenericInterfaces(); | ||
if (ObjectUtils.isNotEmpty(genericInterfaces)) { | ||
for (Type genericInterface : genericInterfaces) { | ||
if (genericInterface instanceof Class<?> superInterface) { | ||
List<ParameterizedType> superParameterizedType = drawnClass(superInterface); | ||
types.addAll(superParameterizedType); | ||
} | ||
if (genericInterface instanceof ParameterizedType parameterizedType) { | ||
types.add(parameterizedType); | ||
} | ||
} | ||
} | ||
return types; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
uno-core/src/main/java/cc/allio/uno/core/reflect/DrawnField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cc.allio.uno.core.reflect; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Drawn to {@link Field} | ||
* | ||
* @author j.x | ||
* @date 2024/4/4 18:00 | ||
* @since 1.1.8 | ||
*/ | ||
public class DrawnField implements DrawnGeneric<Field> { | ||
|
||
@Override | ||
public ParameterizedFinder drawn(Field reflectType) { | ||
Type genericType = reflectType.getGenericType(); | ||
if (genericType instanceof ParameterizedType parameterizedType) { | ||
return new ParameterizedFinder(reflectType, Lists.newArrayList(parameterizedType)); | ||
} | ||
return new ParameterizedFinder(reflectType, Collections.emptyList()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
uno-core/src/main/java/cc/allio/uno/core/reflect/DrawnGeneric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cc.allio.uno.core.reflect; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
|
||
/** | ||
* drawn reflection type to actual generic type | ||
* | ||
* @author j.x | ||
* @date 2024/4/4 17:58 | ||
* @since 1.1.8 | ||
*/ | ||
public interface DrawnGeneric<T> { | ||
|
||
/** | ||
* returns {@link ParameterizedType} of list base on reflect type. | ||
* | ||
* @param reflectType like as {@link Class}, {@link Method} ... | ||
* @return the {@link ParameterizedType} list | ||
*/ | ||
ParameterizedFinder drawn(T reflectType); | ||
} |
36 changes: 36 additions & 0 deletions
36
uno-core/src/main/java/cc/allio/uno/core/reflect/DrawnMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cc.allio.uno.core.reflect; | ||
|
||
import com.google.common.collect.Streams; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Drawn to method | ||
* | ||
* @author j.x | ||
* @date 2024/4/4 18:00 | ||
* @since 1.1.8 | ||
*/ | ||
public class DrawnMethod implements DrawnGeneric<Method> { | ||
|
||
@Override | ||
public ParameterizedFinder drawn(Method reflectType) { | ||
Type genericReturnType = reflectType.getGenericReturnType(); | ||
Type[] genericExceptionTypes = reflectType.getGenericExceptionTypes(); | ||
Type[] genericParameterTypes = reflectType.getGenericParameterTypes(); | ||
List<ParameterizedType> parameterizedTypes = | ||
Streams.concat( | ||
Stream.of(genericReturnType), | ||
Stream.of(genericExceptionTypes), | ||
Stream.of(genericParameterTypes) | ||
) | ||
.filter(type -> ParameterizedType.class.isAssignableFrom(type.getClass())) | ||
.map(ParameterizedType.class::cast) | ||
.toList(); | ||
return new ParameterizedFinder(reflectType, parameterizedTypes); | ||
} | ||
} |
Oops, something went wrong.