// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   ClassBuilder.java

package lt.monarch.jasm;

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

// Referenced classes of package lt.monarch.jasm:
//			MethodBuilder, CodeArray, ConstantPool, FieldBuilder

public class ClassBuilder extends ClassLoader
{

	public ConstantPool constantPool;
	int flags;
	String className;
	String superName;
	Vector interfaces;
	Vector methods;
	Vector fields;

	public String getName()
	{
		return className;
	}

	public void setSuperclassName(String s)
	{
		superName = s;
	}

	public void setFlags(int i)
	{
		flags = i;
	}

	public int getFlags()
	{
		return flags;
	}

	public void addInterface(String s)
	{
		interfaces.addElement(s);
	}

	public void addMethod(MethodBuilder methodbuilder)
	{
		methods.addElement(methodbuilder);
	}

	public boolean hasMethod(String s, String s1)
	{
		for(int i = 0; i < methods.size(); i++)
		{
			MethodBuilder methodbuilder = (MethodBuilder)methods.elementAt(i);
			if(methodbuilder.name.equals(s) && methodbuilder.descriptor.equals(s1))
				return true;
		}

		return false;
	}

	public void addField(FieldBuilder fieldbuilder)
	{
		fields.addElement(fieldbuilder);
	}

	public void addDefaultConstructor()
	{
		MethodBuilder methodbuilder = new MethodBuilder(constantPool);
		methodbuilder.setName("<init>", "()V");
		methodbuilder.setFlags(1);
		methodbuilder.code.reserveArguments(1);
		methodbuilder.code.reserveStack(1);
		methodbuilder.code.append(42);
		methodbuilder.code.append(183);
		methodbuilder.code.appendInt(methodbuilder.constantPool.defineMethod(superName, "<init>", "()V"));
		methodbuilder.code.append(177);
		addMethod(methodbuilder);
	}

	private void writeClassImage(DataOutputStream dataoutputstream)
		throws IOException
	{
		dataoutputstream.writeShort(flags | 0x20);
		dataoutputstream.writeShort(constantPool.defineClass(className));
		dataoutputstream.writeShort(constantPool.defineClass(superName));
		dataoutputstream.writeShort(interfaces.size());
		for(int i = 0; i < interfaces.size(); i++)
		{
			String s = (String)interfaces.elementAt(i);
			dataoutputstream.writeShort(constantPool.defineClass(s));
		}

		dataoutputstream.writeShort(fields.size());
		for(int j = 0; j < fields.size(); j++)
		{
			FieldBuilder fieldbuilder = (FieldBuilder)fields.elementAt(j);
			fieldbuilder.writeFieldImage(dataoutputstream);
		}

		dataoutputstream.writeShort(methods.size());
		for(int k = 0; k < methods.size(); k++)
		{
			MethodBuilder methodbuilder = (MethodBuilder)methods.elementAt(k);
			methodbuilder.writeMethodImage(dataoutputstream);
		}

		dataoutputstream.writeShort(0);
	}

	public void writeImage(OutputStream outputstream)
		throws IOException
	{
		ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
		DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
		writeClassImage(dataoutputstream);
		dataoutputstream.close();
		dataoutputstream = new DataOutputStream(outputstream);
		dataoutputstream.writeInt(0xcafebabe);
		dataoutputstream.writeShort(3);
		dataoutputstream.writeShort(45);
		constantPool.writeTo(dataoutputstream);
		bytearrayoutputstream.writeTo(dataoutputstream);
		dataoutputstream.close();
	}

	public Class buildClass()
	{
		try
		{
			ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
			writeImage(bytearrayoutputstream);
			byte abyte0[] = bytearrayoutputstream.toByteArray();
			Class class1 = defineClass(className, abyte0, 0, abyte0.length);
			resolveClass(class1);
			return class1;
		}
		catch(Throwable throwable)
		{
			throwable.printStackTrace();
		}
		return null;
	}

	public Class loadClass(String s, boolean flag)
		throws ClassNotFoundException
	{
		return findSystemClass(s);
	}

	public ClassBuilder(String s)
	{
		constantPool = new ConstantPool();
		className = s;
		superName = "java.lang.Object";
		flags = 17;
		interfaces = new Vector();
		methods = new Vector();
		fields = new Vector();
		constantPool.setThisClassName(className);
	}
}
