File: TreePosRoundsTest.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (210 lines) | stat: -rw-r--r-- 7,727 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6985205 6986246
 * @summary access to tree positions and doc comments may be lost across annotation processing rounds
 * @modules jdk.compiler
 * @build TreePosRoundsTest
 * @compile -proc:only -processor TreePosRoundsTest TreePosRoundsTest.java
 * @run main TreePosRoundsTest
 */

import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.tools.*;

import com.sun.source.tree.*;
import com.sun.source.util.*;
import javax.tools.JavaCompiler.CompilationTask;

// This test is an annotation processor that performs multiple rounds of
// processing, and on each round, it checks that source positions are
// available and correct.
//
// The test can be run directly as a processor from the javac command line
// or via JSR 199 by invoking the main program.

@SupportedAnnotationTypes("*")
public class TreePosRoundsTest extends AbstractProcessor {
    public static void main(String... args) throws Exception {
        String testSrc = System.getProperty("test.src");
        String testClasses = System.getProperty("test.classes");
        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
        try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
            String thisName = TreePosRoundsTest.class.getName();
            File thisFile = new File(testSrc, thisName + ".java");
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
            List<String> options = Arrays.asList(
                    "-proc:only",
                    "-processor", thisName,
                    "-processorpath", testClasses);
            CompilationTask t = c.getTask(null, fm, null, options, null, files);
            boolean ok = t.call();
            if (!ok)
                throw new Exception("processing failed");
        }
    }

    Filer filer;
    Messager messager;
    Trees trees;

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latest();
    }

    @Override
    public void init(ProcessingEnvironment pEnv) {
        super.init(pEnv);
        filer = pEnv.getFiler();
        messager = pEnv.getMessager();
        trees = Trees.instance(pEnv);
    }

    int round = 0;

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        round++;

        // Scan trees for elements, verifying source tree positions
        for (Element e: roundEnv.getRootElements()) {
            try {
                TreePath p = trees.getPath(e);
                new TestTreeScanner(p.getCompilationUnit(), trees).scan(trees.getPath(e), null);
            } catch (IOException ex) {
                messager.printMessage(Diagnostic.Kind.ERROR,
                        "Cannot get source: " + ex, e);
            }
        }

        final int MAXROUNDS = 3;
        if (round < MAXROUNDS)
            generateSource("Gen" + round);

        return true;
    }

    void generateSource(String name) {
        StringBuilder text = new StringBuilder();
        text.append("class ").append(name).append("{\n");
        text.append("    int one = 1;\n");
        text.append("    int two = 2;\n");
        text.append("    int three = one + two;\n");
        text.append("}\n");

        try {
            JavaFileObject fo = filer.createSourceFile(name);
            Writer out = fo.openWriter();
            try {
                out.write(text.toString());
            } finally {
                out.close();
            }
        } catch (IOException e) {
            throw new Error(e);
        }
    }

    class TestTreeScanner extends TreePathScanner<Void,Void> {
        TestTreeScanner(CompilationUnitTree unit, Trees trees) throws IOException {
            this.unit = unit;
            JavaFileObject sf = unit.getSourceFile();
            source = sf.getCharContent(true).toString();
            sourcePositions = trees.getSourcePositions();
        }

        @Override
        public Void visitVariable(VariableTree tree, Void p) {
            check(getCurrentPath());
            return super.visitVariable(tree, p);
        }

        void check(TreePath tp) {
            Tree tree = tp.getLeaf();

            String expect = tree.toString();
            if (tree.getKind() == Tree.Kind.VARIABLE) {
                // tree.toString() does not know enough context to add ";",
                // so deal with that manually...
                Tree.Kind enclKind = tp.getParentPath().getLeaf().getKind();
                //System.err.println("  encl: " +enclKind);
                if (enclKind == Tree.Kind.CLASS || enclKind == Tree.Kind.BLOCK)
                    expect += ";";
                // t-w-r- adds implicit final: remove it
                if (enclKind == Tree.Kind.TRY && expect.startsWith("final "))
                    expect = expect.substring(6);
            }
            //System.err.println("expect: " + expect);

            int start = (int)sourcePositions.getStartPosition(unit, tree);
            if (start == Diagnostic.NOPOS) {
                messager.printMessage(Diagnostic.Kind.ERROR, "start pos not set for " + trim(tree));
                return;
            }

            int end = (int)sourcePositions.getEndPosition(unit, tree);
            if (end == Diagnostic.NOPOS) {
                messager.printMessage(Diagnostic.Kind.ERROR, "end pos not set for " + trim(tree));
                return;
            }

            String found = source.substring(start, end);
            //System.err.println(" found: " + found);

            // allow for long lines, in which case just compare beginning and
            // end of the strings
            boolean equal;
            if (found.contains("\n")) {
                String head = found.substring(0, found.indexOf("\n"));
                String tail = found.substring(found.lastIndexOf("\n")).trim();
                equal = expect.startsWith(head) && expect.endsWith(tail);
            } else {
                equal = expect.equals(found);
            }

            if (!equal) {
                messager.printMessage(Diagnostic.Kind.ERROR,
                        "unexpected value found: '" + found + "'; expected: '" + expect + "'");
            }
        }

        String trim(Tree tree) {
            final int MAXLEN = 32;
            String s = tree.toString().replaceAll("\\s+", " ").trim();
            return (s.length() < MAXLEN) ? s : s.substring(0, MAXLEN);

        }

        CompilationUnitTree unit;
        SourcePositions sourcePositions;
        String source;
    }

}