langtools/src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java
author jfranck
Fri, 31 Aug 2012 10:37:46 +0100
changeset 13689 4d519199a6aa
parent 12213 516b112d6c68
child 14539 507556c4e622
permissions -rw-r--r--
7151010: Add compiler support for repeating annotations Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     1
/*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     4
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    10
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    15
 * accompanied this code).
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    16
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    20
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    23
 * questions.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    24
 */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    25
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.jvm;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    27
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    28
import java.io.IOException;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    29
import java.io.Writer;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    30
import java.util.ArrayList;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    31
import java.util.List;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    32
import java.util.Stack;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    33
import java.util.StringTokenizer;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    34
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    35
import javax.lang.model.element.Element;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    36
import javax.lang.model.element.ExecutableElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    37
import javax.lang.model.element.Modifier;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    38
import javax.lang.model.element.Name;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    39
import javax.lang.model.element.TypeElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    40
import javax.lang.model.element.VariableElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    41
import javax.lang.model.type.ArrayType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    42
import javax.lang.model.type.DeclaredType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    43
import javax.lang.model.type.NoType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    44
import javax.lang.model.type.PrimitiveType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    45
import javax.lang.model.type.TypeKind;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    46
import javax.lang.model.type.TypeMirror;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    47
import javax.lang.model.type.TypeVariable;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    48
import javax.lang.model.type.TypeVisitor;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    49
import javax.lang.model.util.ElementFilter;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    50
import javax.lang.model.util.Elements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    51
import javax.lang.model.util.SimpleTypeVisitor8;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    52
import javax.lang.model.util.Types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    53
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    54
import javax.tools.FileObject;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    55
import javax.tools.JavaFileManager;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    56
import javax.tools.StandardLocation;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    57
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    58
import com.sun.tools.javac.code.Attribute;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    59
import com.sun.tools.javac.code.Flags;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    60
import com.sun.tools.javac.code.Kinds;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    61
import com.sun.tools.javac.code.Scope;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    62
import com.sun.tools.javac.code.Symbol.ClassSymbol;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    63
import com.sun.tools.javac.code.Symtab;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    64
import com.sun.tools.javac.model.JavacElements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    65
import com.sun.tools.javac.model.JavacTypes;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    66
import com.sun.tools.javac.util.Assert;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    67
import com.sun.tools.javac.util.Context;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    68
import com.sun.tools.javac.util.Log;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    69
import com.sun.tools.javac.util.Options;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    70
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    71
import static com.sun.tools.javac.main.Option.*;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    72
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    73
/** This class provides operations to write native header files for classes.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    74
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    75
 *  <p><b>This is NOT part of any supported API.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    76
 *  If you write code that depends on this, you do so at your own risk.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    77
 *  This code and its internal interfaces are subject to change or
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    78
 *  deletion without notice.</b>
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    79
 */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    80
