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

org.nico.aoc.scan.buddy.AnnotationBuddy Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package org.nico.aoc.scan.buddy;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.nico.aoc.scan.annotations.ConstructionLabel;
import org.nico.aoc.scan.annotations.Label;
import org.nico.aoc.scan.entity.LabelType;
import org.nico.util.collection.ArrayUtils;
import org.nico.util.collection.CollectionUtils;

/** 
 * Annotation buddy
 * @author nico
 * @version createTime:2018年1月21日 下午3:08:02
 */

public class AnnotationBuddy {

	/**
	 * Gets the Field for the Label annotation of the specified type.
	 * 
	 * @param clazz The target class
	 * @param type The target type
	 * @return	List
	 */
	public static List getLabelFields(Class clazz, LabelType type){
		List fieldList = new ArrayList();
		List allFields = new ArrayList();
		getFields(allFields, clazz);
		if(CollectionUtils.isNotBlank(allFields)){
			for(Field field: allFields){
				Label label = null;
				if((label = field.getDeclaredAnnotation(Label.class)) != null){
					if(type == null){
						fieldList.add(field);
					}else{
						if(label.type() == type){
							fieldList.add(field);
						}
					}
				}
			}
		}
		return fieldList;
	}
	
	/**
	 * Get field by clazz & superClass
	 * 
	 * @param fieldList 
	 * 			Stroge Field
	 * @param clazz
	 * 			Target class
	 */
	private static void getFields(List fieldList, Class clazz){
		if(clazz != null){
			Field[] fields = clazz.getDeclaredFields();
			if(ArrayUtils.isNotBlank(fields)){
				fieldList.addAll(Arrays.asList(fields));
			}
			getFields(fieldList, clazz.getSuperclass());
		}
	}

	/**
	 * Gets the Field for the Label annotation of the all.
	 * 
	 * @param clazz The target class
	 * @return	List
	 */
	public static List getLabelFields(Class clazz){
		return getLabelFields(clazz, null);
	}
	
	/**
	 * Gets the constructs that need to be injected.
	 * 
	 * @param clazz The target class
	 * @return {@link Constructor}
	 */
	public static Constructor getLabelConstructionParams(Class clazz){
		Constructor[] constructors = clazz.getDeclaredConstructors();
		if(ArrayUtils.isNotBlank(constructors)){
			for(Constructor constructor: constructors){
				if(constructor.isAnnotationPresent(ConstructionLabel.class)){
					return constructor;
				}
			}
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy