|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.aot.test.generator.compile; |
| 18 | + |
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.springframework.aot.test.generator.file.ResourceFile; |
| 25 | +import org.springframework.aot.test.generator.file.ResourceFiles; |
| 26 | +import org.springframework.aot.test.generator.file.SourceFile; |
| 27 | +import org.springframework.aot.test.generator.file.SourceFiles; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * Fully compiled results provided from a {@link TestCompiler}. |
| 33 | + * |
| 34 | + * @author Phillip Webb |
| 35 | + * @since 6.0 |
| 36 | + */ |
| 37 | +public class Compiled { |
| 38 | + |
| 39 | + |
| 40 | + private final ClassLoader classLoader; |
| 41 | + |
| 42 | + private final SourceFiles sourceFiles; |
| 43 | + |
| 44 | + private final ResourceFiles resourceFiles; |
| 45 | + |
| 46 | + @Nullable |
| 47 | + private List<Class<?>> compiledClasses; |
| 48 | + |
| 49 | + |
| 50 | + Compiled(ClassLoader classLoader, SourceFiles sourceFiles, |
| 51 | + ResourceFiles resourceFiles) { |
| 52 | + this.classLoader = classLoader; |
| 53 | + this.sourceFiles = sourceFiles; |
| 54 | + this.resourceFiles = resourceFiles; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Return the classloader containing the compiled content and access to the |
| 60 | + * resources. |
| 61 | + * @return the classLoader |
| 62 | + */ |
| 63 | + public ClassLoader getClassLoader() { |
| 64 | + return this.classLoader; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return the single source file that was compiled. |
| 69 | + * @return the single source file |
| 70 | + * @throws IllegalStateException if the compiler wasn't passed exactly one |
| 71 | + * file |
| 72 | + */ |
| 73 | + public SourceFile getSourceFile() { |
| 74 | + return this.sourceFiles.getSingle(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return all source files that were compiled. |
| 79 | + * @return the source files used by the compiler |
| 80 | + */ |
| 81 | + public SourceFiles getSourceFiles() { |
| 82 | + return this.sourceFiles; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Return the single resource file that was used when compiled. |
| 87 | + * @return the single resource file |
| 88 | + * @throws IllegalStateException if the compiler wasn't passed exactly one |
| 89 | + * file |
| 90 | + */ |
| 91 | + public ResourceFile getResourceFile() { |
| 92 | + return this.resourceFiles.getSingle(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Return all resource files that were compiled. |
| 97 | + * @return the resource files used by the compiler |
| 98 | + */ |
| 99 | + public ResourceFiles getResourceFiles() { |
| 100 | + return this.resourceFiles; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Return a new instance of a compiled class of the given type. There must |
| 105 | + * be only a single instance and it must have a default constructor. |
| 106 | + * @param <T> the required type |
| 107 | + * @param type the required type |
| 108 | + * @return an instance of type created from the compiled classes |
| 109 | + * @throws IllegalStateException if no instance can be found or instantiated |
| 110 | + */ |
| 111 | + public <T> T getInstance(Class<T> type) { |
| 112 | + List<Class<?>> matching = getAllCompiledClasses().stream().filter(type::isAssignableFrom).toList(); |
| 113 | + Assert.state(!matching.isEmpty(), () -> "No instance found of type " + type.getName()); |
| 114 | + Assert.state(matching.size() == 1, () -> "Multiple instances found of type " + type.getName()); |
| 115 | + return newInstance(matching.get(0)); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Return an instance of a compiled class identified by its class name. The |
| 120 | + * class must have a default constructor. |
| 121 | + * @param <T> the type to return |
| 122 | + * @param type the type to return |
| 123 | + * @param className the class name to load |
| 124 | + * @return an instance of the class |
| 125 | + * @throws IllegalStateException if no instance can be found or instantiated |
| 126 | + */ |
| 127 | + public <T> T getInstance(Class<T> type, String className) { |
| 128 | + Class<?> loaded = loadClass(className); |
| 129 | + return newInstance(loaded); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Return all compiled classes. |
| 134 | + * @return a list of all compiled classes |
| 135 | + */ |
| 136 | + public List<Class<?>> getAllCompiledClasses() { |
| 137 | + List<Class<?>> compiledClasses = this.compiledClasses; |
| 138 | + if (compiledClasses == null) { |
| 139 | + compiledClasses = new ArrayList<>(); |
| 140 | + this.sourceFiles.stream().map(this::loadClass).forEach(compiledClasses::add); |
| 141 | + this.compiledClasses = Collections.unmodifiableList(compiledClasses); |
| 142 | + } |
| 143 | + return compiledClasses; |
| 144 | + } |
| 145 | + |
| 146 | + @SuppressWarnings("unchecked") |
| 147 | + private <T> T newInstance(Class<?> loaded) { |
| 148 | + try { |
| 149 | + Constructor<?> constructor = loaded.getDeclaredConstructor(); |
| 150 | + return (T) constructor.newInstance(); |
| 151 | + } |
| 152 | + catch (Exception ex) { |
| 153 | + throw new IllegalStateException(ex); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private Class<?> loadClass(SourceFile sourceFile) { |
| 158 | + return loadClass(sourceFile.getClassName()); |
| 159 | + } |
| 160 | + |
| 161 | + private Class<?> loadClass(String className) { |
| 162 | + try { |
| 163 | + return this.classLoader.loadClass(className); |
| 164 | + } |
| 165 | + catch (ClassNotFoundException ex) { |
| 166 | + throw new IllegalStateException(ex); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments