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