langtools/test/tools/javac/annotations/typeAnnotations/classfile/StaticInitializer.java
author sadayapalam
Thu, 12 Nov 2015 06:13:14 +0530
changeset 33712 fd1ce6d7ac63
permissions -rw-r--r--
8136419: Type annotations in initializers and lambda bodies not written to class file Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33712
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     1
/*
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     4
 *
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     8
 *
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    13
 * accompanied this code).
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    14
 *
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    18
 *
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    21
 * questions.
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    22
 */
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    23
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    24
import java.lang.annotation.*;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    25
import java.util.ArrayList;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    26
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    27
import com.sun.tools.classfile.*;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    28
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    29
/*
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    30
 * @test
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    31
 * @bug 8136419
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    32
 * @summary test that type annotations on entities in static initializers are emitted to classfile
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    33
 * @modules jdk.jdeps/com.sun.tools.classfile
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    34
 */
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    35
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    36
public class StaticInitializer extends ClassfileTestHelper {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    38
        new StaticInitializer().run();
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    39
    }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    40
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    41
    public void run() throws Exception {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    42
        expected_tinvisibles = 4;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    43
        expected_tvisibles = 0;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    44
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    45
        ClassFile cf = getClassFile("StaticInitializer$Test.class");
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    46
        test(cf);
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    47
        for (Field f : cf.fields) {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    48
            test(cf, f);
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    49
        }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    50
        for (Method m: cf.methods) {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    51
            test(cf, m, true);
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    52
        }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    53
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    54
        countAnnotations();
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    55
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    56
        if (errors > 0)
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    57
            throw new Exception(errors + " errors found");
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    58
        System.out.println("PASSED");
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    59
    }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    60
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    61
    /*********************** Test class *************************/
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    62
    static class Test {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    63
        @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    64
        @interface T {}
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    65
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    66
        static {
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    67
            @T String s = null;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    68
            Runnable r = () -> new ArrayList<@T String>();
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    69
        }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    70
        @T static String s = null;
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    71
        static Runnable r = () -> new ArrayList<@T String>();
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    72
    }
fd1ce6d7ac63 8136419: Type annotations in initializers and lambda bodies not written to class file
sadayapalam
parents:
diff changeset
    73
}