Skip to content

Commit

Permalink
Merge pull request quarkusio#28814 from mkouba/issue-27851
Browse files Browse the repository at this point in the history
ArC dev mode - add info about direct dependencies into the json
  • Loading branch information
geoand authored Oct 26, 2022
2 parents 25e76cc + e2d69f8 commit 251e113
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,22 @@ public void collectBeanInfo(ValidationPhaseBuildItem validationPhaseBuildItem,
}
}
}

Map<String, List<String>> beanDependenciesMap = new HashMap<>();

for (BeanInfo bean : beans) {
beanInfos.addDependencyGraph(bean.getIdentifier(),
buildDependencyGraph(bean, validationContext, resolver, beanInfos, allInjectionPoints, declaringToProducers,
directDependents));
DependencyGraph dependencyGraph = buildDependencyGraph(bean, validationContext, resolver, beanInfos,
allInjectionPoints, declaringToProducers,
directDependents);
beanInfos.addDependencyGraph(bean.getIdentifier(), dependencyGraph);
// id -> [dep1Id, dep2Id]
beanDependenciesMap.put(bean.getIdentifier(),
dependencyGraph.filterLinks(link -> link.type.equals("directDependency")).nodes.stream()
.map(DevBeanInfo::getId).filter(id -> !id.equals(bean.getIdentifier()))
.collect(Collectors.toList()));
}
// Set the global that could be used at runtime when generating the json payload for /q/arc/beans
DevConsoleManager.setGlobal(BEAN_DEPENDENCIES, beanDependenciesMap);

beanInfos.sort();
templates.produce(new DevConsoleTemplateInfoBuildItem("devBeanInfos", beanInfos));
Expand Down Expand Up @@ -207,6 +218,8 @@ public void handle(Void ignore) {

static final String BEAN_DESCRIPTION = "io.quarkus.arc.beanDescription";
static final String MAX_DEPENDENCY_LEVEL = "io.quarkus.arc.maxDependencyLevel";
public static final String BEAN_DEPENDENCIES = "io.quarkus.arc.beanDependencies";

static final int DEFAULT_MAX_DEPENDENCY_LEVEL = 10;

private boolean isAdditionalBeanDefiningAnnotationOn(ClassInfo beanClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

public class DependencyGraph {

Expand All @@ -17,12 +18,16 @@ public DependencyGraph(Set<DevBeanInfo> nodes, Set<Link> links) {
}

DependencyGraph forLevel(int level) {
return filterLinks(link -> link.level <= level);
}

DependencyGraph filterLinks(Predicate<Link> predicate) {
// Filter out links first
Set<Link> newLinks = new HashSet<>();
Set<DevBeanInfo> newNodes = new HashSet<>();
Set<String> usedIds = new HashSet<>();
for (Link link : links) {
if (link.level <= level) {
if (predicate.test(link)) {
newLinks.add(link);
usedIds.add(link.source);
usedIds.add(link.target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.quarkus.arc.deployment.ArcConfig;
import io.quarkus.arc.deployment.ValidationPhaseBuildItem;
import io.quarkus.arc.deployment.ValidationPhaseBuildItem.ValidationErrorBuildItem;
import io.quarkus.arc.deployment.devconsole.ArcDevConsoleProcessor;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.BuildExtension;
import io.quarkus.arc.processor.DecoratorInfo;
Expand Down Expand Up @@ -71,7 +72,7 @@ void registerRoutes(ArcConfig arcConfig, ArcDevRecorder recorder,
routes.produce(nonApplicationRootPathBuildItem.routeBuilder()
.route(beansPath)
.displayOnNotFoundPage("Active CDI Beans")
.handler(recorder.createBeansHandler()).build());
.handler(recorder.createBeansHandler(ArcDevConsoleProcessor.BEAN_DEPENDENCIES)).build());

routes.produce(nonApplicationRootPathBuildItem.routeBuilder()
.route(removedBeansPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -18,6 +19,7 @@
import io.quarkus.arc.impl.ArcContainerImpl;
import io.quarkus.arc.runtime.devconsole.EventsMonitor;
import io.quarkus.arc.runtime.devconsole.InvocationsMonitor;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devconsole.runtime.spi.DevConsolePostHandler;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.vertx.http.runtime.devmode.Json.JsonArrayBuilder;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void handle(RoutingContext ctx) {
};
}

public Handler<RoutingContext> createBeansHandler() {
public Handler<RoutingContext> createBeansHandler(String beanDependenciesGlobalKey) {
return new Handler<RoutingContext>() {

@Override
Expand Down Expand Up @@ -96,9 +98,11 @@ public void handle(RoutingContext ctx) {
}
}

Map<String, List<String>> beanDependenciesMap = DevConsoleManager.getGlobal(beanDependenciesGlobalKey);

JsonArrayBuilder array = Json.array();
for (InjectableBean<?> injectableBean : beans) {
JsonObjectBuilder bean = Json.object();
JsonObjectBuilder bean = Json.object(true);
bean.put("id", injectableBean.getIdentifier());
bean.put("kind", injectableBean.getKind().toString());
bean.put("generatedClass", injectableBean.getClass().getName());
Expand Down Expand Up @@ -131,6 +135,18 @@ public void handle(RoutingContext ctx) {
if (injectableBean.isDefaultBean()) {
bean.put("isDefault", true);
}

List<String> beanDependencies = Collections.emptyList();
if (beanDependenciesMap != null) {
beanDependencies = beanDependenciesMap.getOrDefault(injectableBean.getIdentifier(),
Collections.emptyList());
}
JsonArrayBuilder dependencies = Json.array();
for (String beanId : beanDependencies) {
dependencies.add(beanId);
}
bean.put("dependencies", dependencies);

array.add(bean);
}
ctx.response().end(array.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static JsonObjectBuilder object(boolean ignoreEmptyBuilders) {

abstract static class JsonBuilder<T> {

protected boolean ignoreEmptyBuilders = false;
protected final boolean ignoreEmptyBuilders;

/**
*
Expand Down

0 comments on commit 251e113

Please sign in to comment.