langtools/test/tools/javac/T6358024.java
author jjg
Mon, 16 Jun 2008 13:28:00 -0700
changeset 731 1dd22bdb9ca5
parent 10 06bc494ca11e
child 735 372aa565a221
child 811 687d51cb403b
permissions -rw-r--r--
6714364: refactor javac File handling code into new javac.file package Reviewed-by: mcimadamore

/*
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test
 * @bug 6358024
 * @summary TaskListener should be propogated between processing rounds
 */

import java.io.*;
import java.util.*;
import java.util.List;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.*;
import com.sun.source.util.*;
import com.sun.tools.javac.api.*;
import com.sun.tools.javac.file.*;
import com.sun.tools.javac.main.*;
import com.sun.tools.javac.util.*;


@SupportedAnnotationTypes("*")
public class T6358024 extends AbstractProcessor {
    static JavacFileManager fm;
    public static void main(String... args) throws Throwable {
        String self = T6358024.class.getName();

        String testSrc = System.getProperty("test.src");

        fm = new JavacFileManager(new Context(), false, null);
        JavaFileObject f = fm.getFileForInput(testSrc + File.separatorChar + self + ".java");

        test(fm, f,
             new Option[] { new Option("-d", ".")},
             7);

        test(fm, f,
             new Option[] { new XOption("-XprintRounds"),
                            new Option("-processorpath", "."),
                            new Option("-processor", self) },
             11);
    }

    static void test(JavacFileManager fm, JavaFileObject f, Option[] opts, int expect) throws Throwable {
        PrintWriter out = new PrintWriter(System.err, true);

        JavacTool tool = JavacTool.create();
        List<String> flags = new ArrayList<String>();
        for (Option opt: opts) {
            flags.add(opt.name);
            for (Object arg : opt.args)
                flags.add(arg.toString());
        }

        JavacTaskImpl task = (JavacTaskImpl) tool.getTask(out,
                                                          fm,
                                                          null,
                                                          flags,
                                                          null,
                                                          Arrays.asList(f));
        MyTaskListener tl = new MyTaskListener();
        task.setTaskListener(tl);
        task.call();
        if (tl.started != expect)
            throw new AssertionError("Unexpected number of TaskListener events; "
                                     + "expected " + expect + ", found " + tl.started);
    }

    public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
        return true;
    }

    static class MyTaskListener implements TaskListener {
        public void started(TaskEvent e) {
            System.err.println("Started: " + e);
            started++;
        }
        public void finished(TaskEvent e) {
        }

        int started = 0;
    }

    static class Option {
        Option(String name, String... args) {
            this.name = name;
            this.args = args;
        }
        public final String name;
        public final String[] args;
    }

    static class XOption extends Option {
        XOption(String name, String... args) {
            super(name, args);
        }
    }
}