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