Resolves the type of items for a {@link Field} declared as a {@link List}. + * + *
This method will first try to check the parametrized type of the field type. If none is defined, it will try to infer + * the type of items by looking at the value of the field for the given {@code instance}. + * + *
Make sure the field is accessible before invoking this method.
+ *
+ * @param field the field declared as {@link List}
+ * @param instance the instance that should be used to obtain infer the type in case no parametrized type is found in the field.
+ * @return if the field is not a {@link List}, it returns null. Otherwise the type of items of the list. If the type for items can not be inferred, the {@link Object} type is returned.
+ * @throws IllegalAccessException in case it fails to obtain the value of the field from the {@code instance}
+ */
+ public static Class> resolveListType(Field field, Object instance) throws IllegalAccessException {
+ if (!List.class.isAssignableFrom(field.getType())) {
+ return null;
+ }
+
+ Type genericType = field.getGenericType();
+
+ if (genericType instanceof ParameterizedType) {
+ Type[] typeArguments = ParameterizedType.class.cast(genericType)
+ .getActualTypeArguments();
+
+ if (typeArguments[0] instanceof Class) {
+ return (Class>) typeArguments[0];
+ }
+ } else if (instance != null) {
+ // just in case the field is not parametrized
+ List item = List.class.cast(field.get(instance));
+
+ if (!item.isEmpty()) {
+ return item.get(0).getClass();
+ }
+ }
+
+ return Object.class;
+ }
}
\ No newline at end of file
diff --git a/common/src/main/java/org/keycloak/common/util/reflections/UnSetAccessiblePrivilegedAction.java b/common/src/main/java/org/keycloak/common/util/reflections/UnSetAccessiblePrivilegedAction.java
new file mode 100644
index 0000000000..cfdb1302e4
--- /dev/null
+++ b/common/src/main/java/org/keycloak/common/util/reflections/UnSetAccessiblePrivilegedAction.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.keycloak.common.util.reflections;
+
+import java.lang.reflect.AccessibleObject;
+import java.security.PrivilegedAction;
+
+/**
+ * A {@link PrivilegedAction} that calls {@link AccessibleObject#setAccessible(boolean)}
+ */
+public class UnSetAccessiblePrivilegedAction implements PrivilegedAction