Skip to content

Commit

Permalink
Merge pull request #40630 from Sanne/CoreSimplifications
Browse files Browse the repository at this point in the history
Reviewed some Quarkus core classes
  • Loading branch information
Sanne committed May 15, 2024
2 parents 1f5c36f + ad96389 commit 821e889
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Property[] apply(Class<?> type) {
if (existingGetter == null || existingGetter.getReturnType().isAssignableFrom(i.getReturnType())) {
getters.put(name, i);
}
} else if (i.getName().startsWith("is") && i.getName().length() > 3 && i.getParameterCount() == 0
} else if (i.getName().startsWith("is") && i.getName().length() > 2 && i.getParameterCount() == 0
&& (i.getReturnType() == boolean.class || i.getReturnType() == Boolean.class)) {
String name = Character.toLowerCase(i.getName().charAt(2)) + i.getName().substring(3);
isGetters.put(name, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;

Expand All @@ -13,8 +12,8 @@

final class RecordingAnnotationsUtil {

static final List<Class<? extends Annotation>> IGNORED_PROPERTY_ANNOTATIONS;
static final List<Class<? extends Annotation>> RECORDABLE_CONSTRUCTOR_ANNOTATIONS;
private static final Class<? extends Annotation>[] IGNORED_PROPERTY_ANNOTATIONS;
private static final Class<? extends Annotation>[] RECORDABLE_CONSTRUCTOR_ANNOTATIONS;

static {
Set<Class<? extends Annotation>> ignoredPropertyAnnotations = new HashSet<>();
Expand All @@ -33,30 +32,32 @@ final class RecordingAnnotationsUtil {
}
}

IGNORED_PROPERTY_ANNOTATIONS = List.copyOf(ignoredPropertyAnnotations);
RECORDABLE_CONSTRUCTOR_ANNOTATIONS = List.copyOf(recordableConstructorAnnotations);
IGNORED_PROPERTY_ANNOTATIONS = ignoredPropertyAnnotations.toArray(new Class[0]);
RECORDABLE_CONSTRUCTOR_ANNOTATIONS = recordableConstructorAnnotations.toArray(new Class[0]);
}

private RecordingAnnotationsUtil() {
}

static boolean isIgnored(AccessibleObject object) {
for (int i = 0; i < IGNORED_PROPERTY_ANNOTATIONS.size(); i++) {
Class<? extends Annotation> annotation = IGNORED_PROPERTY_ANNOTATIONS.get(i);
if (object.isAnnotationPresent(annotation)) {
return true;
}
}
return false;
static boolean isIgnored(final AccessibleObject object) {
return annotationsMatch(object.getDeclaredAnnotations(), IGNORED_PROPERTY_ANNOTATIONS);
}

static boolean isRecordableConstructor(Constructor<?> ctor) {
for (int i = 0; i < RECORDABLE_CONSTRUCTOR_ANNOTATIONS.size(); i++) {
Class<? extends Annotation> annotation = RECORDABLE_CONSTRUCTOR_ANNOTATIONS.get(i);
if (ctor.isAnnotationPresent(annotation)) {
return true;
static boolean isRecordableConstructor(final Constructor<?> ctor) {
return annotationsMatch(ctor.getDeclaredAnnotations(), RECORDABLE_CONSTRUCTOR_ANNOTATIONS);
}

private static boolean annotationsMatch(
final Annotation[] declaredAnnotations,
final Class<? extends Annotation>[] typesToCheck) {
for (Class<? extends Annotation> annotation : typesToCheck) {
for (Annotation declaredAnnotation : declaredAnnotations) {
if (declaredAnnotation.annotationType().equals(annotation)) {
return true;
}
}
}
return false;
}

}

0 comments on commit 821e889

Please sign in to comment.