Introduce ClassFormatException and spring.classformat.ignore property

Closes gh-27691
This commit is contained in:
Juergen Hoeller
2023-12-09 20:03:57 +01:00
parent 77b0382a6c
commit c57b7e8418
6 changed files with 124 additions and 9 deletions

View File

@@ -52,6 +52,7 @@ import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.InfrastructureProxy;
import org.springframework.core.SpringProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@@ -59,6 +60,7 @@ import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.ClassFormatException;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
@@ -110,6 +112,11 @@ public class LocalSessionFactoryBuilder extends Configuration {
private static final TypeFilter CONVERTER_TYPE_FILTER = new AnnotationTypeFilter(Converter.class, false);
private static final String IGNORE_CLASSFORMAT_PROPERTY_NAME = "spring.classformat.ignore";
private static final boolean shouldIgnoreClassFormatException =
SpringProperties.getFlag(IGNORE_CLASSFORMAT_PROPERTY_NAME);
private final ResourcePatternResolver resourcePatternResolver;
@@ -335,6 +342,14 @@ public class LocalSessionFactoryBuilder extends Configuration {
catch (FileNotFoundException ex) {
// Ignore non-readable resource
}
catch (ClassFormatException ex) {
if (!shouldIgnoreClassFormatException) {
throw new MappingException("Incompatible class format in " + resource, ex);
}
}
catch (Throwable ex) {
throw new MappingException("Failed to read candidate component class: " + resource, ex);
}
}
}
}

View File

@@ -33,11 +33,13 @@ import jakarta.persistence.PersistenceException;
import org.springframework.context.index.CandidateComponentsIndex;
import org.springframework.context.index.CandidateComponentsIndexLoader;
import org.springframework.core.SpringProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.ClassFormatException;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
@@ -59,6 +61,11 @@ public final class PersistenceManagedTypesScanner {
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
private static final String IGNORE_CLASSFORMAT_PROPERTY_NAME = "spring.classformat.ignore";
private static final boolean shouldIgnoreClassFormatException =
SpringProperties.getFlag(IGNORE_CLASSFORMAT_PROPERTY_NAME);
private static final Set<AnnotationTypeFilter> entityTypeFilters = new LinkedHashSet<>(4);
static {
@@ -79,6 +86,7 @@ public final class PersistenceManagedTypesScanner {
this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(resourceLoader.getClassLoader());
}
/**
* Scan the specified packages and return a {@link PersistenceManagedTypes} that
* represents the result of the scanning.
@@ -130,6 +138,14 @@ public final class PersistenceManagedTypesScanner {
catch (FileNotFoundException ex) {
// Ignore non-readable resource
}
catch (ClassFormatException ex) {
if (!shouldIgnoreClassFormatException) {
throw new PersistenceException("Incompatible class format in " + resource, ex);
}
}
catch (Throwable ex) {
throw new PersistenceException("Failed to read candidate component class: " + resource, ex);
}
}
}
catch (IOException ex) {
@@ -150,6 +166,7 @@ public final class PersistenceManagedTypesScanner {
return false;
}
private static class ScanResult {
private final List<String> managedClassNames = new ArrayList<>();
@@ -163,6 +180,6 @@ public final class PersistenceManagedTypesScanner {
return new SimplePersistenceManagedTypes(this.managedClassNames,
this.managedPackages, this.persistenceUnitRootUrl);
}
}
}