calimero-project / calimero-core

Core library for KNX network access and management
Other
128 stars 65 forks source link

calimero doesn't run on concierge / translator lookup magic #61

Closed sjsf closed 5 years ago

sjsf commented 6 years ago

This is meant to be more a question than a real bug report: I noticed that calimero is currently not running on the concierge OSGi framework implementation. To my understanding this is due to a bug in concierge and therefore I created https://github.com/eclipse/concierge/issues/52 there.

Nevertheless, this made me aware of this very hacky place in calimero:

https://github.com/calimero-project/calimero-core/blob/1a513de9e283db460d33da08f8cee8ec691e2cf9/src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java#L357-L393

And therefore I'd like to ask whether this reflection-based lookup really is necessary for whatever reason or if it just convenient?

If that was just for convenience, then maybe something like this would still be reasonably feasible?

    private static final Map<Integer, MainType> map = Collections.synchronizedMap(new HashMap() {
        {
            Stream.of(//
                    DPTXlator1BitControlled.class, //
                    DPTXlator2ByteFloat.class, //
                    DPTXlator2ByteUnsigned.class, //
                    DPTXlator3BitControlled.class, //
                    DPTXlator4ByteFloat.class, //
                    DPTXlator4ByteSigned.class, //
                    DPTXlator4ByteUnsigned.class, //
                    DPTXlator64BitSigned.class, //
                    DPTXlator8BitEnum.class, //
                    DptXlator8BitSet.class, //
                    DPTXlator8BitSigned.class, //
                    DPTXlator8BitUnsigned.class, //
                    DPTXlatorBoolean.class, //
                    DPTXlatorDate.class, //
                    DPTXlatorDateTime.class, //
                    DptXlatorMeteringValue.class, //
                    DPTXlatorRGB.class, //
                    DPTXlatorSceneControl.class, //
                    DPTXlatorSceneNumber.class, //
                    DPTXlatorString.class, //
                    DPTXlatorTime.class, //
                    DPTXlatorUtf8.class //
            ).forEach(x -> {
                try {
                    Map<String, DPT> dpts = (Map<String, DPT>) x
                            .getDeclaredMethod("getSubTypesStatic", (Class<?>[]) null).invoke(null, (Object[]) null);
                    final String id = dpts.values().iterator().next().getID();
                    final String s = id.substring(0, id.indexOf('.'));
                    final int mainNumber = Integer.parseInt(s);
                    String desc = descriptionFor(x) + " (main number " + mainNumber + ")";
                    put(mainNumber, new MainType(mainNumber, x, desc));
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    // ...
                }
            });
        }
    });

The manual effort of adding another translator would be limited to a single line, the rest would still be figured out automatically. Just a little less magical.

calimero-project commented 5 years ago

Seems like this got addressed in concierge.