From e2ccefee607dd554babeb78b78c059f80cc1c008 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Fri, 16 Feb 2024 14:05:42 +0100 Subject: [PATCH] feat: use Editable when possible instead of reflection to infer builder --- .../client/utils/KubernetesResourceUtil.java | 4 --- .../client/impl/ResourceHandlerImpl.java | 27 ++++++++++--------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/KubernetesResourceUtil.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/KubernetesResourceUtil.java index f42393714a5..81cd0159496 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/KubernetesResourceUtil.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/KubernetesResourceUtil.java @@ -480,10 +480,6 @@ public static Class in return (Class) loadRelated(type, "List", DefaultKubernetesResourceList.class); } - public static > Class inferBuilderType(Class type) { - return (Class) loadRelated(type, "Builder", null); - } - private static Class loadRelated(Class type, String suffix, Class defaultClass) { try { return Thread.currentThread().getContextClassLoader().loadClass(type.getName() + suffix); diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/ResourceHandlerImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/ResourceHandlerImpl.java index 1b8b9aaff93..2988a7f0b5e 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/ResourceHandlerImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/ResourceHandlerImpl.java @@ -15,6 +15,8 @@ */ package io.fabric8.kubernetes.client.impl; +import com.fasterxml.jackson.databind.util.ClassUtil; +import io.fabric8.kubernetes.api.builder.Editable; import io.fabric8.kubernetes.api.builder.VisitableBuilder; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesResourceList; @@ -34,35 +36,34 @@ class ResourceHandlerImpl type; - private final Class builderClass; private final Class> defaultListClass; private final Function>> operationConstructor; ResourceHandlerImpl(Class type, Function>> operationConstructor) { - this.type = type; - this.context = ResourceDefinitionContext.fromResourceType(type); - this.builderClass = KubernetesResourceUtil.inferBuilderType(type); - this.defaultListClass = (Class>) KubernetesResourceUtil.inferListType(type); - this.operationConstructor = operationConstructor; + this(type, (Class>) KubernetesResourceUtil.inferListType(type), ResourceDefinitionContext.fromResourceType(type), operationConstructor); } ResourceHandlerImpl(Class type, Class> listClass, ResourceDefinitionContext context) { + this(type, listClass, context, null); + } + + private ResourceHandlerImpl(Class type, Class> listClass, ResourceDefinitionContext context, Function>> operationConstructor) { this.type = type; this.context = context; this.defaultListClass = listClass; - this.builderClass = KubernetesResourceUtil.inferBuilderType(type); - this.operationConstructor = null; + this.operationConstructor = operationConstructor; } + @Override public V edit(T item) { - if (this.builderClass == null) { - throw new KubernetesClientException(String.format("Cannot edit %s with visitors, no builder was found", type.getName())); + if (!(item instanceof Editable)) { + throw new KubernetesClientException(String.format("Cannot edit %s with visitors as it is not Editable", type.getName())); } try { - return this.builderClass.getDeclaredConstructor(item.getClass()).newInstance(item); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { + final Editable editable = (Editable) item; + return (V) editable.edit(); + } catch (ClassCastException e) { throw KubernetesClientException.launderThrowable(e); } }