Skip to content

Commit

Permalink
Merge pull request #1382 from pjonsson/native-image-prep
Browse files Browse the repository at this point in the history
ComponentRegistry: return list-type
  • Loading branch information
nfi authored Dec 27, 2023
2 parents 40ffeb2 + 5401f51 commit 4bb88f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
7 changes: 2 additions & 5 deletions java/se/sics/mspsim/cli/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions java/se/sics/mspsim/cli/MiscCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions java/se/sics/mspsim/util/ComponentRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
package se.sics.mspsim.util;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class ComponentRegistry {

Expand Down Expand Up @@ -111,15 +112,14 @@ public synchronized <T> T getComponent(Class<T> type, String name) {
return null;
}

@SuppressWarnings("unchecked")
public synchronized <T> T[] getAllComponents(Class<T> type, String name) {
public synchronized <T> List<T> getAllComponents(Class<T> type, String name) {
ArrayList<T> 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> T getComponent(Class<T> type) {
Expand All @@ -131,15 +131,14 @@ public synchronized <T> T getComponent(Class<T> type) {
return null;
}

@SuppressWarnings("unchecked")
public synchronized <T> T[] getAllComponents(Class<T> type) {
public synchronized <T> List<T> getAllComponents(Class<T> type) {
ArrayList<T> 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() {
Expand Down

0 comments on commit 4bb88f8

Please sign in to comment.