test/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java

Mon, 04 Dec 2017 10:33:18 -0500

author
sadayapalam
date
Mon, 04 Dec 2017 10:33:18 -0500
changeset 3556
47a91ecb0b87
permissions
-rw-r--r--

8191969: javac produces incorrect RuntimeInvisibleTypeAnnotations length attribute
Reviewed-by: jlahoda, vromero

     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  */
    24 /*
    25  * @test
    26  * @bug 8144185 8191969
    27  * @summary javac produces incorrect RuntimeInvisibleTypeAnnotations length attribute
    28  */
    30 import static java.lang.annotation.ElementType.TYPE_USE;
    31 import static java.lang.annotation.RetentionPolicy.RUNTIME;
    33 import com.sun.tools.classfile.Attribute;
    34 import com.sun.tools.classfile.ClassFile;
    35 import com.sun.tools.classfile.Code_attribute;
    36 import com.sun.tools.classfile.Method;
    37 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
    38 import com.sun.tools.classfile.TypeAnnotation;
    39 import java.lang.annotation.Retention;
    40 import java.lang.annotation.Target;
    41 import java.util.Arrays;
    42 import java.util.Objects;
    44 public class TypeAnnotationPropagationTest extends ClassfileTestHelper {
    45     public static void main(String[] args) throws Exception {
    46         new TypeAnnotationPropagationTest().run();
    47     }
    49     public void run() throws Exception {
    50         ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");
    52         Method f = null;
    53         for (Method m : cf.methods) {
    54             if (m.getName(cf.constant_pool).contains("f")) {
    55                 f = m;
    56                 break;
    57             }
    58         }
    60         int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
    61         Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
    62         idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
    63         RuntimeVisibleTypeAnnotations_attribute attr =
    64                 (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);
    66         TypeAnnotation anno = attr.annotations[0];
    67         assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
    68         assertEquals(anno.position.lvarLength, new int[] {8}, "length");
    69         assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
    70     }
    72     void assertEquals(int[] actual, int[] expected, String message) {
    73         if (!Arrays.equals(actual, expected)) {
    74             throw new AssertionError(
    75                     String.format(
    76                             "actual: %s, expected: %s, %s",
    77                             Arrays.toString(actual), Arrays.toString(expected), message));
    78         }
    79     }
    81     /** ********************* Test class ************************ */
    82     static class Test {
    83         void f() {
    84             @A String s = "";
    85             Runnable r =
    86                     () -> {
    87                         Objects.requireNonNull(s);
    88                         Objects.requireNonNull(s);
    89                         Objects.requireNonNull(s);
    90                         Objects.requireNonNull(s);
    91                         Objects.requireNonNull(s);
    92                         Objects.requireNonNull(s);
    93                     };
    94         }
    96         @Retention(RUNTIME)
    97         @Target(TYPE_USE)
    98         @interface A {}
    99     }
   100 }

mercurial