Skip to content

Commit 3cb0ee0

Browse files
committed
refactor: do not look up constructor on each call
1 parent 520ba94 commit 3cb0ee0

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/Utils.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,23 +203,33 @@ public static <T> T instantiateAndConfigureIfNeeded(Class<? extends T> targetCla
203203
}
204204

205205
try {
206-
final Constructor<? extends T> constructor = targetClass.getDeclaredConstructor();
207-
constructor.setAccessible(true);
208-
final var instance = constructor.newInstance();
206+
final var instance = getConstructor(targetClass).newInstance();
209207

210208
if (configurator != null) {
211209
configurator.configure(instance);
212210
}
213211

214212
return instance;
215213
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
216-
| NoSuchMethodException e) {
214+
| IllegalStateException e) {
217215
throw new OperatorException("Couldn't instantiate " + expectedType.getSimpleName() + " '"
218-
+ targetClass.getName() + "': you need to provide an accessible no-arg constructor."
216+
+ targetClass.getName() + "'."
219217
+ (context != null ? " Context: " + context : ""), e);
220218
}
221219
}
222220

221+
public static <T> Constructor<T> getConstructor(Class<T> targetClass) {
222+
final Constructor<T> constructor;
223+
try {
224+
constructor = targetClass.getDeclaredConstructor();
225+
} catch (NoSuchMethodException e) {
226+
throw new IllegalStateException(
227+
"Couldn't find a no-arg constructor for " + targetClass.getName(), e);
228+
}
229+
constructor.setAccessible(true);
230+
return constructor;
231+
}
232+
223233
public static <T> T instantiate(Class<? extends T> toInstantiate, Class<T> expectedType,
224234
String context) {
225235
return instantiateAndConfigureIfNeeded(toInstantiate, expectedType, context, null);

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/cache/BoundedItemStore.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator.processing.event.source.cache;
22

3+
import java.lang.reflect.Constructor;
34
import java.lang.reflect.InvocationTargetException;
45
import java.util.Map;
56
import java.util.concurrent.ConcurrentHashMap;
@@ -14,6 +15,7 @@
1415
import io.fabric8.kubernetes.client.KubernetesClient;
1516
import io.fabric8.kubernetes.client.informers.cache.Cache;
1617
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
18+
import io.javaoperatorsdk.operator.api.config.Utils;
1719

1820
public class BoundedItemStore<R extends HasMetadata>
1921
implements ItemStore<R> {
@@ -24,7 +26,7 @@ public class BoundedItemStore<R extends HasMetadata>
2426
private final BoundedCache<String, R> cache;
2527
private final Function<R, String> keyFunction;
2628
private final Map<String, R> existingMinimalResources = new ConcurrentHashMap<>();
27-
private final Class<R> resourceClass;
29+
private final Constructor<R> resourceConstructor;
2830

2931
public BoundedItemStore(BoundedCache<String, R> cache, Class<R> resourceClass,
3032
KubernetesClient client) {
@@ -39,7 +41,7 @@ public BoundedItemStore(BoundedCache<String, R> cache,
3941
this.resourceFetcher = resourceFetcher;
4042
this.cache = cache;
4143
this.keyFunction = keyFunction;
42-
this.resourceClass = resourceClass;
44+
this.resourceConstructor = Utils.getConstructor(resourceClass);
4345
}
4446

4547
@Override
@@ -57,14 +59,15 @@ public synchronized R put(String key, R obj) {
5759

5860
private R createMinimalResource(R obj) {
5961
try {
60-
R minimal = resourceClass.getConstructor().newInstance();
61-
minimal.setMetadata(new ObjectMetaBuilder().build());
62-
minimal.getMetadata().setName(obj.getMetadata().getName());
63-
minimal.getMetadata().setNamespace(obj.getMetadata().getNamespace());
64-
minimal.getMetadata().setResourceVersion(obj.getMetadata().getResourceVersion());
62+
R minimal = resourceConstructor.newInstance();
63+
final var metadata = obj.getMetadata();
64+
minimal.setMetadata(new ObjectMetaBuilder()
65+
.withName(metadata.getName())
66+
.withNamespace(metadata.getNamespace())
67+
.withResourceVersion(metadata.getResourceVersion())
68+
.build());
6569
return minimal;
66-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
67-
| NoSuchMethodException e) {
70+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
6871
throw new IllegalStateException(e);
6972
}
7073
}

0 commit comments

Comments
 (0)