1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.turbine.binder;
import static com.google.common.truth.Truth.assertThat;
import static com.google.turbine.testing.TestClassPaths.TURBINE_BOOTCLASSPATH;
import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertThrows;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.turbine.binder.bound.SourceTypeBoundClass;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.diag.TurbineError;
import com.google.turbine.lower.IntegrationTestSupport;
import com.google.turbine.model.TurbineElementType;
import com.google.turbine.model.TurbineFlag;
import com.google.turbine.parse.Parser;
import com.google.turbine.tree.Tree;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BinderTest {
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void hello() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package a;", //
"public class A {",
" public class Inner1 extends b.B {",
" }",
" public class Inner2 extends A.Inner1 {",
" }",
"}"),
parseLines(
"package b;", //
"import a.A;",
"public class B extends A {",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
assertThat(bound.keySet())
.containsExactly(
new ClassSymbol("a/A"),
new ClassSymbol("a/A$Inner1"),
new ClassSymbol("a/A$Inner2"),
new ClassSymbol("b/B"));
SourceTypeBoundClass a = getBoundClass(bound, "a/A");
assertThat(a.superclass()).isEqualTo(new ClassSymbol("java/lang/Object"));
assertThat(a.interfaces()).isEmpty();
assertThat(getBoundClass(bound, "a/A$Inner1").superclass()).isEqualTo(new ClassSymbol("b/B"));
assertThat(getBoundClass(bound, "a/A$Inner2").superclass())
.isEqualTo(new ClassSymbol("a/A$Inner1"));
SourceTypeBoundClass b = getBoundClass(bound, "b/B");
assertThat(b.superclass()).isEqualTo(new ClassSymbol("a/A"));
}
@Test
public void interfaces() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package com.i;", //
"public interface I {",
" public class IInner {",
" }",
"}"),
parseLines(
"package b;", //
"class B implements com.i.I {",
" class BInner extends IInner {}",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
assertThat(bound.keySet())
.containsExactly(
new ClassSymbol("com/i/I"),
new ClassSymbol("com/i/I$IInner"),
new ClassSymbol("b/B"),
new ClassSymbol("b/B$BInner"));
assertThat(getBoundClass(bound, "b/B").interfaces())
.containsExactly(new ClassSymbol("com/i/I"));
assertThat(getBoundClass(bound, "b/B$BInner").superclass())
.isEqualTo(new ClassSymbol("com/i/I$IInner"));
assertThat(getBoundClass(bound, "b/B$BInner").interfaces()).isEmpty();
}
@Test
public void imports() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package com.test;", //
"public class Test {",
" public static class Inner {}",
"}"),
parseLines(
"package other;", //
"import com.test.Test.Inner;",
"import no.such.Class;", // imports are resolved lazily on-demand
"public class Foo extends Inner {",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
assertThat(getBoundClass(bound, "other/Foo").superclass())
.isEqualTo(new ClassSymbol("com/test/Test$Inner"));
}
@Test
public void cycle() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package a;", //
"import b.B;",
"public class A extends B.Inner {",
" class Inner {}",
"}"),
parseLines(
"package b;", //
"import a.A;",
"public class B extends A.Inner {",
" class Inner {}",
"}"));
TurbineError e =
assertThrows(
TurbineError.class,
() ->
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty()));
assertThat(e).hasMessageThat().contains("cycle in class hierarchy: a.A -> b.B -> a.A");
}
@Test
public void annotationDeclaration() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package com.test;", //
"public @interface Annotation {",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
SourceTypeBoundClass a = getBoundClass(bound, "com/test/Annotation");
assertThat(a.access())
.isEqualTo(
TurbineFlag.ACC_PUBLIC
| TurbineFlag.ACC_INTERFACE
| TurbineFlag.ACC_ABSTRACT
| TurbineFlag.ACC_ANNOTATION);
assertThat(a.superclass()).isEqualTo(new ClassSymbol("java/lang/Object"));
assertThat(a.interfaces()).containsExactly(new ClassSymbol("java/lang/annotation/Annotation"));
}
@Test
public void helloBytecode() throws Exception {
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"package a;", //
"import java.util.Map.Entry;",
"public class A implements Entry {",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of()),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
SourceTypeBoundClass a = getBoundClass(bound, "a/A");
assertThat(a.interfaces()).containsExactly(new ClassSymbol("java/util/Map$Entry"));
}
@Test
public void incompleteClasspath() throws Exception {
Map<String, byte[]> lib =
IntegrationTestSupport.runJavac(
ImmutableMap.of(
"A.java", "class A {}",
"B.java", "class B extends A {}"),
ImmutableList.of());
// create a jar containing only B
Path libJar = temporaryFolder.newFile("lib.jar").toPath();
try (OutputStream os = Files.newOutputStream(libJar);
JarOutputStream jos = new JarOutputStream(os)) {
jos.putNextEntry(new JarEntry("B.class"));
jos.write(requireNonNull(lib.get("B")));
}
ImmutableList<Tree.CompUnit> units =
ImmutableList.of(
parseLines(
"import java.lang.annotation.Target;",
"import java.lang.annotation.ElementType;",
"public class C extends B {",
" @Target(ElementType.TYPE_USE)",
" @interface A {};",
"}"));
ImmutableMap<ClassSymbol, SourceTypeBoundClass> bound =
Binder.bind(
units,
ClassPathBinder.bindClasspath(ImmutableList.of(libJar)),
TURBINE_BOOTCLASSPATH,
/* moduleVersion=*/ Optional.empty())
.units();
SourceTypeBoundClass a = getBoundClass(bound, "C$A");
assertThat(a.annotationMetadata().target()).containsExactly(TurbineElementType.TYPE_USE);
}
private Tree.CompUnit parseLines(String... lines) {
return Parser.parse(Joiner.on('\n').join(lines));
}
private static SourceTypeBoundClass getBoundClass(
Map<ClassSymbol, SourceTypeBoundClass> bound, String name) {
// requireNonNull is safe as long as we call this method with classes that exist in our sources.
return requireNonNull(bound.get(new ClassSymbol(name)));
}
}
|