1/*
2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. 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 8011181
27 * @summary javac, empty UTF8 entry generated for inner class
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 *          jdk.compiler/com.sun.tools.javac.util
30 */
31
32import java.io.BufferedInputStream;
33import java.nio.file.Files;
34import java.nio.file.Path;
35import java.nio.file.Paths;
36
37import com.sun.tools.javac.util.Assert;
38import com.sun.tools.classfile.ClassFile;
39
40import static com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8;
41import static com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
42import static com.sun.tools.classfile.ConstantPool.CPInfo;
43
44public class EmptyUTF8ForInnerClassNameTest {
45
46    public static void main(String[] args) throws Exception {
47        new EmptyUTF8ForInnerClassNameTest().run();
48    }
49
50    void run() throws Exception {
51        checkClassFile(Paths.get(System.getProperty("test.classes"),
52                this.getClass().getName() + "$1.class"));
53        checkClassFile(Paths.get(System.getProperty("test.classes"),
54                this.getClass().getName() + "$EnumPlusSwitch.class"));
55    }
56
57    void checkClassFile(final Path path) throws Exception {
58        ClassFile classFile = ClassFile.read(
59                new BufferedInputStream(Files.newInputStream(path)));
60        for (CPInfo cpInfo : classFile.constant_pool.entries()) {
61            if (cpInfo.getTag() == CONSTANT_Utf8) {
62                CONSTANT_Utf8_info utf8Info = (CONSTANT_Utf8_info)cpInfo;
63                Assert.check(utf8Info.value.length() > 0,
64                        "UTF8 with length 0 found at class " + classFile.getName());
65            }
66        }
67    }
68
69    static class EnumPlusSwitch {
70        enum E {E1}
71
72        public int m (E e) {
73            switch (e) {
74                case E1:
75                    return 0;
76            }
77            return -1;
78        }
79    }
80
81}
82