使用注解确实提高代码的强壮度和开发速度,何乐而不为呢。
org.apache.commons.io.FileUtils:常用的不同目录(临时目录、用户目录)、文件大小格式化输出、写文件、读文件、创建包含父目录的文件、拷贝文件、删除文件、过滤文件、比较文件内容等。
org.apache.commons.io.FilenameUtils:根据路径分隔符判断不同系统、格式化文件路径、不同文件路径转换、获取文件名、扩展名等。
org.apache.commons.io.IOUtils:不同格式数据格式转换、开启或关闭文件读写流等。
Spring
org.springframework.web.util.WebUtil:对request、session、cookie和servletContext的一些操作。
ClassUtil:对于类的一些操作,可以用org.reflections.ReflectionUtils ,功能更强大。
StringUtil:常用字符串处理,裁剪、查找、空值判断、大写首字母、替换等。
NumberUtils:解析数字为指定类型(Integer、Float、Double)
ObjectUtils:主要包括对于数组的一些操作,结合java.util.Arrays对数组操作基本都包括。
BeanUtils:对实例化对象的操作,主要包括属性过滤拷贝、实例化类操作。
org.springframework.util.ReflectionUtils:spring提供的反射帮助类
CollectionUtils:对于集合的操作,包括查找、空判断、转Array等。
StreamUtil:用于inputStream、outputStream和byte[ ]之间的相互转换。
PatternMatchUtils:匹配String和Annotation表达式。
DigestUtils:MD5等算法的加密。
AnnotatedElementUtils: 相比较AnnotationUtils , 支持@inherited属性的注解
AnnotationUtils:获取在类上的注解信息。也可以自己写方法获取在类、方法和属性上的注解,参考如下。
private static final Map<String, Map<String, ?>> classAnnotationMap = new HashMap<String, Map<String, ?>>() ;
/**
* 查询在get方法上是否存在注解
*/
public static <T extends Annotation> Map<String, T> getMethodAnnotation(Class beanClass , Class<T> annotationClass) throws IntrospectionException{
Map<String, T> methodAnnotationMap = new HashMap<String, T>() ;
PropertyDescriptor[] ps = Introspector.getBeanInfo(beanClass).getPropertyDescriptors() ;
for (PropertyDescriptor propertyDescriptor : ps) {
Annotation[] annotations = propertyDescriptor.getReadMethod().getAnnotations() ;
T methodField = propertyDescriptor.getReadMethod().getAnnotation(annotationClass) ;
if(methodField!=null){
methodAnnotationMap.put(propertyDescriptor.getName(), methodField) ;
}
}
return methodAnnotationMap ;
}
/**
* 查询在属性上是否存在注解
*/
public static <T extends Annotation> Map<String, T> getFieldAnnotation(Class beanClass , Class<T> annotationClass){
Map<String, T> fieldAnnotationMap = new HashMap<String, T>() ;
java.lang.reflect.Field[] fields = beanClass.getDeclaredFields() ;
for (java.lang.reflect.Field field : fields) {
T attributeField = field.getAnnotation(annotationClass) ;
if(attributeField!=null){
fieldAnnotationMap.put(field.getName(), attributeField) ;
}
}
return fieldAnnotationMap ;
}
/**
* 获取注解
* @param beanClass:fieldName所在的类
* @param fieldName:字段名称
* @param annotationClass:注解类
* @return
* @throws IntrospectionException
*/
public static <T extends Annotation> T getAnnotation(Class beanClass , String fieldName, Class<T> annotationClass) throws IntrospectionException{
if(classAnnotationMap.containsKey(beanClass.getName())){
return (T) classAnnotationMap.get(beanClass.getName()).get(fieldName) ;
}
Map<String, T> annotationMap = getMethodAnnotation(beanClass, annotationClass) ;
Map<String, T> fieldAnnotationMap = getFieldAnnotation(beanClass, annotationClass) ;
//合并annotationMap和fieldAnnotationMap
annotationMap.putAll(fieldAnnotationMap) ;
classAnnotationMap.put(beanClass.getName(), annotationMap) ;
return annotationMap.get(fieldName) ;
}
Assert:检验变量是否为目标类型,如果不是,抛出异常。这是一个抽象类,只提供了静态方法。
获取类型信息:(spring帮助类),能够获取get和set方法,
TypeInformation<Product> typeInformation = ClassTypeInformation.from(Product.class) ;
Reflections
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9-RC1</version>
</dependency>
例如:获取在某个包下一个父类的所有子类:
public static void main(String[] args) {
org.reflections.Reflections reflections = new Reflections("com.projects.system.entity") ;
Set<Class<? extends BaseEntity>> subSet = reflections.getSubTypesOf(BaseEntity.class) ;
for (Class<? extends BaseEntity> clazz : subSet) {
System.out.println(clazz.getName());
}
}