Skip to content

Commit

Permalink
Traversing class hierarchy for mixins subcommands parentcommands annot
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlasov committed Oct 8, 2024
1 parent 3b048b1 commit 95b6e6d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
33 changes: 23 additions & 10 deletions cli/src/main/java/org/nasdanika/cli/MixInCapabilityFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import org.nasdanika.capability.CapabilityProvider;
import org.nasdanika.capability.ServiceCapabilityFactory;
import org.nasdanika.common.Adaptable;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.common.Util;

import picocli.CommandLine;

Expand Down Expand Up @@ -51,22 +53,33 @@ protected boolean match(List<CommandLine> commandPath) {
CommandLine parent = commandPath.get(commandPath.size() - 1);
Object userObject = parent.getCommandSpec().userObject();
if (userObject != null) {
MixIns mixInsAnnotation = userObject.getClass().getAnnotation(MixIns.class);
Class<T> mixInType = getMixInType();
if (mixInsAnnotation != null && mixInType != null) {
for (Class<?> at: mixInsAnnotation.value()) {
if (at.isAssignableFrom(mixInType)) {
return true;
if (mixInType != null) {
List<MixIns> mixInsAnnotations = Util.lineage(userObject.getClass())
.stream()
.map(c -> c.getAnnotation(MixIns.class))
.filter(Objects::nonNull)
.toList();
for (MixIns mixInsAnnotation: mixInsAnnotations) {
List<Class<?>> lineage = Util.lineage(mixInType);
for (Class<?> at: mixInsAnnotation.value()) {
for (Class<?> le: lineage) {
if (at.isAssignableFrom(le)) {
return true;
}
}
}
}
}

if (mixInType != null) {
ParentCommands parentCommands = mixInType.getAnnotation(ParentCommands.class);
if (parentCommands != null) {
for (Class<?> pt: parentCommands.value()) {
if (Adaptable.adaptTo(userObject, pt) != null) {
return true;
for (Class<?> le: Util.lineage(mixInType)) {
ParentCommands parentCommands = le.getAnnotation(ParentCommands.class);
if (parentCommands != null) {
for (Class<?> pt: parentCommands.value()) {
if (Adaptable.adaptTo(userObject, pt) != null) {
return true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -217,22 +218,31 @@ protected boolean match(List<CommandLine> parentPath) {
CommandLine parent = parentPath.get(parentPath.size() - 1);
Object userObject = parent.getCommandSpec().userObject();
if (userObject != null) {
SubCommands subCommandsAnnotation = userObject.getClass().getAnnotation(SubCommands.class);
Class<T> commandType = getCommandType();
if (subCommandsAnnotation != null && commandType != null) {
for (Class<?> at: subCommandsAnnotation.value()) {
if (at.isAssignableFrom(commandType)) {
return true;
if (commandType != null) {
List<SubCommands> subCommandsAnnotations = Util.lineage(userObject.getClass())
.stream()
.map(c -> c.getAnnotation(SubCommands.class))
.filter(Objects::nonNull)
.toList();

for (SubCommands subCommandsAnnotation: subCommandsAnnotations) {
for (Class<?> at: subCommandsAnnotation.value()) {
if (at.isAssignableFrom(commandType)) {
return true;
}
}
}
}

if (commandType != null) {
ParentCommands parentCommands = commandType.getAnnotation(ParentCommands.class);
if (parentCommands != null) {
for (Class<?> pt: parentCommands.value()) {
if (Adaptable.adaptTo(userObject, pt) != null) {
return true;
for (Class<?> le: Util.lineage(commandType)) {
ParentCommands parentCommands = le.getAnnotation(ParentCommands.class);
if (parentCommands != null) {
for (Class<?> pt: parentCommands.value()) {
if (Adaptable.adaptTo(userObject, pt) != null) {
return true;
}
}
}
}
Expand Down

0 comments on commit 95b6e6d

Please sign in to comment.