プライベートメソッドに外部からアクセスする

private String Hoge#fuga(String arg) の場合

Hoge hogeClass = new Hoge();
Method fugaMethod =  hogeClass.getClass().getDeclaredMethod("fuga", new Class[] {String.class});
fugaMethod.setAccessible(true);
String ret = (String) fugaMethod.invoke(hogeClass, new Object[] {"abc"});

private String Hoge#fuga(int i, String arg) の場合

Hoge hogeClass = new Hoge();
Method fugaMethod =  hogeClass.getClass().getDeclaredMethod("fuga", new Class[] {int.class, String.class});
fugaMethod.setAccessible(true);
String ret = (String) fugaMethod.invoke(hogeClass, new Object[] {1, "abc"});

private boolean isPiyo;
private void Hoge#fuga(String[] args) の場合

Hoge hogeClass = new Hoge();
Method fugaMethod =  hogeClass.getClass().getDeclaredMethod("fuga", new Class[] {String[].class});
fugaMethod.setAccessible(true);
String ret = (String) fugaMethod.invoke(hogeClass, new Object[] { new String[] {"aaa", "bbb"} });

Field piyoField = batch.getClass().getDeclaredField("isPiyo");
piyoField.setAccessible(true);
System.out.println(piyoField.getBoolean(hogeClass));


private static void Hoge#fuga(String args) の場合

Class<Hoge> clazz = Hoge.class;
Method fugaMethod = clazz.getDeclaredMethod("fuga", String.class);
fugaMethod.setAccessible(true);
String ret = (String) fugaMethod.invoke(null, "123");