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