All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xj4.XJ4TestClass Maven / Gradle / Ivy

/**
 * Copyright 2008 Peach Jean Solutions
 * 
 * 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.xj4;

import java.lang.reflect.Field;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.runners.model.TestClass;

/**
 * @author jbunting
 * @TODO Document Me!
 * Created by IntelliJ IDEA.
 * Date: Feb 20, 2008
 * Time: 9:21:52 AM
 */
public class XJ4TestClass extends TestClass {

  private static final Log logger = LogFactory.getLog(XJ4TestClass.class);

  private Class javaClass;

  public XJ4TestClass(Class javaClass) {
    super(javaClass);
    this.javaClass = javaClass;
  }

  public List getAnnotatedFields(Class annotationClass) {
    List results= new ArrayList();
    return getAnnotatedFields(annotationClass, results);
  }

  public List getAnnotatedFields(Class annotationClass, List results) {
    logger.trace("Retrieving fields annotated with " + annotationClass.getName());
    for (Class eachClass : getSuperClasses(javaClass)) {
      Field[] fields= eachClass.getDeclaredFields();
      for (Field eachField : fields) {
        if(logger.isTraceEnabled()) {
          logger.trace("Checking field: " + eachField.getDeclaringClass().getName() + " : " + eachField.getName());
          Annotation[] annotations = eachField.getAnnotations();
          for(Annotation annotation: annotations) {
            logger.trace("Annotation: " + annotation.getClass().getName());
          }
        }
        if (eachField.isAnnotationPresent(annotationClass) && ! isShadowed(eachField, results)) {
          logger.trace("Found one!");
          results.add(eachField);
        }
      }
    }
    return results;
  }

  private boolean isShadowed(Field field, List results) {
		for (Field each : results) {
			if (isShadowed(field, each))
				return true;
		}
		return false;
	}

	private boolean isShadowed(Field current, Field previous) {
		return previous.getName().equals(current.getName());
	}

  private List> getSuperClasses(Class< ?> testClass) {
    ArrayList> results= new ArrayList>();
    Class current= testClass;
    while (current != null) {
      results.add(current);
      current= current.getSuperclass();
    }
    return results;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy