/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.internal.util;

import static java.util.Arrays.stream;
import static java.util.stream.Stream.concat;

import java.lang.reflect.Field;
import java.util.Optional;
import java.util.stream.Stream;

import org.jenetics.util.ISeq;

/**
 * Helper methods concerning Java reflection.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 1.6
 * @version 3.0
 */
public class reflect {
	private reflect() {require.noInstance();}


	public static void setField(
		final Object target,
		final String name,
		final Object value
	) {
		final Field field = findField(target.getClass(), name)
			.orElseThrow(() -> new IllegalArgumentException(name + " not found."));

		try {
			field.setAccessible(true);
			field.set(target, value);
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(e);
		}
	}

	private static Optional<Field> findField(final Class<?> cls, final String name) {
		return allFields(cls)
			.filter(f -> f.getName().equals(name))
			.findFirst();
	}

	private static Stream<Field> allFields(final Class<?> cls) {
		return allClasses(cls).flatMap(c -> Stream.of(c.getDeclaredFields()));
	}

	private static Stream<Class<?>> allClasses(final Class<?> cls) {
		return Stream.concat(
			Stream.of(cls),
			Optional.ofNullable(cls.getSuperclass())
				.map(reflect::allClasses)
				.orElse(Stream.empty())
		);
	}

	/**
	 * Return all declared classes of the given class, with arbitrary nested
	 * level.
	 *
	 * @param cls the class for which the declared classes are retrieved.
	 * @return all nested classes
	 */
	public static Stream<Class<?>> innerClasses(final Class<?> cls) {
		return concat(
			stream(cls.getDeclaredClasses()).flatMap(reflect::innerClasses),
			stream(cls.getDeclaredClasses())
		);
	}

	/**
	 * Return the class of the given value or the value if it is already from
	 * the type {@code Class}.
	 *
	 * @param value the value to get the class from.
	 * @return the class from the given value, or {@code value} if it is already
	 *         a {@code Class}.
	 */
	public static Class<?> classOf(final Object value) {
		return value instanceof Class<?> ? (Class<?>)value : value.getClass();
	}

	@SuppressWarnings("unchecked")
	public static <T> Optional<T> newInstance(final Class<?> type) {
		try {
			return Optional.of((T)type.newInstance());
		} catch (InstantiationException | IllegalAccessException e) {
			return Optional.empty();
		}
	}

	@SuppressWarnings("unchecked")
	public static <A, B extends A> ISeq<A> cast(final ISeq<B> seq) {
		return (ISeq<A>)seq;
	}
}

@@ -1146,11 +1146,21 @@
 ion 
-3.0
+!__version__!
 %0A */
@@ -1227,17 +1227,457 @@
 ce();%7D%0A%0A
+%09/**%0A%09 * Reflectively sets the field with the given %7B@code name%7D to the new%0A%09 * %7B@code value%7D. The new value is set to the first found field in the%0A%09 * whole class hierarchy.%0A%09 *%0A%09 * @param target the object which owns the field%0A%09 * @param name the field name%0A%09 * @param value the new field value%0A%09 * @throws IllegalArgumentException if no field with the given %7B@code name%7D%0A%09 *         can be found or it's not allowed to set the field%0A%09 */
 %0A
-
 %09public 
@@ -2099,16 +2099,19 @@
 ndField(
+%0A%09%09
 final Cl
@@ -2121,17 +2121,19 @@
 %3C?%3E cls,
- 
+%0A%09%09
 final St
@@ -2141,16 +2141,18 @@
 ing name
+%0A%09
 ) %7B%0A%09%09re

Add Javadoc.