Java Code Examples for picocli.CommandLine.Model.CommandSpec#userObject()

The following examples show how to use picocli.CommandLine.Model.CommandSpec#userObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotatedCommandSourceGeneratorProcessor.java    From picocli with Apache License 2.0 6 votes vote down vote up
public List<CommandSpec> commandHierarchies() {
    List<CommandSpec> result = new ArrayList<CommandSpec>();
    for (CommandSpec cmd : commands) {
        String excludeReason = null;
        for (CommandSpec any : commands) {
            if (cmd != any && isNestedCommand(cmd, any)) {
                // TODO Exclude if nested in shared surrounding element
                excludeReason = "Excluding " + cmd + ": it is nested in " + any;
                break;
            }
            if (isBuiltInMixin(cmd) || isBuiltInSubcommand(cmd)) {
                excludeReason = "Excluding built-in " + cmd.userObject();
                break;
            }
        }
        if (excludeReason == null) {
            result.add(cmd);
        } else {
            logger.info(excludeReason);
        }
    }
    return result;
}
 
Example 2
Source File: DynamicProxyConfigGenerator.java    From picocli with Apache License 2.0 6 votes vote down vote up
void visitCommandSpec(CommandSpec spec) {
    Object userObject = spec.userObject();
    if (Proxy.isProxyClass(userObject.getClass())) {
        Class<?>[] interfaces = userObject.getClass().getInterfaces();
        String names = "";
        for (Class<?> interf : interfaces) {
            if (names.length() > 0) { names += ","; }
            names += interf.getCanonicalName(); // TODO or Class.getName()?
        }
        if (names.length() > 0) {
            commandInterfaces.add(names);
        }
    } else if (spec.userObject() instanceof Element && ((Element) spec.userObject()).getKind() == ElementKind.INTERFACE) {
        commandInterfaces.add(((Element) spec.userObject()).asType().toString());
    }
    for (CommandSpec mixin : spec.mixins().values()) {
        visitCommandSpec(mixin);
    }
    for (CommandLine sub : spec.subcommands().values()) {
        visitCommandSpec(sub.getCommandSpec());
    }
}
 
Example 3
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractCommandSpec() throws Exception {
    Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
    Method extractCommandSpec = reflection.getDeclaredMethod("extractCommandSpec", Object.class, CommandLine.IFactory.class, boolean.class);

    CommandLine.IFactory myFactory = new CommandLine.IFactory() {
        public <K> K create(Class<K> cls) {
            throw new InitializationException("boom");
        }
    };

    CommandSpec spec = (CommandSpec) extractCommandSpec.invoke(null, Object.class, myFactory, false);
    try {
        spec.userObject();
        fail("expected Exception");
    } catch (InitializationException ex) {
        //assertEquals("Could not instantiate class java.lang.Object: picocli.CommandLine$InitializationException: boom", ex.getMessage());
        assertEquals("boom", ex.getMessage());
    }
}
 
Example 4
Source File: MixeeTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
private static String qualifiedName(CommandSpec spec) {
    if (spec.userObject() instanceof TypeElement) {
        TypeElement type = (TypeElement) spec.userObject();
        return type.getQualifiedName().toString();
    }
    if (spec.userObject() instanceof ExecutableElement) {
        ExecutableElement method = (ExecutableElement) spec.userObject();
        return method.getEnclosingElement().getSimpleName() + "." + method.getSimpleName().toString();
    }
    return null;
}
 
Example 5
Source File: ManPageGenerator.java    From picocli with Apache License 2.0 5 votes vote down vote up
private static void traceAllSpecs(CommandSpec[] specs, Config config) {
    List<String> all = new ArrayList<String>();
    for (CommandSpec spec: specs) {
        Object obj = spec.userObject();
        if (obj == null) {
            all.add(spec.name() + " (no user object)");
        } else if (obj instanceof Method) {
            all.add(spec.name() + " (" + ((Method) obj).toGenericString() + ")");
        } else {
            all.add(obj.getClass().getName());
        }
    }
    config.verbose("Generating man pages for %s and all subcommands%n", all);
}
 
Example 6
Source File: AnnotatedCommandSourceGenerator.java    From picocli with Apache License 2.0 4 votes vote down vote up
public static boolean isNestedCommand(CommandSpec inner, CommandSpec outer) {
    Object innerUserObject = inner.userObject();
    Object outerUserObject = outer.userObject();
    return isNested(innerUserObject, outerUserObject);
}
 
Example 7
Source File: AnnotatedCommandSourceGenerator.java    From picocli with Apache License 2.0 4 votes vote down vote up
private static boolean isCommandMethod(CommandSpec spec) {
    Object userObject = spec.userObject();
    return userObject instanceof Method || userObject instanceof ExecutableElement;
}
 
Example 8
Source File: ReflectionConfigGenerator.java    From picocli with Apache License 2.0 4 votes vote down vote up
void visitCommandSpec(CommandSpec spec) throws Exception {
    Object userObject = spec.userObject();
    if (userObject != null) {
        if (userObject instanceof Method) {
            Method method = (Method) spec.userObject();
            ReflectedClass cls = getOrCreateClass(method.getDeclaringClass());
            cls.addMethod(method);
        } else if (userObject instanceof Element) {
            visitElement((Element) userObject);
        } else if (Proxy.isProxyClass(spec.userObject().getClass())) {
            // do nothing: requires DynamicProxyConfigGenerator
        } else {
            visitAnnotatedFields(spec.userObject().getClass());
        }
    }
    visitObjectType(spec.versionProvider());
    visitObjectType(spec.defaultValueProvider());

    for (UnmatchedArgsBinding binding : spec.unmatchedArgsBindings()) {
        visitGetter(binding.getter());
        visitSetter(binding.setter());
    }
    for (IAnnotatedElement specElement : spec.specElements()) {
        visitGetter(specElement.getter());
        visitSetter(specElement.setter());
    }
    for (IAnnotatedElement parentCommandElement : spec.parentCommandElements()) {
        visitGetter(parentCommandElement.getter());
        visitSetter(parentCommandElement.setter());
    }
    for (OptionSpec option : spec.options()) {
        visitArgSpec(option);
    }
    for (PositionalParamSpec positional : spec.positionalParameters()) {
        visitArgSpec(positional);
    }
    for (ArgGroupSpec group : spec.argGroups()) {
        visitGroupSpec(group);
    }
    for (Map.Entry<String, CommandSpec> entry : spec.mixins().entrySet()) {
        CommandSpec mixin = entry.getValue();
        visitCommandSpec(mixin);

        String name = entry.getKey();
        IAnnotatedElement annotatedElement = spec.mixinAnnotatedElements().get(name);
        if (annotatedElement != null) {
            visitGetter(annotatedElement.getter());
        }
    }
    for (CommandLine sub : spec.subcommands().values()) {
        visitCommandSpec(sub.getCommandSpec());
    }
}