0N/A/*
797N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
738N/A * @bug 6412669 6997958
0N/A * @summary Should be able to get SourcePositions from 269 world
0N/A */
0N/A
0N/Aimport java.io.*;
738N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport javax.annotation.*;
0N/Aimport javax.annotation.processing.*;
0N/Aimport javax.lang.model.*;
0N/Aimport javax.lang.model.element.*;
0N/Aimport javax.tools.*;
0N/Aimport com.sun.source.util.*;
0N/Aimport com.sun.tools.javac.api.*;
0N/A
0N/A@SupportedAnnotationTypes("*")
0N/Apublic class T6412669 extends AbstractProcessor {
738N/A public static void main(String... args) throws Exception {
738N/A File testSrc = new File(System.getProperty("test.src", "."));
738N/A File testClasses = new File(System.getProperty("test.classes", "."));
738N/A
738N/A // Determine location of necessary tools classes. Assume all in one place.
738N/A // Likely candidates are typically tools.jar (when testing JDK build)
738N/A // or build/classes or dist/javac.jar (when testing langtools, using -Xbootclasspath/p:)
738N/A File toolsClasses;
738N/A URL u = T6412669.class.getClassLoader().getResource("com/sun/source/util/JavacTask.class");
738N/A switch (u.getProtocol()) {
738N/A case "file":
738N/A toolsClasses = new File(u.toURI());
738N/A break;
738N/A case "jar":
738N/A String s = u.getFile(); // will be file:path!/entry
738N/A int sep = s.indexOf("!");
738N/A toolsClasses = new File(new URI(s.substring(0, sep)));
738N/A break;
738N/A default:
738N/A throw new AssertionError("Cannot locate tools classes");
738N/A }
738N/A //System.err.println("toolsClasses: " + toolsClasses);
0N/A
0N/A JavacTool tool = JavacTool.create();
0N/A StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
738N/A fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(testClasses, toolsClasses));
0N/A Iterable<? extends JavaFileObject> files =
0N/A fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
738N/A String[] opts = { "-proc:only", "-processor", T6412669.class.getName()};
738N/A StringWriter sw = new StringWriter();
738N/A JavacTask task = tool.getTask(sw, fm, null, Arrays.asList(opts), null, files);
738N/A boolean ok = task.call();
738N/A String out = sw.toString();
738N/A if (!out.isEmpty())
738N/A System.err.println(out);
738N/A if (!ok)
738N/A throw new AssertionError("compilation of test program failed");
738N/A // verify we found an annotated element to exercise the SourcePositions API
738N/A if (!out.contains("processing element"))
738N/A throw new AssertionError("expected text not found in compilation output");
0N/A }
0N/A
0N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
0N/A Trees trees = Trees.instance(processingEnv);
0N/A SourcePositions sp = trees.getSourcePositions();
0N/A Messager m = processingEnv.getMessager();
738N/A m.printMessage(Diagnostic.Kind.NOTE, "processing annotations");
738N/A int count = 0;
0N/A for (TypeElement anno: annotations) {
738N/A count++;
738N/A m.printMessage(Diagnostic.Kind.NOTE, " processing annotation " + anno);
0N/A for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
738N/A m.printMessage(Diagnostic.Kind.NOTE, " processing element " + e);
0N/A TreePath p = trees.getPath(e);
0N/A long start = sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
0N/A long end = sp.getEndPosition(p.getCompilationUnit(), p.getLeaf());
0N/A Diagnostic.Kind k = (start > 0 && end > 0 && start < end
0N/A ? Diagnostic.Kind.NOTE : Diagnostic.Kind.ERROR);
0N/A m.printMessage(k, "test [" + start + "," + end + "]", e);
0N/A }
0N/A }
738N/A if (count == 0)
738N/A m.printMessage(Diagnostic.Kind.NOTE, "no annotations found");
0N/A return true;
0N/A }
494N/A
494N/A @Override
494N/A public SourceVersion getSupportedSourceVersion() {
494N/A return SourceVersion.latest();
494N/A }
0N/A}