<commit_before>package org.codehaus.groovy.tools.javac;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.ResolveVisitor;
import org.objectweb.asm.Opcodes;

import groovy.lang.GroovyObjectSupport;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class JavaStubGenerator {
    private File outputPath;
    private ArrayList toCompile = new ArrayList();

    private ResolveVisitor resolver;

    public JavaStubGenerator(JavaAwareCompilationUnit cu, File outputPath) {
        this.outputPath = outputPath;
    }
    
    private void mkdirs(File parent, String relativeFile) {
        int index = relativeFile.lastIndexOf('/');
        if (index==-1) return;
        File dir = new File(parent,relativeFile.substring(0,index));
        dir.mkdirs();
    }
    
    public void generateClass(ClassNode classNode) throws FileNotFoundException {
        String fileName = classNode.getName().replace('.', '/');
        mkdirs(outputPath,fileName);
        toCompile.add(fileName);

        PrintWriter out = new PrintWriter(new File(outputPath, fileName + ".java"));

        String packageName = classNode.getPackageName();
        if (packageName != null) {
            out.println("package " + packageName + ";\n");
        }

        genImports(classNode, out);

        boolean isInterface = classNode.isInterface();

        printModifiers(out, classNode.getModifiers()
                & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0));
        out.println((isInterface ? "interface " : "class ")
                + classNode.getNameWithoutPackage());

        ClassNode superClass = classNode.getSuperClass();

        if (!isInterface) {
            if (superClass.equals(ClassHelper.OBJECT_TYPE))
                superClass = ClassHelper.make(GroovyObjectSupport.class);
            out.println("  extends " + superClass.getName());
        } else {
            if (!superClass.equals(ClassHelper.OBJECT_TYPE))
                out.println("  extends " + superClass.getName());
        }

        ClassNode[] interfaces = classNode.getInterfaces();
        if (interfaces != null && interfaces.length > 0) {
            out.println("  implements");
            for (int i = 0; i < interfaces.length - 1; ++i)
                out.println("    " + interfaces[i].getName() + ",");
            out.println("    "
                    + interfaces[interfaces.length - 1].getName());
        }
        out.println("{");

        genMethods(classNode, out);
        genFields(classNode, out);
        genProps(classNode, out);

        out.println("}");

        out.close();
    }

    private void genMethods(ClassNode classNode, PrintWriter out) {
        getContructors(classNode, out);

        List methods = classNode.getMethods();
        if (methods != null)
            for (Iterator it = methods.iterator(); it.hasNext();) {
                MethodNode methodNode = (MethodNode) it.next();
                genMethod(methodNode, out);
            }
    }

    private void getContructors(ClassNode classNode, PrintWriter out) {
        List constrs = classNode.getDeclaredConstructors();<commit_msg>remove use of Java 1.5 only constructor to allow compilation on Java 1.4<commit_after>package org.codehaus.groovy.tools.javac;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.ResolveVisitor;
import org.objectweb.asm.Opcodes;

import groovy.lang.GroovyObjectSupport;

import java.io.*;
import java.util.*;

public class JavaStubGenerator {
    private File outputPath;
    private ArrayList toCompile = new ArrayList();

    private ResolveVisitor resolver;

    public JavaStubGenerator(JavaAwareCompilationUnit cu, File outputPath) {
        this.outputPath = outputPath;
    }
    
    private void mkdirs(File parent, String relativeFile) {
        int index = relativeFile.lastIndexOf('/');
        if (index==-1) return;
        File dir = new File(parent,relativeFile.substring(0,index));
        dir.mkdirs();
    }
    
    public void generateClass(ClassNode classNode) throws FileNotFoundException {
        String fileName = classNode.getName().replace('.', '/');
        mkdirs(outputPath,fileName);
        toCompile.add(fileName);

        File file = new File(outputPath, fileName + ".java");
        FileOutputStream fos = new FileOutputStream(file);
        PrintWriter out = new PrintWriter(fos);

        try {
            String packageName = classNode.getPackageName();
            if (packageName != null) {
                out.println("package " + packageName + ";\n");
            }

            genImports(classNode, out);

            boolean isInterface = classNode.isInterface();

            printModifiers(out, classNode.getModifiers()
                    & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0));
            out.println((isInterface ? "interface " : "class ")
                    + classNode.getNameWithoutPackage());

            ClassNode superClass = classNode.getSuperClass();

            if (!isInterface) {
                if (superClass.equals(ClassHelper.OBJECT_TYPE))
                    superClass = ClassHelper.make(GroovyObjectSupport.class);
                out.println("  extends " + superClass.getName());
            } else {
                if (!superClass.equals(ClassHelper.OBJECT_TYPE))
                    out.println("  extends " + superClass.getName());
            }

            ClassNode[] interfaces = classNode.getInterfaces();
            if (interfaces != null && interfaces.length > 0) {
                out.println("  implements");
                for (int i = 0; i < interfaces.length - 1; ++i)
                    out.println("    " + interfaces[i].getName() + ",");
                out.println("    "
                        + interfaces[interfaces.length - 1].getName());
            }
            out.println("{");

            genMethods(classNode, out);
            genFields(classNode, out);
            genProps(classNode, out);

            out.println("}");
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
            try {
                fos.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    private void genMethods(ClassNode classNode, PrintWriter out) {
        getContructors(classNode, out);

        List methods = classNode.getMethods();
        if (methods != null)
            for (Iterator it = methods.iterator(); it.hasNext();) {
                MethodNode methodNode = (MethodNode) it.next();
                genMethod(methodNode, out);
            }
    }

    private void getContructors(ClassNode classNode, PrintWriter out) {
        List constrs = classNode.getDeclaredConstructors();
{'fraction_non_alphanumeric': 0.11471747700394218, 'fraction_numerical': 0.0023653088042049934, 'mean_word_length': 2.357300397000441, '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': '5395308', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2023, 'n_words': 417}