java 反射实现JSTL


  //getMethod
    static public Object invokeMethod(String propertiesName, Object object) {
        try {
            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);
            System.out.print(propertiesName.substring(propertiesName.indexOf(".")+1));
            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(345l);
        video.setAlbum(album);
        video.setVideoId(123l);
        System.out.println(ReflexUtil.invokeMethod("album.albumId", video));
    }


你可能感兴趣的:(java 反射实现JSTL)