8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute
Reviewed-by: jjg, shurailine, emc
This commit is contained in:
parent
f77173467d
commit
230b553e37
@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary local variable table attribute test.
|
||||
* @bug 8040097
|
||||
* @library /tools/javac/lib ../lib
|
||||
* @build LocalVariableTestBase TestBase InMemoryFileManager ToolBox
|
||||
* @compile -g LocalVariableTableTest.java
|
||||
* @run main LocalVariableTableTest
|
||||
*/
|
||||
|
||||
import com.sun.tools.classfile.Code_attribute;
|
||||
import com.sun.tools.classfile.LocalVariableTable_attribute;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class LocalVariableTableTest extends LocalVariableTestBase {
|
||||
|
||||
public LocalVariableTableTest(Class<?> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new LocalVariableTableTest(LocalVariableTableTest.class).test();
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "l", type = "D")
|
||||
@ExpectedLocals(name = "i", type = "J")
|
||||
public static void onlyTwoCellParameters(double l, long i) {
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "l", type = "D")
|
||||
@ExpectedLocals(name = "dl", type = "D")
|
||||
@ExpectedLocals(name = "i", type = "J")
|
||||
@ExpectedLocals(name = "il", type = "J")
|
||||
@ExpectedLocals(name = "d", type = "J")
|
||||
@ExpectedLocals(name = "ll", type = "J")
|
||||
public static void onlyTwoCellLocals(double l, long i, long d) {
|
||||
double dl = 1.1;
|
||||
long il = 1;
|
||||
long ll = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<VariableTable> getVariableTables(Code_attribute codeAttribute) {
|
||||
return Stream.of(codeAttribute.attributes.attrs)
|
||||
.filter(at -> at instanceof LocalVariableTable_attribute)
|
||||
.map(at -> (LocalVariableTable_attribute) at)
|
||||
.map((t) -> new LocalVariableTable(t)).collect(toList());
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "l", type = "J")
|
||||
@ExpectedLocals(name = "i", type = "I")
|
||||
@ExpectedLocals(name = "d", type = "D")
|
||||
@ExpectedLocals(name = "ll", type = "J")
|
||||
@ExpectedLocals(name = "obj", type = "Ljava/lang/Object;")
|
||||
@ExpectedLocals(name = "dd", type = "D")
|
||||
@ExpectedLocals(name = "bb", type = "B")
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTableTest;")
|
||||
public double longDoubleOverlap(long l, int i, double d) {
|
||||
long ll = 1L;
|
||||
Object obj = 2;
|
||||
double dd = 3.0;
|
||||
byte bb = 0;
|
||||
return l + i + d + ll + Integer.valueOf(obj.toString()) + dd + bb;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "bool", type = "Z")
|
||||
@ExpectedLocals(name = "b", type = "B")
|
||||
@ExpectedLocals(name = "ch", type = "C")
|
||||
@ExpectedLocals(name = "sh", type = "S")
|
||||
@ExpectedLocals(name = "i", type = "I")
|
||||
@ExpectedLocals(name = "l", type = "J")
|
||||
@ExpectedLocals(name = "d", type = "D")
|
||||
@ExpectedLocals(name = "f", type = "F")
|
||||
@ExpectedLocals(name = "ref", type = "Ljava/lang/Integer;")
|
||||
@ExpectedLocals(name = "arr", type = "[Ljava/lang/Integer;")
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTableTest;")
|
||||
public void allTypesWithoutParameters() {
|
||||
boolean bool = true;
|
||||
byte b = 0x1;
|
||||
char ch = 'a';
|
||||
short sh = 1_1;
|
||||
int i = -2;
|
||||
long l = 1L;
|
||||
float f = 1.1f;
|
||||
double d = 0.1;
|
||||
Integer ref = 2;
|
||||
Integer[] arr = null;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "bool", type = "Z")
|
||||
@ExpectedLocals(name = "b", type = "B")
|
||||
@ExpectedLocals(name = "ch", type = "C")
|
||||
@ExpectedLocals(name = "sh", type = "S")
|
||||
@ExpectedLocals(name = "i", type = "I")
|
||||
@ExpectedLocals(name = "l", type = "J")
|
||||
@ExpectedLocals(name = "d", type = "D")
|
||||
@ExpectedLocals(name = "f", type = "F")
|
||||
@ExpectedLocals(name = "ref", type = "Ljava/lang/Integer;")
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTableTest;")
|
||||
public void allTypesWithParameters(boolean bool, byte b, char ch) {
|
||||
short sh = 1_1;
|
||||
int i = -2;
|
||||
long l = 1L;
|
||||
float f = 1.1f;
|
||||
double d = 0.1;
|
||||
Integer ref = 2;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "list", type = "Ljava/util/List;")
|
||||
@ExpectedLocals(name = "list2", type = "[Ljava/util/List;")
|
||||
@ExpectedLocals(name = "p", type = "Ljava/lang/Object;")
|
||||
@ExpectedLocals(name = "k", type = "Ljava/lang/Integer;")
|
||||
@ExpectedLocals(name = "i", type = "I")
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTableTest;")
|
||||
public <T extends List<Integer>, P, K extends Integer> void genericType(K k) {
|
||||
T list = null;
|
||||
int i = 0;
|
||||
P p = null;
|
||||
List<T>[] list2 = null;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTableTest;")
|
||||
@ExpectedLocals(name = "inWhile", type = "I")
|
||||
@ExpectedLocals(name = "inTry", type = "D")
|
||||
@ExpectedLocals(name = "inSync", type = "F")
|
||||
@ExpectedLocals(name = "inDo", type = "B")
|
||||
@ExpectedLocals(name = "inSwitch", type = "S")
|
||||
@ExpectedLocals(name = "inFor", type = "J")
|
||||
@ExpectedLocals(name = "s", type = "Ljava/util/stream/Stream;")
|
||||
public void deepScope() {
|
||||
{
|
||||
while (true) {
|
||||
int inWhile = 0;
|
||||
for (long inFor : Arrays.asList(0)) {
|
||||
try (Stream<? extends Integer> s = Stream.of(0)) {
|
||||
double inTry = 0.0;
|
||||
synchronized (this) {
|
||||
float inSync = -1.0f;
|
||||
do {
|
||||
byte inDo = 0;
|
||||
switch (1) {
|
||||
default:
|
||||
short inSwitch = 100;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "i", type = "I", scope = 0)
|
||||
@ExpectedLocals(name = "i", type = "J", scope = 1)
|
||||
public void reuseByLong() {
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
{
|
||||
long i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
class LocalVariableTable implements VariableTable {
|
||||
|
||||
final LocalVariableTable_attribute att;
|
||||
|
||||
public LocalVariableTable(LocalVariableTable_attribute att) {
|
||||
this.att = att;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int localVariableTableLength() {
|
||||
return att.local_variable_table_length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Entry> entries() {
|
||||
return Stream.of(att.local_variable_table).map(LocalVariableTableEntry::new).collect(toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attributeLength() {
|
||||
return att.attribute_length;
|
||||
}
|
||||
|
||||
private class LocalVariableTableEntry implements Entry {
|
||||
|
||||
final LocalVariableTable_attribute.Entry entry;
|
||||
|
||||
private LocalVariableTableEntry(LocalVariableTable_attribute.Entry entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int index() {
|
||||
return entry.index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int startPC() {
|
||||
return entry.start_pc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return entry.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return getString(entry.name_index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String type() {
|
||||
return getString(entry.descriptor_index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return dump();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import com.sun.tools.classfile.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Repeatable;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static java.util.stream.Collectors.*;
|
||||
|
||||
|
||||
public abstract class LocalVariableTestBase extends TestBase {
|
||||
public static final int DEFAULT_SCOPE = 0;
|
||||
private final ClassFile classFile;
|
||||
private final Class<?> clazz;
|
||||
|
||||
protected abstract List<VariableTable> getVariableTables(Code_attribute codeAttribute);
|
||||
|
||||
public LocalVariableTestBase(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
try {
|
||||
this.classFile = ClassFile.read(getClassFile(clazz));
|
||||
} catch (IOException | ConstantPoolException e) {
|
||||
throw new IllegalArgumentException("Can't read classfile for specified class", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//info in the LocalVariableTable attribute is compared against expected info stored in annotations
|
||||
public void test() throws IOException {
|
||||
List<java.lang.reflect.Method> testMethods = Stream.of(clazz.getDeclaredMethods())
|
||||
.filter(m -> m.getAnnotationsByType(ExpectedLocals.class).length > 0)
|
||||
.collect(toList());
|
||||
int failed = 0;
|
||||
for (java.lang.reflect.Method method : testMethods) {
|
||||
try {
|
||||
Map<String, String> expectedLocals2Types = new HashMap<>();
|
||||
Map<String, Integer> sig2scope = new HashMap<>();
|
||||
for (ExpectedLocals anno : method.getDeclaredAnnotationsByType(ExpectedLocals.class)) {
|
||||
expectedLocals2Types.put(anno.name(), anno.type());
|
||||
sig2scope.put(anno.name() + "&" + anno.type(), anno.scope());
|
||||
}
|
||||
|
||||
test(method.getName(), expectedLocals2Types, sig2scope);
|
||||
} catch (AssertionFailedException ex) {
|
||||
System.err.printf("Test %s failed.%n", method.getName());
|
||||
ex.printStackTrace();
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
if (failed > 0)
|
||||
throw new RuntimeException(format("Failed %d out of %d. See logs.", failed, testMethods.size()));
|
||||
}
|
||||
|
||||
public void test(String methodName, Map<String, String> expectedLocals2Types, Map<String, Integer> sig2scope)
|
||||
throws IOException {
|
||||
|
||||
for (Method m : classFile.methods) {
|
||||
String mName = getString(m.name_index);
|
||||
if (methodName.equals(mName)) {
|
||||
System.out.println("Testing local variable table in method " + mName);
|
||||
Code_attribute code_attribute = (Code_attribute) m.attributes.get(Attribute.Code);
|
||||
|
||||
List<? extends VariableTable> variableTables = getVariableTables(code_attribute);
|
||||
generalLocalVariableTableCheck(variableTables);
|
||||
|
||||
List<VariableTable.Entry> entries = variableTables.stream()
|
||||
.flatMap(table -> table.entries().stream())
|
||||
.collect(toList());
|
||||
|
||||
generalEntriesCheck(entries, code_attribute);
|
||||
assertIndexesAreUnique(entries, sig2scope);
|
||||
checkNamesAndTypes(entries, expectedLocals2Types);
|
||||
checkDoubleAndLongIndexes(entries, sig2scope, code_attribute.max_locals);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void generalLocalVariableTableCheck(List<? extends VariableTable> variableTables) {
|
||||
for (VariableTable localTable : variableTables) {
|
||||
//only one per variable.
|
||||
assertEquals(localTable.localVariableTableLength(),
|
||||
localTable.entries().size(), "Incorrect local variable table length");
|
||||
//attribute length is offset(line_number_table_length) + element_size*element_count
|
||||
assertEquals(localTable.attributeLength(),
|
||||
2 + (5 * 2) * localTable.localVariableTableLength(), "Incorrect attribute length");
|
||||
}
|
||||
}
|
||||
|
||||
private void generalEntriesCheck(List<VariableTable.Entry> entries, Code_attribute code_attribute) {
|
||||
for (VariableTable.Entry e : entries) {
|
||||
assertTrue(e.index() >= 0 && e.index() < code_attribute.max_locals,
|
||||
"Index " + e.index() + " out of variable array. Size of array is " + code_attribute.max_locals);
|
||||
assertTrue(e.startPC() >= 0, "StartPC is less then 0. StartPC = " + e.startPC());
|
||||
assertTrue(e.length() >= 0, "Length is less then 0. Length = " + e.length());
|
||||
assertTrue(e.startPC() + e.length() <= code_attribute.code_length,
|
||||
format("StartPC+Length > code length.%n" +
|
||||
"%s%n" +
|
||||
"code_length = %s"
|
||||
, e, code_attribute.code_length));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkNamesAndTypes(List<LocalVariableTableTest.LocalVariableTable.Entry> entries,
|
||||
Map<String, String> expectedLocals2Types) {
|
||||
Map<String, List<String>> actualNames2Types = entries.stream()
|
||||
.collect(
|
||||
groupingBy(VariableTable.Entry::name,
|
||||
mapping(VariableTable.Entry::type, toList())));
|
||||
for (Map.Entry<String, String> name2type : expectedLocals2Types.entrySet()) {
|
||||
String name = name2type.getKey();
|
||||
String type = name2type.getValue();
|
||||
|
||||
assertTrue(actualNames2Types.containsKey(name),
|
||||
format("There is no record for local variable %s%nEntries: %s", name, entries));
|
||||
|
||||
assertTrue(actualNames2Types.get(name).contains(type),
|
||||
format("Types are different for local variable %s%nExpected type: %s%nActual type: %s",
|
||||
name, type, actualNames2Types.get(name)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void assertIndexesAreUnique(Collection<VariableTable.Entry> entries, Map<String, Integer> scopes) {
|
||||
//check every scope separately
|
||||
Map<Object, List<VariableTable.Entry>> entriesByScope = groupByScope(entries, scopes);
|
||||
for (Map.Entry<Object, List<VariableTable.Entry>> mapEntry : entriesByScope.entrySet()) {
|
||||
mapEntry.getValue().stream()
|
||||
.collect(groupingBy(VariableTable.Entry::index))
|
||||
.entrySet()
|
||||
.forEach(e ->
|
||||
assertTrue(e.getValue().size() == 1,
|
||||
"Multiple variables point to the same index in common scope. " + e.getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkDoubleAndLongIndexes(Collection<LocalVariableTableTest.LocalVariableTable.Entry> entries,
|
||||
Map<String, Integer> scopes, int maxLocals) {
|
||||
//check every scope separately
|
||||
Map<Object, List<VariableTable.Entry>> entriesByScope = groupByScope(entries, scopes);
|
||||
for (List<VariableTable.Entry> entryList : entriesByScope.values()) {
|
||||
Map<Integer, VariableTable.Entry> index2Entry = entryList.stream()
|
||||
.collect(toMap(VariableTable.Entry::index, e -> e));
|
||||
|
||||
entryList.stream()
|
||||
.filter(e -> "J".equals(e.type()) || "D".equals(e.type()))
|
||||
.forEach(e -> {
|
||||
assertTrue(e.index() + 1 < maxLocals,
|
||||
format("Index %s is out of variable array. Long and double occupy 2 cells." +
|
||||
" Size of array is %d", e.index() + 1, maxLocals));
|
||||
assertTrue(!index2Entry.containsKey(e.index() + 1),
|
||||
format("An entry points to the second cell of long/double entry.%n%s%n%s", e,
|
||||
index2Entry.get(e.index() + 1)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Object, List<VariableTable.Entry>> groupByScope(
|
||||
Collection<LocalVariableTableTest.LocalVariableTable.Entry> entries, Map<String, Integer> scopes) {
|
||||
return entries.stream().collect(groupingBy(e -> scopes.getOrDefault(e.name() + "&" + e.type(), DEFAULT_SCOPE)));
|
||||
}
|
||||
|
||||
protected String getString(int i) {
|
||||
try {
|
||||
return classFile.constant_pool.getUTF8Info(i).value;
|
||||
} catch (ConstantPool.InvalidIndex | ConstantPool.UnexpectedEntry ex) {
|
||||
ex.printStackTrace();
|
||||
throw new AssertionFailedException("Issue while reading constant pool");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface VariableTable {
|
||||
|
||||
int localVariableTableLength();
|
||||
|
||||
List<LocalVariableTableTest.VariableTable.Entry> entries();
|
||||
|
||||
int attributeLength();
|
||||
|
||||
interface Entry {
|
||||
|
||||
int index();
|
||||
|
||||
int startPC();
|
||||
|
||||
int length();
|
||||
|
||||
String name();
|
||||
|
||||
String type();
|
||||
|
||||
default String dump() {
|
||||
return format("Entry{" +
|
||||
"%n name = %s" +
|
||||
"%n type = %s" +
|
||||
"%n index = %d" +
|
||||
"%n startPC = %d" +
|
||||
"%n length = %d" +
|
||||
"%n}", name(), type(), index(), startPC(), length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Repeatable(Container.class)
|
||||
@interface ExpectedLocals {
|
||||
String name();
|
||||
|
||||
String type();
|
||||
|
||||
//variables from different scopes can share local variable table index and/or name.
|
||||
int scope() default DEFAULT_SCOPE;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Container {
|
||||
ExpectedLocals[] value();
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary local variable type table attribute test.
|
||||
* @bug 8040097
|
||||
* @library /tools/javac/lib ../lib
|
||||
* @build LocalVariableTestBase TestBase InMemoryFileManager ToolBox
|
||||
* @compile -g LocalVariableTypeTableTest.java
|
||||
* @run main LocalVariableTypeTableTest
|
||||
*/
|
||||
|
||||
import com.sun.tools.classfile.Code_attribute;
|
||||
import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class LocalVariableTypeTableTest<THIS> extends LocalVariableTestBase {
|
||||
|
||||
public LocalVariableTypeTableTest(Class<?> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new LocalVariableTypeTableTest(LocalVariableTypeTableTest.class).test();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<VariableTable> getVariableTables(Code_attribute codeAttribute) {
|
||||
return Stream.of(codeAttribute.attributes.attrs)
|
||||
.filter(at -> at instanceof LocalVariableTypeTable_attribute)
|
||||
.map(at -> (LocalVariableTypeTable_attribute) at)
|
||||
.map(LocalVariableTypeTable::new).collect(toList());
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "list", type = "TT;")
|
||||
@ExpectedLocals(name = "p", type = "[TP;")
|
||||
@ExpectedLocals(name = "k", type = "TK;")
|
||||
@ExpectedLocals(name = "c1", type = "Ljava/util/Collection<-Ljava/lang/Integer;>;")
|
||||
@ExpectedLocals(name = "c2", type = "Ljava/util/Collection<*>;")
|
||||
@ExpectedLocals(name = "c3", type = "Ljava/util/Collection<+TE;>;")
|
||||
public <T extends List<Integer>, P, K extends Integer, E extends Supplier & Runnable>
|
||||
void genericTypeWithParametersOnly(K k, T list, P[] p,
|
||||
Collection<? super Integer> c1,
|
||||
Collection<?> c2, Collection<? extends E> c3) {
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "list", type = "TT;")
|
||||
@ExpectedLocals(name = "p", type = "[TP;")
|
||||
@ExpectedLocals(name = "k", type = "TK;")
|
||||
@ExpectedLocals(name = "c1", type = "Ljava/util/Collection<-Ljava/lang/Integer;>;")
|
||||
@ExpectedLocals(name = "c2", type = "Ljava/util/Collection<*>;")
|
||||
@ExpectedLocals(name = "c3", type = "Ljava/util/Collection<+TE;>;")
|
||||
public <T extends List<Integer>, P, K extends Integer, E extends Supplier & Runnable>
|
||||
void genericType(K k, T list, P[] p) {
|
||||
Collection<? super Integer> c1 = null;
|
||||
Collection<?> c2 = null;
|
||||
Collection<? extends E> c3 = null;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "list", type = "TT;")
|
||||
@ExpectedLocals(name = "p", type = "[[TP;")
|
||||
public <T extends List<Integer>, P, K extends Integer> void genericTypeWithoutParameters() {
|
||||
T list = null;
|
||||
list.add(1);
|
||||
int i = 0;
|
||||
P[][] p = null;
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTypeTableTest<TTHIS;>;")
|
||||
public void genericThis() {
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "this", type = "LLocalVariableTypeTableTest<TTHIS;>;")
|
||||
@ExpectedLocals(name = "inWhile", type = "TTHIS;")
|
||||
@ExpectedLocals(name = "inTry", type = "TTHIS;")
|
||||
@ExpectedLocals(name = "inSync", type = "TTHIS;")
|
||||
@ExpectedLocals(name = "inDo", type = "TTHIS;")
|
||||
@ExpectedLocals(name = "inSwitch", type = "TTHIS;")
|
||||
@ExpectedLocals(name = "inFor", type = "LLocalVariableTypeTableTest<-TTHIS;>;")
|
||||
@ExpectedLocals(name = "s", type = "Ljava/util/stream/Stream<+Ljava/lang/Integer;>;")
|
||||
public void deepScope() {
|
||||
{
|
||||
while (true) {
|
||||
THIS inWhile = null;
|
||||
for (LocalVariableTypeTableTest<? super THIS> inFor : Arrays.asList(this)) {
|
||||
try (Stream<? extends Integer> s = Stream.of(0)) {
|
||||
THIS inTry = null;
|
||||
synchronized (this) {
|
||||
THIS inSync = null;
|
||||
do {
|
||||
THIS inDo = null;
|
||||
switch (1) {
|
||||
default:
|
||||
THIS inSwitch = null;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ExpectedLocals(name = "i", type = "TTHIS;", scope = 0)
|
||||
@ExpectedLocals(name = "i", type = "Ljava/util/List<TTHIS;>;", scope = 1)
|
||||
public void reuseByLong() {
|
||||
{
|
||||
THIS i = null;
|
||||
}
|
||||
{
|
||||
List<THIS> i = null;
|
||||
}
|
||||
}
|
||||
|
||||
class LocalVariableTypeTable implements VariableTable {
|
||||
|
||||
final LocalVariableTypeTable_attribute att;
|
||||
|
||||
|
||||
public LocalVariableTypeTable(LocalVariableTypeTable_attribute att) {
|
||||
this.att = att;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int localVariableTableLength() {
|
||||
return att.local_variable_table_length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Entry> entries() {
|
||||
return Stream.of(att.local_variable_table).map(LocalVariableTypeTableEntry::new).collect(toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attributeLength() {
|
||||
return att.attribute_length;
|
||||
}
|
||||
|
||||
private class LocalVariableTypeTableEntry implements Entry {
|
||||
|
||||
final LocalVariableTypeTable_attribute.Entry entry;
|
||||
|
||||
private LocalVariableTypeTableEntry(LocalVariableTypeTable_attribute.Entry entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int index() {
|
||||
return entry.index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int startPC() {
|
||||
return entry.start_pc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return entry.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return getString(entry.name_index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String type() {
|
||||
return getString(entry.signature_index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return dump();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class TestBase {
|
||||
|
||||
public Map<String, ? extends JavaFileObject> compile(String... sources) throws IOException,
|
||||
CompilationException {
|
||||
return compile(emptyList(), sources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options - compiler options
|
||||
* @param sources
|
||||
* @return map where key is className, value is corresponding ClassFile.
|
||||
* @throws IOException
|
||||
*/
|
||||
public Map<String, ? extends JavaFileObject> compile(List<String> options, String... sources) throws IOException,
|
||||
CompilationException {
|
||||
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
List<? extends JavaFileObject> src = Stream.of(sources).map(ToolBox.JavaSource::new).collect(toList());
|
||||
|
||||
try (InMemoryFileManager fileManager = new InMemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
|
||||
boolean success = compiler.getTask(null, fileManager, null, options, null, src).call();
|
||||
if (!success) throw new CompilationException("Compilation Error");
|
||||
return fileManager.getClasses();
|
||||
}
|
||||
}
|
||||
|
||||
public void assertEquals(Object actual, Object expected, String message) {
|
||||
if (!Objects.equals(actual, expected))
|
||||
throw new AssertionFailedException(format("%s%nGot: %s, Expected: ", message, actual, expected));
|
||||
}
|
||||
|
||||
public void assertNull(Object actual, String message) {
|
||||
assertEquals(actual, null, message);
|
||||
}
|
||||
|
||||
public void assertNotNull(Object actual, String message) {
|
||||
if (Objects.isNull(actual)) {
|
||||
throw new AssertionFailedException(message + " : Expected not null value");
|
||||
}
|
||||
}
|
||||
|
||||
public void assertTrue(boolean actual, String message) {
|
||||
assertEquals(actual, true, message);
|
||||
}
|
||||
|
||||
public File getSourceFile(String fileName) {
|
||||
return new File(System.getProperty("test.src", "."), fileName);
|
||||
}
|
||||
|
||||
public File getClassFile(String fileName) {
|
||||
return new File(System.getProperty("test.classes", TestBase.class.getResource(".").getPath()), fileName);
|
||||
}
|
||||
|
||||
public File getClassFile(Class clazz) {
|
||||
return getClassFile(clazz.getName().replace(".", "/") + ".class");
|
||||
}
|
||||
|
||||
public static class CompilationException extends Exception {
|
||||
|
||||
public CompilationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AssertionFailedException extends RuntimeException {
|
||||
public AssertionFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
88
langtools/test/tools/javac/lib/InMemoryFileManager.java
Normal file
88
langtools/test/tools/javac/lib/InMemoryFileManager.java
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.tools.*;
|
||||
|
||||
import static java.util.Collections.unmodifiableMap;
|
||||
|
||||
/**
|
||||
* class for storing source/byte code in memory.
|
||||
*/
|
||||
public class InMemoryFileManager extends ForwardingJavaFileManager {
|
||||
|
||||
private final Map<String, InMemoryJavaFile> classes = new HashMap<>();
|
||||
|
||||
public InMemoryFileManager(JavaFileManager fileManager) {
|
||||
super(fileManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
|
||||
|
||||
InMemoryJavaFile javaFile = new InMemoryJavaFile(className);
|
||||
classes.put(className, javaFile);
|
||||
return javaFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader(Location location) {
|
||||
return new ClassLoader(this.getClass().getClassLoader()) {
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
InMemoryJavaFile classData = classes.get(name);
|
||||
if (classData == null) throw new ClassNotFoundException(name);
|
||||
byte[] byteCode = classData.bos.toByteArray();
|
||||
return defineClass(name, byteCode, 0, byteCode.length);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Map<String, ? extends JavaFileObject> getClasses() {
|
||||
return unmodifiableMap(classes);
|
||||
}
|
||||
|
||||
private static class InMemoryJavaFile extends SimpleJavaFileObject {
|
||||
|
||||
private final ByteArrayOutputStream bos =
|
||||
new ByteArrayOutputStream();
|
||||
|
||||
|
||||
protected InMemoryJavaFile(String name) {
|
||||
super(URI.create("mfm:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream openOutputStream() throws IOException {
|
||||
return bos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
@ -851,7 +851,7 @@ public class ToolBox {
|
||||
* This method is intended for simple files and uses regular expressions,
|
||||
* so comments matching the pattern can make the method fail.
|
||||
*/
|
||||
private static String getJavaFileNameFromSource(String source) {
|
||||
static String getJavaFileNameFromSource(String source) {
|
||||
String className = null;
|
||||
Matcher matcher = publicClassPattern.matcher(source);
|
||||
if (matcher.find()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user