TypeAnnotationPropagationTest.java revision 4210:0b32afcabef4
1/*
2 * Copyright (c) 2017, Google Inc. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8144185
27 * @summary javac produces incorrect RuntimeInvisibleTypeAnnotations length attribute
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 */
30
31import static java.lang.annotation.ElementType.TYPE_USE;
32import static java.lang.annotation.RetentionPolicy.RUNTIME;
33
34import com.sun.tools.classfile.Attribute;
35import com.sun.tools.classfile.ClassFile;
36import com.sun.tools.classfile.Code_attribute;
37import com.sun.tools.classfile.Method;
38import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
39import com.sun.tools.classfile.TypeAnnotation;
40import java.lang.annotation.Retention;
41import java.lang.annotation.Target;
42import java.util.Arrays;
43import java.util.Objects;
44
45public class TypeAnnotationPropagationTest extends ClassfileTestHelper {
46    public static void main(String[] args) throws Exception {
47        new TypeAnnotationPropagationTest().run();
48    }
49
50    public void run() throws Exception {
51        ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");
52
53        Method f = null;
54        for (Method m : cf.methods) {
55            if (m.getName(cf.constant_pool).contains("f")) {
56                f = m;
57                break;
58            }
59        }
60
61        int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
62        Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
63        idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
64        RuntimeVisibleTypeAnnotations_attribute attr =
65                (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);
66
67        TypeAnnotation anno = attr.annotations[0];
68        assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
69        assertEquals(anno.position.lvarLength, new int[] {8}, "length");
70        assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
71    }
72
73    void assertEquals(int[] actual, int[] expected, String message) {
74        if (!Arrays.equals(actual, expected)) {
75            throw new AssertionError(
76                    String.format(
77                            "actual: %s, expected: %s, %s",
78                            Arrays.toString(actual), Arrays.toString(expected), message));
79        }
80    }
81
82    /** ********************* Test class ************************ */
83    static class Test {
84        void f() {
85            @A String s = "";
86            Runnable r =
87                    () -> {
88                        Objects.requireNonNull(s);
89                        Objects.requireNonNull(s);
90                        Objects.requireNonNull(s);
91                        Objects.requireNonNull(s);
92                        Objects.requireNonNull(s);
93                        Objects.requireNonNull(s);
94                    };
95        }
96
97        @Retention(RUNTIME)
98        @Target(TYPE_USE)
99        @interface A {}
100    }
101}
102
103