Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove any further possibility of reflection warnings #36

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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