<commit_before>package org.ibex.classgen;

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

/** Class generation object representing the whole classfile */
public class ClassGen implements CGConst {
    private final Type.Object thisType;
    private final Type.Object superType;
    final int flags;
    
    private String sourceFile; 
    private final Vector interfaces = new Vector();
    private final Vector fields = new Vector();
    private final Vector methods = new Vector();
    
    final CPGen cp;
    private final AttrGen attributes;
    
    /** @see #ClassGen(Type.Object,Type.Object,int) */
    public ClassGen(String name, String superName, int flags) {
        this(new Type.Object(name),new Type.Object(superName),flags);
    }
    
    /** Creates a new ClassGen object 
        @param thisType The type of the class to generate
        @param superType The superclas of the generated class (commonly Type.OBJECT) 
        @param flags The access flags for this class (ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, and ACC_ABSTRACT)
    */
    public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
        if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
            throw new IllegalArgumentException("invalid flags");
        this.thisType = thisType;
        this.superType = superType;
        this.flags = flags;
        
        cp = new CPGen();
        attributes = new AttrGen(cp);
    }
    
    /** Adds a new method to this class 
        @param name The name of the method (not the signature, just the name)
        @param ret The return type of the method
        @param args The arguments to the method
        @param flags The flags for the method
                     (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT)
        @return A new MethodGen object for the method
        @exception IllegalArgumentException if illegal flags are specified
        @see MethodGen
        @see CGConst
    */
    public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
        MethodGen mg = new MethodGen(this,name,ret,args,flags);
        methods.addElement(mg);
        return mg;
    }
    
    /** Adds a new field to this class 
        @param name The name of the filed (not the signature, just the name)
        @param type The type of the field
        @param flags The flags for the field
        (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT)
        @return A new FieldGen object for the method
        @exception IllegalArgumentException if illegal flags are specified
        @see FieldGen
        @see CGConst
        */  
    public final FieldGen addField(String name, Type type, int flags) {
        FieldGen fg = new FieldGen(this,name,type,flags);
        fields.addElement(fg);
        return fg;
    }
    
    /** Sets the source value of the SourceFile attribute of this class 
        @param sourceFile The string to be uses as the SourceFile of this class
    */
    public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; }
    
    /** Writes the classfile data to the file specifed
        @see ClassGen#dump(OutputStream)
    */
    public void dump(String file) throws IOException { dump(new File(file)); }
    
    /** Writes the classfile data to the file specified
        If <i>f</i> is a directory directory components under it are created for the package the class is in (like javac's -d option)
        @see ClassGen#dump(OutputStream)
    */
    public void dump(File f) throws IOException {
        if(f.isDirectory()) {
            String[] a = thisType.components();
            int i;
            for(i=0;i<a.length-1;i++) {
                f = new File(f,a[i]);
                f.mkdir();
            }
            f = new File(f,a[i] + ".class");
        }
        dump(new FileOutputStream(f));
    }
   
    /** Writes the classfile data to the outputstream specified
        @param os The stream to write the class to
        @exception IOException if an IOException occures while writing the class data
        @exception IllegalStateException if the data for a method is in an inconsistent state (required arguments missing, etc)
        @exception Exn if the classfile could not be written for any other reason (constant pool full, etc)
    */
    public void dump(OutputStream os) throws IOException {
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
        _dump(dos);
        dos.flush();
    }
    
    private void _dump(DataOutput o) throws IOException {
        cp.add(thisType);
        cp.add(superType);
        for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
        if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
        
        cp.stable();
        
        for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
        for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish();
        
        cp.seal();
        
        o.writeInt(0xcafebabe); // magic
        o.writeShort(3); // minor_version
        o.writeShort(45); // major_version
        
        o.writeShort(cp.size()); // constant_pool_count
        cp.dump(o); // constant_pool
        
        o.writeShort(flags);
        o.writeShort(cp.getIndex(thisType)); // this_class
        o.writeShort(cp.getIndex(superType)); // super_class
        
        o.writeShort(interfaces.size()); // interfaces_count
        for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
        
        o.writeShort(fields.size()); // fields_count
        for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields

        o.writeShort(methods.size()); // methods_count
        for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
        
        o.writeShort(attributes.size()); // attributes_count
        attributes.dump(o); // attributes        
    }
    
    /** Thrown when class generation fails for a reason not under the control of the user
        (IllegalStateExceptions are thrown in those cases */
    public static class Exn extends RuntimeException {
        public Exn(String s) { super(s); }
    }
    
    /** A class representing a field or method reference. This is used as an argument to the INVOKE*, GET*, and PUT* bytecodes
        @see MethodRef
        @see FieldRef
        @see MethodRef.I
        @see FieldRef
    */
    public static abstract class FieldOrMethodRef {
        Type.Object klass;
        String name;<commit_msg>interface support<commit_after>package org.ibex.classgen;

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

/** Class generation object representing the whole classfile */
public class ClassGen implements CGConst {
    private final Type.Object thisType;
    private final Type.Object superType;
    private final Type.Object[] interfaces;
    final int flags;
    
    private String sourceFile; 
    private final Vector fields = new Vector();
    private final Vector methods = new Vector();
    
    final CPGen cp;
    private final AttrGen attributes;
    
    /** @see #ClassGen(Type.Object,Type.Object,int) */
    public ClassGen(String name, String superName, int flags) {
        this(new Type.Object(name),new Type.Object(superName),flags);
    }

    /** @see #ClassGen(Type.Object,Type.Object,int,Type.Object[]) */
    public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
        this(thisType,superType,flags,null);
    }
    
    /** Creates a new ClassGen object 
        @param thisType The type of the class to generate
        @param superType The superclass of the generated class (commonly Type.OBJECT) 
        @param flags The access flags for this class (ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, and ACC_ABSTRACT)
    */
    public ClassGen(Type.Object thisType,Type.Object superType, int flags, Type.Object[] interfaces) {
        if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
            throw new IllegalArgumentException("invalid flags");
        this.thisType = thisType;
        this.superType = superType;
        this.interfaces = interfaces;
        this.flags = flags;
        
        cp = new CPGen();
        attributes = new AttrGen(cp);
    }
    
    /** Adds a new method to this class 
        @param name The name of the method (not the signature, just the name)
        @param ret The return type of the method
        @param args The arguments to the method
        @param flags The flags for the method
                     (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT)
        @return A new MethodGen object for the method
        @exception IllegalArgumentException if illegal flags are specified
        @see MethodGen
        @see CGConst
    */
    public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
        MethodGen mg = new MethodGen(this,name,ret,args,flags);
        methods.addElement(mg);
        return mg;
    }
    
    /** Adds a new field to this class 
        @param name The name of the filed (not the signature, just the name)
        @param type The type of the field
        @param flags The flags for the field
        (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT)
        @return A new FieldGen object for the method
        @exception IllegalArgumentException if illegal flags are specified
        @see FieldGen
        @see CGConst
        */  
    public final FieldGen addField(String name, Type type, int flags) {
        FieldGen fg = new FieldGen(this,name,type,flags);
        fields.addElement(fg);
        return fg;
    }
    
    /** Sets the source value of the SourceFile attribute of this class 
        @param sourceFile The string to be uses as the SourceFile of this class
    */
    public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; }
    
    /** Writes the classfile data to the file specifed
        @see ClassGen#dump(OutputStream)
    */
    public void dump(String file) throws IOException { dump(new File(file)); }
    
    /** Writes the classfile data to the file specified
        If <i>f</i> is a directory directory components under it are created for the package the class is in (like javac's -d option)
        @see ClassGen#dump(OutputStream)
    */
    public void dump(File f) throws IOException {
        if(f.isDirectory()) {
            String[] a = thisType.components();
            int i;
            for(i=0;i<a.length-1;i++) {
                f = new File(f,a[i]);
                f.mkdir();
            }
            f = new File(f,a[i] + ".class");
        }
        dump(new FileOutputStream(f));
    }
   
    /** Writes the classfile data to the outputstream specified
        @param os The stream to write the class to
        @exception IOException if an IOException occures while writing the class data
        @exception IllegalStateException if the data for a method is in an inconsistent state (required arguments missing, etc)
        @exception Exn if the classfile could not be written for any other reason (constant pool full, etc)
    */
    public void dump(OutputStream os) throws IOException {
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
        _dump(dos);
        dos.flush();
    }
    
    private void _dump(DataOutput o) throws IOException {
        cp.add(thisType);
        cp.add(superType);
        if(interfaces != null) for(int i=0;i<interfaces.length;i++) cp.add(interfaces[i]);
        if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
        
        cp.stable();
        
        for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
        for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish();
        
        cp.seal();
        
        o.writeInt(0xcafebabe); // magic
        o.writeShort(3); // minor_version
        o.writeShort(45); // major_version
        
        o.writeShort(cp.size()); // constant_pool_count
        cp.dump(o); // constant_pool
        
        o.writeShort(flags);
        o.writeShort(cp.getIndex(thisType)); // this_class
        o.writeShort(cp.getIndex(superType)); // super_class
        
        o.writeShort(interfaces==null ? 0 : interfaces.length); // interfaces_count
        if(interfaces != null) for(int i=0;i<interfaces.length;i++) o.writeShort(cp.getIndex(interfaces[i])); // interfaces
        
        o.writeShort(fields.size()); // fields_count
        for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields

        o.writeShort(methods.size()); // methods_count
        for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
        
        o.writeShort(attributes.size()); // attributes_count
        attributes.dump(o); // attributes        
    }
    
    /** Thrown when class generation fails for a reason not under the control of the user
        (IllegalStateExceptions are thrown in those cases */
    public static class Exn extends RuntimeException {
        public Exn(String s) { super(s); }
    }
    
    /** A class representing a field or method reference. This is used as an argument to the INVOKE*, GET*, and PUT* bytecodes
        @see MethodRef
        @see FieldRef
        @see MethodRef.I
        @see FieldRef
    */
    public static abstract class FieldOrMethodRef {
        Type.Object klass;
        String name;
{'fraction_non_alphanumeric': 0.0991991919774908, 'fraction_numerical': 0.002092201139888897, 'mean_word_length': 2.537126818065833, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 7, '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': '3728936', 'n_tokens_mistral': 3934, 'n_tokens_neox': 3829, 'n_words': 1221}