看似很鸡肋其实在某些特殊场景还是比较有用的。
比如你将实体类转Map或者拿到一个Map结果的时候,你是怎么获取某个map的key和value。
方法一:
声明 String key1="aaa"; key为 key1,value 为map.get(key1);
1 2 3 4 5 6 7 8 |
Map<String,Object> map= new HashMap<>(); map.put( "aaa" , 1 );
//获取map的key 和value //key 为key1 String key1= "aaa" ; //value 为 map.get(key1) map.get(key1); |
然后好像日常使用中也没有其他的方法了,下面将带来另外一种使用方法,话不多说直接上代码[/code]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import java.io.Serializable; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.invoke.SerializedLambda; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.function.Function;
/** * Java8通过Function函数获取字段名称(获取实体类的字段名称) * @see ColumnUtil#main(java.lang.String[]) 使用示例 * @author jx */ public class ColumnUtil {
/** * 使Function获取序列化能力 */ @FunctionalInterface public interface SFunction<T, R> extends Function<T, R>, Serializable { }
/** * 字段名注解,声明表字段 */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) public @interface TableField { String value() default "" ; }
//默认配置 static String defaultSplit = "" ; static Integer defaultToType = 0 ;
/** * 获取实体类的字段名称(实体声明的字段名称) */ public static <T> String getFieldName(SFunction<T, ?> fn) { return getFieldName(fn, defaultSplit); }
/** * 获取实体类的字段名称 * @param split 分隔符,多个字母自定义分隔符 */ public static <T> String getFieldName(SFunction<T, ?> fn, String split) { return getFieldName(fn, split, defaultToType); }
/** * 获取实体类的字段名称 * @param split 分隔符,多个字母自定义分隔符 * @param toType 转换方式,多个字母以大小写方式返回 0.不做转换 1.大写 2.小写 */ public static <T> String getFieldName(SFunction<T, ?> fn, String split, Integer toType) { SerializedLambda serializedLambda = getSerializedLambda(fn);
// 从lambda信息取出method、field、class等 String fieldName = serializedLambda.getImplMethodName().substring( "get" .length()); fieldName = fieldName.replaceFirst(fieldName.charAt( 0 ) + "" , (fieldName.charAt( 0 ) + "" ).toLowerCase()); Field field; try { field = Class.forName(serializedLambda.getImplClass().replace( "/" , "." )).getDeclaredField(fieldName); } catch (ClassNotFoundException | NoSuchFieldException e) { throw new RuntimeException(e); }
// 从field取出字段名,可以根据实际情况调整 TableField tableField = field.getAnnotation(TableField. class ); if (tableField != null && tableField.value().length() > 0 ) { return tableField.value(); } else {
//0.不做转换 1.大写 2.小写 switch (toType) { case 1 : return fieldName.replaceAll( "[A-Z]" , split + "$0" ).toUpperCase(); case 2 : return fieldName.replaceAll( "[A-Z]" , split + "$0" ).toLowerCase(); default : return fieldName.replaceAll( "[A-Z]" , split + "$0" ); }
}
}
private static <T> SerializedLambda getSerializedLambda(SFunction<T, ?> fn) { // 从function取出序列化方法 Method writeReplaceMethod; try { writeReplaceMethod = fn.getClass().getDeclaredMethod( "writeReplace" ); } catch (NoSuchMethodException e) { throw new RuntimeException(e); }
// 从序列化方法取出序列化的lambda信息 boolean isAccessible = writeReplaceMethod.isAccessible(); writeReplaceMethod.setAccessible( true ); SerializedLambda serializedLambda; try { serializedLambda = (SerializedLambda) writeReplaceMethod.invoke(fn); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } writeReplaceMethod.setAccessible(isAccessible); return serializedLambda; }
/** * 测试用户实体类 */ public static class TestUserDemo implements Serializable {
private static final long serialVersionUID = 1L;
private String loginName; private String name; private String companySimpleName;
@ColumnUtil .TableField( "nick" ) private String nickName;
public String getLoginName() { return loginName; }
public void setLoginName(String loginName) { this .loginName = loginName; }
public String getNickName() { return nickName; }
public void setNickName(String nickName) { this .nickName = nickName; }
public static long getSerialVersionUID() { return serialVersionUID; }
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public String getCompanySimpleName() { return companySimpleName; }
public void setCompanySimpleName(String companySimpleName) { this .companySimpleName = companySimpleName; } }
/** * 参考示例 */ public static void main(String[] args) {
//实体类原字段名称返回 System.out.println(); System.out.println( "实体类原字段名称返回" ); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getLoginName)); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getNickName)); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getCompanySimpleName));
System.out.println(); System.out.println( "实体类字段名称增加分隔符" ); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getCompanySimpleName, "_" ));
System.out.println(); System.out.println( "实体类字段名称增加分隔符 + 大小写" ); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getCompanySimpleName, "_" , 0 )); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getCompanySimpleName, "_" , 1 )); System.out.println( "字段名:" + ColumnUtil.getFieldName(TestUserDemo::getCompanySimpleName, "_" , 2 ));
}
} |
输出结果:
到此这篇关于Java8通过Function获取字段名(获取实体类的字段名称)的文章就介绍到这了,更多相关Java8通过Function获取字段名内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://www.cnblogs.com/IT-study/p/15351980.html
查看更多关于Java8通过Function获取字段名的方法(获取实体类的字段名称)的详细内容...