<commit_before>package annotations.util;

import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;

import plume.UtilMDE;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
 * Class to generate class formatted names from Trees.
 *
 * @author mcarthur
 */
public class JVMNames {

    /**
     * Converts a MethodTree into a jvml format method signature.
     * There is probably an API to do this, but I couldn't find it.
     *
     * @param methodTree the tree to convert
     * @return a String signature of methodTree in jvml format
     */
    public static String getJVMMethodName(MethodTree methodTree) {
        ExecutableElement methodElement = ((JCMethodDecl) methodTree).sym;
        if (methodElement != null) {
            return getJVMMethodName(methodElement);
        }
        StringBuilder builder = new StringBuilder();
        String returnTypeStr;
        builder.append(methodTree.getName());
        builder.append("(");

        // use source AST in lieu of symbol table
        List<JCVariableDecl> params = ((JCMethodDecl) methodTree).params;
        JCVariableDecl param = params.head;
        JCExpression typeTree = ((JCMethodDecl) methodTree).restype;
        returnTypeStr = treeToJVMLString(typeTree);
        while (param != null) {
            builder.append(treeToJVMLString(param.vartype));
            params = params.tail;
            param = params.head;
        }
        builder.append(")");
        builder.append(returnTypeStr);
        return builder.toString();
    }

    /**
     * Converts a method element into a jvml format method signature.
     * There is probably an API to do this, but I couldn't find it.
     *
     * @param methodElement the method element to convert
     * @return a String signature of methodElement in jvml format
     */
    public static String getJVMMethodName(ExecutableElement methodElement) {
        StringBuilder builder = new StringBuilder();
        String returnTypeStr;
        builder.append(methodElement.getSimpleName());
        builder.append("(");
        TypeMirror returnType = methodElement.getReturnType();
        returnTypeStr = typeToJvmlString((Type)returnType);
        for (VariableElement ve : methodElement.getParameters()) {
            Type vt = (Type) ve.asType();
            if (vt.getTag() == TypeTag.TYPEVAR) {
                vt = vt.getUpperBound();
            }
            builder.append(typeToJvmlString(vt));
        }
        builder.append(")");<commit_msg>Uses symbol table rather than AST when the former is available.<commit_after>package annotations.util;

import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;

import plume.UtilMDE;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
 * Class to generate class formatted names from Trees.
 *
 * @author mcarthur
 */
public class JVMNames {

    /**
     * Converts a MethodTree into a jvml format method signature.
     * There is probably an API to do this, but I couldn't find it.
     *
     * @param methodTree the tree to convert
     * @return a String signature of methodTree in jvml format
     */
    public static String getJVMMethodName(MethodTree methodTree) {
        ExecutableElement methodElement = ((JCMethodDecl) methodTree).sym;
        StringBuilder builder = new StringBuilder();
        String returnTypeStr;
        builder.append(methodTree.getName());
        builder.append("(");

        if (methodElement == null) {
            // use source AST in lieu of symbol table
            List<JCVariableDecl> params = ((JCMethodDecl) methodTree).params;
            JCVariableDecl param = params.head;
            JCExpression typeTree = ((JCMethodDecl) methodTree).restype;
            returnTypeStr = treeToJVMLString(typeTree);
            while (param != null) {
                builder.append(treeToJVMLString(param.vartype));
                params = params.tail;
                param = params.head;
            }
        } else {
            TypeMirror returnType = methodElement.getReturnType();
            returnTypeStr = typeToJvmlString((Type)returnType);
            for (VariableElement ve : methodElement.getParameters()) {
                Type vt = (Type) ve.asType();
                if (vt.getTag() == TypeTag.TYPEVAR) {
                    vt = vt.getUpperBound();
                }
                builder.append(typeToJvmlString(vt));
            }
        }
        builder.append(")");
        builder.append(returnTypeStr);
        return builder.toString();
    }

    /**
     * Converts a method element into a jvml format method signature.
     * There is probably an API to do this, but I couldn't find it.
     *
     * @param methodElement the method element to convert
     * @return a String signature of methodElement in jvml format
     */
    public static String getJVMMethodName(ExecutableElement methodElement) {
        StringBuilder builder = new StringBuilder();
        String returnTypeStr;
        builder.append(methodElement.getSimpleName());
        builder.append("(");
        TypeMirror returnType = methodElement.getReturnType();
        returnTypeStr = typeToJvmlString((Type)returnType);
        for (VariableElement ve : methodElement.getParameters()) {
            Type vt = (Type) ve.asType();
            if (vt.getTag() == TypeTag.TYPEVAR) {
                vt = vt.getUpperBound();
            }
            builder.append(typeToJvmlString(vt));
        }
        builder.append(")");
{'fraction_non_alphanumeric': 0.10095856524427953, 'fraction_numerical': 0.0, 'mean_word_length': 2.669313669880885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3860667', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1798, 'n_words': 445}