Fri, 24 Apr 2020 03:58:51 +0100
Merge
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
jjg@739 | 26 | * @bug 6412669 6997958 |
duke@1 | 27 | * @summary Should be able to get SourcePositions from 269 world |
duke@1 | 28 | */ |
duke@1 | 29 | |
duke@1 | 30 | import java.io.*; |
jjg@739 | 31 | import java.net.*; |
duke@1 | 32 | import java.util.*; |
duke@1 | 33 | import javax.annotation.*; |
duke@1 | 34 | import javax.annotation.processing.*; |
duke@1 | 35 | import javax.lang.model.*; |
duke@1 | 36 | import javax.lang.model.element.*; |
duke@1 | 37 | import javax.tools.*; |
duke@1 | 38 | import com.sun.source.util.*; |
duke@1 | 39 | import com.sun.tools.javac.api.*; |
duke@1 | 40 | |
duke@1 | 41 | @SupportedAnnotationTypes("*") |
duke@1 | 42 | public class T6412669 extends AbstractProcessor { |
jjg@739 | 43 | public static void main(String... args) throws Exception { |
jjg@739 | 44 | File testSrc = new File(System.getProperty("test.src", ".")); |
jjg@739 | 45 | File testClasses = new File(System.getProperty("test.classes", ".")); |
jjg@739 | 46 | |
jjg@739 | 47 | // Determine location of necessary tools classes. Assume all in one place. |
jjg@739 | 48 | // Likely candidates are typically tools.jar (when testing JDK build) |
jjg@739 | 49 | // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:) |
jjg@739 | 50 | File toolsClasses; |
jjg@739 | 51 | URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class"); |
jjg@739 | 52 | switch (u.getProtocol()) { |
jjg@739 | 53 | case "file": |
jjg@739 | 54 | toolsClasses = new File(u.toURI()); |
jjg@739 | 55 | break; |
jjg@739 | 56 | case "jar": |
jjg@739 | 57 | String s = u.getFile(); // will be file:path!/entry |
jjg@739 | 58 | int sep = s.indexOf("!"); |
jjg@739 | 59 | toolsClasses = new File(new URI(s.substring(0, sep))); |
jjg@739 | 60 | break; |
jjg@739 | 61 | default: |
jjg@739 | 62 | throw new AssertionError("Cannot locate tools classes"); |
jjg@739 | 63 | } |
jjg@739 | 64 | //System.err.println("toolsClasses: " + toolsClasses); |
duke@1 | 65 | |
duke@1 | 66 | JavacTool tool = JavacTool.create(); |
duke@1 | 67 | StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
jjg@739 | 68 | fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses)); |
duke@1 | 69 | Iterable<? extends JavaFileObject> files = |
duke@1 | 70 | fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java"))); |
jjg@739 | 71 | String[] opts = { "-proc:only", "-processor", T6412669.class.getName()}; |
jjg@739 | 72 | StringWriter sw = new StringWriter(); |
jjg@739 | 73 | JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files); |
jjg@739 | 74 | boolean ok = task.call(); |
jjg@739 | 75 | String out = sw.toString(); |
jjg@739 | 76 | if (!out.isEmpty()) |
jjg@739 | 77 | System.err.println(out); |
jjg@739 | 78 | if (!ok) |
jjg@739 | 79 | throw new AssertionError("compilation of test program failed"); |
jjg@739 | 80 | // verify we found an annotated element to exercise the SourcePositions API |
jjg@739 | 81 | if (!out.contains("processing element")) |
jjg@739 | 82 | throw new AssertionError("expected text not found in compilation output"); |
duke@1 | 83 | } |
duke@1 | 84 | |
duke@1 | 85 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
duke@1 | 86 | Trees trees = Trees.instance(processingEnv); |
duke@1 | 87 | SourcePositions sp = trees.getSourcePositions(); |
duke@1 | 88 | Messager m = processingEnv.getMessager(); |
jjg@739 | 89 | m.printMessage(Diagnostic.Kind.NOTE, "processing annotations"); |
jjg@739 | 90 | int count = 0; |
duke@1 | 91 | for (TypeElement anno: annotations) { |
jjg@739 | 92 | count++; |
jjg@739 | 93 | m.printMessage(Diagnostic.Kind.NOTE, " processing annotation " + anno); |
duke@1 | 94 | for (Element e: roundEnv.getElementsAnnotatedWith(anno)) { |
jjg@739 | 95 | m.printMessage(Diagnostic.Kind.NOTE, " processing element " + e); |
duke@1 | 96 | TreePath p = trees.getPath(e); |
duke@1 | 97 | long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf()); |
duke@1 | 98 | long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf()); |
duke@1 | 99 | Diagnostic.Kind k = (start > 0 && end > 0 && start < end |
duke@1 | 100 | ? Diagnostic.Kind.NOTE : Diagnostic.Kind.ERROR); |
duke@1 | 101 | m.printMessage(k, "test [" + start + "," + end + "]", e); |
duke@1 | 102 | } |
duke@1 | 103 | } |
jjg@739 | 104 | if (count == 0) |
jjg@739 | 105 | m.printMessage(Diagnostic.Kind.NOTE, "no annotations found"); |
duke@1 | 106 | return true; |
duke@1 | 107 | } |
darcy@495 | 108 | |
darcy@495 | 109 | @Override |
darcy@495 | 110 | public SourceVersion getSupportedSourceVersion() { |
darcy@495 | 111 | return SourceVersion.latest(); |
darcy@495 | 112 | } |
duke@1 | 113 | } |