Package org.strecks.injection.internal

Source Code of org.strecks.injection.internal.InjectionAnnotationReader

/*
* 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.injection.internal;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.strecks.controller.Injectable;
import org.strecks.controller.internal.ActionBeanAnnotationReader;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.injection.factory.InjectionFactoryClass;
import org.strecks.injection.factory.InjectionHandlerFactory;
import org.strecks.injection.handler.InjectionHandler;
import org.strecks.util.BeanUtils;
import org.strecks.util.ReflectHelper;

/**
* Collects information about the inputs in a stateful action object. Builds an
* <code>InjectionWrapper</code> for each property setter method which has an annotation which is
* itself annotated using the <code>InjectionFactoryClass</code> annotation
* @author Phil Zoio
*/
public class InjectionAnnotationReader implements ActionBeanAnnotationReader<Injectable>
{

  private Map<String, InjectionWrapper> injectionMap;

  public void populateController(Injectable controller)
  {
    controller.setInjectionHandlers(injectionMap);
  }

  /**
   * Inspects bean for annotations which themselves are annotated using the
   * <code>InjectionFactoryClass</code> annotation. The factory class is instantiated, and used
   * to create an <code>InjectionHandler</code> instance which is itself wrapped using an
   * <code>InjectionWrapper</code>. Does this for each property, building a map of property
   * names to <code>InjectionWrapper</code> instances
   */
  public boolean readAnnotations(Class actionClass)
  {

    for (Method m : actionClass.getMethods())
    {

      Annotation[] annotations = m.getAnnotations();
      boolean hasFactory = false;
      InjectionWrapper wrapper = null;

      for (Annotation annotation : annotations)
      {

        Class<? extends Annotation> annotationType = annotation.annotationType();
        InjectionFactoryClass factoryClass = annotationType.getAnnotation(InjectionFactoryClass.class);

        if (factoryClass != null)
        {

          hasFactory = true;
          InjectionHandlerFactory factory = ReflectHelper.createInstance(factoryClass.value(),
              InjectionHandlerFactory.class);
         
          if (wrapper == null)
          {
            InjectionSetter inputSetter = createSetter(actionClass, m);
            PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(m);
           
            if (propertyDescriptor == null)
            {
              throw new ApplicationConfigurationException("Method " + m.getName() + " has no valid property descriptor. Is this a valid property");
            }
           
            wrapper = new InjectionWrapper(inputSetter, propertyDescriptor);
          }
          InjectionHandler handler = factory.createInjectionHandler(annotation, actionClass, wrapper
              .getPropertyDescriptor());
          wrapper.addHandler(handler);

        }

      }

      if (hasFactory)
      {

        if (injectionMap == null)
          injectionMap = new HashMap<String, InjectionWrapper>();

        injectionMap.put(wrapper.getInjectionSetter().getPropertyName(), wrapper);

      }

    }

    return injectionMap != null;
  }

  public InjectionSetter createSetter(Class clazz, Method m)
  {

    Class<?>[] parameterTypes = m.getParameterTypes();
    String methodName = ReflectHelper.checkSetterMethodName(m);
    String propertyName = ReflectHelper.getPropertyName(methodName);

    Class type = parameterTypes[0];

    InjectionSetter setter = new InjectionSetter(clazz, methodName, propertyName, type);

    return setter;

  }

  public Map<String, InjectionWrapper> getInjectionMap()
  {
    return injectionMap;
  }

}
TOP

Related Classes of org.strecks.injection.internal.InjectionAnnotationReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.