langtools/test/tools/javac/7153958/CPoolRefClassContainingInlinedCts.java
changeset 14721 071e3587f212
child 14948 6a2008d8e9ba
equal deleted inserted replaced
14720:c24c61d0d9a6 14721:071e3587f212
       
     1 /*
       
     2  * Copyright (c) 2012, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @bug 7153958
       
    29  * @summary add constant pool reference to class containing inlined constants
       
    30  * @compile pkg/ClassToBeStaticallyImported.java
       
    31  * @run main CPoolRefClassContainingInlinedCts
       
    32  */
       
    33 
       
    34 import com.sun.tools.classfile.ClassFile;
       
    35 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
       
    36 import com.sun.tools.classfile.ConstantPool.CPInfo;
       
    37 import com.sun.tools.classfile.ConstantPoolException;
       
    38 import java.io.File;
       
    39 import java.io.IOException;
       
    40 
       
    41 import static pkg.ClassToBeStaticallyImported.staticField;
       
    42 
       
    43 public class CPoolRefClassContainingInlinedCts {
       
    44 
       
    45     public static void main(String args[]) throws Exception {
       
    46         new CPoolRefClassContainingInlinedCts().run();
       
    47     }
       
    48 
       
    49     void run() throws Exception {
       
    50         checkReferences();
       
    51     }
       
    52 
       
    53     int numberOfReferencedClassesToBeChecked = 0;
       
    54 
       
    55     void checkClassName(String className) {
       
    56         switch (className) {
       
    57             case "SimpleAssignClass" : case "BinaryExpClass":
       
    58             case "UnaryExpClass" : case "CastClass":
       
    59             case "ParensClass" : case "CondClass":
       
    60             case "IfClass" : case "pkg/ClassToBeStaticallyImported":
       
    61                 numberOfReferencedClassesToBeChecked++;
       
    62         }
       
    63     }
       
    64 
       
    65     void checkReferences() throws IOException, ConstantPoolException {
       
    66         File testClasses = new File(System.getProperty("test.classes"));
       
    67         File file = new File(testClasses,
       
    68                 CPoolRefClassContainingInlinedCts.class.getName() + ".class");
       
    69         ClassFile classFile = ClassFile.read(file);
       
    70         int i = 1;
       
    71         CPInfo cpInfo;
       
    72         while (i < classFile.constant_pool.size()) {
       
    73             cpInfo = classFile.constant_pool.get(i);
       
    74             if (cpInfo instanceof CONSTANT_Class_info) {
       
    75                 checkClassName(((CONSTANT_Class_info)cpInfo).getName());
       
    76             }
       
    77             i += cpInfo.size();
       
    78         }
       
    79         if (numberOfReferencedClassesToBeChecked != 8) {
       
    80             throw new AssertionError("Class reference missing in the constant pool");
       
    81         }
       
    82     }
       
    83 
       
    84     private int assign = SimpleAssignClass.x;
       
    85     private int binary = BinaryExpClass.x + 1;
       
    86     private int unary = -UnaryExpClass.x;
       
    87     private int cast = (int)CastClass.x;
       
    88     private int parens = (ParensClass.x);
       
    89     private int cond = (CondClass.x == 1) ? 1 : 2;
       
    90     private static int ifConstant;
       
    91     private static int importStatic;
       
    92     static {
       
    93         if (IfClass.x == 1) {
       
    94             ifConstant = 1;
       
    95         } else {
       
    96             ifConstant = 2;
       
    97         }
       
    98     }
       
    99     static {
       
   100         if (staticField == 1) {
       
   101             importStatic = 1;
       
   102         } else {
       
   103             importStatic = 2;
       
   104         }
       
   105     }
       
   106 }
       
   107 
       
   108 class SimpleAssignClass {
       
   109     public static final int x = 1;
       
   110 }
       
   111 
       
   112 class BinaryExpClass {
       
   113     public static final int x = 1;
       
   114 }
       
   115 
       
   116 class UnaryExpClass {
       
   117     public static final int x = 1;
       
   118 }
       
   119 
       
   120 class CastClass {
       
   121     public static final int x = 1;
       
   122 }
       
   123 
       
   124 class ParensClass {
       
   125     public static final int x = 1;
       
   126 }
       
   127 
       
   128 class CondClass {
       
   129     public static final int x = 1;
       
   130 }
       
   131 
       
   132 class IfClass {
       
   133     public static final int x = 1;
       
   134 }