Skip to content

Commit

Permalink
finding list/builder from the type's classloader as well
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jul 18, 2021
1 parent 043acff commit 2fd9243
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ResourceHandlerImpl<T extends HasMetadata, V extends VisitableBuilder<T, V
@Override
public V edit(T item) {
if (this.builderClass == null) {
throw new KubernetesClientException("Cannot edit with visitors, no builder was found");
throw new KubernetesClientException(String.format("Cannot edit %s with visitors, no builder was found", type.getName()));
}
try {
return this.builderClass.getDeclaredConstructor(item.getClass()).newInstance(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,22 @@ public static Duration getAge(HasMetadata kubernetesResource) {
}

public static <T extends HasMetadata> Class<? extends KubernetesResourceList> inferListType(Class<T> type) {
try {
Class<?> listTypeClass = Thread.currentThread().getContextClassLoader().loadClass(type.getName() + "List");
return (Class<KubernetesResourceList<T>>) listTypeClass;
} catch (ClassNotFoundException | ClassCastException e) {
return CustomResourceList.class;
}
return (Class<? extends KubernetesResourceList>) loadRelated(type, "List", CustomResourceList.class);
}

public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> Class<V> inferBuilderType(Class<T> type) {
return (Class<V>) loadRelated(type, "Builder", null);
}

private static Class<?> loadRelated(Class<?> type, String suffix, Class<?> defaultClass) {
try {
Class<?> builderTypeClass = Thread.currentThread().getContextClassLoader().loadClass(type.getName() + "Builder");
return (Class<V>) builderTypeClass;
return Thread.currentThread().getContextClassLoader().loadClass(type.getName() + suffix);
} catch (ClassNotFoundException | ClassCastException e) {
return null;
try {
return type.getClassLoader().loadClass(type.getName() + suffix);
} catch (ClassNotFoundException | ClassCastException ex) {
return defaultClass;
}
}
}

Expand Down

0 comments on commit 2fd9243

Please sign in to comment.