author | jlahoda |
Wed, 27 Nov 2019 09:00:01 +0100 | |
changeset 59285 | 7799a51dbe30 |
permissions | -rw-r--r-- |
59285
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
1 |
/* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
4 |
* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
8 |
* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
14 |
* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
18 |
* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
22 |
*/ |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
23 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
24 |
/* |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
25 |
* @test |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
26 |
* @bug 8231827 |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
27 |
* @summary Check proper positions. |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
28 |
* @build PatternMatchPosTest |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
29 |
* @compile/ref=PatternMatchPosTest.out -processor PatternMatchPosTest -Xlint:unchecked -XDrawDiagnostics --enable-preview -source ${jdk.version} PatternMatchPosTestData.java |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
30 |
*/ |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
31 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
32 |
import java.io.IOException; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
33 |
import java.util.Set; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
34 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
35 |
import javax.annotation.processing.AbstractProcessor; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
36 |
import javax.annotation.processing.RoundEnvironment; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
37 |
import javax.annotation.processing.SupportedAnnotationTypes; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
38 |
import javax.lang.model.SourceVersion; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
39 |
import javax.lang.model.element.TypeElement; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
40 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
41 |
import com.sun.source.tree.IfTree; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
42 |
import com.sun.source.tree.Tree; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
43 |
import com.sun.source.util.SourcePositions; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
44 |
import com.sun.source.util.TreePath; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
45 |
import com.sun.source.util.TreeScanner; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
46 |
import com.sun.source.util.Trees; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
47 |
import javax.tools.Diagnostic; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
48 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
49 |
@SupportedAnnotationTypes("*") |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
50 |
public class PatternMatchPosTest extends AbstractProcessor { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
51 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
52 |
int round; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
53 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
54 |
@Override |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
55 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
56 |
if (round++ != 0) |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
57 |
return false; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
58 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
59 |
try { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
60 |
TypeElement data = processingEnv.getElementUtils().getTypeElement("PatternMatchPosTestData"); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
61 |
Trees trees = Trees.instance(processingEnv); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
62 |
SourcePositions sp = trees.getSourcePositions(); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
63 |
TreePath dataPath = trees.getPath(data); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
64 |
String text = dataPath.getCompilationUnit().getSourceFile().getCharContent(true).toString(); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
65 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
66 |
new TreeScanner<Void, Void>() { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
67 |
boolean print; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
68 |
@Override |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
69 |
public Void visitIf(IfTree node, Void p) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
70 |
boolean prevPrint = print; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
71 |
try { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
72 |
print = true; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
73 |
scan(node.getCondition(), p); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
74 |
} finally { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
75 |
print = prevPrint; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
76 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
77 |
scan(node.getThenStatement(), p); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
78 |
scan(node.getElseStatement(), p); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
79 |
return null; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
80 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
81 |
@Override |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
82 |
public Void scan(Tree tree, Void p) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
83 |
if (tree == null) |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
84 |
return null; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
85 |
if (print) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
86 |
int start = (int) sp.getStartPosition(dataPath.getCompilationUnit(), tree); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
87 |
int end = (int) sp.getEndPosition(dataPath.getCompilationUnit(), tree); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
88 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
89 |
text.substring(start, end)); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
90 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
91 |
return super.scan(tree, p); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
92 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
93 |
}.scan(dataPath.getLeaf(), null); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
94 |
return false; |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
95 |
} catch (IOException ex) { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
96 |
throw new IllegalStateException(ex); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
97 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
98 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
99 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
100 |
@Override |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
101 |
public SourceVersion getSupportedSourceVersion() { |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
102 |
return SourceVersion.latestSupported(); |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
103 |
} |
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
104 |
|
7799a51dbe30
8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff
changeset
|
105 |
} |