Skip to content

Commit

Permalink
Fix config model initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Apr 24, 2023
1 parent db6bcb4 commit b3a1a39
Showing 1 changed file with 18 additions and 13 deletions.
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation. All rights reserved.
* Copyright (c) 2007, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -183,9 +184,8 @@ public String getName() {
*
* @param intf a @Configured interface
* @return List of all @Configured subclasses
* @throws ClassNotFoundException
*/
public synchronized List<ConfigModel> getAllModelsImplementing(Class intf) throws ClassNotFoundException {
public synchronized List<ConfigModel> getAllModelsImplementing(Class<?> intf) throws ClassNotFoundException {
if (implementorsOf.size()==0) {
initXRef();
}
Expand All @@ -195,33 +195,38 @@ public synchronized List<ConfigModel> getAllModelsImplementing(Class intf) throw
/**
* probably a bit slow, calculates all the @Configured interfaces subclassing, useful
* to find all possible subclasses of a type.
*
* @throws ClassNotFoundException
*/
private void initXRef() throws ClassNotFoundException {

// force initialization of all the config models.
for (ServiceHandle<?> i : habitat.getAllServiceHandles(ConfigInjector.class)) {
buildModel((ActiveDescriptor<? extends ConfigInjector>) i.getActiveDescriptor());
}

for (ConfigModel cm : models.values()) {
Class targetType = cm.classLoaderHolder.loadClass(cm.targetTypeName);
Class<?> targetType = cm.classLoaderHolder.loadClass(cm.targetTypeName);

Set<Class<?>> visited = new HashSet<>();
do {
Class[] intfs = targetType.getInterfaces();
for (Class intf : intfs) {
if (intf.isAnnotationPresent(Configured.class)) {
addXRef(intf, cm);
Deque<Class<?>> interfaces = new ArrayDeque<>(Arrays.asList(targetType.getInterfaces()));
Class<?> intf;
while ((intf = interfaces.poll()) != null) {
if (visited.add(intf)) {
if (intf.isAnnotationPresent(Configured.class)) {
addXRef(intf, cm);
}
interfaces.addAll(Arrays.asList(intf.getInterfaces()));
}
}
targetType = targetType.getSuperclass();
} while (targetType!=null);
} while (targetType != null);
}
}

private void addXRef(Class type, ConfigModel cm) {
private void addXRef(Class<?> type, ConfigModel cm) {
List<ConfigModel> models = implementorsOf.getOne(type);
if (models==null) {
models= new ArrayList<ConfigModel>();
if (models == null) {
models = new ArrayList<>();
implementorsOf.add(type, models);
}
models.add(cm);
Expand Down

0 comments on commit b3a1a39

Please sign in to comment.