Skip to content

Commit

Permalink
Dev UI - minor improvements
Browse files Browse the repository at this point in the history
- throw an exception instead of NOT_FOUND, resolves #14872
- sort beans and observers, application components go first
- show shortened package where appropriate
  • Loading branch information
mkouba committed Feb 25, 2021
1 parent 3aeb74e commit 745d1fd
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.quarkus.deployment.dev.console;

import java.util.Objects;

/**
* This class is used to represent a class in dev console templates.
*/
public class ClassName implements Comparable<ClassName> {

private final String name;

public ClassName(String name) {
this.name = Objects.requireNonNull(name);
}

public String getName() {
return name;
}

public String getLocalName() {
int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
return name.substring(lastDot + 1);
}
return name;
}

public String getPackageName() {
int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
return name.substring(0, lastDot);
}
return "";
}

public String getShortenedPackageName() {
return shorten(getPackageName());
}

public String getShortenedName() {
return shorten(name);
}

@Override
public int compareTo(ClassName other) {
return name.compareTo(other.name);
}

@Override
public String toString() {
return name;
}

private String shorten(String value) {
String[] parts = value.split("\\.");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (i == parts.length - 1) {
builder.append(parts[i]);
} else {
builder.append(parts[i].charAt(0));
builder.append('.');
}
}
return builder.toString();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
package io.quarkus.arc.deployment.devconsole;

import java.util.ArrayList;
import java.util.List;

