Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed Dec 13, 2022
1 parent fc34b59 commit 63b80bc
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/tuwien/auto/calimero/dptxlator/TranslatorTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -306,13 +306,7 @@ public final String getDescription()
public Map<String, DPT> getSubTypes() throws KNXException
{
try {
@SuppressWarnings("unchecked")
final Map<String, DPT> m = (Map<String, DPT>) 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
Expand Down Expand Up @@ -387,22 +381,31 @@ private static void addTranslator(final String className)

@SuppressWarnings("unchecked")
final Class<? extends DPTXlator> x = (Class<? extends DPTXlator>) c;
@SuppressWarnings("unchecked")
final Map<String, DPT> dpts = (Map<String, DPT>) x.getDeclaredMethod("getSubTypesStatic").invoke(null);
final Map<String, DPT> 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<String, DPT> subTypes(final Class<? extends DPTXlator> x)
throws IllegalAccessException, InvocationTargetException {
try {
@SuppressWarnings("unchecked")
final Map<String, DPT> dpts = (Map<String, DPT>) 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<? extends DPTXlator> x) throws IllegalAccessException
{
try {
Expand Down Expand Up @@ -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('.')));
}
}

0 comments on commit 63b80bc

Please sign in to comment.