1 /*
   2  * Copyright (c) 2001, 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 4419450
  27  * @summary Test newInstance() for arrays - currently covers
  28  * only reference type arrays (see bug #4450091).
  29  * @author Robert Field
  30  *
  31  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  32  * @run compile -g NewInstanceTest.java
  33  * @run driver NewInstanceTest
  34  */
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.event.*;
  37 import com.sun.jdi.request.*;
  38 
  39 import java.util.*;
  40 
  41     /********** target program **********/
  42 
  43 interface IfcFoo {
  44 }
  45 class ClsFoo implements IfcFoo {
  46 }
  47 
  48 class NewInstanceTarg {
  49     static ClsFoo aFoo = new ClsFoo();
  50 
  51     static Object[] objArray = new Object[10];
  52     static ClsFoo[] clsArray = new ClsFoo[10];
  53     static IfcFoo[] ifc0Array = new IfcFoo[10];
  54     static IfcFoo[] ifcArray = {aFoo, aFoo};
  55 
  56     public static void main(String[] args){
  57         System.out.println("Howdy!");
  58         System.out.println("Goodbye from NewInstanceTarg!");
  59     }
  60 }
  61 
  62     /********** test program **********/
  63 
  64 public class NewInstanceTest extends TestScaffold {
  65     ReferenceType targetClass;
  66 
  67     NewInstanceTest (String args[]) {
  68         super(args);
  69     }
  70 
  71     public static void main(String[] args)      throws Exception {
  72         new NewInstanceTest(args).startTests();
  73     }
  74 
  75     /********** test assist **********/
  76 
  77     void makeArray(String fieldName) throws Exception {
  78         println("Making array for field: " + fieldName);
  79         Field arrayField = targetClass.fieldByName(fieldName);
  80         ArrayType arrayType = (ArrayType)arrayField.type();
  81         println("Type for " + fieldName + " is " + arrayType);
  82         ArrayReference arrayReference = arrayType.newInstance(20);
  83         println("Passed subtest: " + fieldName);
  84     }
  85 
  86     /********** test core **********/
  87 
  88     protected void runTests() throws Exception {
  89         /*
  90          * Get to the top of main()
  91          * to determine targetClass
  92          */
  93         BreakpointEvent bpe = startToMain("NewInstanceTarg");
  94         targetClass = bpe.location().declaringType();
  95 
  96         makeArray("objArray");
  97         makeArray("clsArray");
  98         makeArray("ifc0Array");
  99         makeArray("ifcArray");
 100 
 101         /*
 102          * resume the target until end
 103          */
 104         listenUntilVMDisconnect();
 105 
 106         /*
 107          * deal with results of test
 108          * if anything has called failure("foo") testFailed will be true
 109          */
 110         if (!testFailed) {
 111             println("NewInstanceTest: passed");
 112         } else {
 113             throw new Exception("NewInstanceTest: failed");
 114         }
 115     }
 116 }