package org.bootstrapjsp.util;

import java.lang.reflect.Method;

import org.bootstrapjsp.tags.Component;

public class ComponentUtil {

	public static String getComponentName(Component component) {
		final Class<? extends Component> clazz = component.getClass();
		final String packageName = clazz.getPackage().getName();
		final String componentName = clazz.getName().replace(packageName + ".", "");
		return componentName.toLowerCase();
	}
	
	public static boolean setProperty(Component component, String property, String value) {
		final StringBuilder methodNameBuilder = new StringBuilder("set");
		methodNameBuilder.append(property.substring(0, 1).toUpperCase());
		methodNameBuilder.append(property.substring(1));
		final String setMethodName = methodNameBuilder.toString();
		try {
			for (Method setMethod :component.getClass().getMethods()) {
				if (setMethod.getName().equals(setMethodName)) {
					final Class<?> parameterType = setMethod.getParameterTypes()[1];
					if (String.class.equals(parameterType)) {
						setMethod.invoke(component, new Object[] {value});
					} else if (Boolean.class.equals(parameterType)) {
						final Boolean bool = Boolean.valueOf(value);
						setMethod.invoke(component, new Object[] {bool});
					}
					return true;
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return false;
	}
}

@@ -965,17 +965,17 @@
 Types()%5B
-1
+0
 %5D;%0A%09%09%09%09%09
@@ -1116,32 +1116,70 @@
 s(parameterType)
+ %7C%7C Boolean.TYPE.equals(parameterType)
 ) %7B%0A%09%09%09%09%09%09final 
@@ -1304,21 +1304,66 @@
 ;%0A%09%09%09%09%7D%0A
-
 %09%09%09%7D%0A
+%09%09%7D catch (RuntimeException e) %7B%0A%09%09%09throw e;%0A
 %09%09%7D catc

Fix detection of primitive boolean parameter type on set method