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