1 /*
   2  * Copyright (c) 2002, 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 4349534 4690242 4695338
  27  *  @summary regression - bad LocalVariableTable attribute when no initialization needed
  28  *
  29  *  @author Tim Bell
  30  *
  31  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  32  *  @run compile -g GetLocalVariables2Test.java
  33  *  @run driver GetLocalVariables2Test
  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 class GetLocalVariables2Targ {
  44     static boolean bar(int i) {
  45         if (i < 2) {
  46             return true;
  47         } else {
  48             return false;
  49         }
  50     }
  51 
  52     public static void main(String[] args) {
  53         int i = 1;
  54         String command;
  55         if (i == 0) {
  56             command = "0";
  57         } else if (bar(i)) {
  58             command = "1";
  59         } else {
  60             command = "2";
  61         }
  62     }
  63 }
  64 
  65     /********** test program **********/
  66 
  67 public class GetLocalVariables2Test extends TestScaffold {
  68     ReferenceType targetClass;
  69     ThreadReference mainThread;
  70 
  71     GetLocalVariables2Test (String args[]) {
  72         super(args);
  73     }
  74 
  75     public static void main(String[] args)      throws Exception {
  76         new GetLocalVariables2Test(args).startTests();
  77     }
  78 
  79     protected void runTests() throws Exception {
  80         /*
  81          * Get to the top of main()
  82          * to determine targetClass and mainThread
  83          */
  84         BreakpointEvent bpe = startToMain("GetLocalVariables2Targ");
  85         targetClass = bpe.location().declaringType();
  86         mainThread = bpe.thread();
  87         EventRequestManager erm = vm().eventRequestManager();
  88 
  89         bpe = resumeTo("GetLocalVariables2Targ", "bar", "(I)Z");
  90 
  91         /*
  92          * Inspect the stack frame for main(), not bar()...
  93          */
  94         StackFrame frame = bpe.thread().frame(1);
  95         List localVars = frame.visibleVariables();
  96         System.out.println("    Visible variables at this point are: ");
  97         for (Iterator it = localVars.iterator(); it.hasNext();) {
  98             LocalVariable lv = (LocalVariable) it.next();
  99             System.out.print(lv.name());
 100             System.out.print(" typeName: ");
 101             System.out.print(lv.typeName());
 102             System.out.print(" signature: ");
 103             System.out.print(lv.type().signature());
 104             System.out.print(" primitive type: ");
 105             System.out.println(lv.type().name());
 106 
 107             if("command".equals(lv.name())) {
 108                 failure("Failure: LocalVariable \"command\" should not be visible at this point.");
 109                 if (lv.isVisible(frame)) {
 110                     System.out.println("Failure: \"command.isvisible(frame)\" returned true.");
 111                 }
 112             }
 113         }
 114 
 115         /*
 116          * resume the target listening for events
 117          */
 118         listenUntilVMDisconnect();
 119 
 120         /*
 121          * deal with results of test
 122          * if anything has called failure("foo") testFailed will be true
 123          */
 124         if (!testFailed) {
 125             println("GetLocalVariables2Test: passed");
 126         } else {
 127             throw new Exception("GetLocalVariables2Test: failed");
 128         }
 129     }
 130 }