1 /*
   2  * Copyright (c) 2017, 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  * @summary Check proper positions.
  27  * @build PatternMatchPosTest
  28  * @compile/ref=PatternMatchPosTest.out -processor PatternMatchPosTest -Xlint:unchecked PatternMatchPosTest.java
  29  */
  30 
  31 import java.io.IOException;
  32 import java.util.Set;
  33 
  34 import javax.annotation.processing.AbstractProcessor;
  35 import javax.annotation.processing.RoundEnvironment;
  36 import javax.annotation.processing.SupportedAnnotationTypes;
  37 import javax.lang.model.SourceVersion;
  38 import javax.lang.model.element.TypeElement;
  39 
  40 import com.sun.source.tree.CaseTree;
  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 
  48 @SupportedAnnotationTypes("*")
  49 public class PatternMatchPosTest extends AbstractProcessor {
  50 
  51     int round;
  52 
  53     @Override
  54     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  55         if (round++ != 0)
  56             return false;
  57 
  58         try {
  59             TypeElement data = processingEnv.getElementUtils().getTypeElement("PatternMatchPosTestData");
  60             Trees trees = Trees.instance(processingEnv);
  61             SourcePositions sp = trees.getSourcePositions();
  62             TreePath dataPath = trees.getPath(data);
  63             String text = dataPath.getCompilationUnit().getSourceFile().getCharContent(true).toString();
  64 
  65             new TreeScanner<Void, Void>() {
  66                 boolean print;
  67                 @Override
  68                 public Void visitIf(IfTree node, Void p) {
  69                     boolean prevPrint = print;
  70                     try {
  71                         print = true;
  72                         scan(node.getCondition(), p);
  73                     } finally {
  74                         print = prevPrint;
  75                     }
  76                     scan(node.getThenStatement(), p);
  77                     scan(node.getElseStatement(), p);
  78                     return null;
  79                 }
  80                 @Override
  81                 public Void scan(Tree tree, Void p) {
  82                     if (tree == null)
  83                         return null;
  84                     if (print) {
  85                         int start = (int) sp.getStartPosition(dataPath.getCompilationUnit(), tree);
  86                         int end = (int) sp.getEndPosition(dataPath.getCompilationUnit(), tree);
  87                         System.out.println(text.substring(start, end));
  88                     }
  89                     return super.scan(tree, p);
  90                 }
  91             }.scan(dataPath.getLeaf(), null);
  92             return false;
  93         } catch (IOException ex) {
  94             throw new IllegalStateException(ex);
  95         }
  96     }
  97 
  98     @Override
  99     public SourceVersion getSupportedSourceVersion() {
 100         return SourceVersion.latestSupported();
 101     }
 102 
 103 }
 104 
 105 class PatternMatchPosTestData {
 106     void data(Object o) {
 107         if (o instanceof String s) { }
 108         if (o instanceof java.lang.String s) { }
 109     }
 110 }