From 5401f51cb019c05e21f2348ac6b8442898d4fba8 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Wed, 27 Dec 2023 19:16:33 +0100 Subject: [PATCH] ComponentRegistry: return list-type This avoids the casting and prepares for using native-image out of the box with Cooja. --- java/se/sics/mspsim/cli/CommandHandler.java | 7 ++----- java/se/sics/mspsim/cli/MiscCommands.java | 4 ++-- java/se/sics/mspsim/util/ComponentRegistry.java | 11 +++++------ 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/java/se/sics/mspsim/cli/CommandHandler.java b/java/se/sics/mspsim/cli/CommandHandler.java index 20c865c7c..e45b5b40e 100644 --- a/java/se/sics/mspsim/cli/CommandHandler.java +++ b/java/se/sics/mspsim/cli/CommandHandler.java @@ -182,11 +182,8 @@ public void init(String name, ComponentRegistry registry) { @Override public void start() { - Object[] commandBundles = registry.getAllComponents(CommandBundle.class); - if (commandBundles != null) { - for (Object commandBundle : commandBundles) { - ((CommandBundle) commandBundle).setupCommands(registry, this); - } + for (var commandBundle: registry.getAllComponents(CommandBundle.class)) { + commandBundle.setupCommands(registry, this); } } diff --git a/java/se/sics/mspsim/cli/MiscCommands.java b/java/se/sics/mspsim/cli/MiscCommands.java index fda20c065..4856e7692 100644 --- a/java/se/sics/mspsim/cli/MiscCommands.java +++ b/java/se/sics/mspsim/cli/MiscCommands.java @@ -332,8 +332,8 @@ public int executeCommand(CommandContext context) { verbose = false; } if (context.getArgumentCount() == index) { - ServiceComponent[] sc = registry.getAllComponents(ServiceComponent.class); - if (sc.length == 0) { + var sc = registry.getAllComponents(ServiceComponent.class); + if (sc.isEmpty()) { context.out.println("No services found."); } else { for (ServiceComponent service : sc) { diff --git a/java/se/sics/mspsim/util/ComponentRegistry.java b/java/se/sics/mspsim/util/ComponentRegistry.java index a998cfc19..3b6372311 100644 --- a/java/se/sics/mspsim/util/ComponentRegistry.java +++ b/java/se/sics/mspsim/util/ComponentRegistry.java @@ -35,6 +35,7 @@ package se.sics.mspsim.util; import java.io.PrintStream; import java.util.ArrayList; +import java.util.List; public class ComponentRegistry { @@ -111,15 +112,14 @@ public synchronized T getComponent(Class type, String name) { return null; } - @SuppressWarnings("unchecked") - public synchronized T[] getAllComponents(Class type, String name) { + public synchronized List getAllComponents(Class type, String name) { ArrayList list = new ArrayList<>(); for (ComponentEntry entry : components) { if (type.isInstance(entry.component) && name.equals(entry.name)) { list.add(type.cast(entry.component)); } } - return list.toArray((T[]) java.lang.reflect.Array.newInstance(type, list.size())); + return list; } public synchronized T getComponent(Class type) { @@ -131,15 +131,14 @@ public synchronized T getComponent(Class type) { return null; } - @SuppressWarnings("unchecked") - public synchronized T[] getAllComponents(Class type) { + public synchronized List getAllComponents(Class type) { ArrayList list = new ArrayList<>(); for (ComponentEntry entry : components) { if (type.isInstance(entry.component)) { list.add(type.cast(entry.component)); } } - return list.toArray((T[]) java.lang.reflect.Array.newInstance(type, list.size())); + return list; } public void start() {