package org.skywalking.apm.agent.core.plugin.match;

import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

/**
 * Match the class by the given super class or interfaces.
 *
 * @author wusheng
 */
public class HierarchyMatch implements IndirectMatch {
    private String[] parentTypes;

    private HierarchyMatch(String[] parentTypes) {
        if (parentTypes == null || parentTypes.length == 0) {
            throw new IllegalArgumentException("parentTypes is null");
        }
        this.parentTypes = parentTypes;
    }

    @Override
    public ElementMatcher.Junction buildJunction() {
        ElementMatcher.Junction junction = null;
        for (String superTypeName : parentTypes) {
            if (junction == null) {
                junction = buildEachAnnotation(superTypeName);
            } else {
                junction = junction.and(buildEachAnnotation(superTypeName));
            }
        }
        junction = junction.and(not(isInterface()));
        return junction;
    }

    private ElementMatcher.Junction buildEachAnnotation(String superTypeName) {
        return hasSuperType(named(superTypeName));
    }

    @Override
    public boolean isMatch(TypeDescription typeDescription) {
        return false;
    }

    public static ClassMatch byHierarchyMatch(String[] parentTypes) {
        return new HierarchyMatch(parentTypes);
    }
}

@@ -57,54 +57,178 @@
 ort 
-net.bytebuddy.description.type.TypeDescription
+java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0Aimport net.bytebuddy.description.type.TypeDescription;%0Aimport net.bytebuddy.description.type.TypeList
 ;%0Aim
@@ -1170,38 +1170,41 @@
 tion = build
-EachAnnotation
+SuperClassMatcher
 (superTypeNa
@@ -1274,30 +1274,33 @@
 nd(build
-EachAnnotation
+SuperClassMatcher
 (superTy
@@ -1463,22 +1463,25 @@
 uild
-EachAnnotation
+SuperClassMatcher
 (Str
@@ -1647,21 +1647,1064 @@
     
-return false;
+List%3CString%3E parentTypes = new ArrayList%3CString%3E(Arrays.asList(this.parentTypes));%0A%0A        TypeList.Generic implInterfaces = typeDescription.getInterfaces();%0A        for (TypeDescription.Generic implInterface : implInterfaces) %7B%0A            matchHierarchyClass(implInterface, parentTypes);%0A        %7D%0A%0A        matchHierarchyClass(typeDescription.getSuperClass(), parentTypes);%0A%0A        if (parentTypes.size() == 0) %7B%0A            return true;%0A        %7D%0A%0A        return false;%0A    %7D%0A%0A    private void matchHierarchyClass(TypeDescription.Generic clazz, List%3CString%3E parentTypes) %7B%0A        parentTypes.remove(clazz.getTypeName());%0A        if (parentTypes.size() == 0) %7B%0A            return;%0A        %7D%0A%0A        for (TypeDescription.Generic generic : clazz.getInterfaces()) %7B%0A            matchHierarchyClass(generic, parentTypes);%0A        %7D%0A%0A        TypeDescription.Generic superClazz = clazz.getSuperClass();%0A        if (superClazz != null && !clazz.getTypeName().equals(%22java.lang.Object%22)) %7B%0A            matchHierarchyClass(superClazz, parentTypes);%0A        %7D%0A
 %0A   

fix issue that the match of HierarchyMatch do not work