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

org.onetwo.common.annotation.AnnotationInfo Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
package org.onetwo.common.annotation;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.onetwo.common.utils.Assert;
import org.onetwo.common.utils.LangUtils;


@SuppressWarnings("unchecked")
public class AnnotationInfo {

	private final Class sourceClass;
	private final List annotations;
	

	public AnnotationInfo(Class sourceClass) {
		this(sourceClass, sourceClass.getAnnotations());
	}
	
	public AnnotationInfo(Class sourceClass, Annotation...annoArray) {
		this.sourceClass = sourceClass;
		if(LangUtils.isEmpty(annoArray)){
			this.annotations = Collections.EMPTY_LIST;
			return ;
		}
		this.annotations = new ArrayList(annoArray.length);
		for(Annotation a : annoArray)
			annotations.add(a);
	}


	public Class getSourceClass() {
		return sourceClass;
	}


	public List getAnnotations() {
		return annotations;
	}
	
	public boolean hasAnnotation(Class annoClass){
		return getAnnotation(annoClass)!=null;
	}
	
	public  T getAnnotation(Class annoClass){
		Assert.notNull(annoClass, "annotation class can not be null");
		for(Annotation anno : getAnnotations()){
			if(annoClass.isInstance(anno))
				return (T)anno;
		}
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy