src/share/classes/com/sun/tools/javah/LLNI.java

changeset 1
9a66ca7c79fa
child 184
905e151a185a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javah/LLNI.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,604 @@
     1.4 +/*
     1.5 + * Copyright 2002-2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +package com.sun.tools.javah;
    1.31 +
    1.32 +import java.io.File;
    1.33 +import java.io.OutputStream;
    1.34 +import java.io.PrintWriter;
    1.35 +import java.util.Hashtable;
    1.36 +import com.sun.javadoc.*;
    1.37 +
    1.38 + /*
    1.39 +  * @author  Sucheta Dambalkar(Revised)
    1.40 +  */
    1.41 +public class LLNI extends Gen {
    1.42 +
    1.43 +    protected final char  pathChar = File.separatorChar;
    1.44 +    protected final char  innerDelim = '$';     /* For inner classes */
    1.45 +    protected Hashtable   doneHandleTypes;
    1.46 +    MemberDoc []fields;
    1.47 +    MemberDoc [] methods;
    1.48 +    private boolean       doubleAlign;
    1.49 +    private int           padFieldNum = 0;
    1.50 +
    1.51 +
    1.52 +    LLNI(boolean doubleAlign, RootDoc root) {
    1.53 +        super(root);
    1.54 +        this.doubleAlign = doubleAlign;
    1.55 +    }
    1.56 +
    1.57 +
    1.58 +    protected String getIncludes() {
    1.59 +        return "";
    1.60 +    }
    1.61 +
    1.62 +    protected void write(OutputStream o, ClassDoc clazz)
    1.63 +        throws ClassNotFoundException {
    1.64 +        String cname     = mangleClassName(clazz.qualifiedName());
    1.65 +        PrintWriter pw   = wrapWriter(o);
    1.66 +        fields = clazz.fields();
    1.67 +        methods = clazz.methods();
    1.68 +        generateDeclsForClass(pw, clazz, cname);
    1.69 +    }
    1.70 +
    1.71 +    protected void generateDeclsForClass(PrintWriter pw,
    1.72 +                                         ClassDoc clazz, String cname)
    1.73 +        throws ClassNotFoundException {
    1.74 +        doneHandleTypes  = new Hashtable();
    1.75 +        /* The following handle types are predefined in "typedefs.h". Suppress
    1.76 +           inclusion in the output by generating them "into the blue" here. */
    1.77 +        genHandleType(null, "java.lang.Class");
    1.78 +        genHandleType(null, "java.lang.ClassLoader");
    1.79 +        genHandleType(null, "java.lang.Object");
    1.80 +        genHandleType(null, "java.lang.String");
    1.81 +        genHandleType(null, "java.lang.Thread");
    1.82 +        genHandleType(null, "java.lang.ThreadGroup");
    1.83 +        genHandleType(null, "java.lang.Throwable");
    1.84 +
    1.85 +        pw.println("/* LLNI Header for class " + clazz.qualifiedName() + " */" + lineSep);
    1.86 +        pw.println("#ifndef _Included_" + cname);
    1.87 +        pw.println("#define _Included_" + cname);
    1.88 +        pw.println("#include \"typedefs.h\"");
    1.89 +        pw.println("#include \"llni.h\"");
    1.90 +        pw.println("#include \"jni.h\"" + lineSep);
    1.91 +
    1.92 +        forwardDecls(pw, clazz);
    1.93 +        structSectionForClass(pw, clazz, cname);
    1.94 +        methodSectionForClass(pw, clazz, cname);
    1.95 +        pw.println("#endif");
    1.96 +    }
    1.97 +
    1.98 +    protected void genHandleType(PrintWriter pw, String clazzname) {
    1.99 +        String cname = mangleClassName(clazzname);
   1.100 +        if (!doneHandleTypes.containsKey(cname)) {
   1.101 +            doneHandleTypes.put(cname, cname);
   1.102 +            if (pw != null) {
   1.103 +                pw.println("#ifndef DEFINED_" + cname);
   1.104 +                pw.println("    #define DEFINED_" + cname);
   1.105 +                pw.println("    GEN_HANDLE_TYPES(" + cname + ");");
   1.106 +                pw.println("#endif" + lineSep);
   1.107 +            }
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    protected String mangleClassName(String s) {
   1.112 +        return s.replace('.', '_')
   1.113 +            .replace(pathChar, '_')
   1.114 +            .replace(innerDelim, '_');
   1.115 +    }
   1.116 +
   1.117 +    protected void forwardDecls(PrintWriter pw, ClassDoc clazz)
   1.118 +        throws ClassNotFoundException {
   1.119 +        ClassDoc clazzfield = null;
   1.120 +
   1.121 +        if (clazz.qualifiedName().equals("java.lang.Object"))
   1.122 +            return;
   1.123 +        genHandleType(pw, clazz.qualifiedName());
   1.124 +        ClassDoc superClass = clazz.superclass();
   1.125 +
   1.126 +        if(superClass != null){
   1.127 +            String superClassName = superClass.qualifiedName();
   1.128 +            forwardDecls(pw, superClass);
   1.129 +        }
   1.130 +
   1.131 +        for (int i = 0; i < fields.length; i++) {
   1.132 +            FieldDoc field = (FieldDoc)fields[i];
   1.133 +
   1.134 +            if (!field.isStatic()) {
   1.135 +                Type t = field.type();
   1.136 +                String tname = t.qualifiedTypeName();
   1.137 +                TypeSignature newTypeSig = new TypeSignature(root);
   1.138 +                String sig = newTypeSig.getTypeSignature(tname);
   1.139 +
   1.140 +                if (sig.charAt(0) != '[')
   1.141 +                    forwardDeclsFromSig(pw, sig);
   1.142 +            }
   1.143 +        }
   1.144 +
   1.145 +        for (int i = 0; i < methods.length; i++) {
   1.146 +            MethodDoc method = (MethodDoc)methods[i];
   1.147 +
   1.148 +            if (method.isNative()) {
   1.149 +                Type retType = method.returnType();
   1.150 +                String typesig = method.signature();
   1.151 +                TypeSignature newTypeSig = new TypeSignature(root);
   1.152 +                String sig = newTypeSig.getTypeSignature(typesig, retType);
   1.153 +
   1.154 +                if (sig.charAt(0) != '[')
   1.155 +                    forwardDeclsFromSig(pw, sig);
   1.156 +
   1.157 +            }
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    protected void forwardDeclsFromSig(PrintWriter pw, String sig) {
   1.162 +        int    len = sig.length();
   1.163 +        int    i   = sig.charAt(0) == '(' ? 1 : 0;
   1.164 +
   1.165 +        /* Skip the initial "(". */
   1.166 +        while (i < len) {
   1.167 +            if (sig.charAt(i) == 'L') {
   1.168 +                int j = i + 1;
   1.169 +                while (sig.charAt(j) != ';') j++;
   1.170 +                genHandleType(pw, sig.substring(i + 1, j));
   1.171 +                i = j + 1;
   1.172 +            } else {
   1.173 +                i++;
   1.174 +            }
   1.175 +        }
   1.176 +    }
   1.177 +
   1.178 +    protected void structSectionForClass(PrintWriter pw,
   1.179 +                                         ClassDoc jclazz, String cname)
   1.180 +        throws ClassNotFoundException {
   1.181 +
   1.182 +        String jname = jclazz.qualifiedName();
   1.183 +
   1.184 +        if (cname.equals("java_lang_Object")) {
   1.185 +            pw.println("/* struct java_lang_Object is defined in typedefs.h. */");
   1.186 +            pw.println();
   1.187 +            return;
   1.188 +        }
   1.189 +        pw.println("#if !defined(__i386)");
   1.190 +        pw.println("#pragma pack(4)");
   1.191 +        pw.println("#endif");
   1.192 +        pw.println();
   1.193 +        pw.println("struct " + cname + " {");
   1.194 +        pw.println("    ObjHeader h;");
   1.195 +        pw.print(fieldDefs(jclazz, cname));
   1.196 +
   1.197 +        if (jname.equals("java.lang.Class"))
   1.198 +            pw.println("    Class *LLNI_mask(cClass);" +
   1.199 +                       "  /* Fake field; don't access (see oobj.h) */");
   1.200 +        pw.println("};" + lineSep + lineSep + "#pragma pack()");
   1.201 +        pw.println();
   1.202 +        return;
   1.203 +    }
   1.204 +
   1.205 +    private static class FieldDefsRes {
   1.206 +        public String className;        /* Name of the current class. */
   1.207 +        public FieldDefsRes parent;
   1.208 +        public String s;
   1.209 +        public int byteSize;
   1.210 +        public boolean bottomMost;
   1.211 +        public boolean printedOne = false;
   1.212 +
   1.213 +        FieldDefsRes(ClassDoc clazz, FieldDefsRes parent, boolean bottomMost) {
   1.214 +            this.className = clazz.qualifiedName();
   1.215 +            this.parent = parent;
   1.216 +            this.bottomMost = bottomMost;
   1.217 +            int byteSize = 0;
   1.218 +            if (parent == null) this.s = "";
   1.219 +            else this.s = parent.s;
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    /* Returns "true" iff added a field. */
   1.224 +    private boolean doField(FieldDefsRes res, FieldDoc field,
   1.225 +                            String cname, boolean padWord)
   1.226 +        throws ClassNotFoundException {
   1.227 +
   1.228 +        String fieldDef = addStructMember(field, cname, padWord);
   1.229 +        if (fieldDef != null) {
   1.230 +            if (!res.printedOne) { /* add separator */
   1.231 +                if (res.bottomMost) {
   1.232 +                    if (res.s.length() != 0)
   1.233 +                        res.s = res.s + "    /* local members: */" + lineSep;
   1.234 +                } else {
   1.235 +                    res.s = res.s + "    /* inherited members from " +
   1.236 +                        res.className + ": */" + lineSep;
   1.237 +                }
   1.238 +                res.printedOne = true;
   1.239 +            }
   1.240 +            res.s = res.s + fieldDef;
   1.241 +            return true;
   1.242 +        }
   1.243 +
   1.244 +        // Otherwise.
   1.245 +        return false;
   1.246 +    }
   1.247 +
   1.248 +    private int doTwoWordFields(FieldDefsRes res, ClassDoc clazz,
   1.249 +                                int offset, String cname, boolean padWord)
   1.250 +        throws ClassNotFoundException {
   1.251 +        boolean first = true;
   1.252 +        FieldDoc[] fields = clazz.fields();
   1.253 +
   1.254 +        for (int i = 0; i <fields.length; i++) {
   1.255 +            FieldDoc field = fields[i];
   1.256 +            String tc =field.type().typeName();
   1.257 +            boolean twoWords = (tc.equals("long") || tc.equals("double"));
   1.258 +            if (twoWords && doField(res, field, cname, first && padWord)) {
   1.259 +                offset += 8; first = false;
   1.260 +            }
   1.261 +        }
   1.262 +        return offset;
   1.263 +    }
   1.264 +
   1.265 +    protected String fieldDefs(ClassDoc clazz, String cname)
   1.266 +        throws ClassNotFoundException {
   1.267 +        FieldDefsRes res = fieldDefs(clazz, cname, true);
   1.268 +        return res.s;
   1.269 +    }
   1.270 +
   1.271 +    protected FieldDefsRes fieldDefs(ClassDoc clazz, String cname,
   1.272 +                                     boolean bottomMost)
   1.273 +        throws ClassNotFoundException {
   1.274 +        FieldDefsRes res;
   1.275 +        int offset;
   1.276 +        boolean didTwoWordFields = false;
   1.277 +        ClassDoc superclazz = clazz.superclass();
   1.278 +
   1.279 +        if (superclazz != null) {
   1.280 +            String supername = superclazz.qualifiedName();
   1.281 +            res = new FieldDefsRes(clazz,
   1.282 +                                   fieldDefs(superclazz, cname, false),
   1.283 +                                   bottomMost);
   1.284 +            offset = res.parent.byteSize;
   1.285 +        } else {
   1.286 +            res = new FieldDefsRes(clazz, null, bottomMost);
   1.287 +            offset = 0;
   1.288 +        }
   1.289 +
   1.290 +        FieldDoc[] fields = clazz.fields();
   1.291 +
   1.292 +        for (int i = 0; i < fields.length; i++) {
   1.293 +            FieldDoc field = fields[i];
   1.294 +
   1.295 +            if (doubleAlign && !didTwoWordFields && (offset % 8) == 0) {
   1.296 +                offset = doTwoWordFields(res, clazz, offset, cname, false);
   1.297 +                didTwoWordFields = true;
   1.298 +            }
   1.299 +
   1.300 +            String tc = field.type().typeName();
   1.301 +            boolean twoWords = (tc.equals("long") ||tc.equals("double"));
   1.302 +
   1.303 +            if (!doubleAlign || !twoWords) {
   1.304 +                if (doField(res, field, cname, false)) offset += 4;
   1.305 +            }
   1.306 +
   1.307 +        }
   1.308 +
   1.309 +        if (doubleAlign && !didTwoWordFields) {
   1.310 +            if ((offset % 8) != 0) offset += 4;
   1.311 +            offset = doTwoWordFields(res, clazz, offset, cname, true);
   1.312 +        }
   1.313 +
   1.314 +        res.byteSize = offset;
   1.315 +        return res;
   1.316 +    }
   1.317 +
   1.318 +    /* OVERRIDE: This method handles instance fields */
   1.319 +    protected String addStructMember(FieldDoc member, String cname,
   1.320 +                                     boolean padWord)
   1.321 +        throws ClassNotFoundException {
   1.322 +        String res = null;
   1.323 +
   1.324 +        if (member.isStatic()) {
   1.325 +            res = addStaticStructMember(member, cname);
   1.326 +            //   if (res == null) /* JNI didn't handle it, print comment. */
   1.327 +            //  res = "    /* Inaccessible static: " + member + " */" + lineSep;
   1.328 +        } else {
   1.329 +            if (padWord) res = "    java_int padWord" + padFieldNum++ + ";" + lineSep;
   1.330 +            res = "    " + llniType(member.type(), false, false) + " " + llniFieldName(member);
   1.331 +            if (isLongOrDouble(member.type())) res = res + "[2]";
   1.332 +            res = res + ";" + lineSep;
   1.333 +        }
   1.334 +        return res;
   1.335 +    }
   1.336 +
   1.337 +    static private final boolean isWindows =
   1.338 +        System.getProperty("os.name").startsWith("Windows");
   1.339 +
   1.340 +    /*
   1.341 +     * This method only handles static final fields.
   1.342 +     */
   1.343 +    protected String addStaticStructMember(FieldDoc field, String cname)
   1.344 +        throws ClassNotFoundException {
   1.345 +        String res = null;
   1.346 +        Object exp = null;
   1.347 +
   1.348 +        if (!field.isStatic())
   1.349 +            return res;
   1.350 +        if (!field.isFinal())
   1.351 +            return res;
   1.352 +
   1.353 +        exp = field.constantValue();
   1.354 +
   1.355 +        if (exp != null) {
   1.356 +            /* Constant. */
   1.357 +
   1.358 +            String     cn     = cname + "_" + field.name();
   1.359 +            String     suffix = null;
   1.360 +            long           val = 0;
   1.361 +            /* Can only handle int, long, float, and double fields. */
   1.362 +            if (exp instanceof Integer) {
   1.363 +                suffix = "L";
   1.364 +                val = ((Integer)exp).intValue();
   1.365 +            }
   1.366 +            if (exp instanceof Long) {
   1.367 +                // Visual C++ supports the i64 suffix, not LL
   1.368 +                suffix = isWindows ? "i64" : "LL";
   1.369 +                val = ((Long)exp).longValue();
   1.370 +            }
   1.371 +            if (exp instanceof Float)  suffix = "f";
   1.372 +            if (exp instanceof Double) suffix = "";
   1.373 +            if (suffix != null) {
   1.374 +                // Some compilers will generate a spurious warning
   1.375 +                // for the integer constants for Integer.MIN_VALUE
   1.376 +                // and Long.MIN_VALUE so we handle them specially.
   1.377 +                if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
   1.378 +                    (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
   1.379 +                    res = "    #undef  " + cn + lineSep
   1.380 +                        + "    #define " + cn
   1.381 +                        + " (" + (val + 1) + suffix + "-1)" + lineSep;
   1.382 +                } else {
   1.383 +                    res = "    #undef  " + cn + lineSep
   1.384 +                        + "    #define " + cn + " "+ exp.toString() + suffix + lineSep;
   1.385 +                }
   1.386 +            }
   1.387 +        }
   1.388 +        return res;
   1.389 +    }
   1.390 +
   1.391 +    protected void methodSectionForClass(PrintWriter pw,
   1.392 +                                         ClassDoc clazz, String cname)
   1.393 +        throws ClassNotFoundException {
   1.394 +        String methods = methodDecls(clazz, cname);
   1.395 +
   1.396 +        if (methods.length() != 0) {
   1.397 +            pw.println("/* Native method declarations: */" + lineSep);
   1.398 +            pw.println("#ifdef __cplusplus");
   1.399 +            pw.println("extern \"C\" {");
   1.400 +            pw.println("#endif" + lineSep);
   1.401 +            pw.println(methods);
   1.402 +            pw.println("#ifdef __cplusplus");
   1.403 +            pw.println("}");
   1.404 +            pw.println("#endif");
   1.405 +        }
   1.406 +    }
   1.407 +
   1.408 +    protected String methodDecls(ClassDoc clazz, String cname)
   1.409 +        throws ClassNotFoundException {
   1.410 +
   1.411 +        String res = "";
   1.412 +        for (int i = 0; i < methods.length; i++) {
   1.413 +            MethodDoc method = (MethodDoc)methods[i];
   1.414 +            if (method.isNative())
   1.415 +                res = res + methodDecl(method, clazz, cname);
   1.416 +        }
   1.417 +        return res;
   1.418 +    }
   1.419 +
   1.420 +    protected String methodDecl(MethodDoc method,
   1.421 +                                ClassDoc clazz, String cname)
   1.422 +        throws ClassNotFoundException {
   1.423 +        String res = null;
   1.424 +
   1.425 +        Type retType = method.returnType();
   1.426 +        String typesig = method.signature();
   1.427 +        TypeSignature newTypeSig = new TypeSignature(root);
   1.428 +        String sig = newTypeSig.getTypeSignature(typesig, retType);
   1.429 +        boolean longName = needLongName(method, clazz);
   1.430 +
   1.431 +        if (sig.charAt(0) != '(')
   1.432 +            Util.error("invalid.method.signature", sig);
   1.433 +
   1.434 +
   1.435 +        res = "JNIEXPORT " + jniType(retType) + " JNICALL" + lineSep + jniMethodName(method, cname, longName)
   1.436 +            + "(JNIEnv *, " + cRcvrDecl(method, cname);
   1.437 +        Parameter[] params = method.parameters();
   1.438 +        Type argTypes[] = new Type[params.length];
   1.439 +        for(int p = 0; p <  params.length; p++){
   1.440 +            argTypes[p] =  params[p].type();
   1.441 +        }
   1.442 +
   1.443 +        /* It would have been nice to include the argument names in the
   1.444 +           declaration, but there seems to be a bug in the "BinaryField"
   1.445 +           class, causing the getArguments() method to return "null" for
   1.446 +           most (non-constructor) methods. */
   1.447 +        for (int i = 0; i < argTypes.length; i++)
   1.448 +            res = res + ", " + jniType(argTypes[i]);
   1.449 +        res = res + ");" + lineSep;
   1.450 +        return res;
   1.451 +    }
   1.452 +
   1.453 +    protected final boolean needLongName(MethodDoc method,
   1.454 +                                         ClassDoc clazz)
   1.455 +        throws ClassNotFoundException {
   1.456 +        String methodName = method.name();
   1.457 +        for (int i = 0; i < methods.length; i++) {
   1.458 +            MethodDoc memberMethod = (MethodDoc) methods[i];
   1.459 +            if ((memberMethod != method) &&
   1.460 +                memberMethod.isNative() && (methodName == memberMethod.name()))
   1.461 +                return true;
   1.462 +        }
   1.463 +        return false;
   1.464 +    }
   1.465 +
   1.466 +    protected final String jniMethodName(MethodDoc method, String cname,
   1.467 +                                         boolean longName) {
   1.468 +        String res = "Java_" + cname + "_" + method.name();
   1.469 +
   1.470 +        if (longName) {
   1.471 +            Type mType =  method.returnType();
   1.472 +            Parameter[] params = method.parameters();
   1.473 +            Type argTypes[] = new Type[params.length];
   1.474 +            for(int p = 0; p <  params.length; p++){
   1.475 +                argTypes[p] =  params[p].type();
   1.476 +            }
   1.477 +
   1.478 +            res = res + "__";
   1.479 +            for (int i = 0; i < argTypes.length; i++){
   1.480 +                Type t = argTypes[i];
   1.481 +                String tname = t.typeName();
   1.482 +                TypeSignature newTypeSig = new TypeSignature(root);
   1.483 +                String sig = newTypeSig.getTypeSignature(tname);
   1.484 +                res = res + nameToIdentifier(sig);
   1.485 +            }
   1.486 +        }
   1.487 +        return res;
   1.488 +    }
   1.489 +
   1.490 +    protected final String jniType(Type t) {
   1.491 +        String elmT =t.typeName();
   1.492 +        if (t.dimension().indexOf("[]") != -1) {
   1.493 +            if(elmT.equals("boolean"))return "jbooleanArray";
   1.494 +            else if(elmT.equals("byte"))return "jbyteArray";
   1.495 +            else if(elmT.equals("char"))return "jcharArray";
   1.496 +            else if(elmT.equals("short"))return "jshortArray";
   1.497 +            else if(elmT.equals("int"))return "jintArray";
   1.498 +            else if(elmT.equals("long"))return "jlongArray";
   1.499 +            else if(elmT.equals("float"))return "jfloatArray";
   1.500 +            else if(elmT.equals("double"))return "jdoubleArray";
   1.501 +            else if((t.dimension().indexOf("[][]") != -1) || (t.asClassDoc() != null))  return "jobjectArray";
   1.502 +        } else {
   1.503 +            if(elmT.equals("void"))return "void";
   1.504 +            else if(elmT.equals("boolean"))return "jboolean";
   1.505 +            else if(elmT.equals("byte"))return "jbyte";
   1.506 +            else if(elmT.equals("char"))return "jchar";
   1.507 +            else if(elmT.equals("short"))return "jshort";
   1.508 +            else if(elmT.equals("int"))return "jint";
   1.509 +            else if(elmT.equals("long"))return "jlong";
   1.510 +            else if(elmT.equals("float"))return "jfloat";
   1.511 +            else if(elmT.equals("double"))return "jdouble";
   1.512 +            else if (t.asClassDoc() != null) {
   1.513 +                if (elmT.equals("String"))
   1.514 +                    return "jstring";
   1.515 +                else if (t.asClassDoc().subclassOf(root.classNamed("java.lang.Class")))
   1.516 +                    return "jclass";
   1.517 +                else
   1.518 +                    return "jobject";
   1.519 +            }
   1.520 +        }
   1.521 +        Util.bug("jni.unknown.type");
   1.522 +        return null; /* dead code. */
   1.523 +    }
   1.524 +
   1.525 +    protected String llniType(Type t, boolean handleize, boolean longDoubleOK) {
   1.526 +        String res = null;
   1.527 +        String elmt = t.typeName();
   1.528 +        if (t.dimension().indexOf("[]") != -1) {
   1.529 +            if((t.dimension().indexOf("[][]") != -1)
   1.530 +               || (t.asClassDoc() != null)) res = "IArrayOfRef";
   1.531 +            else if(elmt.equals("boolean")) res =  "IArrayOfBoolean";
   1.532 +            else if(elmt.equals("byte")) res =  "IArrayOfByte";
   1.533 +            else if(elmt.equals("char")) res =  "IArrayOfChar";
   1.534 +            else if(elmt.equals("int")) res =  "IArrayOfInt";
   1.535 +            else if(elmt.equals("long")) res =  "IArrayOfLong";
   1.536 +            else if(elmt.equals("float")) res =  "IArrayOfFloat";
   1.537 +            else if(elmt.equals("double")) res =  "IArrayOfDouble";
   1.538 +            if (!handleize) res = "DEREFERENCED_" + res;
   1.539 +        } else {
   1.540 +            if(elmt.equals("void")) res =  "void";
   1.541 +            else if( (elmt.equals("boolean")) || (elmt.equals("byte"))
   1.542 +                     ||(elmt.equals("char")) || (elmt.equals("short"))
   1.543 +                     || (elmt.equals("int")))   res = "java_int";
   1.544 +            else   if(elmt.equals("long")) res = longDoubleOK
   1.545 +                                               ? "java_long" : "val32 /* java_long */";
   1.546 +            else   if(elmt.equals("float")) res =  "java_float";
   1.547 +            else   if(elmt.equals("double")) res =  res = longDoubleOK
   1.548 +                                                 ? "java_double" : "val32 /* java_double */";
   1.549 +            else if(t.asClassDoc() != null) {
   1.550 +                res = "I" +  mangleClassName(t.asClassDoc().qualifiedName());
   1.551 +                if (!handleize) res = "DEREFERENCED_" + res;
   1.552 +            }
   1.553 +        }
   1.554 +        return res;
   1.555 +    }
   1.556 +
   1.557 +    protected final String cRcvrDecl(MemberDoc field, String cname) {
   1.558 +        return (field.isStatic() ? "jclass" : "jobject");
   1.559 +    }
   1.560 +
   1.561 +    protected String maskName(String s) {
   1.562 +        return "LLNI_mask(" + s + ")";
   1.563 +    }
   1.564 +
   1.565 +    protected String llniFieldName(MemberDoc field) {
   1.566 +        return maskName(field.name());
   1.567 +    }
   1.568 +
   1.569 +    protected final boolean isLongOrDouble(Type t) {
   1.570 +        String tc = t.typeName();
   1.571 +        return (tc.equals("long") || tc.equals("double"));
   1.572 +    }
   1.573 +
   1.574 +    /* Do unicode to ansi C identifier conversion.
   1.575 +       %%% This may not be right, but should be called more often. */
   1.576 +    protected final String nameToIdentifier(String name) {
   1.577 +        int len = name.length();
   1.578 +        StringBuffer buf = new StringBuffer(len);
   1.579 +        for (int i = 0; i < len; i++) {
   1.580 +            char c = name.charAt(i);
   1.581 +            if (isASCIILetterOrDigit(c))
   1.582 +                buf.append(c);
   1.583 +            else if (c == '/')
   1.584 +                buf.append('_');
   1.585 +            else if (c == '.')
   1.586 +                buf.append('_');
   1.587 +            else if (c == '_')
   1.588 +                buf.append("_1");
   1.589 +            else if (c == ';')
   1.590 +                buf.append("_2");
   1.591 +            else if (c == '[')
   1.592 +                buf.append("_3");
   1.593 +            else
   1.594 +                buf.append("_0" + ((int)c));
   1.595 +        }
   1.596 +        return new String(buf);
   1.597 +    }
   1.598 +
   1.599 +    protected final boolean isASCIILetterOrDigit(char c) {
   1.600 +        if (((c >= 'A') && (c <= 'Z')) ||
   1.601 +            ((c >= 'a') && (c <= 'z')) ||
   1.602 +            ((c >= '0') && (c <= '9')))
   1.603 +            return true;
   1.604 +        else
   1.605 +            return false;
   1.606 +    }
   1.607 +}

mercurial