您的位置:首页 > 编程语言 > Java开发

多字段 java对象排序

2016-03-12 15:07 288 查看
public class ReflexUtil {
static Logger logger = LoggerFactory.getLogger(ReflexUtil.class);

//getMethod
static public Object invokeMethod(String propertiesName, Object object) {
try {
if(object==null) return null;
if (!propertiesName.contains(".")) {
String methodName = "get"+getMethodName(propertiesName);
Method method = object.getClass().getMethod(methodName);
return method.invoke(object);
}
String methodName = "get"+getMethodName(propertiesName.substring(0,propertiesName.indexOf(".")));
Method method = object.getClass().getMethod(methodName);
return invokeMethod(propertiesName.substring(propertiesName.indexOf(".")+1), method.invoke(object));

} catch (Exception e) {
logger.error(e.toString(), e);
return null;
}
}

private static String getMethodName(String fildeName) {
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}

public static void main(String args[]) {
Video video = new Video();
Album album = new Album();
album.setAlbumId(346l);
video.setAlbum(album);
video.setVideoId(126l);
System.out.println(ReflexUtil.invokeMethod("album.albumId", video));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: