Path: blob/master/test/jdk/com/sun/jdi/ConstantPoolInfo.java
41149 views
/*1* Copyright (c) 2005, 2015, 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* @author Swamy Venkataramanappa28*29* @run build TestScaffold VMConnection30* @run compile -g ConstantPoolInfo.java31* @run driver ConstantPoolInfo32*/33import com.sun.jdi.*;34import com.sun.jdi.event.*;35import com.sun.jdi.request.*;3637import java.util.*;38import java.io.*;3940/********** target program **********/4142class ConstantPoolTarg {43public static void main(String[] args){44System.out.println("Howdy!"); // don't change the string value "Howdy!" it is45// used to test the constant pool entry46}47}4849/********** test program **********/5051public class ConstantPoolInfo extends TestScaffold {52ReferenceType targetClass;53ThreadReference mainThread;54int cpool_count;55byte[] cpbytes;56static int expectedMajorVersion;57static int expectedMinorVersion;58static int expectedCpoolCount;5960/* Class File Constants */61public static final int JAVA_MAGIC = 0xcafebabe;6263/* Constant table : copied from sun/javap */64public static final int CONSTANT_UTF8 = 1;65public static final int CONSTANT_UNICODE = 2;66public static final int CONSTANT_INTEGER = 3;67public static final int CONSTANT_FLOAT = 4;68public static final int CONSTANT_LONG = 5;69public static final int CONSTANT_DOUBLE = 6;70public static final int CONSTANT_CLASS = 7;71public static final int CONSTANT_STRING = 8;72public static final int CONSTANT_FIELD = 9;73public static final int CONSTANT_METHOD = 10;74public static final int CONSTANT_INTERFACEMETHOD = 11;75public static final int CONSTANT_NAMEANDTYPE = 12;7677ConstantPoolInfo (String args[]) {78super(args);79}8081public static void main(String[] args) throws Exception {82new ConstantPoolInfo(args).startTests();83}8485/********** test core **********/8687protected void runTests() throws Exception {88/*89* Get to the top of main()90* to determine targetClass and mainThread91*/92BreakpointEvent bpe = startToMain("ConstantPoolTarg");93targetClass = bpe.location().declaringType();94mainThread = bpe.thread();959697String targPathname = System.getProperty("test.classes") + File.separator + "ConstantPoolTarg.class";9899readClassData(new FileInputStream(targPathname));100101/* Test constant pool apis102*/103if (vm().canGetClassFileVersion()) {104if (expectedMajorVersion != targetClass.majorVersion()) {105failure("unexpected major version: actual value: " + targetClass.majorVersion()106+ "expected value :" + expectedMajorVersion);107108}109if (expectedMinorVersion != targetClass.minorVersion()) {110failure("unexpected minor version: actual value: " + targetClass.minorVersion()111+ "expected value :" + expectedMinorVersion);112113}114} else {115System.out.println("can get class version not supported");116}117118119if (vm().canGetConstantPool()) {120121cpool_count = targetClass.constantPoolCount();122123cpbytes = targetClass.constantPool();124125try {126printcp();127} catch (IOException x){128System.out.println("IOexception reading cpool bytes " + x);129}130131if (expectedCpoolCount != cpool_count) {132failure("unexpected constant pool count: actual value: " + cpool_count133+ "expected value :" + expectedCpoolCount);134}135136} else {137System.out.println("can get constant pool version not supported");138}139140141/*142* resume until end143*/144listenUntilVMDisconnect();145146/*147* deal with results of test148* if anything has called failure("foo") testFailed will be true149*/150if (!testFailed) {151println("ConstantPoolInfo: passed");152} else {153throw new Exception("ConstantPoolInfo: failed");154}155}156157public void printcp() throws IOException {158boolean found = false;159160ByteArrayInputStream bytesStream = new ByteArrayInputStream(cpbytes);161DataInputStream in = new DataInputStream(bytesStream);162for (int i = 1; i < cpool_count; i++) {163int tag = in.readByte();164System.out.print("const #" + i + ": ");165switch(tag) {166case CONSTANT_UTF8:167String str=in.readUTF();168System.out.println("Asciz " + str);169// "Howdy!" is an expected constant pool entry170// of test program. It should exist.171if (str.compareTo("Howdy!") == 0) {172found = true;173}174break;175case CONSTANT_INTEGER:176System.out.println("int " + in.readInt());177break;178case CONSTANT_FLOAT:179System.out.println("Float " + in.readFloat());180break;181case CONSTANT_LONG:182System.out.println("Long " + in.readLong());183break;184case CONSTANT_DOUBLE:185System.out.println("Double " + in.readDouble());186break;187case CONSTANT_CLASS:188System.out.println("Class " + in.readUnsignedShort());189break;190case CONSTANT_STRING:191System.out.println("String " + in.readUnsignedShort());192break;193case CONSTANT_FIELD:194System.out.println("Field " + in.readUnsignedShort() + " " + in.readUnsignedShort());195break;196case CONSTANT_METHOD:197System.out.println("Method " + in.readUnsignedShort() + " " + in.readUnsignedShort());198break;199case CONSTANT_INTERFACEMETHOD:200System.out.println("InterfaceMethod " + in.readUnsignedShort() + " " + in.readUnsignedShort());201break;202case CONSTANT_NAMEANDTYPE:203System.out.println("NameAndType " + in.readUnsignedShort() + " " + in.readUnsignedShort());204break;205case 0:206default:207System.out.println("class format error");208}209210}211212if (!found) {213failure("expected string \"Howdy!\" not found in constant pool");214}215}216217218/**219* Read classfile220*/221void readClassData(InputStream infile){222try{223this.read(new DataInputStream(infile));224}catch (FileNotFoundException ee) {225failure("cant read file");226}catch (Error ee) {227ee.printStackTrace();228failure("fatal error");229} catch (Exception ee) {230ee.printStackTrace();231failure("fatal exception");232}233}234235/**236* Read major, minor and cp count.237*/238public void read(DataInputStream in) throws IOException {239int magic = in.readInt();240if (magic != JAVA_MAGIC) {241failure("fatal bad class file format");242}243expectedMinorVersion = in.readShort();;244expectedMajorVersion = in.readShort();245expectedCpoolCount = in.readUnsignedShort();246in.close();247} // end read()248}249250251