Milán Major

netscape-revival

1 branch
Code

sun-java/classsrc/netscape/tools/jmc/ModuleGenerator.java

// Copyright (c) 1995 Netscape Communications Corporation. All rights reserved. 

package netscape.tools.jmc;

import netscape.tools.jmc.*;
import netscape.tools.jric.*;
import netscape.tools.ClassFileParser.*;
import java.io.*;
import java.util.Vector;

public 
class ModuleGenerator extends JMCGenerator {

    public ModuleGenerator(netscape.tools.jmc.Main global, ClassDef clazz) {
	super(global, clazz);
    }

    protected String getFileName() {
	return "M" + clazz.getPrefix() + ".c";
    }

    protected String getHeaderString() {
	return clazz.name + " module file\n * Generated by jmc";
    }

    protected void generateOutput() throws IOException {
	int i;
	    
	Vector interfaces = getInterfaces(clazz);
        String prefix = clazz.getPrefix();

	out.println("#include \"M" + prefix + ".h\"\n");

	out.println("/* The interface ID of the " + clazzCName + " interface. */");
	out.print("JRIInterfaceID " + clazzCName + "ID = { ");
	out.println(getInterfaceID(interfaces) + "};\n");

	// Name and Sig Descriptor
	out.println("const char* " + clazzCName + "Descriptor[] = {");
	out.println("\t\"GetInterface(I)Ljava/lang/Object;\",");
	out.println("\t\"AddRef()V\",");
	out.println("\t\"Release()V\",");
	out.print("\t\"Description()\"");
	for (i = interfaces.size() - 1; i >= 0; i--) {
	    ClassDef intf = (ClassDef)interfaces.elementAt(i);
	    int j;
	    for (j = 0; j < intf.methods.length; j++) {
		MethodDef method = intf.methods[j];
		out.print(",\n\t\"" + method.name + method.signature + "\"");
	    }
	}
	out.println("\n};\n");
    }

    //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\
    
    protected String getInterfaceID(Vector interfaces) {
	byte[] accumulator = new byte[16];
	int i, j;
	for (i = interfaces.size() - 1; i >= 0; i--) {
	    ClassDef intf = (ClassDef)interfaces.elementAt(i);
	    for (j = 0; j < intf.methods.length; j++) {
		MethodDef method = intf.methods[j];
		String name = method.name;
		String sig = method.signature;
		int k, n = 0;
		for (k = 0; k < name.length(); k++, n = (n+1) % 16) {
		    char c = name.charAt(k);
/*
		    System.out.println("k="+k+" n="+n+" c="+c
				       +" i=" +((int)c)
				       +" d=" +((int)c & 0xff)
				       +" e="+(accumulator[n] ^( (int)c & 0xff)));
*/
		    accumulator[n] ^= (int)c & 0xff;
		}
		for (k = 0; k < sig.length(); k++, n = (n+1) % 16) {
		    char c = sig.charAt(k);
/*
		    System.out.println("k="+k+" n="+n+" c="+c
				       +" i=" +((int)c)
				       +" d=" +((int)c & 0xff)
				       +" e="+(accumulator[n] ^( (int)c & 0xff)));
*/
		    accumulator[n] ^= (int)c & 0xff;
		}
	    }
	}
	StringBuffer result = new StringBuffer();
//	for (i = 0; i < 16; i++) {
//	    System.out.println("acc "+i+" "+accumulator[i]);
//	}
	for (i = 0; i < 4; i++) {
	    result.append("0x");
	    for (j = 0; j < 4; j++) {
		int value = (int)accumulator[i*4+j];
		String hex = Integer.toString(value, 16);
//		System.out.println("value = "+ value + " hex = "+hex);
		if (hex.length() == 1)
		    result.append("0");
		result.append(hex);
	    }
	    result.append(", ");
	}
	return result.toString();
    }

}