src/share/classes/com/sun/tools/javah/JNI.java

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 712
a1d31ab7b525
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

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

mercurial