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