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