src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java

Tue, 25 Jun 2013 16:12:53 +0100

author
vromero
date
Tue, 25 Jun 2013 16:12:53 +0100
changeset 1853
831467c4c6a7
parent 1802
8fb68f73d4b1
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8017104: javac should have a class for primitive types that inherits from Type
Reviewed-by: jjg

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

mercurial