/*
 * 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 java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import org.jenetics.util.StaticObject;

/**
 * Helper methods concerning Java reflection.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version @__version__@ &mdash; <em>$Date: 2014-02-01 $</em>
 * @since @__version__@
 */
public class reflect extends StaticObject {
	private reflect() {}

	/**
	 * 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 List<Class<?>> allDeclaredClasses(final Class<?> cls) {
		final Deque<Class<?>> stack = new LinkedList<>();
		stack.addFirst(cls);

		final List<Class<?>> result = new ArrayList<>();
		while (!stack.isEmpty()) {
			final Class<?>[] classes = stack.pollFirst().getDeclaredClasses();
			for (final Class<?> c : classes) {
				result.add(c);
				stack.addFirst(c);
			}
		}

		return Collections.unmodifiableList(result);
	}

	public static Method getMethod(
		final Class<?> type,
		final String name,
		Class<?>[] parameterTypes
	) {
		Method method = null;
		final Method[] methods = type.getMethods();

		for (int i = 0; i < methods.length && method == null; ++i) {
			if (name.equals(methods[i].getName()) &&
				equals(parameterTypes, methods[i].getParameterTypes()))
			{
				method = methods[i];
			}
		}

		return method;
	}

	private static boolean equals(final Class<?>[] p1, final Class<?>[] p2) {
		boolean equals = p1.length == p2.length;
		for (int i = 0; i < p1.length && equals; ++i) {
			equals = toClassType(p1[i]) == toClassType(p2[i]);
		}

		return equals;
	}

	private static Class<?> toClassType(final Class<?> type) {
		switch (type.getCanonicalName()) {
			case "void": return Void.class;
			case "boolean": return Boolean.class;
			case "byte": return Byte.class;
			case "char": return Character.class;
			case "short": return Short.class;
			case "int": return Integer.class;
			case "long": return Long.class;
			case "float": return Float.class;
			case "double": return Double.class;
			default: return type;
		}
	}

	public static Class<?> classOf(final Object value) {
		return value instanceof Class<?> ? (Class<?>)value : value.getClass();
	}

}

@@ -1907,24 +1907,395 @@
 esult);%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns a Method object that reflects the specified public member method%0A%09 * of the class or interface represented by this Class object.%0A%09 *%0A%09 * @param type the class for getting the desired method.%0A%09 * @param name the method name%0A%09 * @param parameterTypes the method parameter types.%0A%09 * @return the method, or %7B@code null%7D if no such method can be found.%0A%09 */%0A
 %09public stat
@@ -3405,16 +3405,290 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Return the class of the given value or the value if it is already from%0A%09 * the type %7B@code Class%7D.%0A%09 *%0A%09 * @param value the value to get the class from.%0A%09 * @return the class from the given value, or %7B@code value%7D if it is already%0A%09 *         a %7B@code Class%7D.%0A%09 */%0A
 %09public 

Add javadoc.