public class DevBeanInfo implements Comparable<DevBeanInfo> {

private ClassName name;
private String methodName;
private DevBeanKind kind;
private ClassName type;
private List<ClassName> qualifiers = new ArrayList<>();
private ClassName scope;
import io.quarkus.deployment.dev.console.ClassName;

public DevBeanInfo() {
}
public class DevBeanInfo implements Comparable<DevBeanInfo> {

public DevBeanInfo(ClassName name, String methodName, ClassName type, List<ClassName> qualifiers, ClassName scope,
DevBeanKind kind) {
this.name = name;
this.methodName = methodName;
private final boolean isApplicationBean;
private final ClassName declaringClassName;
private final String memberName;
private final DevBeanKind kind;
// TODO this is more a raw type, i.e. we don't handle arrays, parameterized types, etc.
private final ClassName type;
private final List<ClassName> qualifiers;
private final ClassName scope;

public DevBeanInfo(ClassName declaringClassName, String methodName, ClassName type, List<ClassName> qualifiers,
ClassName scope,
DevBeanKind kind, boolean isApplicationBean) {
this.declaringClassName = declaringClassName;
this.memberName = methodName;
this.type = type;
this.qualifiers = qualifiers;
this.scope = scope;
this.kind = kind;
}

public void setKind(DevBeanKind kind) {
this.kind = kind;
this.isApplicationBean = isApplicationBean;
}

public DevBeanKind getKind() {
return kind;
}

public void setScope(ClassName scope) {
this.scope = scope;
}

public ClassName getScope() {
return scope;
}
Expand All @@ -45,36 +39,28 @@ public List<ClassName> getQualifiers() {
return qualifiers;
}

public void setQualifiers(List<ClassName> qualifiers) {
this.qualifiers = qualifiers;
}

public void setType(ClassName type) {
this.type = type;
}

public ClassName getType() {
return type;
}

public String getMethodName() {
return methodName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
public String getMemberName() {
return memberName;
}

public void setName(ClassName name) {
this.name = name;
public ClassName getDeclaringClassName() {
return declaringClassName;
}

public ClassName getName() {
return name;
public boolean isApplicationBean() {
return isApplicationBean;
}

@Override
public int compareTo(DevBeanInfo o) {
return type.getLocalName().compareTo(o.type.getLocalName());
// Application beans should go first
if (isApplicationBean == o.isApplicationBean) {
return type.compareTo(o.type);
}
return isApplicationBean ? -1 : 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.ArcConfig;
import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem;
import io.quarkus.arc.deployment.CompletedApplicationClassPredicateBuildItem;
import io.quarkus.arc.deployment.CustomScopeAnnotationsBuildItem;
import io.quarkus.arc.deployment.ValidationPhaseBuildItem;
import io.quarkus.arc.processor.AnnotationsTransformer;
Expand All @@ -38,6 +39,7 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.dev.console.ClassName;
import io.quarkus.devconsole.spi.DevConsoleRuntimeTemplateInfoBuildItem;
import io.quarkus.devconsole.spi.DevConsoleTemplateInfoBuildItem;

Expand Down Expand Up @@ -94,22 +96,24 @@ public void transform(TransformationContext transformationContext) {
}

@BuildStep(onlyIf = IsDevelopment.class)
public DevConsoleTemplateInfoBuildItem collectBeanInfo(ValidationPhaseBuildItem validationPhaseBuildItem) {
public DevConsoleTemplateInfoBuildItem collectBeanInfo(ValidationPhaseBuildItem validationPhaseBuildItem,
CompletedApplicationClassPredicateBuildItem predicate) {
BeanDeploymentValidator.ValidationContext validationContext = validationPhaseBuildItem.getContext();
DevBeanInfos beanInfos = new DevBeanInfos();
for (BeanInfo beanInfo : validationContext.beans()) {
beanInfos.addBean(createBeanInfo(beanInfo));
beanInfos.addBean(createBeanInfo(beanInfo, predicate));
}
for (BeanInfo beanInfo : validationContext.removedBeans()) {
beanInfos.addRemovedBean(createBeanInfo(beanInfo));
beanInfos.addRemovedBean(createBeanInfo(beanInfo, predicate));
}
for (ObserverInfo observerInfo : validationContext.get(BuildExtension.Key.OBSERVERS)) {
beanInfos.addObserver(createDevInfo(observerInfo));
beanInfos.addObserver(createObserverInfo(observerInfo, predicate));
}
beanInfos.sort();
return new DevConsoleTemplateInfoBuildItem("devBeanInfos", beanInfos);
}

private DevBeanInfo createBeanInfo(BeanInfo beanInfo) {
private DevBeanInfo createBeanInfo(BeanInfo beanInfo, CompletedApplicationClassPredicateBuildItem predicate) {
List<ClassName> qualifiers = new ArrayList<>();
for (AnnotationInstance qualAnnotation : beanInfo.getQualifiers()) {
qualifiers.add(new ClassName(qualAnnotation.name().toString()));
Expand All @@ -118,48 +122,55 @@ private DevBeanInfo createBeanInfo(BeanInfo beanInfo) {
Optional<AnnotationTarget> target = beanInfo.getTarget();
if (target.isPresent()) {
AnnotationTarget annotated = target.get();
String methodName = null;
DotName name = null;
String memberName = null;
ClassName type = null;
ClassName providerClass = null;
DevBeanKind kind = null;
if (annotated.kind() == Kind.METHOD) {
MethodInfo method = annotated.asMethod();
methodName = method.name();
type = new ClassName(method.returnType().toString());
memberName = method.name();
name = method.returnType().name();
type = new ClassName(name.toString());
providerClass = new ClassName(method.declaringClass().toString());
kind = DevBeanKind.METHOD;
} else if (annotated.kind() == Kind.FIELD) {
FieldInfo field = annotated.asField();
methodName = field.name();
type = new ClassName(field.type().toString());
memberName = field.name();
name = field.type().name();
type = new ClassName(name.toString());
providerClass = new ClassName(field.declaringClass().toString());
kind = DevBeanKind.FIELD;
} else if (annotated.kind() == Kind.CLASS) {
ClassInfo klass = annotated.asClass();
name = klass.name();
type = new ClassName(klass.name().toString());
kind = DevBeanKind.CLASS;
}
return new DevBeanInfo(providerClass, methodName, type, qualifiers, scope, kind);
return new DevBeanInfo(providerClass, memberName, type, qualifiers, scope, kind,
target.isPresent() ? predicate.test(name) : false);
} else {
return new DevBeanInfo(null, null, new ClassName(beanInfo.getBeanClass().toString()),
qualifiers,
scope, DevBeanKind.SYNTHETIC);
scope, DevBeanKind.SYNTHETIC, false);
}
}

private DevObserverInfo createDevInfo(ObserverInfo observer) {
private DevObserverInfo createObserverInfo(ObserverInfo observer, CompletedApplicationClassPredicateBuildItem predicate) {
List<ClassName> qualifiers = new ArrayList<>();
ClassName name = null;
ClassName declaringClassName = null;
String methodName = null;
for (AnnotationInstance qualAnnotation : observer.getQualifiers()) {
qualifiers.add(new ClassName(qualAnnotation.name().toString()));
}
if (observer.getDeclaringBean() != null) {
name = new ClassName(observer.getObserverMethod().declaringClass().name().toString());
declaringClassName = new ClassName(observer.getObserverMethod().declaringClass().name().toString());
methodName = observer.getObserverMethod().name();
}
return new DevObserverInfo(name, methodName, observer.getObservedType().toString(), qualifiers, observer.getPriority(),
observer.isAsync(), observer.getReception(), observer.getTransactionPhase());
return new DevObserverInfo(declaringClassName, methodName, observer.getObservedType().toString(), qualifiers,
observer.getPriority(),
observer.isAsync(), observer.getReception(), observer.getTransactionPhase(),
declaringClassName != null ? predicate.test(observer.getObserverMethod().declaringClass().name()) : false);
}

private boolean isAdditionalBeanDefiningAnnotationOn(ClassInfo beanClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import javax.enterprise.event.Reception;
import javax.enterprise.event.TransactionPhase;

import io.quarkus.deployment.dev.console.ClassName;

public class DevObserverInfo implements Comparable<DevObserverInfo> {

private final ClassName name;
private final boolean isApplicationObserver;
private final ClassName declaringClassName;
private final String methodName;
private final String observedType;
private List<ClassName> qualifiers;
Expand All @@ -16,20 +19,22 @@ public class DevObserverInfo implements Comparable<DevObserverInfo> {
private final Reception reception;
private final TransactionPhase transactionPhase;

public DevObserverInfo(ClassName name, String methodInfo, String observedType, List<ClassName> qualifiers, int priority,
boolean isAsync, Reception reception, TransactionPhase transactionPhase) {
this.name = name;
public DevObserverInfo(ClassName declaringClassName, String methodInfo, String observedType, List<ClassName> qualifiers,
int priority,
boolean isAsync, Reception reception, TransactionPhase transactionPhase, boolean isApplicationObserver) {
this.declaringClassName = declaringClassName;
this.methodName = methodInfo;
this.observedType = observedType;
this.qualifiers = qualifiers;
this.priority = priority;
this.isAsync = isAsync;
this.reception = reception;
this.transactionPhase = transactionPhase;
this.isApplicationObserver = isApplicationObserver;
}

public ClassName getName() {
return name;
public ClassName getDeclaringClassName() {
return declaringClassName;
}

public String getMethodName() {
Expand Down Expand Up @@ -60,14 +65,22 @@ public TransactionPhase getTransactionPhase() {
return transactionPhase;
}

public boolean isApplicationObserver() {
return isApplicationObserver;
}

@Override
public int compareTo(DevObserverInfo o) {
if (name == null && o.name != null) {
return -1;
} else if (name != null && o.name == null) {
return 1;
// Application beans should go first
if (isApplicationObserver == o.isApplicationObserver) {
if (declaringClassName == null && o.declaringClassName != null) {
return -1;
} else if (declaringClassName != null && o.declaringClassName == null) {
return 1;
}
int ret = declaringClassName.compareTo(o.declaringClassName);
return ret == 0 ? methodName.compareTo(o.methodName) : ret;
}
int ret = name.getLocalName().compareTo(o.name.getLocalName());
return ret == 0 ? methodName.compareTo(o.methodName) : ret;
return isApplicationObserver ? -1 : 1;
}
}
Loading

0 comments on commit 745d1fd

Please sign in to comment.