langtools/test/tools/javac/T6406771.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 4935 ff8adaa7bb8e
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
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
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    36
    public static void main(String[] args) throws IOException {
10
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();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    42
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    43
            JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    45
            List<String> opts = Arrays.asList("-d", ".", "-processorpath", testClasses, "-processor", self, "-proc:only");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    47
            JavacTask task = tool.getTask(null, fm, null, opts, null, Arrays.asList(f));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    49
            if (!task.call())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    50
                throw new AssertionError("failed");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 4935
diff changeset
    51
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        final String LINE = "line" + ':';   // avoid matching this string
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        final String COLUMN  = "col" + ':';
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        final Messager messager = processingEnv.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        final Trees trees = Trees.instance(processingEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        TreeScanner<Void,LineMap> s = new  TreeScanner<Void,LineMap>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            public Void visitLiteral(LiteralTree tree, LineMap lineMap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                if (tree.getKind() == Tree.Kind.STRING_LITERAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                    String s = (String) tree.getValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
                    int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
                    String prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                    long found;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                    if (s.startsWith(LINE)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                        prefix = LINE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                        found = lineMap.getLineNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                    else if (s.startsWith(COLUMN)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                        prefix = COLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                        found = lineMap.getColumnNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                    int expect = Integer.parseInt(s.substring(prefix.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                    if (expect != found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                        messager.printMessage(Diagnostic.Kind.ERROR,
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                                              "Error: " + prefix + " pos=" + pos
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                                              + " expect=" + expect + " found=" + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        for (Element e: rEnv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            Tree t = trees.getTree(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            TreePath p = trees.getPath(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap());
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
4935
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
    99
    @Override
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   100
    public SourceVersion getSupportedSourceVersion() {
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   101
        return SourceVersion.latest();
ff8adaa7bb8e 6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents: 10
diff changeset
   102
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
}