LocalVariableTableTest.java revision 2942:08092deced3f
1/* 2 * Copyright (c) 2014, 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 * @summary local variable table attribute test. 27 * @bug 8040097 28 * @library /tools/lib /tools/javac/lib ../lib 29 * @modules jdk.jdeps/com.sun.tools.classfile 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.file 32 * jdk.compiler/com.sun.tools.javac.main 33 * @build ToolBox LocalVariableTestBase TestBase InMemoryFileManager 34 * @compile -g LocalVariableTableTest.java 35 * @run main LocalVariableTableTest 36 */ 37 38import com.sun.tools.classfile.Code_attribute; 39import com.sun.tools.classfile.LocalVariableTable_attribute; 40 41import java.io.IOException; 42import java.util.Arrays; 43import java.util.List; 44import java.util.stream.Stream; 45 46import static java.util.stream.Collectors.toList; 47 48public class LocalVariableTableTest extends LocalVariableTestBase { 49 50 public LocalVariableTableTest(Class<?> clazz) { 51 super(clazz); 52 } 53 54 public static void main(String[] args) throws IOException { 55 new LocalVariableTableTest(LocalVariableTableTest.class).test(); 56 } 57 58 @ExpectedLocals(name = "l", type = "D") 59 @ExpectedLocals(name = "i", type = "J") 60 public static void onlyTwoCellParameters(double l, long i) { 61 } 62 63 @ExpectedLocals(name = "l", type = "D") 64 @ExpectedLocals(name = "dl", type = "D") 65 @ExpectedLocals(name = "i", type = "J") 66 @ExpectedLocals(name = "il", type = "J") 67 @ExpectedLocals(name = "d", type = "J") 68 @ExpectedLocals(name = "ll", type = "J") 69 public static void onlyTwoCellLocals(double l, long i, long d) { 70 double dl = 1.1; 71 long il = 1; 72 long ll = 1; 73 } 74 75 @Override 76 protected List<VariableTable> getVariableTables(Code_attribute codeAttribute) { 77 return Stream.of(codeAttribute.attributes.attrs) 78 .filter(at -> at instanceof LocalVariableTable_attribute) 79 .map(at -> (LocalVariableTable_attribute) at) 80 .map((t) -> new LocalVariableTable(t)).collect(toList()); 81 } 82 83 @ExpectedLocals(name = "l", type = "J") 84 @ExpectedLocals(name = "i", type = "I") 85 @ExpectedLocals(name = "d", type = "D") 86 @ExpectedLocals(name = "ll", type = "J") 87 @ExpectedLocals(name = "obj", type = "Ljava/lang/Object;") 88 @ExpectedLocals(name = "dd", type = "D") 89 @ExpectedLocals(name = "bb", type = "B") 90 @ExpectedLocals(name = "this", type = "LLocalVariableTableTest;") 91 public double longDoubleOverlap(long l, int i, double d) { 92 long ll = 1L; 93 Object obj = 2; 94 double dd = 3.0; 95 byte bb = 0; 96 return l + i + d + ll + Integer.valueOf(obj.toString()) + dd + bb; 97 } 98 99 @ExpectedLocals(name = "bool", type = "Z") 100 @ExpectedLocals(name = "b", type = "B") 101 @ExpectedLocals(name = "ch", type = "C") 102 @ExpectedLocals(name = "sh", type = "S") 103 @ExpectedLocals(name = "i", type = "I") 104 @ExpectedLocals(name = "l", type = "J") 105 @ExpectedLocals(name = "d", type = "D") 106 @ExpectedLocals(name = "f", type = "F") 107 @ExpectedLocals(name = "ref", type = "Ljava/lang/Integer;") 108 @ExpectedLocals(name = "arr", type = "[Ljava/lang/Integer;") 109 @ExpectedLocals(name = "this", type = "LLocalVariableTableTest;") 110 public void allTypesWithoutParameters() { 111 boolean bool = true; 112 byte b = 0x1; 113 char ch = 'a'; 114 short sh = 1_1; 115 int i = -2; 116 long l = 1L; 117 float f = 1.1f; 118 double d = 0.1; 119 Integer ref = 2; 120 Integer[] arr = null; 121 } 122 123 @ExpectedLocals(name = "bool", type = "Z") 124 @ExpectedLocals(name = "b", type = "B") 125 @ExpectedLocals(name = "ch", type = "C") 126 @ExpectedLocals(name = "sh", type = "S") 127 @ExpectedLocals(name = "i", type = "I") 128 @ExpectedLocals(name = "l", type = "J") 129 @ExpectedLocals(name = "d", type = "D") 130 @ExpectedLocals(name = "f", type = "F") 131 @ExpectedLocals(name = "ref", type = "Ljava/lang/Integer;") 132 @ExpectedLocals(name = "this", type = "LLocalVariableTableTest;") 133 public void allTypesWithParameters(boolean bool, byte b, char ch) { 134 short sh = 1_1; 135 int i = -2; 136 long l = 1L; 137 float f = 1.1f; 138 double d = 0.1; 139 Integer ref = 2; 140 } 141 142 @ExpectedLocals(name = "list", type = "Ljava/util/List;") 143 @ExpectedLocals(name = "list2", type = "[Ljava/util/List;") 144 @ExpectedLocals(name = "p", type = "Ljava/lang/Object;") 145 @ExpectedLocals(name = "k", type = "Ljava/lang/Integer;") 146 @ExpectedLocals(name = "i", type = "I") 147 @ExpectedLocals(name = "this", type = "LLocalVariableTableTest;") 148 public <T extends List<Integer>, P, K extends Integer> void genericType(K k) { 149 T list = null; 150 int i = 0; 151 P p = null; 152 List<T>[] list2 = null; 153 } 154 155 @ExpectedLocals(name = "this", type = "LLocalVariableTableTest;") 156 @ExpectedLocals(name = "inWhile", type = "I") 157 @ExpectedLocals(name = "inTry", type = "D") 158 @ExpectedLocals(name = "inSync", type = "F") 159 @ExpectedLocals(name = "inDo", type = "B") 160 @ExpectedLocals(name = "inFor", type = "J") 161 @ExpectedLocals(name = "s", type = "Ljava/util/stream/Stream;") 162 public void deepScope() { 163 { 164 while (true) { 165 int inWhile = 0; 166 for (long inFor : Arrays.asList(0)) { 167 try (Stream<? extends Integer> s = Stream.of(0)) { 168 double inTry = 0.0; 169 synchronized (this) { 170 float inSync = -1.0f; 171 do { 172 byte inDo = 0; 173 switch (1) { 174 default: 175 short inSwitch = 100; 176 } 177 } while (true); 178 } 179 } 180 } 181 } 182 } 183 } 184 185 class LocalVariableTable implements VariableTable { 186 187 final LocalVariableTable_attribute att; 188 189 public LocalVariableTable(LocalVariableTable_attribute att) { 190 this.att = att; 191 } 192 193 @Override 194 public int localVariableTableLength() { 195 return att.local_variable_table_length; 196 } 197 198 @Override 199 public List<Entry> entries() { 200 return Stream.of(att.local_variable_table).map(LocalVariableTableEntry::new).collect(toList()); 201 } 202 203 @Override 204 public int attributeLength() { 205 return att.attribute_length; 206 } 207 208 private class LocalVariableTableEntry implements Entry { 209 210 final LocalVariableTable_attribute.Entry entry; 211 212 private LocalVariableTableEntry(LocalVariableTable_attribute.Entry entry) { 213 this.entry = entry; 214 } 215 216 @Override 217 public int index() { 218 return entry.index; 219 } 220 221 @Override 222 public int startPC() { 223 return entry.start_pc; 224 } 225 226 @Override 227 public int length() { 228 return entry.length; 229 } 230 231 @Override 232 public String name() { 233 return getString(entry.name_index); 234 } 235 236 @Override 237 public String type() { 238 return getString(entry.descriptor_index); 239 } 240 241 @Override 242 public String toString() { 243 return dump(); 244 } 245 } 246 } 247} 248