{"id":1545,"date":"2016-10-03T18:04:17","date_gmt":"2016-10-03T16:04:17","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1545"},"modified":"2017-05-11T10:13:40","modified_gmt":"2017-05-11T08:13:40","slug":"java-plugin","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1545","title":{"rendered":"Java &#8211; Plugin"},"content":{"rendered":"<p>In this short example, we will create an &#8220;easy&#8221; plugin manager.<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Interface\"><span class=\"toc_number toc_depth_1\">1<\/span> Interface<\/a><\/li><li><a href=\"#Plugin_1\"><span class=\"toc_number toc_depth_1\">2<\/span> Plugin 1<\/a><\/li><li><a href=\"#Plugin_2\"><span class=\"toc_number toc_depth_1\">3<\/span> Plugin 2<\/a><\/li><li><a href=\"#Get_the_plugin_class_list\"><span class=\"toc_number toc_depth_1\">4<\/span> Get the plugin class list<\/a><\/li><li><a href=\"#Load_plugins\"><span class=\"toc_number toc_depth_1\">5<\/span> Load plugins<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Interface\">Interface<\/span><\/h1>\n<p>We must create an interface. Our plugins will implement this interface.<\/p>\n<pre lang=\"java\">\r\npublic interface Plugin {\r\n\t\r\n    \/**\r\n     *  Execute something.\r\n     *\/\r\n    public void execute();\r\n    \r\n    \/**\r\n     * @return the class name.\r\n     *\/\r\n    public String getClassName();\r\n}\r\n<\/pre>\n<h1><span id=\"Plugin_1\">Plugin 1<\/span><\/h1>\n<p>Now we can create our first plugin :<\/p>\n<pre lang=\"java\">\r\npublic class Plugin1 implements Plugin {\r\n\r\n\tprivate static final String NAME=\"plugin-1\";\r\n\t\r\n\t\/* (non-Javadoc)\r\n\t * @see Plugin#execute()\r\n\t *\/\r\n\tpublic void execute() {\r\n\t\tSystem.out.println(\"I am in \"+NAME);\r\n\t}\r\n\r\n\t\/* (non-Javadoc)\r\n\t * @see Plugin#getClassName()\r\n\t *\/\r\n\tpublic String getClassName() {\t\t\r\n\t\treturn this.getClass().getName();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h1><span id=\"Plugin_2\">Plugin 2<\/span><\/h1>\n<p>Now we can create our second plugin :<\/p>\n<pre lang=\"java\">\r\npublic class Plugin2 implements Plugin {\r\n\r\n\tprivate static final String NAME=\"plugin-2\";\r\n\t\r\n\t\/* (non-Javadoc)\r\n\t * @see Plugin#execute()\r\n\t *\/\r\n\tpublic void execute() {\r\n\t\tSystem.out.println(\"I am in \"+NAME);\r\n\t}\r\n\r\n\t\/* (non-Javadoc)\r\n\t * @see Plugin#getClassName()\r\n\t *\/\r\n\tpublic String getClassName() {\t\t\r\n\t\treturn this.getClass().getName();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h1><span id=\"Get_the_plugin_class_list\">Get the plugin class list<\/span><\/h1>\n<pre lang=\"java\">\r\nReflections reflections = new Reflections(Plugin.class);\r\nSet<Class<? extends Plugin>> pluginClasses = reflections.getSubTypesOf(pluginInterface);\r\n<\/pre>\n<h1><span id=\"Load_plugins\">Load plugins<\/span><\/h1>\n<p>In the example below, we got the plugin class list. It is ONLY classes (so it is NOT instance !).<br \/>\nWe must create an instance for each plugin to load them :<\/p>\n<pre lang=\"java\">\r\nfor(Class<? extends Plugin> clazz : classes) {\r\n  Plugin plugin = (Plugin) clazz.newInstance();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this short example, we will create an &#8220;easy&#8221; plugin manager. Contents1 Interface2 Plugin 13 Plugin 24 Get the plugin class list5 Load plugins Interface We must create an interface. Our plugins will implement this interface. public interface Plugin { \/** * Execute something. *\/ public void execute(); \/** * @return the class name. *\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1538,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1545","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1545"}],"version-history":[{"count":1,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1545\/revisions"}],"predecessor-version":[{"id":1546,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1545\/revisions\/1546"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1538"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}