1 /*
   2  * Copyright (c) 2005, 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 5024104
  27  *  @summary Test ReferenceType.majorVersion(), minorVersion, constantPoolCount and ConstantPool apis.
  28  *
  29  *  @author Swamy Venkataramanappa
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection
  33  *  @run compile -g ConstantPoolInfo.java
  34  *  @run driver ConstantPoolInfo
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 import java.io.*;
  42 
  43     /********** target program **********/
  44 
  45 class ConstantPoolTarg {
  46     public static void main(String[] args){
  47         System.out.println("Howdy!"); // don't change the string value "Howdy!" it is
  48                                       // used to test the constant pool entry
  49     }
  50 }
  51 
  52     /********** test program **********/
  53 
  54 public class ConstantPoolInfo extends TestScaffold {
  55     ReferenceType targetClass;
  56     ThreadReference mainThread;
  57     int cpool_count;
  58     byte[] cpbytes;
  59     static int expectedMajorVersion;
  60     static int expectedMinorVersion;
  61     static int expectedCpoolCount;
  62 
  63     /* Class File Constants */
  64     public static final int JAVA_MAGIC                   = 0xcafebabe;
  65 
  66     /* Constant table :  copied from sun/javap */
  67     public static final int CONSTANT_UTF8                = 1;
  68     public static final int CONSTANT_UNICODE             = 2;
  69     public static final int CONSTANT_INTEGER             = 3;
  70     public static final int CONSTANT_FLOAT               = 4;
  71     public static final int CONSTANT_LONG                = 5;
  72     public static final int CONSTANT_DOUBLE              = 6;
  73     public static final int CONSTANT_CLASS               = 7;
  74     public static final int CONSTANT_STRING              = 8;
  75     public static final int CONSTANT_FIELD               = 9;
  76     public static final int CONSTANT_METHOD              = 10;
  77     public static final int CONSTANT_INTERFACEMETHOD     = 11;
  78     public static final int CONSTANT_NAMEANDTYPE         = 12;
  79 
  80     ConstantPoolInfo (String args[]) {
  81         super(args);
  82     }
  83 
  84     public static void main(String[] args)      throws Exception {
  85         new ConstantPoolInfo(args).startTests();
  86     }
  87 
  88     /********** test core **********/
  89 
  90     protected void runTests() throws Exception {
  91         /*
  92          * Get to the top of main()
  93          * to determine targetClass and mainThread
  94          */
  95         BreakpointEvent bpe = startToMain("ConstantPoolTarg");
  96         targetClass = bpe.location().declaringType();
  97         mainThread = bpe.thread();
  98 
  99 
 100         String targPathname = System.getProperty("test.classes") + File.separator + "ConstantPoolTarg.class";
 101 
 102         readClassData(new FileInputStream(targPathname));
 103 
 104         /* Test constant pool apis
 105          */
 106         if (vm().canGetClassFileVersion()) {
 107             if (expectedMajorVersion != targetClass.majorVersion()) {
 108                 failure("unexpected major version: actual value: " + targetClass.majorVersion()
 109                         + "expected value :" + expectedMajorVersion);
 110 
 111             }
 112             if (expectedMinorVersion != targetClass.minorVersion()) {
 113                 failure("unexpected minor version: actual value: " + targetClass.minorVersion()
 114                         + "expected value :" + expectedMinorVersion);
 115 
 116             }
 117         } else {
 118             System.out.println("can get class version not supported");
 119         }
 120 
 121 
 122         if (vm().canGetConstantPool()) {
 123 
 124             cpool_count = targetClass.constantPoolCount();
 125 
 126             cpbytes = targetClass.constantPool();
 127 
 128             try {
 129                 printcp();
 130             } catch (IOException x){
 131                 System.out.println("IOexception reading cpool bytes " + x);
 132             }
 133 
 134             if (expectedCpoolCount != cpool_count) {
 135                 failure("unexpected constant pool count: actual value: " + cpool_count
 136                         + "expected value :" + expectedCpoolCount);
 137             }
 138 
 139         } else {
 140             System.out.println("can get constant pool version not supported");
 141         }
 142 
 143 
 144         /*
 145          * resume until end
 146          */
 147         listenUntilVMDisconnect();
 148 
 149         /*
 150          * deal with results of test
 151          * if anything has called failure("foo") testFailed will be true
 152          */
 153         if (!testFailed) {
 154             println("ConstantPoolInfo: passed");
 155         } else {
 156             throw new Exception("ConstantPoolInfo: failed");
 157         }
 158     }
 159 
 160     public void printcp() throws IOException {
 161         boolean found = false;
 162 
 163         ByteArrayInputStream bytesStream = new ByteArrayInputStream(cpbytes);
 164         DataInputStream in = new DataInputStream(bytesStream);
 165         for (int i = 1; i < cpool_count; i++) {
 166             int tag = in.readByte();
 167             System.out.print("const #" + i + ":   ");
 168             switch(tag) {
 169                     case CONSTANT_UTF8:
 170                     String str=in.readUTF();
 171                     System.out.println("Asciz " + str);
 172                     // "Howdy!" is an expected constant pool entry
 173                     // of test program. It should exist.
 174                     if (str.compareTo("Howdy!") == 0) {
 175                         found = true;
 176                     }
 177                     break;
 178                 case CONSTANT_INTEGER:
 179                     System.out.println("int " + in.readInt());
 180                     break;
 181                 case CONSTANT_FLOAT:
 182                     System.out.println("Float " + in.readFloat());
 183                     break;
 184                 case CONSTANT_LONG:
 185                     System.out.println("Long " + in.readLong());
 186                     break;
 187                 case CONSTANT_DOUBLE:
 188                     System.out.println("Double " + in.readDouble());
 189                     break;
 190                 case CONSTANT_CLASS:
 191                     System.out.println("Class " + in.readUnsignedShort());
 192                     break;
 193                 case CONSTANT_STRING:
 194                     System.out.println("String " + in.readUnsignedShort());
 195                     break;
 196                 case CONSTANT_FIELD:
 197                     System.out.println("Field " + in.readUnsignedShort() + " " + in.readUnsignedShort());
 198                     break;
 199                 case CONSTANT_METHOD:
 200                     System.out.println("Method " + in.readUnsignedShort() + " " + in.readUnsignedShort());
 201                     break;
 202                 case CONSTANT_INTERFACEMETHOD:
 203                     System.out.println("InterfaceMethod " + in.readUnsignedShort() + " " + in.readUnsignedShort());
 204                     break;
 205                 case CONSTANT_NAMEANDTYPE:
 206                     System.out.println("NameAndType " + in.readUnsignedShort() + " " + in.readUnsignedShort());
 207                     break;
 208                 case 0:
 209                 default:
 210                     System.out.println("class format error");
 211             }
 212 
 213         }
 214 
 215         if (!found) {
 216             failure("expected string \"Howdy!\" not found in constant pool");
 217         }
 218     }
 219 
 220 
 221     /**
 222      * Read classfile
 223      */
 224     void readClassData(InputStream infile){
 225         try{
 226             this.read(new DataInputStream(infile));
 227         }catch (FileNotFoundException ee) {
 228             failure("cant read file");
 229         }catch (Error ee) {
 230             ee.printStackTrace();
 231             failure("fatal error");
 232         } catch (Exception ee) {
 233             ee.printStackTrace();
 234             failure("fatal exception");
 235         }
 236     }
 237 
 238         /**
 239          * Read major, minor and cp count.
 240          */
 241     public void read(DataInputStream in) throws IOException {
 242         int magic = in.readInt();
 243         if (magic != JAVA_MAGIC) {
 244             failure("fatal bad class file format");
 245         }
 246         expectedMinorVersion = in.readShort();;
 247         expectedMajorVersion = in.readShort();
 248         expectedCpoolCount = in.readUnsignedShort();
 249         in.close();
 250     } // end read()
 251 }