Skip to content

Commit

Permalink
ISPN-10863 java.lang.IllegalAccessException logged by PropertyFormatter
Browse files Browse the repository at this point in the history
* should not attempt to access static or non-public methods and should
  mark them accessible to prevent eventual cross-module accessibility
  checks from failing
  • Loading branch information
anistor committed Nov 4, 2019
1 parent ad6ed88 commit 82eb448
Showing 1 changed file with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.infinispan.configuration.format;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
Expand All @@ -12,14 +12,15 @@
import org.infinispan.configuration.global.GlobalConfiguration;

/**
*
* Extracts the configuration into flat key-value property structure by reflection.
*
* @author Michal Linhard (mlinhard@redhat.com)
* @since 6.0
*/
public class PropertyFormatter {
public final class PropertyFormatter {

private static Method plainToString = null;

static {
try {
plainToString = Object.class.getMethod("toString");
Expand All @@ -41,27 +42,40 @@ public PropertyFormatter() {
/**
* Create a new PropertyFormatter instance.
*
* @param globalConfigPrefix
* Prefix used for global configuration property keys.
* @param configPrefix
* Prefix used for cache configuration property keys.
* @param globalConfigPrefix Prefix used for global configuration property keys.
* @param configPrefix Prefix used for cache configuration property keys.
*/
public PropertyFormatter(String globalConfigPrefix, String configPrefix) {
this.globalConfigPrefix = globalConfigPrefix;
this.configPrefix = configPrefix;
}

private static List<Method> getMethods(Class<?> clazz) {
/**
* Get all public, non-static, non-deprecated methods with 0 arguments (except toString() and other unuuseful ones).
*/
private static List<Method> getConfigMethods(Class<?> clazz) {
Class<?> c = clazz;
List<Method> r = new ArrayList<>();
while (c != null && c != Object.class) {
Collections.addAll(r, c.getDeclaredMethods());
for (Method m : c.getDeclaredMethods()) {
if (Modifier.isPublic(m.getModifiers())
&& !Modifier.isStatic(m.getModifiers())
&& m.getParameters().length == 0
&& !m.isAnnotationPresent(Deprecated.class)
&& !"hashCode".equals(m.getName())
&& !"toString".equals(m.getName())
&& !"toProperties".equals(m.getName())) {
m.setAccessible(true);
r.add(m);
}
}
c = c.getSuperclass();
}
return r;
}

private static boolean hasPlainToString(Class<?> cls, Object obj) {
private static boolean hasPlainToString(Object obj) {
Class<?> cls = obj.getClass();
try {
if (cls.getMethod("toString") == plainToString) {
return true;
Expand All @@ -81,14 +95,9 @@ private static void reflect(Object obj, Properties p, String prefix) {
}
Class<?> cls = obj.getClass();
if (cls.getName().startsWith("org.infinispan.config") && !cls.isEnum()) {
for (Method m : getMethods(obj.getClass())) {
if (m.getParameterTypes().length != 0 || "toString".equals(m.getName())
|| "hashCode".equals(m.getName()) || "toProperties".equals(m.getName())
|| m.isAnnotationPresent(Deprecated.class)) {
continue;
}
for (Method m : getConfigMethods(obj.getClass())) {
try {
String prefixDot = prefix == null || "".equals(prefix) ? "" : prefix + ".";
String prefixDot = prefix == null || prefix.isEmpty() ? "" : prefix + ".";
reflect(m.invoke(obj), p, prefixDot + m.getName());
} catch (IllegalAccessException e) {
// ok
Expand All @@ -105,7 +114,7 @@ private static void reflect(Object obj, Properties p, String prefix) {
for (int i = 0; i < a.length; i++) {
reflect(a[i], p, prefix + "[" + i + "]");
}
} else if (hasPlainToString(cls, obj)) {
} else if (hasPlainToString(obj)) {
// we have a class that doesn't have a nice toString implementation
p.put(prefix, cls.getName());
} else {
Expand Down

0 comments on commit 82eb448

Please sign in to comment.