CPoolRefClassContainingInlinedCts.java revision 2948:6709549d97be
1/* 2 * Copyright (c) 2012, 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. 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 8073372 29 * @summary add constant pool reference to class containing inlined constants 30 * @modules jdk.jdeps/com.sun.tools.classfile 31 * @compile pkg/ClassToBeStaticallyImportedA.java pkg/ClassToBeStaticallyImportedB.java CPoolRefClassContainingInlinedCts.java 32 * @run main CPoolRefClassContainingInlinedCts 33 */ 34 35import com.sun.tools.classfile.ClassFile; 36import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info; 37import com.sun.tools.classfile.ConstantPool.CPInfo; 38import com.sun.tools.classfile.ConstantPoolException; 39import java.io.File; 40import java.io.IOException; 41 42import static pkg.ClassToBeStaticallyImportedA.staticFieldA; 43import static pkg.ClassToBeStaticallyImportedB.staticFieldB; 44 45public class CPoolRefClassContainingInlinedCts { 46 47 public static void main(String args[]) throws Exception { 48 new CPoolRefClassContainingInlinedCts().run(); 49 } 50 51 void run() throws Exception { 52 checkReferences(); 53 } 54 55 int numberOfReferencedClassesToBeChecked = 0; 56 57 void checkClassName(String className) { 58 switch (className) { 59 case "SimpleAssignClassA" : case "BinaryExpClassA": 60 case "UnaryExpClassA" : case "CastClassA": 61 case "ParensClassA" : case "CondClassA": 62 case "IfClassA" : case "pkg/ClassToBeStaticallyImportedA": 63 case "SimpleAssignClassB" : case "BinaryExpClassB": 64 case "UnaryExpClassB" : case "CastClassB": 65 case "ParensClassB" : case "CondClassB": 66 case "IfClassB" : case "pkg/ClassToBeStaticallyImportedB": 67 numberOfReferencedClassesToBeChecked++; 68 } 69 } 70 71 void checkReferences() throws IOException, ConstantPoolException { 72 File testClasses = new File(System.getProperty("test.classes")); 73 File file = new File(testClasses, 74 CPoolRefClassContainingInlinedCts.class.getName() + ".class"); 75 ClassFile classFile = ClassFile.read(file); 76 int i = 1; 77 CPInfo cpInfo; 78 while (i < classFile.constant_pool.size()) { 79 cpInfo = classFile.constant_pool.get(i); 80 if (cpInfo instanceof CONSTANT_Class_info) { 81 checkClassName(((CONSTANT_Class_info)cpInfo).getName()); 82 } 83 i += cpInfo.size(); 84 } 85 if (numberOfReferencedClassesToBeChecked != 16) { 86 throw new AssertionError("Class reference missing in the constant pool"); 87 } 88 } 89 90 private int assignA = SimpleAssignClassA.x; 91 private int binaryA = BinaryExpClassA.x + 1; 92 private int unaryA = -UnaryExpClassA.x; 93 private int castA = (int)CastClassA.x; 94 private int parensA = (ParensClassA.x); 95 private int condA = (CondClassA.x == 1) ? 1 : 2; 96 private static int ifConstantA; 97 private static int importStaticA; 98 static { 99 if (IfClassA.x == 1) { 100 ifConstantA = 1; 101 } else { 102 ifConstantA = 2; 103 } 104 } 105 static { 106 if (staticFieldA == 1) { 107 importStaticA = 1; 108 } else { 109 importStaticA = 2; 110 } 111 } 112 113 // now as final constants 114 private static final int assignB = SimpleAssignClassB.x; 115 private static final int binaryB = BinaryExpClassB.x + 1; 116 private static final int unaryB = -UnaryExpClassB.x; 117 private static final int castB = (int)CastClassB.x; 118 private static final int parensB = (ParensClassB.x); 119 private static final int condB = (CondClassB.x == 1) ? 1 : 2; 120 private static final int ifConstantB; 121 private static final int importStaticB; 122 static { 123 if (IfClassB.x == 1) { 124 ifConstantB = 1; 125 } else { 126 ifConstantB = 2; 127 } 128 } 129 static { 130 if (staticFieldB == 1) { 131 importStaticB = 1; 132 } else { 133 importStaticB = 2; 134 } 135 } 136} 137 138class SimpleAssignClassA { 139 public static final int x = 1; 140} 141 142class SimpleAssignClassB { 143 public static final int x = 1; 144} 145 146class BinaryExpClassA { 147 public static final int x = 1; 148} 149 150class BinaryExpClassB { 151 public static final int x = 1; 152} 153 154class UnaryExpClassA { 155 public static final int x = 1; 156} 157 158class UnaryExpClassB { 159 public static final int x = 1; 160} 161 162class CastClassA { 163 public static final int x = 1; 164} 165 166class CastClassB { 167 public static final int x = 1; 168} 169 170class ParensClassA { 171 public static final int x = 1; 172} 173 174class ParensClassB { 175 public static final int x = 1; 176} 177 178class CondClassA { 179 public static final int x = 1; 180} 181 182class CondClassB { 183 public static final int x = 1; 184} 185 186class IfClassA { 187 public static final int x = 1; 188} 189 190class IfClassB { 191 public static final int x = 1; 192} 193