langtools/test/tools/javac/T6406771.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
child 36526 3b41f1c69604
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
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.
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
import javax.lang.model.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
import com.sun.tools.javac.api.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
4935
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
    23
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
@SupportedAnnotationTypes("*")
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
public class T6406771 extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
    String[] tests = {
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    27
        "line:27",
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
        "line:28",
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
        "line:29", "line:29",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
//       1         2         3         4         5         6
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
//3456789012345678901234567890123456789012345678901234567890
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
      "col:7", "col:16", "col:26",                 // this line uses spaces
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
        "col:9",        "col:25",       "col:41",  // this line uses tabs
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
                   "col:20",              "col:43" // this line uses a mixture
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    // White-space after this point does not matter
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    39
    public static void main(String[] args) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
        String self = T6406771.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
        String testSrc = System.getProperty("test.src");
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
        String testClasses = System.getProperty("test.classes");
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        JavacTool tool = JavacTool.create();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    45
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    46
            JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    48
            List<String> opts = Arrays.asList("-d", ".", "-processorpath", testClasses, "-processor", self, "-proc:only");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    50
            JavacTask task = tool.getTask(null, fm, null, opts, null, Arrays.asList(f));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    52
            if (!task.call())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    53
                throw new AssertionError("failed");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    54
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        final String LINE = "line" + ':';   // avoid matching this string
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        final String COLUMN  = "col" + ':';
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        final Messager messager = processingEnv.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        final Trees trees = Trees.instance(processingEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        TreeScanner<Void,LineMap> s = new  TreeScanner<Void,LineMap>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            public Void visitLiteral(LiteralTree tree, LineMap lineMap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
                if (tree.getKind() == Tree.Kind.STRING_LITERAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                    String s = (String) tree.getValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                    int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                    String prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                    long found;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                    if (s.startsWith(LINE)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                        prefix = LINE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                        found = lineMap.getLineNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
                    else if (s.startsWith(COLUMN)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                        prefix = COLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                        found = lineMap.getColumnNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                    int expect = Integer.parseInt(s.substring(prefix.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                    if (expect != found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                        messager.printMessage(Diagnostic.Kind.ERROR,
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                                              "Error: " + prefix + " pos=" + pos
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                                              + " expect=" + expect + " found=" + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        for (Element e: rEnv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            Tree t = trees.getTree(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            TreePath p = trees.getPath(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap());
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
4935
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   102
    @Override
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   103
    public SourceVersion getSupportedSourceVersion() {
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   104
        return SourceVersion.latest();
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   105
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
}