Java如何獲取接口所有的實(shí)現(xiàn)類
Java獲取接口所有的實(shí)現(xiàn)類
最近因業(yè)務(wù)需求,要實(shí)現(xiàn)NodeRed服務(wù)后端化,為使各個節(jié)點(diǎn)的解析進(jìn)行插件化(NodeRed各個節(jié)點(diǎn)也是插件化,安裝插件即可使用) ,后端不得不動態(tài)加載解析NodeRed節(jié)點(diǎn)json,用一個接口來統(tǒng)一管理。
import cn.hutool.core.util.ClassUtil;
import java.util.*;
/**
* 此類用來解析NodeRed服務(wù)器的json串
*/
public class NodeRedParseUtil {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
//獲取所有的模型實(shí)現(xiàn)類
Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.ciih.nodred.plugins", NodeRedModel.class);
Map<String, NodeRedModel> map = new HashMap<>(classes.size());
for (Class<?> aClass : classes) {
//實(shí)例化
Object o = aClass.newInstance();
System.out.println();
if (o instanceof NodeRedModel) {
NodeRedModel nodeRedModel = (NodeRedModel) o;
map.put(nodeRedModel.getType(), nodeRedModel);
}
}
}
}反射獲取接口的所有實(shí)現(xiàn)類
添加依賴
implementation 'org.reflections:reflections:0.9.12'
接口
package com.example.myapplication.people;
public interface IPeople {
? String say();
}實(shí)現(xiàn)類
package com.example.myapplication.people;
public class Student implements IPeople{
? @Override
? public String say() {
? ? return "I am a student";
? }
}package com.example.myapplication.people;
public class Teacher implements IPeople{
? @Override
? public String say() {
? ? return "I am a teacher";
? }
}工具類
package com.example.myapplication;
import android.net.IpPrefix;
import com.example.myapplication.people.IPeople;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.reflections.Reflections;
public class Utils {
? public static Map<String,Class> getAllIPeopleImpl(){
? ? Reflections reflection=new Reflections("com.example.myapplication.people");
? ? Map<String,Class> map=new HashMap<>();
? ? Set<Class<? extends IPeople>> set=reflection.getSubTypesOf(IPeople.class);
? ? for(Class c:set){
? ? ? map.put(c.getSimpleName(),c);
? ? }
? ? return map;
? }
}測試類
package com.example.myapplication;
import com.example.myapplication.people.IPeople;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
/**
?* Example local unit test, which will execute on the development machine (host).
?*
?* @see <a rel="external nofollow" >Testing documentation</a>
?*/
public class ExampleUnitTest {
??
? @Test
? public void test() {
? ? Map<String, Class> map = Utils.getAllIPeopleImpl();
? ? try {
? ? ? for (String str : map.keySet()) {
? ? ? ? Object people = map.get(str).newInstance();
? ? ? ? if(people instanceof IPeople){
? ? ? ? ? System.out.println(str);
? ? ? ? ? System.out.println(((IPeople) people).say());
? ? ? ? }
? ? ? }
? ? } catch (IllegalAccessException | InstantiationException e) {
? ? ? e.printStackTrace();
? ? }
? }
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java接口操作(繼承父類并實(shí)現(xiàn)多個接口)
這篇文章主要介紹了Java接口操作(繼承父類并實(shí)現(xiàn)多個接口),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
SpringBoot如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證
為了保證參數(shù)的正確性,我們需要使用參數(shù)驗(yàn)證機(jī)制,來檢測并處理傳入的參數(shù)格式是否符合規(guī)范,所以本文就來和大家聊聊如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證吧2023-08-08
解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題
這篇文章主要介紹了解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Java實(shí)現(xiàn)升級版布谷鳥闖關(guān)游戲的示例代碼
升級版布谷鳥闖關(guān)游戲是一個基于java的布谷鳥闖關(guān)游戲,鼠標(biāo)左鍵點(diǎn)擊控制鳥的位置穿過管道間的縫隙。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-02-02
為什么說要慎用SpringBoot @ComponentScan
本文主要介紹了為什么說要慎用SpringBoot @ComponentScan,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Java實(shí)現(xiàn)監(jiān)聽UDP協(xié)議的指定端口并收到數(shù)據(jù)按照十六進(jìn)制輸出方式
這篇文章主要介紹了Java實(shí)現(xiàn)監(jiān)聽UDP協(xié)議的指定端口并收到數(shù)據(jù)按照十六進(jìn)制輸出方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

