langtools/src/share/classes/com/sun/tools/javah/Gen.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1264 076a3cde30d5
child 1870 57a1138dffc8
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 868
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javah;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.Stack;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * An abstraction for generating support files required by native methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * Subclasses are for specific native interfaces. At the time of its
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * original writing, this interface is rich enough to support JNI and the
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * old 1.0-style native method interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * @author  Sucheta Dambalkar(Revised)
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public abstract class Gen {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    protected String lineSep = System.getProperty("line.separator");
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    RootDoc root;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * List of classes for which we must generate output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    protected ClassDoc[] classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    static private final boolean isWindows =
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        System.getProperty("os.name").startsWith("Windows");
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public Gen(RootDoc root){
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        this.root = root;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * Override this abstract method, generating content for the named
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * class into the outputstream.
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    protected abstract void write(OutputStream o, ClassDoc clazz)
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        throws ClassNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * Override this method to provide a list of #include statements
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * required by the native interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    protected abstract String getIncludes();
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * Output location.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    protected String outDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    protected String outFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public void setOutDir(String outDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        /* Check important, otherwise concatenation of two null strings
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
         * produces the "nullnull" String.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        if (outDir != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            this.outDir = outDir + System.getProperty("file.separator");
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            File d = new File(outDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            if (!d.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                if (!d.mkdirs())
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                    Util.error("cant.create.dir", d.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public void setOutFile(String outFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        this.outFile = outFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    public void setClasses(ClassDoc[] classes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        this.classes = classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Smartness with generated files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    protected boolean force = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    public void setForce(boolean state) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        force = state;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * We explicitly need to write ASCII files because that is what C
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * compilers understand.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    protected PrintWriter wrapWriter(OutputStream o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            return new
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            PrintWriter(new OutputStreamWriter(o, "ISO8859_1"), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        } catch (UnsupportedEncodingException use) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            Util.bug("encoding.iso8859_1.not.found");
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            return null; /* dead code */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     * After initializing state of an instance, use this method to start
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     * Buffer size chosen as an approximation from a single sampling of:
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     *         expr `du -sk` / `ls *.h | wc -l`
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public void run() throws IOException, ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        if (outFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            /* Everything goes to one big file... */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            writeFileTop(bout); /* only once */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            for (i = 0; i < classes.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                write(bout, classes[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            writeIfChanged(bout.toByteArray(), outFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            /* Each class goes to its own file... */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            for (i = 0; i < classes.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                writeFileTop(bout);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                ClassDoc clazz = classes[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                write(bout, clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                writeIfChanged(bout.toByteArray(), getFileName(clazz.qualifiedName()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * Write the contents of byte[] b to a file named file.  Writing
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * is done if either the file doesn't exist or if the contents are
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * different.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    private void writeIfChanged(byte[] b, String file) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        File f = new File(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        boolean mustWrite = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        String event = "[No need to update file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        if (force) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            event = "[Forcefully writing file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            if (!f.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                event = "[Creating file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                int l = (int)f.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                if (b.length != l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                    mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    event = "[Overwriting file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                    /* Lengths are equal, so read it. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                    byte[] a = new byte[l];
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                    FileInputStream in = new FileInputStream(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    if (in.read(a) != l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                        in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                        /* This can't happen, we already checked the length. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                        Util.error("not.enough.bytes", Integer.toString(l),
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                                   f.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                    in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                    while (--l >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                        if (a[l] != b[l]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                            mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                            event = "[Overwriting file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        if (Util.verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            Util.log(event + file + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        if (mustWrite) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            OutputStream out = new FileOutputStream(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            out.write(b); /* No buffering, just one big write! */
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    protected String defineForStatic(ClassDoc c, FieldDoc f){
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        String cnamedoc = c.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        String fnamedoc = f.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        String cname = Mangle.mangle(cnamedoc, Mangle.Type.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        String fname = Mangle.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        if (!f.isStatic())
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            Util.bug("tried.to.define.non.static");
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        if (f.isFinal()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            Object value = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            value = f.constantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            if (value != null) { /* so it is a ConstantExpression */
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                String constString = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                if ((value instanceof Integer)
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                    || (value instanceof Byte)
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                    || (value instanceof Character)
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                    || (value instanceof Short)
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                    || (value instanceof Boolean)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                    /* covers byte, boolean, char, short, int */
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                    if(value instanceof Boolean)
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                        constString = (value.toString() == "true") ? "1L" : "0L";
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                        constString = value.toString() + "L";
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                } else if (value instanceof Long) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    // Visual C++ supports the i64 suffix, not LL.
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                    if (isWindows)
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                        constString = value.toString() + "i64";
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                        constString = value.toString() + "LL";
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                } else if (value instanceof Float) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                    /* bug for bug */
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                    float fv = ((Float)value).floatValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                    if (Float.isInfinite(fv))
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                        constString = ((fv < 0) ? "-" : "") + "Inff";
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                        constString = value.toString() + "f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                } else if (value instanceof Double) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                    /* bug for bug */
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                    double d = ((Double)value).doubleValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    if (Double.isInfinite(d))
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                        constString = ((d < 0) ? "-" : "") + "InfD";
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                        constString = value.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                if (constString != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                    StringBuffer s = new StringBuffer("#undef ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                    s.append("#define "); s.append(cname); s.append("_");
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    s.append(fname); s.append(" "); s.append(constString);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                    return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * Deal with the C pre-processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    protected String cppGuardBegin() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        return "#ifdef __cplusplus" + lineSep + "extern \"C\" {" + lineSep + "#endif";
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    protected String cppGuardEnd() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        return "#ifdef __cplusplus" + lineSep + "}" + lineSep + "#endif";
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    protected String guardBegin(String cname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        return "/* Header for class " + cname + " */" + lineSep + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            "#ifndef _Included_" + cname + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            "#define _Included_" + cname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    protected String guardEnd(String cname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        return "#endif";
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     * File name and file preamble related operations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    protected void writeFileTop(OutputStream o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        PrintWriter pw = wrapWriter(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        pw.println("/* DO NOT EDIT THIS FILE - it is machine generated */" + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                   getIncludes());
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    protected String baseFileName(String clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        StringBuffer f =
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            new StringBuffer(Mangle.mangle(clazz,
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                                           Mangle.Type.CLASS));
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        if (outDir != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            f.insert(0, outDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        return f.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    protected String getFileName(String clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return baseFileName(clazz) + getFileSuffix();
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    protected String getFileSuffix() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return ".h";
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * Including super classes' fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    FieldDoc[] getAllFields(ClassDoc subclazz)
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                throws ClassNotFoundException {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   327
        Vector<FieldDoc> fields = new Vector<FieldDoc>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        ClassDoc cd = null;
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   329
        Stack<Object> s = new Stack<Object>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        cd = subclazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            s.push(cd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            ClassDoc c = cd.superclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            if (c == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            cd = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        while (!s.empty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            cd = (ClassDoc)s.pop();
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            fields.addAll(Arrays.asList(cd.fields()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        return (FieldDoc[]) fields.toArray(new FieldDoc[fields.size()]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
}