From 63b80bcde8fec5a8c5c33248965f17e1e28a8a05 Mon Sep 17 00:00:00 2001 From: bmalinowsky Date: Fri, 25 Nov 2022 12:37:18 +0100 Subject: [PATCH] Minor refactoring --- .../calimero/dptxlator/TranslatorTypes.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java b/src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java index c41bb876..a64473ff 100644 --- a/src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java +++ b/src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java @@ -51,6 +51,7 @@ import tuwien.auto.calimero.KNXException; import tuwien.auto.calimero.KNXFormatException; import tuwien.auto.calimero.KNXIllegalArgumentException; +import tuwien.auto.calimero.KnxRuntimeException; /** * Maintains available KNX datapoint main numbers and its associated DPT translators, and provides all available, @@ -263,8 +264,7 @@ public DPTXlator createTranslator(final String dptId) throws KNXException throw new KNXFormatException("failed to init translator", dptId); } catch (final NoSuchMethodException e) { - DPTXlator.logger.error("DPT translator is required to have a public constructor(String dptId)"); - throw new KNXException("interface specification error at translator"); + throw new KnxRuntimeException("interface specification error, no public constructor(String dptId)"); } catch (final Exception e) { // for SecurityException, InstantiationException, IllegalAccessException @@ -306,13 +306,7 @@ public final String getDescription() public Map getSubTypes() throws KNXException { try { - @SuppressWarnings("unchecked") - final Map m = (Map) xlator - .getDeclaredMethod("getSubTypesStatic", (Class[]) null).invoke(null, (Object[]) null); - return m; - } - catch (final NoSuchMethodException e) { - throw new KNXException("no method to get subtypes, " + e.getMessage()); + return subTypes(xlator); } catch (final Exception e) { // for SecurityException and IllegalAccessException @@ -387,22 +381,31 @@ private static void addTranslator(final String className) @SuppressWarnings("unchecked") final Class x = (Class) c; - @SuppressWarnings("unchecked") - final Map dpts = (Map) x.getDeclaredMethod("getSubTypesStatic").invoke(null); + final Map dpts = subTypes(x); final String id = dpts.values().iterator().next().getID(); - final String s = id.substring(0, id.indexOf('.')); - final int mainNumber = Integer.parseInt(s); + final int mainNumber = getMainNumber(0, id); final String desc = descriptionFor(x) + " (main number " + mainNumber + ")"; map.put(mainNumber, new MainType(mainNumber, x, desc)); DPTXlator.logger.trace("loaded DPT translator for {}", desc); } - catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { + catch (ReflectiveOperationException | RuntimeException e) { DPTXlator.logger.warn("lookup DPT translator class {}", className, e); } } + private static Map subTypes(final Class x) + throws IllegalAccessException, InvocationTargetException { + try { + @SuppressWarnings("unchecked") + final Map dpts = (Map) x.getDeclaredMethod("getSubTypesStatic").invoke(null); + return dpts; + } + catch (final NoSuchMethodException e) { + throw new KnxRuntimeException("interface specification error, no method getSubTypesStatic"); + } + } + private static String descriptionFor(final Class x) throws IllegalAccessException { try { @@ -634,9 +637,11 @@ public static DPTXlator createTranslator(final String dptId, final byte... data) return t; } - // throws NumberFormatException + // throws NumberFormatException or KNXIllegalArgumentException private static int getMainNumber(final int mainNumber, final String dptId) { + if (mainNumber == 0 && dptId == null) + throw new KNXIllegalArgumentException("no DPT main number nor DPT ID"); return mainNumber != 0 ? mainNumber : Integer.parseInt(dptId.substring(0, dptId.indexOf('.'))); } }