001: /*
002: * @(#)CNIHeader.java 1.12 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package runtime;
029:
030: import consts.Const;
031: import jcc.Util;
032: import util.*;
033: import components.*;
034: import vm.*;
035: import java.io.PrintStream;
036: import java.util.Enumeration;
037: import java.util.Vector;
038: import java.util.Hashtable;
039:
040: public class CNIHeader extends HeaderDump {
041:
042: public CNIHeader() {
043: super ('_');
044: }
045:
046: private boolean generateNatives(MethodInfo methods[],
047: boolean cplusplusguard) {
048: if (methods == null)
049: return false;
050: boolean anyMethods = false;
051: for (int i = 0; i < methods.length; i++) {
052: MethodInfo m = methods[i];
053: if ((m.access & Const.ACC_NATIVE) == 0)
054: continue;
055: if (cplusplusguard && !anyMethods) {
056: o.println("#ifdef __cplusplus\nextern \"C\"{\n#endif");
057: anyMethods = true;
058: }
059: String methodsig = m.type.string;
060: o.println("/*");
061: o.println(" * Class: " + className);
062: o.println(" * Method: " + m.name.string);
063: o.println(" * Signature: " + methodsig);
064: o.println(" */");
065:
066: //
067: // decide whether to use long name or short.
068: // we use the long name only if we find another
069: // native method with the same short name.
070: //
071: String nameParams = null;
072: for (int j = 0; j < methods.length; j++) {
073: MethodInfo other = methods[j];
074: if ((other.access & Const.ACC_NATIVE) == 0)
075: continue;
076: if (other == m)
077: continue; // don't compare with self!
078: if (other.name.string.equals(m.name.string)) {
079: nameParams = methodsig;
080: break;
081: }
082: }
083: // print return type and name.
084: o.print("CNINativeMethod ");
085: String name = Util.convertToJNIName(className,
086: m.name.string, nameParams);
087: name = "CNI" + name.substring(5);
088: o.println(name + ";");
089: }
090: if (cplusplusguard && anyMethods) {
091: o.println("#ifdef __cplusplus\n}\n#endif");
092: return true;
093: }
094: return false;
095: }
096:
097: private void prolog() {
098: o
099: .println("/* DO NOT EDIT THIS FILE - it is machine generated */\n"
100: + "#include \"javavm/include/cni.h\"");
101: o.println("/* Header for class " + className + " */\n");
102: String includeName = "_CVM_CNI" + strsub(className, CDelim);
103: o.println("#ifndef " + includeName);
104: o.println("#define " + includeName);
105: }
106:
107: private void epilog() {
108: o.println("#endif");
109: }
110:
111: synchronized public boolean dumpHeader(ClassInfo c,
112: PrintStream outfile) {
113: boolean didWork;
114: o = outfile;
115: className = c.className;
116: prolog();
117: didWork = generateConsts(Util.convertToClassName(c.className),
118: c.fields);
119: didWork |= generateNatives(c.methods, true);
120: epilog();
121: return didWork;
122: }
123:
124: synchronized public boolean dumpExternals(ClassInfo c,
125: PrintStream outfile) {
126: boolean didWork;
127: o = outfile;
128: className = c.className;
129: didWork = generateNatives(c.methods, false);
130: System.err.println("CNIHeader " + className + " didWork "
131: + didWork);
132: return didWork;
133: }
134: }
|