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

changeset 3556
47a91ecb0b87
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/TypeAnnotationPropagationTest.java	Mon Dec 04 10:33:18 2017 -0500
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 + * Copyright (c) 2017, Google Inc. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8144185 8191969
    1.30 + * @summary javac produces incorrect RuntimeInvisibleTypeAnnotations length attribute
    1.31 + */
    1.32 +
    1.33 +import static java.lang.annotation.ElementType.TYPE_USE;
    1.34 +import static java.lang.annotation.RetentionPolicy.RUNTIME;
    1.35 +
    1.36 +import com.sun.tools.classfile.Attribute;
    1.37 +import com.sun.tools.classfile.ClassFile;
    1.38 +import com.sun.tools.classfile.Code_attribute;
    1.39 +import com.sun.tools.classfile.Method;
    1.40 +import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
    1.41 +import com.sun.tools.classfile.TypeAnnotation;
    1.42 +import java.lang.annotation.Retention;
    1.43 +import java.lang.annotation.Target;
    1.44 +import java.util.Arrays;
    1.45 +import java.util.Objects;
    1.46 +
    1.47 +public class TypeAnnotationPropagationTest extends ClassfileTestHelper {
    1.48 +    public static void main(String[] args) throws Exception {
    1.49 +        new TypeAnnotationPropagationTest().run();
    1.50 +    }
    1.51 +
    1.52 +    public void run() throws Exception {
    1.53 +        ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");
    1.54 +
    1.55 +        Method f = null;
    1.56 +        for (Method m : cf.methods) {
    1.57 +            if (m.getName(cf.constant_pool).contains("f")) {
    1.58 +                f = m;
    1.59 +                break;
    1.60 +            }
    1.61 +        }
    1.62 +
    1.63 +        int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
    1.64 +        Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
    1.65 +        idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
    1.66 +        RuntimeVisibleTypeAnnotations_attribute attr =
    1.67 +                (RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);
    1.68 +
    1.69 +        TypeAnnotation anno = attr.annotations[0];
    1.70 +        assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
    1.71 +        assertEquals(anno.position.lvarLength, new int[] {8}, "length");
    1.72 +        assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
    1.73 +    }
    1.74 +
    1.75 +    void assertEquals(int[] actual, int[] expected, String message) {
    1.76 +        if (!Arrays.equals(actual, expected)) {
    1.77 +            throw new AssertionError(
    1.78 +                    String.format(
    1.79 +                            "actual: %s, expected: %s, %s",
    1.80 +                            Arrays.toString(actual), Arrays.toString(expected), message));
    1.81 +        }
    1.82 +    }
    1.83 +
    1.84 +    /** ********************* Test class ************************ */
    1.85 +    static class Test {
    1.86 +        void f() {
    1.87 +            @A String s = "";
    1.88 +            Runnable r =
    1.89 +                    () -> {
    1.90 +                        Objects.requireNonNull(s);
    1.91 +                        Objects.requireNonNull(s);
    1.92 +                        Objects.requireNonNull(s);
    1.93 +                        Objects.requireNonNull(s);
    1.94 +                        Objects.requireNonNull(s);
    1.95 +                        Objects.requireNonNull(s);
    1.96 +                    };
    1.97 +        }
    1.98 +
    1.99 +        @Retention(RUNTIME)
   1.100 +        @Target(TYPE_USE)
   1.101 +        @interface A {}
   1.102 +    }
   1.103 +}

mercurial