langtools/test/tools/javac/T6403466.java
author jjg
Wed, 10 Aug 2016 15:47:46 -0700
changeset 40308 274367a99f98
parent 36526 3b41f1c69604
child 41938 8e66bf10fcec
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers 8160489: Multiple -Xpatch lines ignored by javac 8156998: javac should support new option -XinheritRuntimeEnvironment Reviewed-by: jlahoda, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36526
diff changeset
     2
 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4934
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4934
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4934
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6403466
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary javac TaskListener should be informed when annotation processing occurs
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.lang.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.api.JavacTool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
@Wrap
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
@SupportedAnnotationTypes("Wrap")
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
public class T6403466 extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    static final String testSrcDir = System.getProperty("test.src");
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    static final String testClassDir = System.getProperty("test.classes");
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    static final String self = T6403466.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    static PrintWriter out = new PrintWriter(System.err, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static void main(String[] args) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        JavacTool tool = JavacTool.create();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    56
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    57
            Iterable<? extends JavaFileObject> files =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    58
                fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    59
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    60
            Iterable<String> options = Arrays.asList(
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36526
diff changeset
    61
                "--add-exports",
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36526
diff changeset
    62
                    "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED,"
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    63
                    + "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36526
diff changeset
    64
                "--processor-path", testClassDir,
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    65
                "-processor", self,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    66
                "-s", ".",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    67
                "-d", ".");
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    68
            JavacTask task = tool.getTask(out, fm, null, options, null, files);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    70
            VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    71
            task.setTaskListener(vtl);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    73
            if (!task.call())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    74
                throw new AssertionError("compilation failed");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    76
            if (vtl.iter.hasNext() || vtl.errors)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    77
                throw new AssertionError("comparison against golden file failed.");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    78
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
4934
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    82
        if (!rEnv.processingOver()) {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    83
            Filer filer = processingEnv.getFiler();
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    84
            for (TypeElement anno: annos) {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    85
                Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno);
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    86
                System.err.println("anno: " + anno);
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    87
                System.err.println("elts: " + elts);
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    88
                for (TypeElement te: ElementFilter.typesIn(elts)) {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    89
                    try {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    90
                        Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter();
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    91
                        out.write("class " + te.getSimpleName() + "Wrapper { }");
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    92
                        out.close();
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    93
                    } catch (IOException ex) {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    94
                        ex.printStackTrace();
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    95
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                }
4934
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
    97
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
4934
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
   102
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
   103
    @Override
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
   104
    public SourceVersion getSupportedSourceVersion() {
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
   105
        return SourceVersion.latest();
c6a2cf3b6b82 6634138: Source generated in last round not compiled
darcy
parents: 10
diff changeset
   106
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
@Retention(RetentionPolicy.SOURCE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
@Target(ElementType.TYPE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
@interface Wrap {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
class VerifyingTaskListener implements TaskListener {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    VerifyingTaskListener(File ref) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        BufferedReader in = new BufferedReader(new FileReader(ref));
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        String line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        List<String> lines = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        while ((line = in.readLine()) != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            lines.add(line);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        iter = lines.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    public void started(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        check("Started " + toString(e));
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public void finished(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        check("Finished " + toString(e));
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    // similar to TaskEvent.toString(), but just prints basename of the file
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private String toString(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        JavaFileObject file = e.getSourceFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        return "TaskEvent["
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            + e.getKind() + ","
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            + (file == null ? null : new File(file.toUri().getPath()).getName()) + ","
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            // the compilation unit is identified by the file
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            + e.getTypeElement() + "]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private void check(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        System.out.println(s); // write a reference copy of expected output to stdout
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        String ref = iter.hasNext() ? iter.next() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        line++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        if (!s.equals(ref)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            if (ref != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                System.err.println(line + ": expected: " + ref);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            System.err.println(line + ":    found: " + s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            errors = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    Iterator<String> iter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    int line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    boolean errors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
}