1/*
2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javah;
27
28import java.io.OutputStream;
29import java.io.PrintWriter;
30import java.util.ArrayList;
31import java.util.List;
32import javax.lang.model.element.Element;
33import javax.lang.model.element.ExecutableElement;
34import javax.lang.model.element.Modifier;
35import javax.lang.model.element.TypeElement;
36import javax.lang.model.element.VariableElement;
37import javax.lang.model.type.ArrayType;
38import javax.lang.model.type.TypeMirror;
39import javax.lang.model.util.ElementFilter;
40
41
42/**
43 * Header file generator for JNI.
44 *
45 * <p><b>This is NOT part of any supported API.
46 * If you write code that depends on this, you do so at your own
47 * risk.  This code and its internal interfaces are subject to change
48 * or deletion without notice.</b></p>
49 *
50 * @author  Sucheta Dambalkar(Revised)
51 */
52public class JNI extends Gen {
53    JNI(Util util) {
54        super(util);
55    }
56
57    public String getIncludes() {
58        return "#include <jni.h>";
59    }
60
61    public void write(OutputStream o, TypeElement clazz) throws Util.Exit {
62        try {
63            String cname = mangler.mangle(clazz.getQualifiedName(), Mangle.Type.CLASS);
64            PrintWriter pw = wrapWriter(o);
65            pw.println(guardBegin(cname));
66            pw.println(cppGuardBegin());
67
68            /* Write statics. */
69            List<VariableElement> classfields = getAllFields(clazz);
70
71            for (VariableElement v: classfields) {
72                if (!v.getModifiers().contains(Modifier.STATIC))
73                    continue;
74                String s = null;
75                s = defineForStatic(clazz, v);
76                if (s != null) {
77                    pw.println(s);
78                }
79            }
80
81            /* Write methods. */
82            List<ExecutableElement> classmethods = ElementFilter.methodsIn(clazz.getEnclosedElements());
83            for (ExecutableElement md: classmethods) {
84                if(md.getModifiers().contains(Modifier.NATIVE)){
85                    TypeMirror mtr = types.erasure(md.getReturnType());
86                    String sig = signature(md);
87                    TypeSignature newtypesig = new TypeSignature(elems);
88                    CharSequence methodName = md.getSimpleName();
89                    boolean longName = false;
90                    for (ExecutableElement md2: classmethods) {
91                        if ((md2 != md)
92                            && (methodName.equals(md2.getSimpleName()))
93                            && (md2.getModifiers().contains(Modifier.NATIVE)))
94                            longName = true;
95
96                    }
97                    pw.println("/*");
98                    pw.println(" * Class:     " + cname);
99                    pw.println(" * Method:    " +
100                               mangler.mangle(methodName, Mangle.Type.FIELDSTUB));
101                    pw.println(" * Signature: " + newtypesig.getTypeSignature(sig, mtr));
102                    pw.println(" */");
103                    pw.println("JNIEXPORT " + jniType(mtr) +
104                               " JNICALL " +
105                               mangler.mangleMethod(md, clazz,
106                                                   (longName) ?
107                                                   Mangle.Type.METHOD_JNI_LONG :
108                                                   Mangle.Type.METHOD_JNI_SHORT));
109                    pw.print("  (JNIEnv *, ");
110                    List<? extends VariableElement> paramargs = md.getParameters();
111                    List<TypeMirror> args = new ArrayList<>();
112                    for (VariableElement p: paramargs) {
113                        args.add(types.erasure(p.asType()));
114                    }
115                    if (md.getModifiers().contains(Modifier.STATIC))
116                        pw.print("jclass");
117                    else
118                        pw.print("jobject");
119
120                    for (TypeMirror arg: args) {
121                        pw.print(", ");
122                        pw.print(jniType(arg));
123                    }
124                    pw.println(");" + lineSep);
125                }
126            }
127            pw.println(cppGuardEnd());
128            pw.println(guardEnd(cname));
129        } catch (TypeSignature.SignatureException e) {
130            util.error("jni.sigerror", e.getMessage());
131        }
132    }
133
134
135    protected final String jniType(TypeMirror t) throws Util.Exit {
136        TypeElement throwable = elems.getTypeElement("java.lang.Throwable");
137        TypeElement jClass = elems.getTypeElement("java.lang.Class");
138        TypeElement jString = elems.getTypeElement("java.lang.String");
139        Element tclassDoc = types.asElement(t);
140
141
142        switch (t.getKind()) {
143            case ARRAY: {
144                TypeMirror ct = ((ArrayType) t).getComponentType();
145                switch (ct.getKind()) {
146                    case BOOLEAN:  return "jbooleanArray";
147                    case BYTE:     return "jbyteArray";
148                    case CHAR:     return "jcharArray";
149                    case SHORT:    return "jshortArray";
150                    case INT:      return "jintArray";
151                    case LONG:     return "jlongArray";
152                    case FLOAT:    return "jfloatArray";
153                    case DOUBLE:   return "jdoubleArray";
154                    case ARRAY:
155                    case DECLARED: return "jobjectArray";
156                    default: throw new Error(ct.toString());
157                }
158            }
159
160            case VOID:     return "void";
161            case BOOLEAN:  return "jboolean";
162            case BYTE:     return "jbyte";
163            case CHAR:     return "jchar";
164            case SHORT:    return "jshort";
165            case INT:      return "jint";
166            case LONG:     return "jlong";
167            case FLOAT:    return "jfloat";
168            case DOUBLE:   return "jdouble";
169
170            case DECLARED: {
171                if (tclassDoc.equals(jString))
172                    return "jstring";
173                else if (types.isAssignable(t, throwable.asType()))
174                    return "jthrowable";
175                else if (types.isAssignable(t, jClass.asType()))
176                    return "jclass";
177                else
178                    return "jobject";
179            }
180        }
181
182        util.bug("jni.unknown.type");
183        return null; /* dead code. */
184    }
185}
186