<commit_before>package kalang.util;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import kalang.ast.AssignExpr;
import kalang.ast.BlockStmt;
import kalang.ast.CastExpr;
import kalang.ast.ClassNode;
import kalang.ast.ExprNode;
import kalang.ast.ExprStmt;
import kalang.ast.MethodNode;
import kalang.ast.ObjectInvokeExpr;
import kalang.ast.ParameterExpr;
import kalang.ast.ParameterNode;
import kalang.ast.ReturnStmt;
import kalang.ast.ThisExpr;
import kalang.core.ClassType;
import kalang.core.ExecutableDescriptor;
import kalang.core.MethodDescriptor;
import kalang.core.ObjectType;
import kalang.core.Type;
import kalang.core.Types;

/**
 *
 * @author <NAME>
 */
public class InterfaceUtil {

    public static List<MethodDescriptor> checkAndBuildInterfaceMethods(ClassNode clazz) {
        ObjectType[] interfaces = clazz.getInterfaces();
        List<MethodDescriptor> unimplements = new LinkedList();
        for (ObjectType itf : interfaces) {
            unimplements.addAll(checkAndBuildInterfaceMethods(clazz, itf));
        }
        return unimplements;
    }

    public static List<MethodDescriptor> checkAndBuildInterfaceMethods(ClassNode clazz, ObjectType interfaceType) {
        MethodDescriptor[] interfaceMethods = interfaceType.getMethodDescriptors(interfaceType.getClassNode(), true, true);
        ClassType clazzType = Types.getClassType(clazz, new Type[0]);
        MethodDescriptor[] clazzMethods = clazzType.getMethodDescriptors(null, true, false);
        List<MethodDescriptor> unimplements = new LinkedList();
        for (MethodDescriptor im : interfaceMethods) {
            String imDeclKey = im.getDeclarationKey();
            ExecutableDescriptor cm = MethodUtil.getExecutableDescriptor(clazzMethods, imDeclKey);
            if (cm == null) {
                if (Modifier.isAbstract(im.getModifier())) {
                    unimplements.add(im);
                }
                continue;
            }
            Type[] descriptorParamsTypes = im.getParameterTypes();
            Type[] interfaceMethodParamsTypes = MethodUtil.getParameterTypes(im.getMethodNode());
            if (!Arrays.equals(descriptorParamsTypes, interfaceMethodParamsTypes)) {
                createBridgeMethod(clazz, interfaceMethodParamsTypes, cm);
            }
        }
        return unimplements;
    }

    private static void createBridgeMethod(ClassNode clazz, Type[] paramTypes, ExecutableDescriptor targetMethod) {
        Type returnType = targetMethod.getReturnType();
        Type[] oldParamTypes = targetMethod.getParameterTypes();
        MethodNode m = clazz.createMethodNode(returnType, targetMethod.getName(), targetMethod.getModifier());
        ParameterNode[] paramNodes = new ParameterNode[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            paramNodes[i] = m.createParameter(paramTypes[i], "p" + i);
        }
        BlockStmt mb = m.getBody();
        ExprNode[] params = new ExprNode[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            if (!paramTypes[i].equals(oldParamTypes[i])) {
                CastExpr castExpr = new CastExpr(oldParamTypes[i], new ParameterExpr(paramNodes[i]));
                AssignExpr assignExpr = new AssignExpr(new ParameterExpr(paramNodes[i]), castExpr);
                mb.statements.add(new ExprStmt(assignExpr));
            }
            params[i] = new ParameterExpr(paramNodes[i]);
        }
        ObjectInvokeExpr invokeExpr = new ObjectInvokeExpr(new ThisExpr(clazz), targetMethod, params);
        if (!Types.VOID_TYPE.equals(returnType)) {
            mb.statements.add(new ReturnStmt(invokeExpr));
        } else {
            mb.statements.add(new ExprStmt(invokeExpr));
            mb.statements.add(new ReturnStmt());
        }
    }

}<commit_msg>fix: fix interface's methods<commit_after>package kalang.util;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import kalang.ast.AssignExpr;
import kalang.ast.BlockStmt;
import kalang.ast.CastExpr;
import kalang.ast.ClassNode;
import kalang.ast.ExprNode;
import kalang.ast.ExprStmt;
import kalang.ast.MethodNode;
import kalang.ast.ObjectInvokeExpr;
import kalang.ast.ParameterExpr;
import kalang.ast.ParameterNode;
import kalang.ast.ReturnStmt;
import kalang.ast.ThisExpr;
import kalang.core.ClassType;
import kalang.core.ExecutableDescriptor;
import kalang.core.MethodDescriptor;
import kalang.core.ObjectType;
import kalang.core.Type;
import kalang.core.Types;

