Skip to content

Commit

Permalink
concept domain 1.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed May 5, 2023
1 parent 3e99c99 commit e9a370d
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,9 @@ public static <T> Class<T> generic(Class<?> target, int index) {
if (type instanceof ParameterizedType) {
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
Type t = types[index];
if (t instanceof Class) {
return (Class<T>) t;
}
if (t instanceof TypeVariable) {
Type[] bounds = ((TypeVariable<?>) t).getBounds();
if (bounds.length > 0) {
Type bound = bounds[0];
if (bound instanceof Class) {
return (Class<T>) bound;
}
}
Class<T> classForType = getClassForType(t);
if (classForType != null) {
return classForType;
}
}
throw new DomainException("Can not get generic type");
Expand All @@ -64,7 +56,8 @@ public static <T extends DomainObject> Class<T> collection(Class<? extends Domai
for (Type type : target.getGenericInterfaces()) {
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
if (pt.getRawType() == DomainCollection.class) {
Class<?> classForType = getClassForType(pt.getRawType());
if (classForType != null && DomainCollection.class.isAssignableFrom(classForType)) {
Type[] arguments = pt.getActualTypeArguments();
return arguments[0];
}
Expand Down Expand Up @@ -92,4 +85,21 @@ public static <T extends DomainObject> Class<T> collection(Class<? extends Domai
}
});
}

public static <T> Class<T> getClassForType(Type type) {
if (type instanceof Class) {
return (Class<T>) type;
}
if (type instanceof ParameterizedType) {
return getClassForType(((ParameterizedType) type).getRawType());
}
if (type instanceof TypeVariable) {
Type[] bounds = ((TypeVariable<?>) type).getBounds();
if (bounds.length > 0) {
Type bound = bounds[0];
return getClassForType(bound);
}
}
return null;
}
}

0 comments on commit e9a370d

Please sign in to comment.