forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to support testing of generated code
Closes spring-projectsgh-28120
- Loading branch information
Showing
38 changed files
with
3,262 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
description = "Spring Core Test" | ||
|
||
dependencies { | ||
api(project(":spring-core")) | ||
api("org.assertj:assertj-core") | ||
api("com.thoughtworks.qdox:qdox") | ||
} | ||
|
||
tasks.withType(PublishToMavenRepository).configureEach { | ||
it.enabled = false | ||
} |
32 changes: 32 additions & 0 deletions
32
...st/src/main/java/org/springframework/aot/test/generator/compile/CompilationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.aot.test.generator.compile; | ||
|
||
/** | ||
* Exception thrown when code cannot compile. | ||
* | ||
* @author Phillip Webb | ||
* @since 6.0 | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class CompilationException extends RuntimeException { | ||
|
||
CompilationException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
170 changes: 170 additions & 0 deletions
170
spring-core-test/src/main/java/org/springframework/aot/test/generator/compile/Compiled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.aot.test.generator.compile; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.aot.test.generator.file.ResourceFile; | ||
import org.springframework.aot.test.generator.file.ResourceFiles; | ||
import org.springframework.aot.test.generator.file.SourceFile; | ||
import org.springframework.aot.test.generator.file.SourceFiles; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Fully compiled results provided from a {@link TestCompiler}. | ||
* | ||
* @author Phillip Webb | ||
* @since 6.0 | ||
*/ | ||
public class Compiled { | ||
|
||
|
||
private final ClassLoader classLoader; | ||
|
||
private final SourceFiles sourceFiles; | ||
|
||
private final ResourceFiles resourceFiles; | ||
|
||
@Nullable | ||
private List<Class<?>> compiledClasses; | ||
|
||
|
||
Compiled(ClassLoader classLoader, SourceFiles sourceFiles, | ||
ResourceFiles resourceFiles) { | ||
this.classLoader = classLoader; | ||
this.sourceFiles = sourceFiles; | ||
this.resourceFiles = resourceFiles; | ||
} | ||
|
||
|
||
/** | ||
* Return the classloader containing the compiled content and access to the | ||
* resources. | ||
* @return the classLoader | ||
*/ | ||
public ClassLoader getClassLoader() { | ||
return this.classLoader; | ||
} | ||
|
||
/** | ||
* Return the single source file that was compiled. | ||
* @return the single source file | ||
* @throws IllegalStateException if the compiler wasn't passed exactly one | ||
* file | ||
*/ | ||
public SourceFile getSourceFile() { | ||
return this.sourceFiles.getSingle(); | ||
} | ||
|
||
/** | ||
* Return all source files that were compiled. | ||
* @return the source files used by the compiler | ||
*/ | ||
public SourceFiles getSourceFiles() { | ||
return this.sourceFiles; | ||
} | ||
|
||
/** | ||
* Return the single resource file that was used when compiled. | ||
* @return the single resource file | ||
* @throws IllegalStateException if the compiler wasn't passed exactly one | ||
* file | ||
*/ | ||
public ResourceFile getResourceFile() { | ||
return this.resourceFiles.getSingle(); | ||
} | ||
|
||
/** | ||
* Return all resource files that were compiled. | ||
* @return the resource files used by the compiler | ||
*/ | ||
public ResourceFiles getResourceFiles() { | ||
return this.resourceFiles; | ||
} | ||
|
||
/** | ||
* Return a new instance of a compiled class of the given type. There must | ||
* be only a single instance and it must have a default constructor. | ||
* @param <T> the required type | ||
* @param type the required type | ||
* @return an instance of type created from the compiled classes | ||
* @throws IllegalStateException if no instance can be found or instantiated | ||
*/ | ||
public <T> T getInstance(Class<T> type) { | ||
List<Class<?>> matching = getAllCompiledClasses().stream().filter(type::isAssignableFrom).toList(); | ||
Assert.state(!matching.isEmpty(), () -> "No instance found of type " + type.getName()); | ||
Assert.state(matching.size() == 1, () -> "Multiple instances found of type " + type.getName()); | ||
return newInstance(matching.get(0)); | ||
} | ||
|
||
/** | ||
* Return an instance of a compiled class identified by its class name. The | ||
* class must have a default constructor. | ||
* @param <T> the type to return | ||
* @param type the type to return | ||
* @param className the class name to load | ||
* @return an instance of the class | ||
* @throws IllegalStateException if no instance can be found or instantiated | ||
*/ | ||
public <T> T getInstance(Class<T> type, String className) { | ||
Class<?> loaded = loadClass(className); | ||
return newInstance(loaded); | ||
} | ||
|
||
/** | ||
* Return all compiled classes. | ||
* @return a list of all compiled classes | ||
*/ | ||
public List<Class<?>> getAllCompiledClasses() { | ||
List<Class<?>> compiledClasses = this.compiledClasses; | ||
if (compiledClasses == null) { | ||
compiledClasses = new ArrayList<>(); | ||
this.sourceFiles.stream().map(this::loadClass).forEach(compiledClasses::add); | ||
this.compiledClasses = Collections.unmodifiableList(compiledClasses); | ||
} | ||
return compiledClasses; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> T newInstance(Class<?> loaded) { | ||
try { | ||
Constructor<?> constructor = loaded.getDeclaredConstructor(); | ||
return (T) constructor.newInstance(); | ||
} | ||
catch (Exception ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
} | ||
|
||
private Class<?> loadClass(SourceFile sourceFile) { | ||
return loadClass(sourceFile.getClassName()); | ||
} | ||
|
||
private Class<?> loadClass(String className) { | ||
try { | ||
return this.classLoader.loadClass(className); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
.../src/main/java/org/springframework/aot/test/generator/compile/DynamicClassFileObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.aot.test.generator.compile; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
|
||
import javax.tools.JavaFileObject; | ||
import javax.tools.SimpleJavaFileObject; | ||
|
||
/** | ||
* In-memory {@link JavaFileObject} used to hold class bytecode. | ||
* | ||
* @author Phillip Webb | ||
* @since 6.0 | ||
*/ | ||
class DynamicClassFileObject extends SimpleJavaFileObject { | ||
|
||
private volatile byte[] bytes = new byte[0]; | ||
|
||
|
||
DynamicClassFileObject(String className) { | ||
super(URI.create("class:///" + className.replace('.', '/') + ".class"), | ||
Kind.CLASS); | ||
} | ||
|
||
|
||
@Override | ||
public OutputStream openOutputStream() { | ||
return new JavaClassOutputStream(); | ||
} | ||
|
||
byte[] getBytes() { | ||
return this.bytes; | ||
} | ||
|
||
|
||
class JavaClassOutputStream extends ByteArrayOutputStream { | ||
|
||
@Override | ||
public void close() { | ||
DynamicClassFileObject.this.bytes = toByteArray(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.