langtools/test/tools/javac/T6406771.java
author alanb
Fri, 07 Apr 2017 08:08:26 +0000
changeset 44573 245bb4e6f983
parent 40308 274367a99f98
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: jjg Contributed-by: alan.bateman@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * @test  /nodynamiccopyright/
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * @bug 6406771
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 * @summary CompilationUnitTree needs access to a line map
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     5
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     6
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     7
 *          jdk.compiler/com.sun.tools.javac.tree
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
// WARNING: White-space and layout is important in this file, especially tab characters.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    11
// Editing the imports and other leading text may affect the golden text in the tests field.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    12
// Also beware of scripts that auto-expand tabs to spaces.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
44573
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 40308
diff changeset
    14
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 40308
diff changeset
    15
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
import javax.lang.model.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
import javax.tools.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    22
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
import com.sun.source.util.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    25
import com.sun.tools.javac.api.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
@SupportedAnnotationTypes("*")
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
public class T6406771 extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
    String[] tests = {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    31
        "line:31",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    32
        "line:32",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    33
        "line:33", "line:33",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
//       1         2         3         4         5         6
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
//3456789012345678901234567890123456789012345678901234567890
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
      "col:7", "col:16", "col:26",                 // this line uses spaces
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
        "col:9",        "col:25",       "col:41",  // this line uses tabs
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
                   "col:20",              "col:43" // this line uses a mixture
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    // White-space after this point does not matter
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    43
    public static void main(String[] args) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        String self = T6406771.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        String testSrc = System.getProperty("test.src");
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        String testClasses = System.getProperty("test.classes");
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    48
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    49
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    50
            JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    52
            List<String> opts = Arrays.asList(
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37848
diff changeset
    53
                "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37848
diff changeset
    54
                "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    55
                "-XDaccessInternalAPI",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    56
                "-d", ".",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    57
                "-processorpath", testClasses,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    58
                "-processor", self,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    59
                "-proc:only");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    61
            JavacTask task = (JavacTask)tool.getTask(null, fm, null, opts, null, Arrays.asList(f));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    63
            if (!task.call())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    64
                throw new AssertionError("failed");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    65
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        final String LINE = "line" + ':';   // avoid matching this string
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        final String COLUMN  = "col" + ':';
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        final Messager messager = processingEnv.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        final Trees trees = Trees.instance(processingEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        TreeScanner<Void,LineMap> s = new  TreeScanner<Void,LineMap>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            public Void visitLiteral(LiteralTree tree, LineMap lineMap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                if (tree.getKind() == Tree.Kind.STRING_LITERAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                    String s = (String) tree.getValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                    int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                    String prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                    long found;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                    if (s.startsWith(LINE)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                        prefix = LINE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                        found = lineMap.getLineNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                    else if (s.startsWith(COLUMN)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                        prefix = COLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                        found = lineMap.getColumnNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    int expect = Integer.parseInt(s.substring(prefix.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                    if (expect != found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                        messager.printMessage(Diagnostic.Kind.ERROR,
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                                              "Error: " + prefix + " pos=" + pos
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
                                              + " expect=" + expect + " found=" + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        for (Element e: rEnv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            Tree t = trees.getTree(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            TreePath p = trees.getPath(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap());
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
4935
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   113
    @Override
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   114
    public SourceVersion getSupportedSourceVersion() {
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   115
        return SourceVersion.latest();
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   116
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
}