Skip to content

Commit

Permalink
Do not share the ReflectiveHierarchyBuildItem.Builder
Browse files Browse the repository at this point in the history
This could lead to unexpected side effects and bugs.
  • Loading branch information
gsmet committed Jun 7, 2024
1 parent aeeaa55 commit eb0cedf
Showing 1 changed file with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.quarkus.deployment.builditem.nativeimage.LambdaCapturingTypeBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem.Builder;
import io.quarkus.deployment.util.JandexUtil;
import io.quarkus.runtime.annotations.RegisterForReflection;

Expand All @@ -42,7 +41,6 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy,
BuildProducer<LambdaCapturingTypeBuildItem> lambdaCapturingTypeProducer) {

ReflectiveHierarchyBuildItem.Builder builder = new ReflectiveHierarchyBuildItem.Builder();
Set<DotName> processedReflectiveHierarchies = new HashSet<DotName>();

IndexView index = combinedIndexBuildItem.getComputingIndex();
Expand Down Expand Up @@ -77,8 +75,7 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
}
registerClass(classLoader, classInfo.name().toString(), methods, fields, ignoreNested, serialization,
unsafeAllocated, reflectiveClass, reflectiveClassHierarchy, processedReflectiveHierarchies,
registerFullHierarchyValue,
builder);
registerFullHierarchyValue);
continue;
}

Expand All @@ -87,7 +84,7 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
for (Type type : targets) {
registerClass(classLoader, type.name().toString(), methods, fields, ignoreNested, serialization,
unsafeAllocated, reflectiveClass, reflectiveClassHierarchy, processedReflectiveHierarchies,
registerFullHierarchyValue, builder);
registerFullHierarchyValue);
}
}

Expand All @@ -96,7 +93,7 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
for (String className : classNames) {
registerClass(classLoader, className, methods, fields, ignoreNested, serialization, unsafeAllocated,
reflectiveClass,
reflectiveClassHierarchy, processedReflectiveHierarchies, registerFullHierarchyValue, builder);
reflectiveClassHierarchy, processedReflectiveHierarchies, registerFullHierarchyValue);
}
}
}
Expand All @@ -115,15 +112,15 @@ private void registerClass(ClassLoader classLoader, String className, boolean me
boolean ignoreNested, boolean serialization, boolean unsafeAllocated,
final BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy, Set<DotName> processedReflectiveHierarchies,
boolean registerFullHierarchyValue, Builder builder) {
boolean registerFullHierarchyValue) {
reflectiveClass.produce(serialization
? ReflectiveClassBuildItem.builder(className).serialization().unsafeAllocated(unsafeAllocated).build()
: ReflectiveClassBuildItem.builder(className).constructors().methods(methods).fields(fields)
.unsafeAllocated(unsafeAllocated).build());

//Search all class hierarchy, fields and methods in order to register its classes for reflection
if (registerFullHierarchyValue) {
registerClassDependencies(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies, methods, builder,
registerClassDependencies(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies, methods,
className);
}

Expand All @@ -136,7 +133,7 @@ private void registerClass(ClassLoader classLoader, String className, boolean me
for (Class<?> clazz : declaredClasses) {
registerClass(classLoader, clazz.getName(), methods, fields, false, serialization, unsafeAllocated,
reflectiveClass,
reflectiveClassHierarchy, processedReflectiveHierarchies, registerFullHierarchyValue, builder);
reflectiveClassHierarchy, processedReflectiveHierarchies, registerFullHierarchyValue);
}
} catch (ClassNotFoundException e) {
log.warnf(e, "Failed to load Class %s", className);
Expand All @@ -145,7 +142,6 @@ private void registerClass(ClassLoader classLoader, String className, boolean me

private void registerClassDependencies(BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy,
ClassLoader classLoader, Set<DotName> processedReflectiveHierarchies, boolean methods,
ReflectiveHierarchyBuildItem.Builder builder,
String className) {
try {
DotName dotName = DotName.createSimple(className);
Expand All @@ -154,19 +150,18 @@ private void registerClassDependencies(BuildProducer<ReflectiveHierarchyBuildIte

processedReflectiveHierarchies.add(dotName);
Index indexView = Index.of(classLoader.loadClass(className));
reflectiveClassHierarchy.produce(builder
.className(dotName)
reflectiveClassHierarchy.produce(ReflectiveHierarchyBuildItem.builder(dotName)
.index(indexView)
.build());

ClassInfo classInfo = indexView.getClassByName(dotName);
if (methods) {
addMethodsForReflection(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies,
indexView, dotName,
builder, classInfo, methods);
classInfo, methods);
}
registerClassFields(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies,
indexView, dotName, builder, classInfo, methods);
indexView, dotName, classInfo, methods);
}
} catch (ClassNotFoundException | IOException ignored) {
log.error("Cannot load class for reflection: " + className);
Expand All @@ -175,7 +170,7 @@ private void registerClassDependencies(BuildProducer<ReflectiveHierarchyBuildIte

private void addMethodsForReflection(BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy,
ClassLoader classLoader, Set<DotName> processedReflectiveHierarchies, IndexView indexView, DotName initialName,
ReflectiveHierarchyBuildItem.Builder builder, ClassInfo classInfo, boolean methods) {
ClassInfo classInfo, boolean methods) {
List<MethodInfo> methodList = classInfo.methods();
for (MethodInfo methodInfo : methodList) {
// we will only consider potential getters
Expand All @@ -185,35 +180,34 @@ private void addMethodsForReflection(BuildProducer<ReflectiveHierarchyBuildItem>
continue;
}
registerType(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies,
methods, builder,
methods,
getMethodReturnType(indexView, initialName, classInfo, methodInfo));
}
}

private void registerClassFields(BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy,
ClassLoader classLoader, Set<DotName> processedReflectiveHierarchies, IndexView indexView, DotName initialName,
ReflectiveHierarchyBuildItem.Builder builder, ClassInfo classInfo, boolean methods) {
ClassInfo classInfo, boolean methods) {
List<FieldInfo> fieldList = classInfo.fields();
for (FieldInfo fieldInfo : fieldList) {
if (Modifier.isStatic(fieldInfo.flags()) ||
fieldInfo.name().startsWith("this$") || fieldInfo.name().startsWith("val$")) {
continue;
}
registerType(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies,
methods, builder,
fieldInfo.type());
methods, fieldInfo.type());
}
}

private void registerType(BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy,
ClassLoader classLoader, Set<DotName> processedReflectiveHierarchies, boolean methods,
ReflectiveHierarchyBuildItem.Builder builder, Type type) {
Type type) {
if (type.kind().equals(Kind.ARRAY)) {
type = type.asArrayType().constituent();
}
if (type.kind() != Kind.PRIMITIVE && !processedReflectiveHierarchies.contains(type.name())) {
registerClassDependencies(reflectiveClassHierarchy, classLoader, processedReflectiveHierarchies,
methods, builder, type.name().toString());
methods, type.name().toString());
}
}

Expand Down

0 comments on commit eb0cedf

Please sign in to comment.