1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 #
   7 # This code is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License version 2 only, as
   9 # published by the Free Software Foundation.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 #  @test
  27 #  @bug 4511950 4843082
  28 #  @summary 1. jdb's expression evaluation doesn't perform string conversion properly
  29 #           2. TTY: run on expression evaluation
  30 #  @author jim/suvasis mukherjee
  31 #
  32 #  @run shell StringConvertTest.sh
  33 
  34 #  Run this script to see the bug.  See comments at the end
  35 #  of the .java file for info on what the bug looks like.
  36 
  37 # These are variables that can be set to control execution
  38 
  39 #pkg=untitled7
  40 classname=StringConvertTest
  41 compileOptions=-g
  42 #java=java_g
  43 #mode=-Xcomp
  44 
  45 #jdbOptions=-dbgtrace
  46 createJavaFile()
  47 {
  48     cat <<EOF > $1.java.1
  49 
  50 class $classname {
  51     String me;
  52     static JJ1 x1;
  53     static JJ2 x2;
  54     static JJ2[] x3 = new JJ2[2];
  55     static String x4 = "abc";
  56     static int ii = 89;
  57     static String grower = "grower";
  58     static StringBuffer sbGrower = new StringBuffer("sbGrower");
  59     int ivar = 89;
  60     $classname(String xx) {
  61         me = xx;
  62     }
  63 
  64     static String fred() {
  65         return "a static method";
  66     }
  67 
  68     void  gus() {
  69         int gusLoc = 1;
  70         StringBuffer sbTim = new StringBuffer("tim");
  71         int kk = 1;                          //@1 breakpoint
  72     }
  73 
  74     static String growit(String extra) {
  75         grower += extra;
  76         return grower;
  77     }
  78 
  79     static String sbGrowit(String extra) {
  80         sbGrower.append(extra);
  81         return sbGrower.toString();
  82     }
  83 
  84     public static void main(String[] args) {
  85         x1 = new JJ1("first JJ1");
  86         x2 = new JJ2("first JJ2");
  87         x3[0] = new JJ2("array0");
  88         x3[1] = new JJ2("array1");
  89         $classname  loc1 = new $classname("first me");
  90         
  91         // These just show what output should look like
  92         System.out.println("x1 = " + x1);
  93         System.out.println("x2 = " + x2);
  94         System.out.println("x3.toString = " + x3.toString());
  95         System.out.println("x4.toString = " + x4.toString());
  96 
  97         // Dont want to call growit since it would change
  98         // the value.
  99 
 100         System.out.println("loc1 = " + loc1);
 101         System.out.println("-" + loc1);
 102         loc1.gus();
 103      }
 104 
 105   // This does not have a toString method
 106   static class JJ1 {
 107     String me;
 108 
 109     JJ1(String whoAmI) {
 110         me = whoAmI;
 111     }
 112   }
 113 
 114   // This has a toString method
 115   static class JJ2 {
 116     String me;
 117 
 118     JJ2(String whoAmI) {
 119         me = whoAmI;
 120     }
 121     public String toString() {
 122         return me;
 123     }
 124 
 125     public int meth1() {
 126         return 89;
 127     }
 128   }
 129 }
 130 
 131 EOF
 132 }
 133 
 134 # This is called to feed cmds to jdb.
 135 dojdbCmds()
 136 {
 137     setBkpts @1
 138     runToBkpt @1
 139 
 140     # Each print without the 'toString()' should print the
 141     # same thing as the following print with the toString().
 142     # The print 1s are just spacers
 143 
 144     cmd print $classname.x1
 145     cmd print "$classname.x1.toString()"
 146     cmd print 1
 147 
 148     cmd print $classname.x2
 149     cmd print "$classname.x2.toString()"
 150     cmd print 1
 151 
 152     # An unreported bug: this isn't handled correctly.
 153     # If we uncomment this line, we will get an 'instance of...'  line
 154     # which will cause the test to fail.
 155     #cmd print "(Object)($classname.x3)"
 156     cmd print "((Object)$classname.x3).toString()"
 157     cmd print 1
 158 
 159     cmd print $classname.x4
 160     cmd print "$classname.x4.toString()"
 161     cmd print 1
 162 
 163     # Make sure jdb doesn't call a method multiple times.
 164     cmd print "$classname.growit(\"xyz\")"
 165     cmd eval  "$classname.sbGrower.append(\"xyz\")"
 166     cmd print 1
 167 
 168     cmd eval "sbTim.toString()"
 169     cmd print 1
 170 
 171     cmd print this
 172     cmd print "this.toString()"
 173     cmd print 1
 174 
 175     # A possible bug is that this ends up with multiple "s
 176     cmd print '"--" + '$classname.x1
 177     cmd print 1
 178 
 179     # This too
 180     cmd print "$classname.x4 + 2"
 181     cmd print 1
 182 
 183     cmd print "this.ivar"
 184     cmd print gusLoc
 185     cmd print 1
 186 }
 187 
 188 mysetup()
 189 {
 190     if [ -z "$TESTSRC" ] ; then
 191         TESTSRC=.
 192     fi
 193 
 194     for ii in . $TESTSRC $TESTSRC/.. ; do
 195         if [ -r "$ii/ShellScaffold.sh" ] ; then
 196             . $ii/ShellScaffold.sh
 197             break
 198         fi
 199     done
 200 }
 201 
 202 # You could replace this next line with the contents
 203 # of ShellScaffold.sh and this script will run just the same.
 204 mysetup
 205 
 206 runit
 207 jdbFailIfPresent '""'
 208 jdbFailIfPresent 'instance of'
 209 jdbFailIfPresent 'xyzxyz'
 210 pass