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