@ -16,10 +16,7 @@
@@ -16,10 +16,7 @@
package org.springframework.aot.test.generate.compile ;
import java.io.ByteArrayInputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.net.URI ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.LinkedHashMap ;
@ -32,7 +29,6 @@ import javax.tools.ForwardingJavaFileManager;
@@ -32,7 +29,6 @@ import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager ;
import javax.tools.JavaFileObject ;
import javax.tools.JavaFileObject.Kind ;
import javax.tools.SimpleJavaFileObject ;
import org.springframework.aot.test.generate.file.ClassFile ;
import org.springframework.aot.test.generate.file.ClassFiles ;
@ -48,19 +44,18 @@ import org.springframework.util.ClassUtils;
@@ -48,19 +44,18 @@ import org.springframework.util.ClassUtils;
* /
class DynamicJavaFileManager extends ForwardingJavaFileManager < JavaFileManager > {
private final ClassFiles existingClasses ;
private final ClassLoader classLoader ;
private final ClassFiles classFiles ;
private final Map < String , DynamicClassFileObject > compiledClasses = Collections . synchronizedMap (
new LinkedHashMap < > ( ) ) ;
DynamicJavaFileManager ( JavaFileManager fileManager , ClassLoader classLoader ,
ClassFiles existingClasses ) {
DynamicJavaFileManager ( JavaFileManager fileManager , ClassLoader classLoader , ClassFiles classFiles ) {
super ( fileManager ) ;
this . classLoader = classLoader ;
this . existingClasses = existingClass es;
this . classFiles = classFil es;
}
@ -84,49 +79,27 @@ class DynamicJavaFileManager extends ForwardingJavaFileManager<JavaFileManager>
@@ -84,49 +79,27 @@ class DynamicJavaFileManager extends ForwardingJavaFileManager<JavaFileManager>
Set < Kind > kinds , boolean recurse ) throws IOException {
List < JavaFileObject > result = new ArrayList < > ( ) ;
if ( kinds . contains ( Kind . CLASS ) ) {
for ( ClassFile existingClass : this . existingClass es) {
String existingPackageName = ClassUtils . getPackageName ( existingClass . getName ( ) ) ;
for ( ClassFile candidate : this . classFil es) {
String existingPackageName = ClassUtils . getPackageName ( candidate . getName ( ) ) ;
if ( existingPackageName . equals ( packageName ) | | ( recurse & & existingPackageName . startsWith ( packageName + "." ) ) ) {
result . add ( new ClassFileJava FileObject ( existingClass ) ) ;
result . add ( new Dynamic ClassFileObject( candidate . getName ( ) , candidate . getContent ( ) ) ) ;
}
}
}
Iterable < JavaFileObject > listed = super . list ( location , packageName , kinds , recurse ) ;
listed . forEach ( result : : add ) ;
super . list ( location , packageName , kinds , recurse ) . forEach ( result : : add ) ;
return result ;
}
@Override
public String inferBinaryName ( Location location , JavaFileObject file ) {
if ( file instanceof ClassFileJava FileObject classFile ) {
return classFile . getClassName ( ) ;
if ( file instanceof Dynamic ClassFileObject dynami cC lassFileObject ) {
return dynami cC lassFileObject . getClassName ( ) ;
}
return super . inferBinaryName ( location , file ) ;
}
ClassFiles getClassFiles ( ) {
return this . existingClasses . and ( this . compiledClasses . entrySet ( ) . stream ( ) . map ( entry - >
ClassFile . of ( entry . getKey ( ) , entry . getValue ( ) . getBytes ( ) ) ) . toList ( ) ) ;
}
private static final class ClassFileJavaFileObject extends SimpleJavaFileObject {
private final ClassFile classFile ;
private ClassFileJavaFileObject ( ClassFile classFile ) {
super ( URI . create ( "class:///" + classFile . getName ( ) . replace ( '.' , '/' ) + ".class" ) , Kind . CLASS ) ;
this . classFile = classFile ;
}
public String getClassName ( ) {
return this . classFile . getName ( ) ;
}
@Override
public InputStream openInputStream ( ) {
return new ByteArrayInputStream ( this . classFile . getContent ( ) ) ;
}
Map < String , DynamicClassFileObject > getCompiledClasses ( ) {
return this . compiledClasses ;
}
}