Skip to content

Commit

Permalink
Merge pull request #31222 from gsmet/refactor-ReflectiveClassBuildItem
Browse files Browse the repository at this point in the history
Refactor ReflectiveClassBuildItem
  • Loading branch information
gsmet authored Feb 18, 2023
2 parents 284052e + fc50f65 commit 0f4a239
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ public final class ReflectiveClassBuildItem extends MultiBuildItem {
private final boolean serialization;
private final boolean unsafeAllocated;

public ReflectiveClassBuildItem(boolean methods, boolean fields, Class<?>... classes) {
this(true, methods, fields, classes);
public static Builder builder(Class<?>... classes) {
String[] classNames = stream(classes)
.map(aClass -> {
if (aClass == null) {
throw new NullPointerException();
}
return aClass.getName();
})
.toArray(String[]::new);

return new Builder().className(classNames);
}

public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, Class<?>... classes) {
this(constructors, methods, fields, false, false, false, classes);
public static Builder builder(String... classNames) {
return new Builder().className(classNames);
}

private ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, boolean weak, boolean serialization,
boolean unsafeAllocated,
Class<?>... classes) {
boolean unsafeAllocated, Class<?>... classes) {
List<String> names = new ArrayList<>();
for (Class<?> i : classes) {
if (i == null) {
Expand All @@ -51,39 +59,72 @@ private ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean
}
}

public ReflectiveClassBuildItem(boolean methods, boolean fields, String... className) {
this(true, methods, fields, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public ReflectiveClassBuildItem(boolean methods, boolean fields, Class<?>... classes) {
this(true, methods, fields, classes);
}

public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, String... className) {
this(constructors, methods, fields, false, false, false, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, Class<?>... classes) {
this(constructors, methods, fields, false, false, false, classes);
}

public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, boolean serialization,
String... className) {
this(constructors, methods, fields, false, serialization, false, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public ReflectiveClassBuildItem(boolean methods, boolean fields, String... classNames) {
this(true, methods, fields, classNames);
}

public static ReflectiveClassBuildItem weakClass(String... className) {
return weakClass(true, true, true, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, String... classNames) {
this(constructors, methods, fields, false, false, false, classNames);
}

public static ReflectiveClassBuildItem weakClass(boolean constructors, boolean methods, boolean fields,
String... className) {
return new ReflectiveClassBuildItem(constructors, methods, fields, true, false, false, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
@Deprecated(since = "3.0", forRemoval = true)
public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, boolean serialization,
String... classNames) {
this(constructors, methods, fields, false, serialization, false, classNames);
}

public static ReflectiveClassBuildItem serializationClass(String... className) {
return serializationClass(false, className);
public static ReflectiveClassBuildItem weakClass(String... classNames) {
return ReflectiveClassBuildItem.builder(classNames).constructors().methods().fields().weak().build();
}

public static ReflectiveClassBuildItem serializationClass(boolean unsafeAllocated, String... className) {
return new ReflectiveClassBuildItem(false, false, false, false, true, unsafeAllocated, className);
/**
* @deprecated Use {@link ReflectiveClassBuildItem#builder(Class...)} or {@link ReflectiveClassBuildItem#builder(String...)}
* instead.
*/
public static ReflectiveClassBuildItem weakClass(boolean constructors, boolean methods, boolean fields,
String... classNames) {
return ReflectiveClassBuildItem.builder(classNames).constructors(constructors).methods(methods).fields(fields).weak()
.build();
}

public static ReflectiveClassBuildItem serializationClass(String... classNames) {
return ReflectiveClassBuildItem.builder(classNames).serialization().build();
}

public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, boolean weak, boolean serialization,
boolean unsafeAllocated,
String... className) {
ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, boolean weak, boolean serialization,
boolean unsafeAllocated, String... className) {
for (String i : className) {
if (i == null) {
throw new NullPointerException();
Expand Down Expand Up @@ -135,25 +176,6 @@ public boolean isUnsafeAllocated() {
return unsafeAllocated;
}

public static Builder builder(Class<?>... className) {
String[] classNameStrings = stream(className)
.map(aClass -> {
if (aClass == null) {
throw new NullPointerException();
}
return aClass.getName();
})
.toArray(String[]::new);

return new Builder()
.className(classNameStrings);
}

public static Builder builder(String... className) {
return new Builder()
.className(className);
}

public static class Builder {
private String[] className;
private boolean constructors = true;
Expand All @@ -176,21 +198,33 @@ public Builder constructors(boolean constructors) {
return this;
}

public Builder constructors() {
return constructors(true);
}

public Builder methods(boolean methods) {
this.methods = methods;
return this;
}

public Builder methods() {
return methods(true);
}

public Builder fields(boolean fields) {
this.fields = fields;
return this;
}

public Builder fields() {
return fields(true);
}

/**
* @deprecated As of GraalVM 21.2 finalFieldsWritable is no longer needed when registering fields for reflection. This
* will be removed in a future verion of Quarkus.
* will be removed in a future version of Quarkus.
*/
@Deprecated
@Deprecated(forRemoval = true)
public Builder finalFieldsWritable(boolean finalFieldsWritable) {
return this;
}
Expand All @@ -200,16 +234,28 @@ public Builder weak(boolean weak) {
return this;
}

public Builder serialization(boolean serialize) {
this.serialization = serialize;
public Builder weak() {
return weak(true);
}

public Builder serialization(boolean serialization) {
this.serialization = serialization;
return this;
}

public Builder serialization() {
return serialization(true);
}

public Builder unsafeAllocated(boolean unsafeAllocated) {
this.unsafeAllocated = unsafeAllocated;
return this;
}

public Builder unsafeAllocated() {
return unsafeAllocated(true);
}

public ReflectiveClassBuildItem build() {
return new ReflectiveClassBuildItem(constructors, methods, fields, weak, serialization, unsafeAllocated, className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
}

/**
* BFS Recursive Method to register a class and it's inner classes for Reflection.
* BFS Recursive Method to register a class and its inner classes for Reflection.
*
* @param unsafeAllocated
* @param reflectiveClassHierarchy
Expand All @@ -115,8 +115,10 @@ private void registerClass(ClassLoader classLoader, String className, boolean me
final BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveClassHierarchy, Set<DotName> processedReflectiveHierarchies,
AnnotationValue registerFullHierarchyValue, Builder builder) {
reflectiveClass.produce(serialization ? ReflectiveClassBuildItem.serializationClass(unsafeAllocated, className)
: new ReflectiveClassBuildItem(true, methods, fields, false, false, unsafeAllocated, className));
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 != null && registerFullHierarchyValue.asBoolean()) {
Expand Down

0 comments on commit 0f4a239

Please sign in to comment.