From 9a2f38242d41f1a6c667165160832f762e4ddcd2 Mon Sep 17 00:00:00 2001 From: Branden Butler Date: Thu, 7 Sep 2023 09:15:59 -0500 Subject: [PATCH] Add comments to ServiceConfig inspection code and change println (#1340) * Fix incorrect duplicate deps checking, regen pom * Add StringUtil docstring and test * Fix incorrect full type creation * Add additional fallback lookup for service configs based on generic superclasses * Fix broken installation * Fix Sphinx, sort of * Add comments to superclass generic introspection code and change sout to log.info --- .../service/config/ServiceConfig.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/myrobotlab/service/config/ServiceConfig.java b/src/main/java/org/myrobotlab/service/config/ServiceConfig.java index 7696e57e2a..233e0c29b8 100755 --- a/src/main/java/org/myrobotlab/service/config/ServiceConfig.java +++ b/src/main/java/org/myrobotlab/service/config/ServiceConfig.java @@ -170,7 +170,7 @@ public ServiceConfig addDefaultGlobalConfig(Plan plan, String key, String global * @param plan * @param key * @param globalName - * @param peer + * @param peerType * @return */ public ServiceConfig addDefaultGlobalConfig(Plan plan, String key, String globalName, String peerType, boolean autoStart) { @@ -215,19 +215,8 @@ public Peer putPeerType(String peerKey, String fullName, String peerType) { } public static Plan getDefault(Plan plan, String name, String inType) { -// if ("Service".equals(inType) || Service.class.getCanonicalName().equals(inType)) { -// ServiceConfig sc = new ServiceConfig(); -// sc.type = inType; -// plan.put(name, sc); -// return plan; -// } try { - // if (type == null) { - // log.error("getDefault(null)"); - // return null; - // } - // FIXME - at some point setting, examining and changing // peer keys to actual names will need to be worky String fullType = getConfigType(inType); @@ -240,12 +229,17 @@ public static Plan getDefault(Plan plan, String name, String inType) { config.getDefault(plan, name); } catch (ClassNotFoundException cnfe) { + // We could not find the config type with the simple {serviceType}Config pattern + // So now we look at its superclasses and try to find a config class in + // the generic type parameters + // FIXME should also perform the simple pattern check on superclasses try { + @SuppressWarnings("rawtypes") Class serviceClass = Class.forName(CodecUtils.makeFullTypeName(inType)).asSubclass(Service.class); Type superClass = serviceClass.getGenericSuperclass(); if (superClass instanceof ParameterizedType) { ParameterizedType genericSuperClass = (ParameterizedType) superClass; - System.out.println("Got generic superclass: " + genericSuperClass + " for service class " + serviceClass); + log.debug("Got generic superclass: " + genericSuperClass + " for service class " + serviceClass); Class configClass = ((Class) genericSuperClass.getActualTypeArguments()[0]).asSubclass(ServiceConfig.class); ServiceConfig newConfig = configClass.getConstructor().newInstance(); newConfig.type = inType; @@ -256,6 +250,10 @@ public static Plan getDefault(Plan plan, String name, String inType) { } catch (NoClassDefFoundError | ClassNotFoundException | NoSuchElementException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException ignored) { + // Many ways for the generic inspection code to fail. NoClassDefFound is thrown when the service isn't installed + // We should probably only attempt to load configs for installed services + // NoSuchElementException is manually thrown when we can't find a generic superclass to inspect + // All others are checked exceptions thrown by the reflection utilities being used log.info("could not find config class for {}, loading generalized ServiceConfig", inType); ServiceConfig sc = new ServiceConfig(); sc.type = inType;