Action { @Nullable public static Action instantiate(JSONObject object) { try { String type = object.getString("type"); JSONObject data = object.optJSONObject("data"); Class clazz = Class.forName(type); return ((Action) clazz.newInstance()).fromJSON(data != null ? data.toString() : ""); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) { log.error("Unhandled exception", e); } return null; }  abstract int friendlyName(); abstract String shortDescription(); abstract void doAction(Callback callback); void generateDialog(LinearLayout root); boolean hasDialog(); String toJSON(); abstract Optional<Integer> icon(); Action fromJSON(String data); @Nullable static Action instantiate(JSONObject object); void apply(Action a); public Trigger precondition; }
@Test public void instantiateTest() throws JSONException { Action action = Action.instantiate(new JSONObject("{\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionTest\"}")); Assert.assertNotEquals(null, action); }