/*
* Copyright 2005-2006 the original author or authors.
*
* 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.strecks.bind.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import org.strecks.bind.factory.BindFactoryClass;
import org.strecks.bind.factory.BindHandlerFactory;
import org.strecks.bind.handler.BindHandler;
import org.strecks.converter.Converter;
import org.strecks.converter.handler.ConversionHandler;
import org.strecks.converter.handler.DefaultConversionHandler;
import org.strecks.converter.internal.ConverterReader;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.util.ReflectHelper;
/**
* Reads annotations on an action bean class and creates relevant <code>BindHandler</code> for
* bound properties
*
* @author Phil Zoio
*/
public class BindAnnotationReader
{
public BindConvertInfo readBindables(Object thisBean)
{
Map<String, BindHandler> bindMap = new LinkedHashMap<String, BindHandler>();
Map<String, Converter> converterMap = new LinkedHashMap<String, Converter>();
Class thisClass = thisBean.getClass();
ConversionHandler conversionHandler = new DefaultConversionHandler();
for (Method getterMethod : thisClass.getMethods())
{
Annotation[] annotations = getterMethod.getAnnotations();
for (Annotation annotation : annotations)
{
Class<? extends Annotation> annotationType = annotation.annotationType();
BindFactoryClass factoryClass = annotationType.getAnnotation(BindFactoryClass.class);
if (factoryClass != null)
{
checkMethodIsGetter(getterMethod, annotationType);
String getterName = getterMethod.getName();
String thisPropertyName = ReflectHelper.getPropertyName(getterName);
// check for an explicitly named converter
ConverterReader converterReader = new ConverterReader();
Converter explicitConverter = converterReader.readConverter(getterMethod);
// create factory
BindHandlerFactory factory = ReflectHelper.createInstance(factoryClass.value(),
BindHandlerFactory.class);
// use factory to create handler and register this
BindHandler handler = factory.createHandler(annotation, getterMethod, explicitConverter,
conversionHandler);
Class converterClass = null;
if (explicitConverter != null)
{
converterClass = explicitConverter.getClass();
}
else
{
converterClass = handler.getConverter().getClass();
}
checkActualConverter(thisPropertyName, getterMethod, converterClass);
bindMap.put(thisPropertyName, handler);
converterMap.put(thisPropertyName, handler.getConverter());
}
}
}
BindConvertInfo bci = new BindConvertInfo(bindMap, converterMap, new DefaultConversionHandler());
return bci;
}
/**
* Checks that the type of the property matches the parameterized type of the converter
*/
void checkActualConverter(String propertyName, Method getterMethod, Class converterClass)
{
Class genericType = ReflectHelper.getGenericType(converterClass, Converter.class);
Method setterMethod = ReflectHelper.getSetter(getterMethod);
if (setterMethod == null)
{
throw new ApplicationConfigurationException("Bind annotation used for " + getterMethod
+ " which has no corresponding setter method");
}
if (genericType != null)
{
Class<?> returnType = getterMethod.getReturnType();
if (!returnType.equals(genericType))
throw new ApplicationConfigurationException("Method " + getterMethod
+ " is not type compatible with the type of the converter class " + converterClass.getName()
+ ", which is parameterized with the type " + genericType.getName());
Class<?> setterParameter = setterMethod.getParameterTypes()[0];
if (!setterParameter.equals(genericType))
throw new RuntimeException("Method " + setterMethod
+ " is not type compatible with the type of the converter class " + converterClass.getName()
+ ", which is parameterized with the type " + genericType.getName());
}
}
private void checkMethodIsGetter(Method method, Class<? extends Annotation> annotationType)
{
if (!ReflectHelper.isGetter(method))
{
throw new ApplicationConfigurationException("Invalid @" + annotationType.getSimpleName()
+ " annotation for method " + method + ": annotation only supports getter methods of type String");
}
}
}