/**
 *
 * @author <NAME>
 */
public class InterfaceUtil {

    public static List<MethodDescriptor> checkAndBuildInterfaceMethods(ClassNode clazz) {
        ObjectType[] interfaces = clazz.getInterfaces();
        List<MethodDescriptor> unimplements = new LinkedList();
        for (ObjectType itf : interfaces) {
            unimplements.addAll(checkAndBuildInterfaceMethods(clazz, itf));
        }
        return unimplements;
    }

    public static List<MethodDescriptor> checkAndBuildInterfaceMethods(ClassNode clazz, ObjectType interfaceType) {
        MethodDescriptor[] interfaceMethods = interfaceType.getMethodDescriptors(interfaceType.getClassNode(), true, true);
        ClassType clazzType = Types.getClassType(clazz, new Type[0]);
        MethodDescriptor[] clazzMethods = clazzType.getMethodDescriptors(null, true, false);
        List<MethodDescriptor> unimplements = new LinkedList();
        for (MethodDescriptor im : interfaceMethods) {
            String imDeclKey = im.getDeclarationKey();
            ExecutableDescriptor cm = MethodUtil.getExecutableDescriptor(clazzMethods, imDeclKey);
            if (cm == null) {
                if (Modifier.isAbstract(im.getModifier())) {
                    unimplements.add(im);
                }
                continue;
            }
            Type[] descriptorParamsTypes = im.getParameterTypes();
            MethodNode imMethodNode = im.getMethodNode();
            Type[] interfaceMethodParamsTypes = MethodUtil.getParameterTypes(imMethodNode);
            if (!Arrays.equals(descriptorParamsTypes, interfaceMethodParamsTypes)) {
                createBridgeMethod(clazz, imMethodNode.getType(),interfaceMethodParamsTypes, cm);
            }
        }
        return unimplements;
    }

    private static void createBridgeMethod(ClassNode clazz,Type returnType,Type[] paramTypes, ExecutableDescriptor targetMethod) {
        Type[] oldParamTypes = targetMethod.getParameterTypes();
        MethodNode m = clazz.createMethodNode(returnType, targetMethod.getName(), targetMethod.getModifier());
        ParameterNode[] paramNodes = new ParameterNode[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            paramNodes[i] = m.createParameter(paramTypes[i], "p" + i);
        }
        BlockStmt mb = m.getBody();
        ExprNode[] params = new ExprNode[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            if (!paramTypes[i].equals(oldParamTypes[i])) {
                CastExpr castExpr = new CastExpr(oldParamTypes[i], new ParameterExpr(paramNodes[i]));
                AssignExpr assignExpr = new AssignExpr(new ParameterExpr(paramNodes[i]), castExpr);
                mb.statements.add(new ExprStmt(assignExpr));
            }
            params[i] = new ParameterExpr(paramNodes[i]);
        }
        ObjectInvokeExpr invokeExpr = new ObjectInvokeExpr(new ThisExpr(clazz), targetMethod, params);
        if (!Types.VOID_TYPE.equals(returnType)) {
            mb.statements.add(new ReturnStmt(invokeExpr));
        } else {
            mb.statements.add(new ExprStmt(invokeExpr));
            mb.statements.add(new ReturnStmt());
        }
    }

}
{'fraction_non_alphanumeric': 0.10350584307178631, 'fraction_numerical': 0.0007705149608321562, 'mean_word_length': 3.2006472491909386, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4254381', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2144, 'n_words': 429}