Skip to content

Commit

Permalink
Remove any further possibility of reflection warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed May 18, 2023
1 parent cc951e8 commit 549d825
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions sisu/src/main/java/io/smallrye/beanbag/sisu/Sisu.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,14 @@ private <T> void addBeanFromXml(final Class<T> clazz, final Class<?> type, final
continue;
}
Field[] declaredFields = cur.getDeclaredFields();
boolean open = cur.getModule().isOpen(cur.getPackageName(), Sisu.class.getModule());
for (Field declaredField : declaredFields) {
int mods = declaredField.getModifiers();
if (Modifier.isStatic(mods) || Modifier.isFinal(mods)
|| injectableFields.containsKey(declaredField.getName())) {
continue;
}
if (!Modifier.isPublic(mods) && !declaredField.trySetAccessible()) {
if (!(Modifier.isPublic(mods) || open && declaredField.trySetAccessible())) {
// cannot inject into this field
continue;
}
Expand Down Expand Up @@ -712,6 +713,8 @@ private static <T> void addFieldInjections(final Class<? super T> clazz, final B
if (superclass != null) {
addFieldInjections(superclass, supplierBuilder, filter);
}
boolean open = clazz.getModule().isOpen(clazz.getPackageName(), Sisu.class.getModule());
boolean publicClass = Modifier.isPublic(clazz.getModifiers());
for (Field field : clazz.getDeclaredFields()) {
final int mods = field.getModifiers();
if (Modifier.isStatic(mods) || Modifier.isFinal(mods)) {
Expand All @@ -721,7 +724,7 @@ private static <T> void addFieldInjections(final Class<? super T> clazz, final B
if (!fieldAnnotations.isInject()) {
continue;
}
if (!field.trySetAccessible()) {
if (!(publicClass && Modifier.isPublic(mods) || open && field.trySetAccessible())) {
continue;
}
boolean optional = fieldAnnotations.isNullable();
Expand Down Expand Up @@ -751,6 +754,8 @@ private static <T> void addMethodInjections(final Class<? super T> clazz, final
for (Class<?> anInterface : clazz.getInterfaces()) {
addMethodInjections((Class<? super T>) anInterface, supplierBuilder, filter);
}
boolean open = clazz.getModule().isOpen(clazz.getPackageName(), Sisu.class.getModule());
boolean publicClass = Modifier.isPublic(clazz.getModifiers());
for (Method method : clazz.getDeclaredMethods()) {
final int mods = method.getModifiers();
if (Modifier.isStatic(mods)) {
Expand All @@ -763,6 +768,9 @@ private static <T> void addMethodInjections(final Class<? super T> clazz, final
if (method.getParameterCount() != 1) {
continue;
}
if (!(publicClass && Modifier.isPublic(mods) || open && method.trySetAccessible())) {
continue;
}
final String named = methodAnnotations.getNamed();
final String name = named == null ? "" : named;
final Parameter argParam = method.getParameters()[0];
Expand All @@ -782,15 +790,22 @@ private static <T> Constructor<T> findConstructor(Class<T> clazz) {
} catch (Throwable t) {
throw new RuntimeException("Cannot get declared constructors from " + clazz, t);
}
boolean open = clazz.getModule().isOpen(clazz.getPackageName(), Sisu.class.getModule());
boolean publicClass = Modifier.isPublic(clazz.getModifiers());
for (Constructor<?> constructor : declaredConstructors) {
if (Annotations.of(constructor).isInject() && constructor.trySetAccessible()) {
final int mods = constructor.getModifiers();
if (Annotations.of(constructor).isInject()
&& (publicClass && Modifier.isPublic(mods) || open && constructor.trySetAccessible())) {
return (Constructor<T>) constructor;
} else if (constructor.getParameterCount() == 0) {
defaultConstructor = (Constructor<T>) constructor;
}
}
if (defaultConstructor != null && defaultConstructor.trySetAccessible()) {
return defaultConstructor;
if (defaultConstructor != null) {
final int mods = defaultConstructor.getModifiers();
if (publicClass && Modifier.isPublic(mods) || open && defaultConstructor.trySetAccessible()) {
return defaultConstructor;
}
}
throw new RuntimeException("No valid constructor found on " + clazz);
}
Expand Down

0 comments on commit 549d825

Please sign in to comment.