public class JNIWriter {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    81
    protected static final Context.Key<JNIWriter> jniWriterKey =
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    82
        new Context.Key<JNIWriter>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    83
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    84
    /** Access to files. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    85
    private final JavaFileManager fileManager;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    86
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    87
    JavacElements elements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    88
    JavacTypes types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    89
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    90
    /** The log to use for verbose output.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    91
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    92
    private final Log log;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    93
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    94
    /** Switch: verbose output.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    95
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    96
    private boolean verbose;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    97
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    98
    /** Switch: check all nested classes of top level class
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    99
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   100
    private boolean checkAll;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   101
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   102
    private Mangle mangler;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   103
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   104
    private Context context;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   105
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   106
    private Symtab syms;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   107
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   108
    private String lineSep;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   109
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   110
    private final boolean isWindows =
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   111
        System.getProperty("os.name").startsWith("Windows");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   112
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   113
    /** Get the ClassWriter instance for this context. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   114
    public static JNIWriter instance(Context context) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   115
        JNIWriter instance = context.get(jniWriterKey);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   116
        if (instance == null)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   117
            instance = new JNIWriter(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   118
        return instance;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   119
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   120
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   121
    /** Construct a class writer, given an options table.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   122
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   123
    private JNIWriter(Context context) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   124
        context.put(jniWriterKey, this);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   125
        fileManager = context.get(JavaFileManager.class);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   126
        log = Log.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   127
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   128
        Options options = Options.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   129
        verbose = options.isSet(VERBOSE);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   130
        checkAll = options.isSet("javah:full");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   131
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   132
        this.context = context; // for lazyInit()
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   133
        syms = Symtab.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   134
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   135
        lineSep = System.getProperty("line.separator");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   136
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   137
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   138
    private void lazyInit() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   139
        if (mangler == null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   140
            elements = JavacElements.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   141
            types = JavacTypes.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   142
            mangler = new Mangle(elements, types);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   143
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   144
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   145
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   146
    public boolean needsHeader(ClassSymbol c) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   147
        if (c.isLocal() || (c.flags() & Flags.SYNTHETIC) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   148
            return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   149
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   150
        if (checkAll)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   151
            return needsHeader(c.outermostClass(), true);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   152
        else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   153
            return needsHeader(c, false);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   154
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   155
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   156
    private boolean needsHeader(ClassSymbol c, boolean checkNestedClasses) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   157
        if (c.isLocal() || (c.flags() & Flags.SYNTHETIC) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   158
            return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   159
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 12213
diff changeset
   160
        for (Attribute.Compound a: c.annotations.getAttributes()) {
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   161
            if (a.type.tsym == syms.nativeHeaderType.tsym)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   162
                return true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   163
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   164
        for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   165
            if (i.sym.kind == Kinds.MTH && (i.sym.flags() & Flags.NATIVE) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   166
                return true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   167
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   168
        if (checkNestedClasses) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   169
            for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   170
                if ((i.sym.kind == Kinds.TYP) && needsHeader(((ClassSymbol) i.sym), true))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   171
                    return true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   172
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   173
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   174
        return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   175
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   176
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   177
    /** Emit a class file for a given class.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   178
     *  @param c      The class from which a class file is generated.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   179
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   180
    public FileObject write(ClassSymbol c)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   181
        throws IOException
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   182
    {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   183
        String className = c.flatName().toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   184
        FileObject outFile
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   185
            = fileManager.getFileForOutput(StandardLocation.NATIVE_HEADER_OUTPUT,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   186
                "", className.replaceAll("[.$]", "_") + ".h", null);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   187
        Writer out = outFile.openWriter();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   188
        try {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   189
            write(out, c);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   190
            if (verbose)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   191
                log.printVerbose("wrote.file", outFile);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   192
            out.close();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   193
            out = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   194
        } finally {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   195
            if (out != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   196
                // if we are propogating an exception, delete the file
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   197
                out.close();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   198
                outFile.delete();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   199
                outFile = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   200
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   201
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   202
        return outFile; // may be null if write failed
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   203
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   204
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   205
    public void write(Writer out, ClassSymbol sym)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   206
            throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   207
        lazyInit();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   208
        try {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   209
            String cname = mangler.mangle(sym.fullname, Mangle.Type.CLASS);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   210
            println(out, fileTop());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   211
            println(out, includes());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   212
            println(out, guardBegin(cname));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   213
            println(out, cppGuardBegin());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   214
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   215
            writeStatics(out, sym);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   216
            writeMethods(out, sym, cname);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   217
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   218
            println(out, cppGuardEnd());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   219
            println(out, guardEnd(cname));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   220
        } catch (TypeSignature.SignatureException e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   221
            throw new IOException(e);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   222
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   223
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   224
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   225
    protected void writeStatics(Writer out, ClassSymbol sym) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   226
        List<VariableElement> classfields = getAllFields(sym);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   227
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   228
        for (VariableElement v: classfields) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   229
            if (!v.getModifiers().contains(Modifier.STATIC))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   230
                continue;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   231
            String s = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   232
            s = defineForStatic(sym, v);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   233
            if (s != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   234
                println(out, s);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   235
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   236
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   237
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   238
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   239
    /**
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   240
     * Including super class fields.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   241
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   242
    List<VariableElement> getAllFields(TypeElement subclazz) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   243
        List<VariableElement> fields = new ArrayList<VariableElement>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   244
        TypeElement cd = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   245
        Stack<TypeElement> s = new Stack<TypeElement>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   246
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   247
        cd = subclazz;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   248
        while (true) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   249
            s.push(cd);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   250
            TypeElement c = (TypeElement) (types.asElement(cd.getSuperclass()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   251
            if (c == null)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   252
                break;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   253
            cd = c;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   254
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   255
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   256
        while (!s.empty()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   257
            cd = s.pop();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   258
            fields.addAll(ElementFilter.fieldsIn(cd.getEnclosedElements()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   259
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   260
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   261
        return fields;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   262
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   263
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   264
    protected String defineForStatic(TypeElement c, VariableElement f) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   265
        CharSequence cnamedoc = c.getQualifiedName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   266
        CharSequence fnamedoc = f.getSimpleName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   267
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   268
        String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   269
        String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   270
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   271
        Assert.check(f.getModifiers().contains(Modifier.STATIC));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   272
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   273
        if (f.getModifiers().contains(Modifier.FINAL)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   274
            Object value = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   275
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   276
            value = f.getConstantValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   277
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   278
            if (value != null) { /* so it is a ConstantExpression */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   279
                String constString = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   280
                if ((value instanceof Integer)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   281
                    || (value instanceof Byte)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   282
                    || (value instanceof Short)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   283
                    /* covers byte, short, int */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   284
                    constString = value.toString() + "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   285
                } else if (value instanceof Boolean) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   286
                    constString = ((Boolean) value) ? "1L" : "0L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   287
                } else if (value instanceof Character) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   288
                    Character ch = (Character) value;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   289
                    constString = String.valueOf(((int) ch) & 0xffff) + "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   290
                } else if (value instanceof Long) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   291
                    // Visual C++ supports the i64 suffix, not LL.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   292
                    if (isWindows)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   293
                        constString = value.toString() + "i64";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   294
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   295
                        constString = value.toString() + "LL";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   296
                } else if (value instanceof Float) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   297
                    /* bug for bug */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   298
                    float fv = ((Float)value).floatValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   299
                    if (Float.isInfinite(fv))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   300
                        constString = ((fv < 0) ? "-" : "") + "Inff";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   301
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   302
                        constString = value.toString() + "f";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   303
                } else if (value instanceof Double) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   304
                    /* bug for bug */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   305
                    double d = ((Double)value).doubleValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   306
                    if (Double.isInfinite(d))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   307
                        constString = ((d < 0) ? "-" : "") + "InfD";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   308
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   309
                        constString = value.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   310
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   311
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   312
                if (constString != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   313
                    StringBuilder s = new StringBuilder("#undef ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   314
                    s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   315
                    s.append("#define "); s.append(cname); s.append("_");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   316
                    s.append(fname); s.append(" "); s.append(constString);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   317
                    return s.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   318
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   319
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   320
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   321
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   322
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   323
        return null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   324
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   325
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   326
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   327
    protected void writeMethods(Writer out, ClassSymbol sym, String cname)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   328
            throws IOException, TypeSignature.SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   329
        List<ExecutableElement> classmethods = ElementFilter.methodsIn(sym.getEnclosedElements());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   330
        for (ExecutableElement md: classmethods) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   331
            if(md.getModifiers().contains(Modifier.NATIVE)){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   332
                TypeMirror mtr = types.erasure(md.getReturnType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   333
                String sig = signature(md);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   334
                TypeSignature newtypesig = new TypeSignature(elements);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   335
                CharSequence methodName = md.getSimpleName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   336
                boolean longName = false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   337
                for (ExecutableElement md2: classmethods) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   338
                    if ((md2 != md)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   339
                        && (methodName.equals(md2.getSimpleName()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   340
                        && (md2.getModifiers().contains(Modifier.NATIVE)))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   341
                        longName = true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   342
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   343
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   344
                println(out, "/*");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   345
                println(out, " * Class:     " + cname);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   346
                println(out, " * Method:    " +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   347
                           mangler.mangle(methodName, Mangle.Type.FIELDSTUB));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   348
                println(out, " * Signature: " + newtypesig.getTypeSignature(sig, mtr));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   349
                println(out, " */");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   350
                println(out, "JNIEXPORT " + jniType(mtr) +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   351
                           " JNICALL " +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   352
                           mangler.mangleMethod(md, sym,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   353
                                               (longName) ?
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   354
                                               Mangle.Type.METHOD_JNI_LONG :
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   355
                                               Mangle.Type.METHOD_JNI_SHORT));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   356
                print(out, "  (JNIEnv *, ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   357
                List<? extends VariableElement> paramargs = md.getParameters();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   358
                List<TypeMirror> args = new ArrayList<TypeMirror>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   359
                for (VariableElement p: paramargs) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   360
                    args.add(types.erasure(p.asType()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   361
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   362
                if (md.getModifiers().contains(Modifier.STATIC))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   363
                    print(out, "jclass");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   364
                else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   365
                    print(out, "jobject");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   366
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   367
                for (TypeMirror arg: args) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   368
                    print(out, ", ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   369
                    print(out, jniType(arg));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   370
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   371
                println(out, ");"
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   372
                        + lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   373
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   374
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   375
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   376
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   377
    // c.f. MethodDoc.signature
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   378
    String signature(ExecutableElement e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   379
        StringBuilder sb = new StringBuilder("(");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   380
        String sep = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   381
        for (VariableElement p: e.getParameters()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   382
            sb.append(sep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   383
            sb.append(types.erasure(p.asType()).toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   384
            sep = ",";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   385
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   386
        sb.append(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   387
        return sb.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   388
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   389
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   390
    protected final String jniType(TypeMirror t) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   391
        TypeElement throwable = elements.getTypeElement("java.lang.Throwable");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   392
        TypeElement jClass = elements.getTypeElement("java.lang.Class");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   393
        TypeElement jString = elements.getTypeElement("java.lang.String");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   394
        Element tclassDoc = types.asElement(t);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   395
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   396
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   397
        switch (t.getKind()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   398
            case ARRAY: {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   399
                TypeMirror ct = ((ArrayType) t).getComponentType();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   400
                switch (ct.getKind()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   401
                    case BOOLEAN:  return "jbooleanArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   402
                    case BYTE:     return "jbyteArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   403
                    case CHAR:     return "jcharArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   404
                    case SHORT:    return "jshortArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   405
                    case INT:      return "jintArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   406
                    case LONG:     return "jlongArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   407
                    case FLOAT:    return "jfloatArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   408
                    case DOUBLE:   return "jdoubleArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   409
                    case ARRAY:
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   410
                    case DECLARED: return "jobjectArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   411
                    default: throw new Error(ct.toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   412
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   413
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   414
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   415
            case VOID:     return "void";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   416
            case BOOLEAN:  return "jboolean";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   417
            case BYTE:     return "jbyte";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   418
            case CHAR:     return "jchar";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   419
            case SHORT:    return "jshort";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   420
            case INT:      return "jint";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   421
            case LONG:     return "jlong";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   422
            case FLOAT:    return "jfloat";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   423
            case DOUBLE:   return "jdouble";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   424
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   425
            case DECLARED: {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   426
                if (tclassDoc.equals(jString))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   427
                    return "jstring";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   428
                else if (types.isAssignable(t, throwable.asType()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   429
                    return "jthrowable";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   430
                else if (types.isAssignable(t, jClass.asType()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   431
                    return "jclass";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   432
                else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   433
                    return "jobject";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   434
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   435
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   436
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   437
        Assert.check(false, "jni unknown type");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   438
        return null; /* dead code. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   439
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   440
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   441
    protected String fileTop() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   442
        return "/* DO NOT EDIT THIS FILE - it is machine generated */";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   443
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   444
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   445
    protected String includes() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   446
        return "#include <jni.h>";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   447
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   448
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   449
    /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   450
     * Deal with the C pre-processor.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   451
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   452
    protected String cppGuardBegin() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   453
        return "#ifdef __cplusplus" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   454
                + "extern \"C\" {" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   455
                + "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   456
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   457
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   458
    protected String cppGuardEnd() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   459
        return "#ifdef __cplusplus" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   460
                + "}" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   461
                + "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   462
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   463
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   464
    protected String guardBegin(String cname) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   465
        return "/* Header for class " + cname + " */" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   466
                + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   467
                + "#ifndef _Included_" + cname + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   468
                + "#define _Included_" + cname;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   469
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   470
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   471
    protected String guardEnd(String cname) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   472
        return "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   473
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   474
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   475
    protected void print(Writer out, String text) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   476
        out.write(text);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   477
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   478
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   479
    protected void println(Writer out, String text) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   480
        out.write(text);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   481
        out.write(lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   482
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   483
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   484
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   485
    private static class Mangle {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   486
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   487
        public static class Type {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   488
            public static final int CLASS            = 1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   489
            public static final int FIELDSTUB        = 2;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   490
            public static final int FIELD            = 3;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   491
            public static final int JNI              = 4;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   492
            public static final int SIGNATURE        = 5;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   493
            public static final int METHOD_JDK_1     = 6;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   494
            public static final int METHOD_JNI_SHORT = 7;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   495
            public static final int METHOD_JNI_LONG  = 8;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   496
        };
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   497
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   498
        private Elements elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   499
        private Types types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   500
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   501
        Mangle(Elements elems, Types types) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   502
            this.elems = elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   503
            this.types = types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   504
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   505
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   506
        public final String mangle(CharSequence name, int mtype) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   507
            StringBuilder result = new StringBuilder(100);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   508
            int length = name.length();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   509
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   510
            for (int i = 0; i < length; i++) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   511
                char ch = name.charAt(i);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   512
                if (isalnum(ch)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   513
                    result.append(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   514
                } else if ((ch == '.') &&
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   515
                           mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   516
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   517
                } else if (( ch == '$') &&
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   518
                           mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   519
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   520
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   521
                } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   522
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   523
                } else if (ch == '_' && mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   524
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   525
                } else if (mtype == Mangle.Type.JNI) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   526
                    String esc = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   527
                    if (ch == '_')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   528
                        esc = "_1";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   529
                    else if (ch == '.')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   530
                        esc = "_";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   531
                    else if (ch == ';')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   532
                        esc = "_2";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   533
                    else if (ch == '[')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   534
                        esc = "_3";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   535
                    if (esc != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   536
                        result.append(esc);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   537
                    } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   538
                        result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   539
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   540
                } else if (mtype == Mangle.Type.SIGNATURE) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   541
                    if (isprint(ch)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   542
                        result.append(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   543
                    } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   544
                        result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   545
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   546
                } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   547
                    result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   548
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   549
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   550
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   551
            return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   552
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   553
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   554
        public String mangleMethod(ExecutableElement method, TypeElement clazz,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   555
                                          int mtype) throws TypeSignature.SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   556
            StringBuilder result = new StringBuilder(100);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   557
            result.append("Java_");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   558
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   559
            if (mtype == Mangle.Type.METHOD_JDK_1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   560
                result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   561
                result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   562
                result.append(mangle(method.getSimpleName(),
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   563
                                     Mangle.Type.FIELD));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   564
                result.append("_stub");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   565
                return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   566
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   567
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   568
            /* JNI */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   569
            result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   570
            result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   571
            result.append(mangle(method.getSimpleName(),
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   572
                                 Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   573
            if (mtype == Mangle.Type.METHOD_JNI_LONG) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   574
                result.append("__");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   575
                String typesig = signature(method);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   576
                TypeSignature newTypeSig = new TypeSignature(elems);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   577
                String sig = newTypeSig.getTypeSignature(typesig,  method.getReturnType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   578
                sig = sig.substring(1);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   579
                sig = sig.substring(0, sig.lastIndexOf(')'));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   580
                sig = sig.replace('/', '.');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   581
                result.append(mangle(sig, Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   582
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   583
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   584
            return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   585
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   586
        //where
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   587
            private String getInnerQualifiedName(TypeElement clazz) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   588
                return elems.getBinaryName(clazz).toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   589
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   590
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   591
        public final String mangleChar(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   592
            String s = Integer.toHexString(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   593
            int nzeros = 5 - s.length();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   594
            char[] result = new char[6];
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   595
            result[0] = '_';
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   596
            for (int i = 1; i <= nzeros; i++)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   597
                result[i] = '0';
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   598
            for (int i = nzeros+1, j = 0; i < 6; i++, j++)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   599
                result[i] = s.charAt(j);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   600
            return new String(result);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   601
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   602
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   603
        // Warning: duplicated in Gen
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   604
        private String signature(ExecutableElement e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   605
            StringBuilder sb = new StringBuilder();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   606
            String sep = "(";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   607
            for (VariableElement p: e.getParameters()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   608
                sb.append(sep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   609
                sb.append(types.erasure(p.asType()).toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   610
                sep = ",";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   611
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   612
            sb.append(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   613
            return sb.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   614
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   615
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   616
        /* Warning: Intentional ASCII operation. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   617
        private static boolean isalnum(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   618
            return ch <= 0x7f && /* quick test */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   619
                ((ch >= 'A' && ch <= 'Z') ||
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   620
                 (ch >= 'a' && ch <= 'z') ||
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   621
                 (ch >= '0' && ch <= '9'));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   622
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   623
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   624
        /* Warning: Intentional ASCII operation. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   625
        private static boolean isprint(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   626
            return ch >= 32 && ch <= 126;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   627
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   628
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   629
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   630
    private static class TypeSignature {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   631
        static class SignatureException extends Exception {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   632
            private static final long serialVersionUID = 1L;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   633
            SignatureException(String reason) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   634
                super(reason);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   635
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   636
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   637
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   638
        Elements elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   639
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   640
        /* Signature Characters */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   641
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   642
        private static final String SIG_VOID                   = "V";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   643
        private static final String SIG_BOOLEAN                = "Z";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   644
        private static final String SIG_BYTE                   = "B";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   645
        private static final String SIG_CHAR                   = "C";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   646
        private static final String SIG_SHORT                  = "S";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   647
        private static final String SIG_INT                    = "I";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   648
        private static final String SIG_LONG                   = "J";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   649
        private static final String SIG_FLOAT                  = "F";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   650
        private static final String SIG_DOUBLE                 = "D";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   651
        private static final String SIG_ARRAY                  = "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   652
        private static final String SIG_CLASS                  = "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   653
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   654
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   655
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   656
        public TypeSignature(Elements elems){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   657
            this.elems = elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   658
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   659
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   660
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   661
         * Returns the type signature of a field according to JVM specs
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   662
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   663
        public String getTypeSignature(String javasignature) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   664
            return getParamJVMSignature(javasignature);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   665
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   666
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   667
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   668
         * Returns the type signature of a method according to JVM specs
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   669
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   670
        public String getTypeSignature(String javasignature, TypeMirror returnType)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   671
                throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   672
            String signature = null; //Java type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   673
            String typeSignature = null; //Internal type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   674
            List<String> params = new ArrayList<String>(); //List of parameters.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   675
            String paramsig = null; //Java parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   676
            String paramJVMSig = null; //Internal parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   677
            String returnSig = null; //Java return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   678
            String returnJVMType = null; //Internal return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   679
            int dimensions = 0; //Array dimension.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   680
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   681
            int startIndex = -1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   682
            int endIndex = -1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   683
            StringTokenizer st = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   684
            int i = 0;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   685
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   686
            // Gets the actual java signature without parentheses.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   687
            if (javasignature != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   688
                startIndex = javasignature.indexOf("(");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   689
                endIndex = javasignature.indexOf(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   690
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   691
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   692
            if (((startIndex != -1) && (endIndex != -1))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   693
                &&(startIndex+1 < javasignature.length())
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   694
                &&(endIndex < javasignature.length())) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   695
                signature = javasignature.substring(startIndex+1, endIndex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   696
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   697
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   698
            // Separates parameters.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   699
            if (signature != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   700
                if (signature.indexOf(",") != -1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   701
                    st = new StringTokenizer(signature, ",");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   702
                    if (st != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   703
                        while (st.hasMoreTokens()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   704
                            params.add(st.nextToken());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   705
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   706
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   707
                } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   708
                    params.add(signature);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   709
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   710
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   711
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   712
            /* JVM type signature. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   713
            typeSignature = "(";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   714
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   715
            // Gets indivisual internal parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   716
            while (params.isEmpty() != true) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   717
                paramsig = params.remove(i).trim();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   718
                paramJVMSig  = getParamJVMSignature(paramsig);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   719
                if (paramJVMSig != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   720
                    typeSignature += paramJVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   721
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   722
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   723
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   724
            typeSignature += ")";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   725
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   726
            // Get internal return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   727
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   728
            returnJVMType = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   729
            if (returnType != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   730
                dimensions = dimensions(returnType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   731
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   732
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   733
            //Gets array dimension of return type.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   734
            while (dimensions-- > 0) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   735
                returnJVMType += "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   736
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   737
            if (returnType != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   738
                returnSig = qualifiedTypeName(returnType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   739
                returnJVMType += getComponentType(returnSig);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   740
            } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   741
                System.out.println("Invalid return type.");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   742
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   743
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   744
            typeSignature += returnJVMType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   745
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   746
            return typeSignature;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   747
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   748
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   749
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   750
         * Returns internal signature of a parameter.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   751
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   752
        private String getParamJVMSignature(String paramsig) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   753
            String paramJVMSig = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   754
            String componentType ="";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   755
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   756
            if(paramsig != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   757
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   758
                if(paramsig.indexOf("[]") != -1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   759
                    // Gets array dimension.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   760
                    int endindex = paramsig.indexOf("[]");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   761
                    componentType = paramsig.substring(0, endindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   762
                    String dimensionString =  paramsig.substring(endindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   763
                    if(dimensionString != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   764
                        while(dimensionString.indexOf("[]") != -1){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   765
                            paramJVMSig += "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   766
                            int beginindex = dimensionString.indexOf("]") + 1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   767
                            if(beginindex < dimensionString.length()){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   768
                                dimensionString = dimensionString.substring(beginindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   769
                            }else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   770
                                dimensionString = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   771
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   772
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   773
                } else componentType = paramsig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   774
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   775
                paramJVMSig += getComponentType(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   776
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   777
            return paramJVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   778
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   779
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   780
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   781
         * Returns internal signature of a component.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   782
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   783
        private String getComponentType(String componentType) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   784
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   785
            String JVMSig = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   786
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   787
            if(componentType != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   788
                if(componentType.equals("void")) JVMSig += SIG_VOID ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   789
                else if(componentType.equals("boolean"))  JVMSig += SIG_BOOLEAN ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   790
                else if(componentType.equals("byte")) JVMSig += SIG_BYTE ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   791
                else if(componentType.equals("char"))  JVMSig += SIG_CHAR ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   792
                else if(componentType.equals("short"))  JVMSig += SIG_SHORT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   793
                else if(componentType.equals("int"))  JVMSig += SIG_INT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   794
                else if(componentType.equals("long"))  JVMSig += SIG_LONG ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   795
                else if(componentType.equals("float")) JVMSig += SIG_FLOAT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   796
                else if(componentType.equals("double"))  JVMSig += SIG_DOUBLE ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   797
                else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   798
                    if(!componentType.equals("")){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   799
                        TypeElement classNameDoc = elems.getTypeElement(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   800
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   801
                        if(classNameDoc == null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   802
                            throw new SignatureException(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   803
                        }else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   804
                            String classname = classNameDoc.getQualifiedName().toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   805
                            String newclassname = classname.replace('.', '/');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   806
                            JVMSig += "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   807
                            JVMSig += newclassname;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   808
                            JVMSig += ";";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   809
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   810
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   811
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   812
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   813
            return JVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   814
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   815
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   816
        int dimensions(TypeMirror t) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   817
            if (t.getKind() != TypeKind.ARRAY)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   818
                return 0;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   819
            return 1 + dimensions(((ArrayType) t).getComponentType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   820
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   821
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   822
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   823
        String qualifiedTypeName(TypeMirror type) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   824
            TypeVisitor<Name, Void> v = new SimpleTypeVisitor8<Name, Void>() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   825
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   826
                public Name visitArray(ArrayType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   827
                    return t.getComponentType().accept(this, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   828
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   829
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   830
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   831
                public Name visitDeclared(DeclaredType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   832
                    return ((TypeElement) t.asElement()).getQualifiedName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   833
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   834
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   835
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   836
                public Name visitPrimitive(PrimitiveType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   837
                    return elems.getName(t.toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   838
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   839
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   840
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   841
                public Name visitNoType(NoType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   842
                    if (t.getKind() == TypeKind.VOID)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   843
                        return elems.getName("void");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   844
                    return defaultAction(t, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   845
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   846
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   847
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   848
                public Name visitTypeVariable(TypeVariable t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   849
                    return t.getUpperBound().accept(this, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   850
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   851
            };
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   852
            return v.visit(type).toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   853
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   854
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   855
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   856
}