Package org.strecks.injection.annotation

Source Code of org.strecks.injection.annotation.TestAnnotationChecker

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

import java.lang.reflect.Method;

import org.apache.commons.beanutils.ConvertUtilsBean;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestAnnotationChecker
{

  private Integer value;

  public void check(String className, String someValue) throws Exception
  {

    Class<?> forName = Class.forName(className);
    for (Method m : forName.getMethods())
    {

      if (m.isAnnotationPresent(InjectRequestParameter.class))
      {

        Class<?>[] parameterTypes = m.getParameterTypes();
        if (parameterTypes.length != 1)
        {
          throw new ApplicationRuntimeException("Method must have one parameter");
        }

        Class type = parameterTypes[0];

        String methodName = m.getName();
        if (!methodName.startsWith("set"))
        {
          throw new ApplicationRuntimeException("Method begin with set");
        }

        Character firstChar = methodName.charAt(3);
        String propertyName = methodName.substring(4);
        propertyName = Character.toLowerCase(firstChar) + propertyName;

        System.out.println("Type: " + type + ", property " + propertyName);

        Object convert = new ConvertUtilsBean().convert(someValue, type);

        Method method = this.getClass().getMethod(methodName, new Class[]
        {
          type
        });
        method.invoke(this, new Object[]
        {
          convert
        });

      }
    }
  }

  @InjectRequestParameter()
  public void setIntegerValue(Integer value)
  {
    this.value = value;
  }

  @Test
  public void testAnnotationChecker() throws Exception
  {
    check(this.getClass().getName(), "1");
    assert value == 1;
  }
}
TOP

Related Classes of org.strecks.injection.annotation.TestAnnotationChecker

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.