- if (List.class.isAssignableFrom(returnType)) {
- try {
- return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(), () -> {
- return ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType()).flatMap(
- result -> result instanceof Class && DataContainer.class.isAssignableFrom((Class) result)
- ? Optional.of((Class<? extends DataContainer>) result) : Optional.empty());
- });
- } catch (Exception e) {
- /*
- * It is safe to log this this exception on debug, since this
- * method should not fail. Only failures are possible if the
- * runtime / backing.
- */
- LOG.debug("Unable to find YANG modeled return type for {}", method, e);
+ return Optional.empty();
+ }
+
+ @SuppressWarnings("checkstyle:illegalCatch")
+ private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
+ final int parameterOffset) {
+ try {
+ return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(),
+ () -> genericParameter(method.getGenericReturnType(), parameterOffset)
+ .flatMap(result -> result instanceof Class ? optionalCast((Class<?>) result) : Optional.empty()));
+ } catch (Exception e) {
+ /*
+ * It is safe to log this this exception on debug, since this
+ * method should not fail. Only failures are possible if the
+ * runtime / backing.
+ */
+ LOG.debug("Unable to find YANG modeled return type for {}", method, e);
+ }
+ return Optional.empty();
+ }
+
+ private static Optional<Class<? extends DataContainer>> optionalCast(final Class<?> type) {
+ return DataContainer.class.isAssignableFrom(type) ? optionalDataContainer(type) : Optional.empty();
+ }
+
+ private static Optional<Class<? extends DataContainer>> optionalDataContainer(final Class<?> type) {
+ return Optional.of(type.asSubclass(DataContainer.class));
+ }
+
+ private static Optional<Type> genericParameter(final Type type, final int offset) {
+ if (type instanceof ParameterizedType) {
+ final Type[] parameters = ((ParameterizedType) type).getActualTypeArguments();
+ if (parameters.length > offset) {
+ return Optional.of(parameters[offset]);