Search in sources :

Example 1 with ArgInfo

use of com.twitter.common.args.apt.Configuration.ArgInfo in project commons by twitter.

the class CmdLineProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    try {
        // Collect all live classnames from this round, so that if they lose all annotated fields, we
        // can delete their previous resources.
        liveClassNamesBuilder.addAll(extractClassNames(roundEnv));
        // Then collect all relevant annotated classes.
        Set<? extends Element> parsers = getAnnotatedElements(roundEnv, ArgParser.class);
        @Nullable Set<String> parsedTypes = getParsedTypes(parsers);
        Set<? extends Element> cmdlineArgs = getAnnotatedElements(roundEnv, CmdLine.class);
        Set<? extends Element> positionalArgs = getAnnotatedElements(roundEnv, Positional.class);
        ImmutableSet<? extends Element> invalidArgs = Sets.intersection(cmdlineArgs, positionalArgs).immutableCopy();
        if (!invalidArgs.isEmpty()) {
            error("An Arg cannot be annotated with both @CmdLine and @Positional, found bad Arg " + "fields: %s", invalidArgs);
        }
        for (ArgInfo cmdLineInfo : processAnnotatedArgs(parsedTypes, cmdlineArgs, CmdLine.class)) {
            getBuilder(cmdLineInfo.className).addCmdLineArg(cmdLineInfo);
        }
        for (ArgInfo positionalInfo : processAnnotatedArgs(parsedTypes, positionalArgs, Positional.class)) {
            getBuilder(positionalInfo.className).addPositionalInfo(positionalInfo);
        }
        checkPositionalArgsAreLists(roundEnv);
        processParsers(parsers);
        Set<? extends Element> verifiers = getAnnotatedElements(roundEnv, VerifierFor.class);
        processVerifiers(verifiers);
        if (roundEnv.processingOver()) {
            for (String className : this.liveClassNamesBuilder.build()) {
                FileObject cmdLinePropertiesResource = createCommandLineDb(className);
                Configuration.Builder configBuilder = getBuilder(className);
                // no fields.
                if (configBuilder.isEmpty()) {
                    cmdLinePropertiesResource.delete();
                    continue;
                }
                // Otherwise, write a new copy of the resource.
                Writer writer = null;
                try {
                    writer = cmdLinePropertiesResource.openWriter();
                    configBuilder.build().store(writer, "Generated via apt by " + getClass().getName());
                } catch (IOException e) {
                    throw new RuntimeException("Failed to write Arg resource file for " + className + ":", e);
                } finally {
                    closeQuietly(writer);
                }
            }
        }
    } catch (RuntimeException e) {
        // SUPPRESS CHECKSTYLE IllegalCatch
        // Catch internal errors - when these bubble more useful queued error messages are lost in
        // some javac implementations.
        error("Unexpected error completing annotation processing:\n%s", Throwables.getStackTraceAsString(e));
    }
    return true;
}
Also used : FileObject(javax.tools.FileObject) IOException(java.io.IOException) Nullable(javax.annotation.Nullable) Writer(java.io.Writer) ArgInfo(com.twitter.common.args.apt.Configuration.ArgInfo)

Aggregations

ArgInfo (com.twitter.common.args.apt.Configuration.ArgInfo)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Nullable (javax.annotation.Nullable)1 FileObject (javax.tools.FileObject)1