T6412669.java revision 494
0N/A/*
0N/A * Copyright 2006 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6412669
0N/A * @summary Should be able to get SourcePositions from 269 world
0N/A */
0N/A
0N/Aimport java.io.*;
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 {
0N/A public static void main(String... args) throws IOException {
0N/A String testSrc = System.getProperty("test.src", ".");
0N/A String testClasses = System.getProperty("test.classes", ".");
0N/A
0N/A JavacTool tool = JavacTool.create();
0N/A StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
0N/A fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File(testClasses)));
0N/A Iterable<? extends JavaFileObject> files =
0N/A fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6412669.class.getName()+".java")));
0N/A String[] opts = { "-proc:only", "-processor", T6412669.class.getName(),
0N/A "-classpath", new File(testClasses).getPath() };
0N/A JavacTask task = tool.getTask(null, fm, null, Arrays.asList(opts), null, files);
0N/A if (!task.call())
0N/A throw new AssertionError("test failed");
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();
0N/A for (TypeElement anno: annotations) {
0N/A for (Element e: roundEnv.getElementsAnnotatedWith(anno)) {
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 }
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}