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