1 /*
   2  * Copyright (c) 2016, 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 6822627
  27  * @summary Test that ReferenceType.constantPool does not produce an NPE
  28  * @author Egor Ushakov
  29  *
  30  * @modules jdk.jdi/com.sun.tools.jdi:+open
  31  *
  32  * @run build TestScaffold VMConnection
  33  * @run compile -g ConstantPoolInfoGC.java
  34  * @run main/othervm ConstantPoolInfoGC
  35  */
  36 
  37 import com.sun.jdi.ReferenceType;
  38 import com.sun.tools.jdi.ReferenceTypeImpl;
  39 
  40 import java.lang.ref.Reference;
  41 import java.lang.reflect.Field;
  42 import java.util.Arrays;
  43 
  44     /********** target program **********/
  45 
  46 class ConstantPoolGCTarg {
  47     public static void main(String[] args){
  48         System.out.println("Anything");
  49     }
  50 }
  51 
  52     /********** test program **********/
  53 
  54 public class ConstantPoolInfoGC extends TestScaffold {
  55     ReferenceType targetClass;
  56 
  57     ConstantPoolInfoGC(String args[]) {
  58         super(args);
  59     }
  60 
  61     public static void main(String[] args)      throws Exception {
  62         new ConstantPoolInfoGC(args).startTests();
  63     }
  64 
  65     /********** test core **********/
  66 
  67     protected void runTests() throws Exception {
  68         targetClass = startToMain("ConstantPoolGCTarg").location().declaringType();
  69 
  70         if (vm().canGetConstantPool()) {
  71             byte[] cpbytes = targetClass.constantPool();
  72 
  73             // imitate SoftReference cleared
  74             Field constantPoolBytesRef = ReferenceTypeImpl.class.getDeclaredField("constantPoolBytesRef");
  75             constantPoolBytesRef.setAccessible(true);
  76             Reference softRef = (Reference) constantPoolBytesRef.get(targetClass);
  77             softRef.clear();
  78 
  79             byte[] cpbytes2 = targetClass.constantPool();
  80             if (!Arrays.equals(cpbytes, cpbytes2)) {
  81                 failure("Consequent constantPool results vary, first was : " + cpbytes + ", now: " + cpbytes2);
  82             };
  83 
  84         } else {
  85             System.out.println("can get constant pool version not supported");
  86         }
  87 
  88 
  89         /*
  90          * resume until end
  91          */
  92         listenUntilVMDisconnect();
  93 
  94         /*
  95          * deal with results of test
  96          * if anything has called failure("foo") testFailed will be true
  97          */
  98         if (!testFailed) {
  99             println("ConstantPoolInfoGC: passed");
 100         } else {
 101             throw new Exception("ConstantPoolInfoGC: failed");
 102         }
 103     }
 104 }