<commit_before>package org.nativescript.staticbindinggenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.Type;

public class Generator {
    private static final String JAVA_EXT = ".java";

    private static final String CLASS_EXT = ".class";

    private static final String DEFAULT_PACKAGE_NAME = "com.tns.gen";

    private final String outputDir;
    private final String[] libs;
    private final Map<String, JavaClass> classes;

    public Generator(String outputDir, String[] libs) throws IOException {
        this(outputDir, libs, false);
    }

    public Generator(String outputDir, String[] libs, boolean throwOnError) throws IOException {
        this.outputDir = outputDir;
        this.libs = libs;
        this.classes = readClasses(libs, throwOnError);
    }


    public void writeBindings(String filename) throws IOException, ClassNotFoundException {
        Binding[] bindings = generateBindings(filename);
        List<File> writenFiles = new ArrayList<File>();
        for (Binding b : bindings) {
            if (!writenFiles.contains(b.getFile())) {
                writenFiles.add(b.getFile());
                try (PrintStream ps = new PrintStream(b.getFile())) {
                    ps.append(b.getContent());
                }
            } else {
                throw new IOException("File already exists. This may lead to undesired behavior.\nPlease change the name of one of the extended classes.\n" + b.getFile());
            }
        }
    }

    public Binding[] generateBindings(String filename) throws IOException, ClassNotFoundException {
        List<DataRow> rows = getRows(filename);

        Binding[] generatedFiles = processRows(rows);

        return generatedFiles;
    }

    public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) throws ClassNotFoundException {
        JavaClass clazz = getClass(dataRow.getBaseClassname());

        boolean hasSpecifiedName = !dataRow.getFilename().isEmpty();
        String packageName = hasSpecifiedName ? getBaseDir(dataRow.getFilename()) : (DEFAULT_PACKAGE_NAME + "." + clazz.getPackageName());
        String baseDirPath = packageName.replace('.', '/');

        File baseDir = new File(outputDir, baseDirPath);
        if (!baseDir.exists()) {<commit_msg>Use HashSet for duplication detection<commit_after>package org.nativescript.staticbindinggenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.Type;

public class Generator {
    private static final String JAVA_EXT = ".java";

    private static final String CLASS_EXT = ".class";

    private static final String DEFAULT_PACKAGE_NAME = "com.tns.gen";

    private final String outputDir;
    private final String[] libs;
    private final Map<String, JavaClass> classes;

    public Generator(String outputDir, String[] libs) throws IOException {
        this(outputDir, libs, false);
    }

    public Generator(String outputDir, String[] libs, boolean throwOnError) throws IOException {
        this.outputDir = outputDir;
        this.libs = libs;
        this.classes = readClasses(libs, throwOnError);
    }


    public void writeBindings(String filename) throws IOException, ClassNotFoundException {
        Binding[] bindings = generateBindings(filename);
        Set<File> writtenFiles = new HashSet<File>();
        for (Binding b : bindings) {
            if (writtenFiles.add(b.getFile())) {
                try (PrintStream ps = new PrintStream(b.getFile())) {
                    ps.append(b.getContent());
                }
            } else {
                throw new IOException("File already exists. This may lead to undesired behavior.\nPlease change the name of one of the extended classes.\n" + b.getFile());
            }
        }
    }

    public Binding[] generateBindings(String filename) throws IOException, ClassNotFoundException {
        List<DataRow> rows = getRows(filename);

        Binding[] generatedFiles = processRows(rows);

        return generatedFiles;
    }

    public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) throws ClassNotFoundException {
        JavaClass clazz = getClass(dataRow.getBaseClassname());

        boolean hasSpecifiedName = !dataRow.getFilename().isEmpty();
        String packageName = hasSpecifiedName ? getBaseDir(dataRow.getFilename()) : (DEFAULT_PACKAGE_NAME + "." + clazz.getPackageName());
        String baseDirPath = packageName.replace('.', '/');

        File baseDir = new File(outputDir, baseDirPath);
        if (!baseDir.exists()) {
{'fraction_non_alphanumeric': 0.10178912628104916, 'fraction_numerical': 0.0, 'mean_word_length': 3.4088820826952526, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6895474', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1547, 'n_words': 403}