From 549d825e4e4997ea0facfded6b5d78ded0b8ecfe Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Thu, 18 May 2023 07:22:46 -0500 Subject: [PATCH] Remove any further possibility of reflection warnings --- .../java/io/smallrye/beanbag/sisu/Sisu.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/sisu/src/main/java/io/smallrye/beanbag/sisu/Sisu.java b/sisu/src/main/java/io/smallrye/beanbag/sisu/Sisu.java index 0849742..d0dee14 100644 --- a/sisu/src/main/java/io/smallrye/beanbag/sisu/Sisu.java +++ b/sisu/src/main/java/io/smallrye/beanbag/sisu/Sisu.java @@ -392,13 +392,14 @@ private void addBeanFromXml(final Class 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; } @@ -712,6 +713,8 @@ private static void addFieldInjections(final Class 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)) { @@ -721,7 +724,7 @@ private static void addFieldInjections(final Class clazz, final B if (!fieldAnnotations.isInject()) { continue; } - if (!field.trySetAccessible()) { + if (!(publicClass && Modifier.isPublic(mods) || open && field.trySetAccessible())) { continue; } boolean optional = fieldAnnotations.isNullable(); @@ -751,6 +754,8 @@ private static void addMethodInjections(final Class clazz, final for (Class anInterface : clazz.getInterfaces()) { addMethodInjections((Class) 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)) { @@ -763,6 +768,9 @@ private static void addMethodInjections(final Class 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]; @@ -782,15 +790,22 @@ private static Constructor findConstructor(Class 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) constructor; } else if (constructor.getParameterCount() == 0) { defaultConstructor = (Constructor) 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); }