Remove not used SingletonManager Now we fully relay on the Guice bindings and injections. Therefore we can safely remove `SingletonManager`, `CustomClassLoader` and `ClassUtils`. Change-Id: I0d079135d358975e8fe5c8c049398bb54d2de03b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/singleton/SingletonManager.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/singleton/SingletonManager.java deleted file mode 100644 index 115f353..0000000 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/singleton/SingletonManager.java +++ /dev/null
@@ -1,42 +0,0 @@ -package com.googlesource.gerrit.plugins.chatgpt.data.singleton; - -import java.util.HashMap; -import java.util.Map; - -import static com.googlesource.gerrit.plugins.chatgpt.utils.ClassUtils.createNewInstanceWithArgs; - -public class SingletonManager { - private static final Map<String, Object> instances = new HashMap<>(); - - public static synchronized <T> T getInstance(Class<T> clazz, String key, Object... constructorArgs) { - if (!instances.containsKey(key)) { - return createNewInstance(clazz, key, constructorArgs); - } - return clazz.cast(instances.get(key)); - } - - public static synchronized <T> T getNewInstance(Class<T> clazz, String key, Object... constructorArgs) { - return createNewInstance(clazz, key, constructorArgs); - } - - public static synchronized void removeInstance(Class<?> clazz, String key) { - instances.remove(key); - } - - private static synchronized <T> T createNewInstance(Class<T> clazz, String key, Object... constructorArgs) { - try { - // Use reflection to invoke constructor with arguments - T instance; - if (constructorArgs == null || constructorArgs.length == 0) { - instance = clazz.getDeclaredConstructor().newInstance(); - } else { - instance = createNewInstanceWithArgs(clazz, constructorArgs); - } - instances.put(key, instance); - return instance; - } catch (Exception e) { - throw new RuntimeException("Error creating instance for class: " + clazz.getName(), e); - } - } - -}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/ClassUtils.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/ClassUtils.java deleted file mode 100644 index 712ec54..0000000 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/ClassUtils.java +++ /dev/null
@@ -1,29 +0,0 @@ -package com.googlesource.gerrit.plugins.chatgpt.utils; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -public class ClassUtils { - public static synchronized <T> T createNewInstanceWithArgs(Class<T> clazz, Object... constructorArgs) { - Class<?>[] argClasses = new Class[constructorArgs.length]; - for (int i = 0; i < constructorArgs.length; i++) { - argClasses[i] = constructorArgs[i].getClass(); - } - try { - return clazz.getDeclaredConstructor(argClasses).newInstance(constructorArgs); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public static void registerDynamicClasses(Class<?>... classes) { - for (Class<?> clazz : classes) { - assert clazz != null; // This assertion is always true; serves to silence "unused" warnings - } - } - - public static String joinUsingDotNotation(List<String> components) { - return String.join(".", components); - } - -}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/CustomClassLoader.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/CustomClassLoader.java deleted file mode 100644 index 0b0b272..0000000 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/CustomClassLoader.java +++ /dev/null
@@ -1,43 +0,0 @@ -package com.googlesource.gerrit.plugins.chatgpt.utils; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; - -public class CustomClassLoader extends ClassLoader { - @Override - protected Class<?> findClass(String name) { - try { - // Attempt to load the class using the default class loader - return Class.forName(name); - } catch (ClassNotFoundException e) { - return loadLocalClass(name); - } - } - - private Class<?> loadLocalClass(String name) { - try { - byte[] b = loadClassData(name); - return defineClass(name, b, 0, b.length); - } catch (IOException e) { - throw new RuntimeException("Failed to load local class " + name, e); - } - } - - private byte[] loadClassData(String name) throws IOException { - ByteArrayOutputStream byteSt; - try (InputStream is = getClass().getClassLoader().getResourceAsStream( - name.replace('.', '/') + ".class")) { - byteSt = new ByteArrayOutputStream(); - int len; - while ((len = is.read()) != -1) { - byteSt.write(len); - } - } - catch (NullPointerException e) { - throw new RuntimeException("Failed to load class data for class " + name, e); - } - return byteSt.toByteArray(); - } - -}