langtools/test/tools/javac/T6972327.java
author jlahoda
Tue, 16 Aug 2016 16:43:00 +0200
changeset 40504 0a01f6710c84
parent 36526 3b41f1c69604
permissions -rw-r--r--
8078561: Error message should be generated once when -source 6 is specified Summary: Code to avoid duplicated errors about features not supported in the current source level moved to Log Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 6152
diff changeset
     2
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     4
 *
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     8
 *
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    13
 * accompanied this code).
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    14
 *
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    18
 *
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    21
 * questions.
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    22
 */
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    23
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    24
/*
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    25
 * @test
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    26
 * @bug 6972327
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    27
 * @summary JCTree.pos incorrect for annotations without modifiers and package
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 6152
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    29
 */
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    30
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    31
import com.sun.source.tree.*;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    32
import com.sun.source.util.Trees;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.api.*;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    34
import java.net.URI;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    35
import java.util.Arrays;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    36
import javax.tools.*;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    37
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    38
public class T6972327 {
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    39
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    41
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    42
        assert tool != null;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    43
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    44
        String code = "\n@interface Test {}";
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    45
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    46
        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    47
            Arrays.asList("-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    48
        CompilationUnitTree cut = ct.parse().iterator().next();
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    49
        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    50
        Trees t = Trees.instance(ct);
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    51
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    52
        long pos = t.getSourcePositions().getStartPosition(cut, clazz);
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    53
        if (pos != code.indexOf(code.trim()))
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    54
            throw new IllegalStateException("Unexpected position=" + pos);
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    55
    }
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    56
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    57
    static class MyFileObject extends SimpleJavaFileObject {
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    58
        private String text;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    59
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    60
        public MyFileObject(String text) {
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    61
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    62
            this.text = text;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    63
        }
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    64
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    65
        @Override
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    66
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    67
            return text;
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    68
        }
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    69
    }
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents:
diff changeset
    70
}