Merge branch '6.1.x'

This commit is contained in:
Stéphane Nicoll 2024-11-06 23:12:06 +09:00
commit c5f8dcc245
4 changed files with 66 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -121,14 +121,14 @@ public class DynamicClassLoader extends ClassLoader {
return createResourceUrl(name, () -> classBytes); return createResourceUrl(name, () -> classBytes);
} }
} }
ResourceFile resourceFile = this.resourceFiles.get(name);
if (resourceFile != null) {
return createResourceUrl(resourceFile.getPath(), resourceFile::getBytes);
}
DynamicResourceFileObject dynamicResourceFile = this.dynamicResourceFiles.get(name); DynamicResourceFileObject dynamicResourceFile = this.dynamicResourceFiles.get(name);
if (dynamicResourceFile != null && dynamicResourceFile.getBytes() != null) { if (dynamicResourceFile != null && dynamicResourceFile.getBytes() != null) {
return createResourceUrl(dynamicResourceFile.getName(), dynamicResourceFile::getBytes); return createResourceUrl(dynamicResourceFile.getName(), dynamicResourceFile::getBytes);
} }
ResourceFile resourceFile = this.resourceFiles.get(name);
if (resourceFile != null) {
return createResourceUrl(resourceFile.getPath(), resourceFile::getBytes);
}
return super.findResource(name); return super.findResource(name);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -72,11 +72,11 @@ class DynamicJavaFileManager extends ForwardingJavaFileManager<JavaFileManager>
@Override @Override
public FileObject getFileForOutput(Location location, String packageName, public FileObject getFileForOutput(Location location, String packageName,
String relativeName, FileObject sibling) { String relativeName, FileObject sibling) {
ResourceFile resourceFile = this.resourceFiles.get(relativeName); return this.dynamicResourceFiles.computeIfAbsent(relativeName, name -> {
if (resourceFile != null) { ResourceFile resourceFile = this.resourceFiles.get(name);
return new DynamicResourceFileObject(relativeName, resourceFile.getContent()); return (resourceFile != null) ? new DynamicResourceFileObject(name, resourceFile.getContent()) :
} new DynamicResourceFileObject(name);
return this.dynamicResourceFiles.computeIfAbsent(relativeName, DynamicResourceFileObject::new); });
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.core.test.tools; package org.springframework.core.test.tools;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.EnumSet; import java.util.EnumSet;
@ -154,6 +155,23 @@ class DynamicJavaFileManagerTests {
"META-INF/second.properties"); "META-INF/second.properties");
} }
@Test
void existingResourceFileCanBeUpdated() throws IOException {
try (InputStream input = getResourceOne().openInputStream()) {
assertThat(input).hasContent("a");
}
try (OutputStream output = getResourceOne().openOutputStream()) {
output.write('b');
}
try (InputStream input = getResourceOne().openInputStream()) {
assertThat(input).hasContent("b");
}
}
private FileObject getResourceOne() {
return this.fileManager.getFileForOutput(this.location, "", "com/example/one/resource.one", null);
}
private void writeDummyBytecode(JavaFileObject fileObject) throws IOException { private void writeDummyBytecode(JavaFileObject fileObject) throws IOException {
try (OutputStream outputStream = fileObject.openOutputStream()) { try (OutputStream outputStream = fileObject.openOutputStream()) {
StreamUtils.copy(DUMMY_BYTECODE, outputStream); StreamUtils.copy(DUMMY_BYTECODE, outputStream);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,10 @@
package org.springframework.core.test.tools; package org.springframework.core.test.tools;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -27,6 +30,8 @@ import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import com.example.PublicInterface; import com.example.PublicInterface;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -367,6 +372,14 @@ class TestCompilerTests {
}); });
} }
@Test
void getUpdatedResourceAsStream() {
SourceFile sourceFile = SourceFile.of(HELLO_WORLD);
TestCompiler.forSystem().withResources(ResourceFile.of("com/example/resource", new byte[] { 'a' }))
.withProcessors(new ResourceModifyingProcessor()).compile(sourceFile, compiled -> assertThat(
compiled.getClassLoader().getResourceAsStream("com/example/resource")).hasContent("b"));
}
private void assertSuppliesHelloWorld(Compiled compiled) { private void assertSuppliesHelloWorld(Compiled compiled) {
assertThat(compiled.getInstance(Supplier.class).get()).isEqualTo("Hello World!"); assertThat(compiled.getInstance(Supplier.class).get()).isEqualTo("Hello World!");
} }
@ -392,4 +405,26 @@ class TestCompilerTests {
} }
} }
@SupportedAnnotationTypes("java.lang.Deprecated")
static class ResourceModifyingProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
try {
FileObject resource = this.processingEnv.getFiler()
.createResource(StandardLocation.CLASS_OUTPUT, "", "com/example/resource");
try (OutputStream output = resource.openOutputStream()) {
output.write('b');
}
}
catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
return true;
}
}
} }