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