Skip to content

Commit

Permalink
Avoid o^2 lookup for Fields, avoid NoSuchFieldException(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed May 14, 2024
1 parent 6e01864 commit 92e5a67
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1200,20 +1200,17 @@ public void prepare(MethodContext context) {

Set<String> handledProperties = new HashSet<>();
Property[] desc = PropertyUtils.getPropertyDescriptors(param);
FieldsHelper fieldsHelper = new FieldsHelper(param.getClass());
for (Property i : desc) {
if (!i.getDeclaringClass().getPackageName().startsWith("java.")) {
// check if the getter is ignored
if ((i.getReadMethod() != null) && RecordingAnnotationsUtil.isIgnored(i.getReadMethod())) {
continue;
}
// check if the matching field is ignored
try {
Field field = param.getClass().getDeclaredField(i.getName());
if (ignoreField(field)) {
continue;
}
} catch (NoSuchFieldException ignored) {

Field field = fieldsHelper.getDeclaredField(i.getName());
if (field != null && ignoreField(field)) {
continue;
}
}
Integer ctorParamIndex = constructorParamNameMap.remove(i.name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.deployment.recording;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

final class FieldsHelper {

private final Map<String, Field> fields;

public FieldsHelper(final Class<?> aClass) {
final Field[] declaredFields = aClass.getDeclaredFields();
this.fields = new HashMap<>(declaredFields.length);
for (Field field : declaredFields) {
this.fields.put(field.getName(), field);
}
}

//Returns the matching Field, or null if not existing
public Field getDeclaredField(final String name) {
return fields.get(name);
}
}

0 comments on commit 92e5a67

Please sign in to comment.