Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/ConstantPoolInfo.java
38855 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 502410426* @summary Test ReferenceType.majorVersion(), minorVersion, constantPoolCount and ConstantPool apis.27*28* @author Swamy Venkataramanappa29*30* @run build TestScaffold VMConnection31* @run compile -g ConstantPoolInfo.java32* @run main ConstantPoolInfo33*/34import com.sun.jdi.*;35import com.sun.jdi.event.*;36import com.sun.jdi.request.*;3738import java.util.*;39import java.io.*;4041/********** target program **********/4243class ConstantPoolTarg {44public static void main(String[] args){45System.out.println("Howdy!"); // don't change the string value "Howdy!" it is46// used to test the constant pool entry47}48}4950/********** test program **********/5152public class ConstantPoolInfo extends TestScaffold {53ReferenceType targetClass;54ThreadReference mainThread;55int cpool_count;56byte[] cpbytes;57static int expectedMajorVersion;58static int expectedMinorVersion;59static int expectedCpoolCount;6061/* Class File Constants */62public static final int JAVA_MAGIC = 0xcafebabe;6364/* Constant table : copied from sun/javap */65public static final int CONSTANT_UTF8 = 1;66public static final int CONSTANT_UNICODE = 2;67public static final int CONSTANT_INTEGER = 3;68public static final int CONSTANT_FLOAT = 4;69public static final int CONSTANT_LONG = 5;70public static final int CONSTANT_DOUBLE = 6;71public static final int CONSTANT_CLASS = 7;72public static final int CONSTANT_STRING = 8;73public static final int CONSTANT_FIELD = 9;74public static final int CONSTANT_METHOD = 10;75public static final int CONSTANT_INTERFACEMETHOD = 11;76public static final int CONSTANT_NAMEANDTYPE = 12;7778ConstantPoolInfo (String args[]) {79super(args);80}8182public static void main(String[] args) throws Exception {83new ConstantPoolInfo(args).startTests();84}8586/********** test core **********/8788protected void runTests() throws Exception {89/*90* Get to the top of main()91* to determine targetClass and mainThread92*/93BreakpointEvent bpe = startToMain("ConstantPoolTarg");94targetClass = bpe.location().declaringType();95mainThread = bpe.thread();969798String targPathname = System.getProperty("test.classes") + File.separator + "ConstantPoolTarg.class";99100readClassData(new FileInputStream(targPathname));101102/* Test constant pool apis103*/104if (vm().canGetClassFileVersion()) {105if (expectedMajorVersion != targetClass.majorVersion()) {106failure("unexpected major version: actual value: " + targetClass.majorVersion()107+ "expected value :" + expectedMajorVersion);108109}110if (expectedMinorVersion != targetClass.minorVersion()) {111failure("unexpected minor version: actual value: " + targetClass.minorVersion()112+ "expected value :" + expectedMinorVersion);113114}115} else {116System.out.println("can get class version not supported");117}118119120if (vm().canGetConstantPool()) {121122cpool_count = targetClass.constantPoolCount();123124cpbytes = targetClass.constantPool();125126try {127printcp();128} catch (IOException x){129System.out.println("IOexception reading cpool bytes " + x);130}131132if (expectedCpoolCount != cpool_count) {133failure("unexpected constant pool count: actual value: " + cpool_count134+ "expected value :" + expectedCpoolCount);135}136137} else {138System.out.println("can get constant pool version not supported");139}140141142/*143* resume until end144*/145listenUntilVMDisconnect();146147/*148* deal with results of test149* if anything has called failure("foo") testFailed will be true150*/151if (!testFailed) {152println("ConstantPoolInfo: passed");153} else {154throw new Exception("ConstantPoolInfo: failed");155}156}157158public void printcp() throws IOException {159boolean found = false;160161ByteArrayInputStream bytesStream = new ByteArrayInputStream(cpbytes);162DataInputStream in = new DataInputStream(bytesStream);163for (int i = 1; i < cpool_count; i++) {164int tag = in.readByte();165System.out.print("const #" + i + ": ");166switch(tag) {167case CONSTANT_UTF8:168String str=in.readUTF();169System.out.println("Asciz " + str);170// "Howdy!" is an expected constant pool entry171// of test program. It should exist.172if (str.compareTo("Howdy!") == 0) {173found = true;174}175break;176case CONSTANT_INTEGER:177System.out.println("int " + in.readInt());178break;179case CONSTANT_FLOAT:180System.out.println("Float " + in.readFloat());181break;182case CONSTANT_LONG:183System.out.println("Long " + in.readLong());184break;185case CONSTANT_DOUBLE:186System.out.println("Double " + in.readDouble());187break;188case CONSTANT_CLASS:189System.out.println("Class " + in.readUnsignedShort());190break;191case CONSTANT_STRING:192System.out.println("String " + in.readUnsignedShort());193break;194case CONSTANT_FIELD:195System.out.println("Field " + in.readUnsignedShort() + " " + in.readUnsignedShort());196break;197case CONSTANT_METHOD:198System.out.println("Method " + in.readUnsignedShort() + " " + in.readUnsignedShort());199break;200case CONSTANT_INTERFACEMETHOD:201System.out.println("InterfaceMethod " + in.readUnsignedShort() + " " + in.readUnsignedShort());202break;203case CONSTANT_NAMEANDTYPE:204System.out.println("NameAndType " + in.readUnsignedShort() + " " + in.readUnsignedShort());205break;206case 0:207default:208System.out.println("class format error");209}210211}212213if (!found) {214failure("expected string \"Howdy!\" not found in constant pool");215}216}217218219/**220* Read classfile221*/222void readClassData(InputStream infile){223try{224this.read(new DataInputStream(infile));225}catch (FileNotFoundException ee) {226failure("cant read file");227}catch (Error ee) {228ee.printStackTrace();229failure("fatal error");230} catch (Exception ee) {231ee.printStackTrace();232failure("fatal exception");233}234}235236/**237* Read major, minor and cp count.238*/239public void read(DataInputStream in) throws IOException {240int magic = in.readInt();241if (magic != JAVA_MAGIC) {242failure("fatal bad class file format");243}244expectedMinorVersion = in.readShort();;245expectedMajorVersion = in.readShort();246expectedCpoolCount = in.readUnsignedShort();247in.close();248} // end read()249}250251252