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 type table attribute test.
  27  * @bug 8040097
  28  * @library /tools/lib /tools/javac/lib ../lib
  29  * @modules jdk.compiler/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 LocalVariableTypeTableTest.java
  35  * @run main LocalVariableTypeTableTest
  36  */
  37 
  38 import com.sun.tools.classfile.Code_attribute;
  39 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
  40 
  41 import java.io.IOException;
  42 import java.util.Arrays;
  43 import java.util.Collection;
  44 import java.util.List;
  45 import java.util.function.Supplier;
  46 import java.util.stream.Stream;
  47 
  48 import static java.util.stream.Collectors.toList;
  49 
  50 public class LocalVariableTypeTableTest<THIS> extends LocalVariableTestBase {
  51 
  52     public LocalVariableTypeTableTest(Class<?> clazz) {
  53         super(clazz);
  54     }
  55 
  56     public static void main(String[] args) throws IOException {
  57         new LocalVariableTypeTableTest(LocalVariableTypeTableTest.class).test();
  58     }
  59 
  60     @Override
  61     protected List<VariableTable> getVariableTables(Code_attribute codeAttribute) {
  62         return Stream.of(codeAttribute.attributes.attrs)
  63                 .filter(at -> at instanceof LocalVariableTypeTable_attribute)
  64                 .map(at -> (LocalVariableTypeTable_attribute) at)
  65                 .map(LocalVariableTypeTable::new).collect(toList());
  66     }
  67 
  68     @ExpectedLocals(name = "list", type = "TT;")
  69     @ExpectedLocals(name = "p", type = "[TP;")
  70     @ExpectedLocals(name = "k", type = "TK;")
  71     @ExpectedLocals(name = "c1", type = "Ljava/util/Collection<-Ljava/lang/Integer;>;")
  72     @ExpectedLocals(name = "c2", type = "Ljava/util/Collection<*>;")
  73     @ExpectedLocals(name = "c3", type = "Ljava/util/Collection<+TE;>;")
  74     public <T extends List<Integer>, P, K extends Integer, E extends Supplier & Runnable>
  75     void genericTypeWithParametersOnly(K k, T list, P[] p,
  76                                        Collection<? super Integer> c1,
  77                                        Collection<?> c2, Collection<? extends E> c3) {
  78     }
  79 
  80     @ExpectedLocals(name = "list", type = "TT;")
  81     @ExpectedLocals(name = "p", type = "[TP;")
  82     @ExpectedLocals(name = "k", type = "TK;")
  83     @ExpectedLocals(name = "c1", type = "Ljava/util/Collection<-Ljava/lang/Integer;>;")
  84     @ExpectedLocals(name = "c2", type = "Ljava/util/Collection<*>;")
  85     @ExpectedLocals(name = "c3", type = "Ljava/util/Collection<+TE;>;")
  86     public <T extends List<Integer>, P, K extends Integer, E extends Supplier & Runnable>
  87     void genericType(K k, T list, P[] p) {
  88         Collection<? super Integer> c1 = null;
  89         Collection<?> c2 = null;
  90         Collection<? extends E> c3 = null;
  91     }
  92 
  93     @ExpectedLocals(name = "list", type = "TT;")
  94     @ExpectedLocals(name = "p", type = "[[TP;")
  95     public <T extends List<Integer>, P, K extends Integer> void genericTypeWithoutParameters() {
  96         T list = null;
  97         list.add(1);
  98         int i = 0;
  99         P[][] p = null;
 100     }
 101 
 102     @ExpectedLocals(name = "this", type = "LLocalVariableTypeTableTest<TTHIS;>;")
 103     public void genericThis() {
 104     }
 105 
 106     @ExpectedLocals(name = "this", type = "LLocalVariableTypeTableTest<TTHIS;>;")
 107     @ExpectedLocals(name = "inWhile", type = "TTHIS;")
 108     @ExpectedLocals(name = "inTry", type = "TTHIS;")
 109     @ExpectedLocals(name = "inSync", type = "TTHIS;")
 110     @ExpectedLocals(name = "inDo", type = "TTHIS;")
 111     @ExpectedLocals(name = "inFor", type = "LLocalVariableTypeTableTest<-TTHIS;>;")
 112     @ExpectedLocals(name = "s", type = "Ljava/util/stream/Stream<+Ljava/lang/Integer;>;")
 113     public void deepScope() {
 114         {
 115             while (true) {
 116                 THIS inWhile = null;
 117                 for (LocalVariableTypeTableTest<? super THIS> inFor : Arrays.asList(this)) {
 118                     try (Stream<? extends Integer> s = Stream.of(0)) {
 119                         THIS inTry = null;
 120                         synchronized (this) {
 121                             THIS inSync = null;
 122                             do {
 123                                 THIS inDo = null;
 124                                 switch (1) {
 125                                     default:
 126                                         THIS inSwitch = null;
 127                                 }
 128                             } while (true);
 129                         }
 130                     }
 131                 }
 132             }
 133         }
 134     }
 135 
 136     class LocalVariableTypeTable implements VariableTable {
 137 
 138         final LocalVariableTypeTable_attribute att;
 139 
 140 
 141         public LocalVariableTypeTable(LocalVariableTypeTable_attribute att) {
 142             this.att = att;
 143         }
 144 
 145         @Override
 146         public int localVariableTableLength() {
 147             return att.local_variable_table_length;
 148         }
 149 
 150         @Override
 151         public List<Entry> entries() {
 152             return Stream.of(att.local_variable_table).map(LocalVariableTypeTableEntry::new).collect(toList());
 153         }
 154 
 155         @Override
 156         public int attributeLength() {
 157             return att.attribute_length;
 158         }
 159 
 160         private class LocalVariableTypeTableEntry implements Entry {
 161 
 162             final LocalVariableTypeTable_attribute.Entry entry;
 163 
 164             private LocalVariableTypeTableEntry(LocalVariableTypeTable_attribute.Entry entry) {
 165                 this.entry = entry;
 166             }
 167 
 168             @Override
 169             public int index() {
 170                 return entry.index;
 171             }
 172 
 173             @Override
 174             public int startPC() {
 175                 return entry.start_pc;
 176             }
 177 
 178             @Override
 179             public int length() {
 180                 return entry.length;
 181             }
 182 
 183             @Override
 184             public String name() {
 185                 return getString(entry.name_index);
 186             }
 187 
 188             @Override
 189             public String type() {
 190                 return getString(entry.signature_index);
 191             }
 192 
 193             @Override
 194             public String toString() {
 195                 return dump();
 196             }
 197         }
 198     }
 199 }