Search in sources :

Example 1 with Parser

use of com.twitter.common.args.Parser in project commons by twitter.

the class CmdLineProcessor method processParsers.

private void processParsers(Set<? extends Element> elements) {
    TypeElement parserType = typeElement(Parser.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found an @ArgParser annotation on a non-class %s", element);
        } else {
            TypeElement parser = (TypeElement) element;
            if (!isAssignable(parser, Parser.class)) {
                error("Found an @ArgParser annotation on a non-Parser %s", element);
                return;
            }
            @Nullable String parsedType = getTypeArgument(parser, parserType);
            if (parsedType != null) {
                String parserClassName = getBinaryName(parser);
                getBuilder(parserClassName).addParser(parsedType, getBinaryName(parser));
            }
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Nullable(javax.annotation.Nullable) ArgParser(com.twitter.common.args.ArgParser) Parser(com.twitter.common.args.Parser)

Example 2 with Parser

use of com.twitter.common.args.Parser in project commons by twitter.

the class CmdLineProcessor method getParsedTypes.

@Nullable
private Set<String> getParsedTypes(Set<? extends Element> parsers) {
    if (!isCheckLinkage) {
        return null;
    }
    Iterable<String> parsersFor = Optional.presentInstances(Iterables.transform(parsers, new Function<Element, Optional<String>>() {

        @Override
        public Optional<String> apply(Element parser) {
            TypeMirror parsedType = getTypeArgument(parser.asType(), typeElement(Parser.class));
            if (parsedType == null) {
                error("failed to find a type argument for Parser: %s", parser);
                return Optional.absent();
            }
            // Equals on TypeMirrors doesn't work - so we compare string representations :/
            return Optional.of(typeUtils.erasure(parsedType).toString());
        }
    }));
    parsersFor = Iterables.concat(parsersFor, Iterables.filter(Iterables.transform(this.persistedConfig.get().parserInfo(), new Function<ParserInfo, String>() {

        @Override
        @Nullable
        public String apply(ParserInfo parserInfo) {
            TypeElement typeElement = elementUtils.getTypeElement(parserInfo.parsedType);
            // long as the no Args in this round that are of the type.
            return (typeElement == null) ? null : typeUtils.erasure(typeElement.asType()).toString();
        }
    }), Predicates.notNull()));
    return ImmutableSet.copyOf(parsersFor);
}
Also used : Function(com.google.common.base.Function) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ParserInfo(com.twitter.common.args.apt.Configuration.ParserInfo) ArgParser(com.twitter.common.args.ArgParser) Parser(com.twitter.common.args.Parser) Nullable(javax.annotation.Nullable)

Aggregations

ArgParser (com.twitter.common.args.ArgParser)2 Parser (com.twitter.common.args.Parser)2 Nullable (javax.annotation.Nullable)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 Function (com.google.common.base.Function)1 ParserInfo (com.twitter.common.args.apt.Configuration.ParserInfo)1 TypeMirror (javax.lang.model.type.TypeMirror)1