duke@1: /* ohair@554: * Copyright (c) 2006, 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 duke@1: * @bug 6404194 duke@1: * @summary javac parser generates incorrect end position for annotations with parentheses. duke@1: * @author Peter von der Ah\u00e9 duke@1: */ duke@1: duke@1: import com.sun.source.tree.ClassTree; duke@1: import com.sun.source.tree.CompilationUnitTree; duke@1: import com.sun.source.tree.Tree; duke@1: import com.sun.source.util.JavacTask; duke@1: import com.sun.source.util.Trees; duke@1: import java.io.IOException; duke@1: import java.net.URI; duke@1: import java.util.Collections; duke@1: import java.util.List; duke@1: import javax.tools.JavaCompiler; duke@1: import javax.tools.JavaFileObject; duke@1: import javax.tools.SimpleJavaFileObject; duke@1: import static javax.tools.JavaFileObject.Kind.SOURCE; duke@1: import javax.tools.ToolProvider; duke@1: duke@1: public class T6404194 { duke@1: public static void main(String... args) throws IOException { duke@1: class MyFileObject extends SimpleJavaFileObject { duke@1: MyFileObject() { duke@1: super(URI.create("myfo:///Test.java"), SOURCE); duke@1: } duke@1: @Override duke@1: public String getCharContent(boolean ignoreEncodingErrors) { duke@1: // 0 1 2 3 duke@1: // 01234567890123456 7890 123456789012345 duke@1: return "@SuppressWarning(\"foo\") @Deprecated class Test { Test() { } }"; duke@1: } duke@1: } duke@1: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); duke@1: List compilationUnits = duke@1: Collections.singletonList(new MyFileObject()); duke@1: JavacTask task = (JavacTask)javac.getTask(null, null, null, null, null, compilationUnits); duke@1: Trees trees = Trees.instance(task); duke@1: CompilationUnitTree toplevel = task.parse().iterator().next(); duke@1: ClassTree classTree = (ClassTree)toplevel.getTypeDecls().get(0); duke@1: List annotations = classTree.getModifiers().getAnnotations(); duke@1: Tree tree1 = annotations.get(0); duke@1: Tree tree2 = annotations.get(1); duke@1: long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1); duke@1: if (pos != 0) duke@1: throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", duke@1: tree1, pos)); duke@1: pos = trees.getSourcePositions().getEndPosition(toplevel, tree1); duke@1: if (pos != 23) duke@1: throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", duke@1: tree1, pos)); duke@1: pos = trees.getSourcePositions().getStartPosition(toplevel, tree2); duke@1: if (pos != 24) duke@1: throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", duke@1: tree2, pos)); duke@1: pos = trees.getSourcePositions().getEndPosition(toplevel, tree2); duke@1: if (pos != 35) duke@1: throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", duke@1: tree2, pos)); duke@1: } duke@1: }