langtools/src/share/classes/com/sun/tools/javah/Gen.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 14263 473b1eaede64
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 5847
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. 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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3996
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3996
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3996
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3996
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3996
diff changeset
    23
 * questions.
10
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;
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    30
import java.io.FileNotFoundException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    32
import java.io.InputStream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.OutputStream;
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    34
import java.io.OutputStreamWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.PrintWriter;
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    36
import java.util.ArrayList;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    37
import java.util.Arrays;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    38
import java.util.List;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    39
import java.util.Set;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.Stack;
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    41
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    42
import javax.annotation.processing.ProcessingEnvironment;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    44
import javax.lang.model.element.ExecutableElement;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    45
import javax.lang.model.element.Modifier;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    46
import javax.lang.model.element.TypeElement;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    47
import javax.lang.model.element.VariableElement;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    48
import javax.lang.model.util.ElementFilter;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    49
import javax.lang.model.util.Elements;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    50
import javax.lang.model.util.Types;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    51
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    52
import javax.tools.FileObject;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    53
import javax.tools.JavaFileManager;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    54
import javax.tools.JavaFileObject;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    55
import javax.tools.StandardLocation;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * An abstraction for generating support files required by native methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * Subclasses are for specific native interfaces. At the time of its
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * original writing, this interface is rich enough to support JNI and the
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * old 1.0-style native method interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    63
 * <p><b>This is NOT part of any supported API.
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    64
 * If you write code that depends on this, you do so at your own
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    65
 * risk.  This code and its internal interfaces are subject to change
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    66
 * or deletion without notice.</b></p>
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    67
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * @author  Sucheta Dambalkar(Revised)
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
public abstract class Gen {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    protected String lineSep = System.getProperty("line.separator");
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    73
    protected ProcessingEnvironment processingEnvironment;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    74
    protected Types types;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    75
    protected Elements elems;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    76
    protected Mangle mangler;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    77
    protected Util util;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    78
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    79
    protected Gen(Util util) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    80
        this.util = util;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    81
    }
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    82
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * List of classes for which we must generate output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    86
    protected Set<TypeElement> classes;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    static private final boolean isWindows =
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        System.getProperty("os.name").startsWith("Windows");
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * Override this abstract method, generating content for the named
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * class into the outputstream.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
    95
    protected abstract void write(OutputStream o, TypeElement clazz) throws Util.Exit;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Override this method to provide a list of #include statements
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * required by the native interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    protected abstract String getIncludes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * Output location.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   106
    protected JavaFileManager fileManager;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   107
    protected JavaFileObject outFile;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   109
    public void setFileManager(JavaFileManager fm) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   110
        fileManager = fm;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   113
    public void setOutFile(JavaFileObject outFile) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        this.outFile = outFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   118
    public void setClasses(Set<TypeElement> classes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        this.classes = classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   122
    void setProcessingEnvironment(ProcessingEnvironment pEnv) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   123
        processingEnvironment = pEnv;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   124
        elems = pEnv.getElementUtils();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   125
        types = pEnv.getTypeUtils();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   126
        mangler = new Mangle(elems, types);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   127
    }
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   128
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     * Smartness with generated files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    protected boolean force = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public void setForce(boolean state) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        force = state;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * We explicitly need to write ASCII files because that is what C
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * compilers understand.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   142
    protected PrintWriter wrapWriter(OutputStream o) throws Util.Exit {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        try {
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   144
            return new PrintWriter(new OutputStreamWriter(o, "ISO8859_1"), true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        } catch (UnsupportedEncodingException use) {
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   146
            util.bug("encoding.iso8859_1.not.found");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            return null; /* dead code */
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * After initializing state of an instance, use this method to start
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * Buffer size chosen as an approximation from a single sampling of:
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     *         expr `du -sk` / `ls *.h | wc -l`
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   158
    public void run() throws IOException, ClassNotFoundException, Util.Exit {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        if (outFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            /* Everything goes to one big file... */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            writeFileTop(bout); /* only once */
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   165
            for (TypeElement t: classes) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   166
                write(bout, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            writeIfChanged(bout.toByteArray(), outFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            /* Each class goes to its own file... */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   172
            for (TypeElement t: classes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                writeFileTop(bout);
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   175
                write(bout, t);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   176
                writeIfChanged(bout.toByteArray(), getFileObject(t.getQualifiedName()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * Write the contents of byte[] b to a file named file.  Writing
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     * is done if either the file doesn't exist or if the contents are
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     * different.
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   186
    private void writeIfChanged(byte[] b, FileObject file) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        boolean mustWrite = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        String event = "[No need to update file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        if (force) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            event = "[Forcefully writing file ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        } else {
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   194
            InputStream in;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   195
            byte[] a;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   196
            try {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   197
                // regrettably, there's no API to get the length in bytes
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   198
                // for a FileObject, so we can't short-circuit reading the
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   199
                // file here
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   200
                in = file.openInputStream();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   201
                a = readBytes(in);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   202
                if (!Arrays.equals(a, b)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    mustWrite = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    event = "[Overwriting file ";
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   205
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                }
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   207
            } catch (FileNotFoundException e) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   208
                mustWrite = true;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   209
                event = "[Creating file ";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        }
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   212
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   213
        if (util.verbose)
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   214
            util.log(event + file + "]");
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   215
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        if (mustWrite) {
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   217
            OutputStream out = file.openOutputStream();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            out.write(b); /* No buffering, just one big write! */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   223
    protected byte[] readBytes(InputStream in) throws IOException {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   224
        try {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   225
            byte[] array = new byte[in.available() + 1];
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   226
            int offset = 0;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   227
            int n;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   228
            while ((n = in.read(array, offset, array.length - offset)) != -1) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   229
                offset += n;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   230
                if (offset == array.length)
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   231
                    array = Arrays.copyOf(array, array.length * 2);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   232
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   234
            return Arrays.copyOf(array, offset);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   235
        } finally {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   236
            in.close();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   237
        }
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   238
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   240
    protected String defineForStatic(TypeElement c, VariableElement f)
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   241
            throws Util.Exit {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   242
        CharSequence cnamedoc = c.getQualifiedName();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   243
        CharSequence fnamedoc = f.getSimpleName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   245
        String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   246
        String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   247
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   248
        if (!f.getModifiers().contains(Modifier.STATIC))
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   249
            util.bug("tried.to.define.non.static");
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   250
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   251
        if (f.getModifiers().contains(Modifier.FINAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            Object value = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   254
            value = f.getConstantValue();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            if (value != null) { /* so it is a ConstantExpression */
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                String constString = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                if ((value instanceof Integer)
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                    || (value instanceof Byte)
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   260
                    || (value instanceof Short)) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   261
                    /* covers byte, short, int */
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   262
                    constString = value.toString() + "L";
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   263
                } else if (value instanceof Boolean) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   264
                    constString = ((Boolean) value) ? "1L" : "0L";
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   265
                } else if (value instanceof Character) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   266
                    Character ch = (Character) value;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   267
                    constString = String.valueOf(((int) ch) & 0xffff) + "L";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                } else if (value instanceof Long) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    // Visual C++ supports the i64 suffix, not LL.
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                    if (isWindows)
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                        constString = value.toString() + "i64";
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                        constString = value.toString() + "LL";
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                } else if (value instanceof Float) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    /* bug for bug */
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                    float fv = ((Float)value).floatValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                    if (Float.isInfinite(fv))
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                        constString = ((fv < 0) ? "-" : "") + "Inff";
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                        constString = value.toString() + "f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                } else if (value instanceof Double) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                    /* bug for bug */
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                    double d = ((Double)value).doubleValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    if (Double.isInfinite(d))
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                        constString = ((d < 0) ? "-" : "") + "InfD";
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                        constString = value.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                if (constString != null) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 5847
diff changeset
   290
                    StringBuilder s = new StringBuilder("#undef ");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                    s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                    s.append("#define "); s.append(cname); s.append("_");
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    s.append(fname); s.append(" "); s.append(constString);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                    return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
     * Deal with the C pre-processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    protected String cppGuardBegin() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        return "#ifdef __cplusplus" + lineSep + "extern \"C\" {" + lineSep + "#endif";
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    protected String cppGuardEnd() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        return "#ifdef __cplusplus" + lineSep + "}" + lineSep + "#endif";
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 guardBegin(String cname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return "/* Header for class " + cname + " */" + lineSep + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            "#ifndef _Included_" + cname + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            "#define _Included_" + cname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    protected String guardEnd(String cname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        return "#endif";
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * File name and file preamble related operations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     */
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   326
    protected void writeFileTop(OutputStream o) throws Util.Exit {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        PrintWriter pw = wrapWriter(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        pw.println("/* DO NOT EDIT THIS FILE - it is machine generated */" + lineSep +
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                   getIncludes());
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   332
    protected String baseFileName(CharSequence className) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   333
        return mangler.mangle(className, Mangle.Type.CLASS);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   336
    protected FileObject getFileObject(CharSequence className) throws IOException {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   337
        String name = baseFileName(className) + getFileSuffix();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   338
        return fileManager.getFileForOutput(StandardLocation.SOURCE_OUTPUT, "", name, null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    protected String getFileSuffix() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        return ".h";
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     * Including super classes' fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   349
    List<VariableElement> getAllFields(TypeElement subclazz) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14263
diff changeset
   350
        List<VariableElement> fields = new ArrayList<>();
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   351
        TypeElement cd = null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14263
diff changeset
   352
        Stack<TypeElement> s = new Stack<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        cd = subclazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            s.push(cd);
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   357
            TypeElement c = (TypeElement) (types.asElement(cd.getSuperclass()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            if (c == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            cd = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        while (!s.empty()) {
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   364
            cd = s.pop();
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   365
            fields.addAll(ElementFilter.fieldsIn(cd.getEnclosedElements()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   368
        return fields;
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   369
    }
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   370
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   371
    // c.f. MethodDoc.signature
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   372
    String signature(ExecutableElement e) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 5847
diff changeset
   373
        StringBuilder sb = new StringBuilder("(");
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   374
        String sep = "";
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   375
        for (VariableElement p: e.getParameters()) {
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   376
            sb.append(sep);
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   377
            sb.append(types.erasure(p.asType()).toString());
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   378
            sep = ",";
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   379
        }
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   380
        sb.append(")");
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   381
        return sb.toString();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
}
3996
dc676a9093b3 6572945: javah should be written as an annotation processor, not a doclet
jjg
parents: 2212
diff changeset
   384