Browse Source

Polish TestCompiler support

pull/28896/head
Sam Brannen 4 years ago
parent
commit
3912ef1507
  1. 1
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccess.java
  2. 1
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccessClassLoader.java
  3. 1
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccessExtension.java
  4. 4
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/Compiled.java
  5. 3
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/DynamicClassFileObject.java
  6. 1
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/DynamicClassLoader.java
  7. 19
      spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/TestCompiler.java
  8. 3
      spring-core-test/src/test/java/org/springframework/aot/test/generator/compile/TestCompilerTests.java

1
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccess.java

@ -37,5 +37,4 @@ import org.junit.jupiter.api.extension.ExtendWith; @@ -37,5 +37,4 @@ import org.junit.jupiter.api.extension.ExtendWith;
@Documented
@ExtendWith(CompileWithTargetClassAccessExtension.class)
public @interface CompileWithTargetClassAccess {
}

1
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccessClassLoader.java

@ -63,6 +63,7 @@ final class CompileWithTargetClassAccessClassLoader extends ClassLoader { @@ -63,6 +63,7 @@ final class CompileWithTargetClassAccessClassLoader extends ClassLoader {
}
// Invoked reflectively by DynamicClassLoader.findDefineClassMethod(ClassLoader)
Class<?> defineClassWithTargetAccess(String name, byte[] b, int off, int len) {
return super.defineClass(name, b, off, len);
}

1
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/CompileWithTargetClassAccessExtension.java

@ -161,6 +161,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor { @@ -161,6 +161,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
}
@FunctionalInterface
interface Action {
static Action NONE = () -> {

4
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/Compiled.java

@ -36,7 +36,6 @@ import org.springframework.util.Assert; @@ -36,7 +36,6 @@ import org.springframework.util.Assert;
*/
public class Compiled {
private final ClassLoader classLoader;
private final SourceFiles sourceFiles;
@ -47,8 +46,7 @@ public class Compiled { @@ -47,8 +46,7 @@ public class Compiled {
private List<Class<?>> compiledClasses;
Compiled(ClassLoader classLoader, SourceFiles sourceFiles,
ResourceFiles resourceFiles) {
Compiled(ClassLoader classLoader, SourceFiles sourceFiles, ResourceFiles resourceFiles) {
this.classLoader = classLoader;
this.sourceFiles = sourceFiles;
this.resourceFiles = resourceFiles;

3
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/DynamicClassFileObject.java

@ -35,8 +35,7 @@ class DynamicClassFileObject extends SimpleJavaFileObject { @@ -35,8 +35,7 @@ class DynamicClassFileObject extends SimpleJavaFileObject {
DynamicClassFileObject(String className) {
super(URI.create("class:///" + className.replace('.', '/') + ".class"),
Kind.CLASS);
super(URI.create("class:///" + className.replace('.', '/') + ".class"), Kind.CLASS);
}

1
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/DynamicClassLoader.java

@ -184,7 +184,6 @@ public class DynamicClassLoader extends ClassLoader { @@ -184,7 +184,6 @@ public class DynamicClassLoader extends ClassLoader {
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(
this.file.getContent().getBytes(StandardCharsets.UTF_8));
}
}

19
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/TestCompiler.java

@ -103,12 +103,11 @@ public final class TestCompiler { @@ -103,12 +103,11 @@ public final class TestCompiler {
*/
public TestCompiler withFiles(InMemoryGeneratedFiles generatedFiles) {
List<SourceFile> sourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach((path,
inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach(
(path, inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
List<ResourceFile> resourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.RESOURCE)
.forEach((path, inputStreamSource) -> resourceFiles
.add(ResourceFile.of(path, inputStreamSource)));
generatedFiles.getGeneratedFiles(Kind.RESOURCE).forEach(
(path, inputStreamSource) -> resourceFiles.add(ResourceFile.of(path, inputStreamSource)));
return withSources(sourceFiles).withResources(resourceFiles);
}
@ -256,7 +255,7 @@ public final class TestCompiler { @@ -256,7 +255,7 @@ public final class TestCompiler {
}
catch (IllegalAccessError ex) {
throw new IllegalAccessError(ex.getMessage() + ". " +
"For non-public access ensure annotate your tests with @CompileWithTargetClassAccess");
"For non-public access ensure you annotate your test class or test method with @CompileWithTargetClassAccess");
}
finally {
Thread.currentThread().setContextClassLoader(previousClassLoader);
@ -318,12 +317,12 @@ public final class TestCompiler { @@ -318,12 +317,12 @@ public final class TestCompiler {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
this.message.append("\n");
this.message.append('\n');
this.message.append(diagnostic.getMessage(Locale.getDefault()));
this.message.append(" ");
this.message.append(' ');
this.message.append(diagnostic.getSource().getName());
this.message.append(" ");
this.message.append(diagnostic.getLineNumber()).append(":")
this.message.append(' ');
this.message.append(diagnostic.getLineNumber()).append(':')
.append(diagnostic.getColumnNumber());
}
}

3
spring-core-test/src/test/java/org/springframework/aot/test/generator/compile/TestCompilerTests.java

@ -42,6 +42,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -42,6 +42,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link TestCompiler}.
*
* @since 6.0
* @author Phillip Webb
* @author Andy Wilkinson
* @author Scott Frederick
@ -89,6 +91,7 @@ class TestCompilerTests { @@ -89,6 +91,7 @@ class TestCompilerTests {
}
""";
@Test
@SuppressWarnings("unchecked")
void compileWhenHasDifferentClassesWithSameClassNameCompilesBoth() {

Loading…
Cancel
Save