ASM 判定一个类,实现了指定接口
生活随笔
收集整理的這篇文章主要介紹了
ASM 判定一个类,实现了指定接口
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
ASM 判定一個(gè)類,實(shí)現(xiàn)了指定接口
技術(shù)支持
ASM 中,ClassReader 類:對(duì)已存在的進(jìn)行解析,并提供獲取類信息的方法。
通過 ClassReader ,實(shí)現(xiàn)對(duì)一個(gè)類的解析。
處理流程
依據(jù)如下流程,判斷出一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口:
- 循環(huán)判斷是否實(shí)現(xiàn)目標(biāo)接口
- 遞歸判斷父接口是否存在目標(biāo)接口
- 遞歸判斷父類是否實(shí)現(xiàn)目標(biāo)接口
具體實(shí)現(xiàn)
具體實(shí)現(xiàn)代碼如下 :
package utils;import org.objectweb.asm.ClassReader;import java.io.IOException; import java.util.Set;/*** 判斷某類是否實(shí)現(xiàn)了指定接口集合** @author pengpj* @date 2018/11/27*/ public class SpecifiedInterfaceImplementionChecked {/*** 判斷是否實(shí)現(xiàn)了指定接口** @param reader class reader* @param interfaceSet interface collection* @return check result*/public static boolean hasImplSpecifiedInterfaces(ClassReader reader, Set<String> interfaceSet) {if (isObject(reader.getClassName())) {return false;}try {if (containedTargetInterface(reader.getInterfaces(), interfaceSet)) {return true;} else {ClassReader parent = new ClassReader(reader.getSuperName());return hasImplSpecifiedInterfaces(parent, interfaceSet);}} catch (IOException e) {return false;}}/*** 檢查當(dāng)前類是 Object 類型** @param className class name* @return checked result*/private static boolean isObject(String className) {return "java/lang/Object".equals(className);}/*** 檢查接口及其父接口是否實(shí)現(xiàn)了目標(biāo)接口** @param interfaceList 待檢查接口* @param interfaceSet 目標(biāo)接口* @return checked result* @throws IOException exp*/private static boolean containedTargetInterface(String[] interfaceList, Set<String> interfaceSet) throws IOException {for (String inter : interfaceList) {if (interfaceSet.contains(inter)) {return true;} else {ClassReader reader = new ClassReader(inter);if (containedTargetInterface(reader.getInterfaces(), interfaceSet)) {return true;}}}return false;}}測(cè)試用例
部分測(cè)試用例如下:
private static Set<String> set;static {if (set == null) {set = new HashSet<>();}set.add("com/cvte/myou/apm/agent/utils/SpecifiedInterfaceImplCheckedTest$A");}@Testpublic void hasImplSpecifiedInterfacesTest() throws IOException {Assert.assertEquals(true, SpecifiedInterfaceImplChecked.hasImplSpecifiedInterfaces(new ClassReader(B.class.getName()), set));Assert.assertEquals(true, SpecifiedInterfaceImplChecked.hasImplSpecifiedInterfaces(new ClassReader(C.class.getName()), set));}@Testpublic void hasImplSpecifiedInterfacesObjectTest() throws IOException {Assert.assertEquals(false, SpecifiedInterfaceImplChecked.hasImplSpecifiedInterfaces(new ClassReader(Object.class.getName()), set));}@Testpublic void hasImplSpecifiedInterfacesInterfaceTest() throws IOException {Assert.assertEquals(true, SpecifiedInterfaceImplChecked.hasImplSpecifiedInterfaces(new ClassReader(D.class.getName()), set));}interface A {void hello();}class B implements A {@Overridepublic void hello() {}}abstract class C implements A {}interface D extends A {}總結(jié)
ASM 中提供了 ClassReader ,對(duì)已存在的類進(jìn)行解析,可以讓我們?cè)跊]有源碼的情況下,對(duì)類進(jìn)行操作。
轉(zhuǎn)載于:https://my.oschina.net/pding/blog/2962373
總結(jié)
以上是生活随笔為你收集整理的ASM 判定一个类,实现了指定接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: angularjs 结构的两种写法(2
- 下一篇: 隐藏频道_《TED频道》-隐藏真实自我