2013-09-09 05:57:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.annotationProcessors;
|
|
|
|
|
2013-11-21 12:41:28 -08:00
|
|
|
import org.mozilla.gecko.annotationProcessors.classloader.AnnotatableEntity;
|
2015-01-09 16:33:57 -08:00
|
|
|
import org.mozilla.gecko.annotationProcessors.classloader.ClassWithOptions;
|
2013-09-09 05:57:37 -07:00
|
|
|
import org.mozilla.gecko.annotationProcessors.utils.Utils;
|
|
|
|
|
2013-11-21 12:41:28 -08:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Member;
|
2013-09-09 05:57:37 -07:00
|
|
|
import java.lang.reflect.Method;
|
2014-11-13 10:47:22 -08:00
|
|
|
import java.lang.reflect.Modifier;
|
2013-09-09 05:57:37 -07:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
public class CodeGenerator {
|
2013-11-21 12:41:28 -08:00
|
|
|
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
|
|
|
|
|
2013-09-09 05:57:37 -07:00
|
|
|
// Buffers holding the strings to ultimately be written to the output files.
|
2015-01-09 16:33:57 -08:00
|
|
|
private final StringBuilder cpp = new StringBuilder();
|
|
|
|
private final StringBuilder header = new StringBuilder();
|
|
|
|
|
|
|
|
private final Class<?> cls;
|
|
|
|
private final String clsName;
|
|
|
|
|
|
|
|
private final HashSet<String> takenMethodNames = new HashSet<String>();
|
|
|
|
|
|
|
|
public CodeGenerator(ClassWithOptions annotatedClass) {
|
|
|
|
this.cls = annotatedClass.wrappedClass;
|
|
|
|
this.clsName = annotatedClass.generatedName;
|
|
|
|
|
|
|
|
header.append(
|
|
|
|
"class " + clsName + " : public mozilla::jni::Class<" + clsName + "> {\n" +
|
|
|
|
"\n" +
|
|
|
|
"public:\n" +
|
|
|
|
" typedef mozilla::jni::Ref<" + clsName + "> Ref;\n" +
|
|
|
|
" typedef mozilla::jni::LocalRef<" + clsName + "> LocalRef;\n" +
|
|
|
|
" typedef mozilla::jni::GlobalRef<" + clsName + "> GlobalRef;\n" +
|
|
|
|
" typedef const typename mozilla::jni::Param<" + clsName + ">::Type& Param;\n" +
|
|
|
|
"\n" +
|
|
|
|
" static constexpr char name[] =\n" +
|
|
|
|
" \"" + cls.getName().replace('.', '/') + "\";\n" +
|
|
|
|
"\n" +
|
|
|
|
"protected:\n" +
|
|
|
|
" " + clsName + "(jobject instance) : Class(instance) {}\n" +
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
cpp.append(
|
|
|
|
"constexpr char " + clsName + "::name[];\n" +
|
|
|
|
"\n");
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private String getTraitsName(String uniqueName, boolean includeScope) {
|
|
|
|
return (includeScope ? clsName + "::" : "") + uniqueName + "_t";
|
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private String getNativeParameterType(Class<?> type, AnnotationInfo info) {
|
|
|
|
if (type == cls) {
|
|
|
|
return clsName + "::Param";
|
|
|
|
}
|
|
|
|
return Utils.getNativeParameterType(type, info);
|
|
|
|
}
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private String getNativeReturnType(Class<?> type, AnnotationInfo info) {
|
|
|
|
if (type == cls) {
|
|
|
|
return clsName + "::LocalRef";
|
|
|
|
}
|
|
|
|
return Utils.getNativeReturnType(type, info);
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private void generateMember(AnnotationInfo info, Member member,
|
|
|
|
String uniqueName, Class<?> type) {
|
|
|
|
header.append(
|
|
|
|
"public:\n" +
|
|
|
|
" struct " + getTraitsName(uniqueName, /* includeScope */ false) + " {\n" +
|
|
|
|
" typedef " + clsName + " Owner;\n" +
|
|
|
|
" typedef " + getNativeReturnType(type, info) + " ReturnType;\n" +
|
|
|
|
" typedef " + getNativeParameterType(type, info) + " SetterType;\n" +
|
|
|
|
" static constexpr char name[] = \"" +
|
|
|
|
Utils.getMemberName(member) + "\";\n" +
|
|
|
|
" static constexpr char signature[] =\n" +
|
|
|
|
" \"" + Utils.getSignature(member) + "\";\n" +
|
|
|
|
" static const bool isStatic = " + Utils.isStatic(member) + ";\n" +
|
|
|
|
" static const bool isMultithreaded = " + info.isMultithreaded + ";\n" +
|
|
|
|
" static const mozilla::jni::ExceptionMode exceptionMode = " + (
|
|
|
|
info.catchException ? "mozilla::jni::ExceptionMode::NSRESULT" :
|
|
|
|
info.noThrow ? "mozilla::jni::ExceptionMode::IGNORE" :
|
|
|
|
"mozilla::jni::ExceptionMode::ABORT") + ";\n" +
|
|
|
|
" };\n" +
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
cpp.append(
|
|
|
|
"constexpr char " + getTraitsName(uniqueName, /* includeScope */ true) +
|
|
|
|
"::name[];\n" +
|
|
|
|
"constexpr char " + getTraitsName(uniqueName, /* includeScope */ true) +
|
|
|
|
"::signature[];\n" +
|
|
|
|
"\n");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private String getUniqueMethodName(String basename) {
|
|
|
|
String newName = basename;
|
|
|
|
int index = 1;
|
2014-11-13 10:47:24 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
while (takenMethodNames.contains(newName)) {
|
|
|
|
newName = basename + (++index);
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
|
|
|
|
takenMethodNames.add(newName);
|
|
|
|
return newName;
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-09 16:33:57 -08:00
|
|
|
* Generate a method prototype that includes return and argument types,
|
|
|
|
* without specifiers (static, const, etc.).
|
2013-09-09 05:57:37 -07:00
|
|
|
*/
|
2015-01-09 16:33:57 -08:00
|
|
|
private String generatePrototype(String name, Class<?>[] argTypes,
|
|
|
|
Class<?> returnType, AnnotationInfo info,
|
|
|
|
boolean includeScope, boolean includeArgName) {
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final StringBuilder proto = new StringBuilder();
|
|
|
|
int argIndex = 0;
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (info.catchException) {
|
|
|
|
proto.append("nsresult ");
|
|
|
|
} else {
|
|
|
|
proto.append(getNativeReturnType(returnType, info)).append(' ');
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (includeScope) {
|
|
|
|
proto.append(clsName).append("::");
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
proto.append(name).append('(');
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
for (Class<?> argType : argTypes) {
|
|
|
|
proto.append(getNativeParameterType(argType, info));
|
|
|
|
if (includeArgName) {
|
|
|
|
proto.append(" a").append(argIndex++);
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
proto.append(", ");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (info.catchException && returnType != void.class) {
|
|
|
|
proto.append(getNativeReturnType(returnType, info)).append('*');
|
|
|
|
if (includeArgName) {
|
|
|
|
proto.append(" a").append(argIndex++);
|
|
|
|
}
|
|
|
|
proto.append(", ");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (proto.substring(proto.length() - 2).equals(", ")) {
|
|
|
|
proto.setLength(proto.length() - 2);
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
return proto.append(')').toString();
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
/**
|
|
|
|
* Generate a method declaration that includes the prototype with specifiers,
|
|
|
|
* but without the method body.
|
|
|
|
*/
|
|
|
|
private String generateDeclaration(String name, Class<?>[] argTypes,
|
|
|
|
Class<?> returnType, AnnotationInfo info,
|
|
|
|
boolean isStatic) {
|
|
|
|
|
|
|
|
return (isStatic ? "static " : "") +
|
|
|
|
generatePrototype(name, argTypes, returnType, info,
|
|
|
|
/* includeScope */ false, /* includeArgName */ false) +
|
|
|
|
(isStatic ? ";" : " const;");
|
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
/**
|
|
|
|
* Generate a method definition that includes the prototype with specifiers,
|
|
|
|
* and with the method body.
|
|
|
|
*/
|
|
|
|
private String generateDefinition(String accessorName, String name, Class<?>[] argTypes,
|
|
|
|
Class<?> returnType, AnnotationInfo info, boolean isStatic) {
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final StringBuilder def = new StringBuilder(
|
|
|
|
generatePrototype(name, argTypes, returnType, info,
|
|
|
|
/* includeScope */ true, /* includeArgName */ true));
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (!isStatic) {
|
|
|
|
def.append(" const");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
def.append("\n{\n");
|
2013-11-21 12:41:28 -08:00
|
|
|
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
// Generate code to handle the return value, if needed.
|
|
|
|
// We initialize rv to NS_OK instead of NS_ERROR_* because loading NS_OK (0) uses
|
|
|
|
// fewer instructions. We are guaranteed to set rv to the correct value later.
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (info.catchException && returnType == void.class) {
|
|
|
|
def.append(
|
|
|
|
" nsresult rv = NS_OK;\n" +
|
|
|
|
" ");
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
} else if (info.catchException) {
|
|
|
|
// Non-void return type
|
|
|
|
final String resultArg = "a" + argTypes.length;
|
|
|
|
def.append(
|
|
|
|
" MOZ_ASSERT(" + resultArg + ");\n" +
|
|
|
|
" nsresult rv = NS_OK;\n" +
|
|
|
|
" *" + resultArg + " = ");
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
} else {
|
|
|
|
def.append(
|
|
|
|
" return ");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2014-11-13 10:47:22 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
// Generate a call, e.g., Method<Traits>::Call(a0, a1, a2);
|
2014-11-13 10:47:22 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
def.append(accessorName).append("(")
|
|
|
|
.append(isStatic ? "nullptr" : "this");
|
2014-11-13 10:47:22 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (info.catchException) {
|
|
|
|
def.append(", &rv");
|
|
|
|
} else {
|
|
|
|
def.append(", nullptr");
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
// Generate the call argument list.
|
|
|
|
for (int argIndex = 0; argIndex < argTypes.length; argIndex++) {
|
|
|
|
def.append(", a").append(argIndex);
|
|
|
|
}
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
def.append(");\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (info.catchException) {
|
|
|
|
def.append(" return rv;\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
|
|
|
|
return def.append("}").toString();
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-09 16:33:57 -08:00
|
|
|
* Append the appropriate generated code to the buffers for the method provided.
|
2013-11-21 12:41:28 -08:00
|
|
|
*
|
2015-01-09 16:33:57 -08:00
|
|
|
* @param annotatedMethod The Java method, plus annotation data.
|
2013-11-21 12:41:28 -08:00
|
|
|
*/
|
2015-01-09 16:33:57 -08:00
|
|
|
public void generateMethod(AnnotatableEntity annotatedMethod) {
|
|
|
|
// Unpack the tuple and extract some useful fields from the Method..
|
|
|
|
final Method method = annotatedMethod.getMethod();
|
|
|
|
final AnnotationInfo info = annotatedMethod.mAnnotationInfo;
|
|
|
|
final String uniqueName = getUniqueMethodName(info.wrapperName);
|
|
|
|
final Class<?> returnType = method.getReturnType();
|
|
|
|
|
|
|
|
if (method.isSynthetic()) {
|
|
|
|
return;
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2013-11-18 20:31:35 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
generateMember(info, method, uniqueName, returnType);
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final Class<?>[] argTypes = method.getParameterTypes();
|
|
|
|
final boolean isStatic = Utils.isStatic(method);
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
header.append(
|
|
|
|
" " + generateDeclaration(info.wrapperName, argTypes,
|
|
|
|
returnType, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
cpp.append(
|
|
|
|
generateDefinition(
|
|
|
|
"mozilla::jni::Method<" +
|
|
|
|
getTraitsName(uniqueName, /* includeScope */ false) + ">::Call",
|
|
|
|
info.wrapperName, argTypes, returnType, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
private String getLiteral(Object val, AnnotationInfo info) {
|
|
|
|
final Class<?> type = val.getClass();
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (type == char.class || type == Character.class) {
|
|
|
|
final char c = (char) val;
|
|
|
|
if (c >= 0x20 && c < 0x7F) {
|
|
|
|
return "'" + c + '\'';
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
return "u'\\u" + Integer.toHexString(0x10000 | (int) c).substring(1) + '\'';
|
|
|
|
|
|
|
|
} else if (type == CharSequence.class || type == String.class) {
|
|
|
|
final CharSequence str = (CharSequence) val;
|
|
|
|
final StringBuilder out = new StringBuilder(info.narrowChars ? "u8\"" : "u\"");
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
|
|
|
final char c = str.charAt(i);
|
|
|
|
if (c >= 0x20 && c < 0x7F) {
|
|
|
|
out.append(c);
|
2013-11-21 12:41:28 -08:00
|
|
|
} else {
|
2015-01-09 16:33:57 -08:00
|
|
|
out.append("\\u").append(Integer.toHexString(0x10000 | (int) c).substring(1));
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
}
|
2015-01-09 16:33:57 -08:00
|
|
|
return out.append('"').toString();
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
return String.valueOf(val);
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
public void generateField(AnnotatableEntity annotatedField) {
|
|
|
|
final Field field = annotatedField.getField();
|
|
|
|
final AnnotationInfo info = annotatedField.mAnnotationInfo;
|
|
|
|
final String uniqueName = info.wrapperName;
|
|
|
|
final Class<?> type = field.getType();
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
// Handles a peculiar case when dealing with enum types. We don't care about this field.
|
|
|
|
// It just gets in the way and stops our code from compiling.
|
|
|
|
if (field.isSynthetic() || field.getName().equals("$VALUES")) {
|
|
|
|
return;
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final boolean isStatic = Utils.isStatic(field);
|
|
|
|
final boolean isFinal = Utils.isFinal(field);
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (isStatic && isFinal && (type.isPrimitive() || type == String.class)) {
|
|
|
|
Object val = null;
|
|
|
|
try {
|
|
|
|
val = field.get(null);
|
|
|
|
} catch (final IllegalAccessException e) {
|
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (val != null && type.isPrimitive()) {
|
|
|
|
// For static final primitive fields, we can use a "static const" declaration.
|
|
|
|
header.append(
|
|
|
|
"public:\n" +
|
|
|
|
" static const " + Utils.getNativeReturnType(type, info) +
|
|
|
|
' ' + info.wrapperName + " = " + getLiteral(val, info) + ";\n" +
|
|
|
|
"\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (val != null && type == String.class) {
|
|
|
|
final String nativeType = info.narrowChars ? "char" : "char16_t";
|
|
|
|
|
|
|
|
header.append(
|
|
|
|
"public:\n" +
|
|
|
|
" static const " + nativeType + ' ' + info.wrapperName + "[];\n" +
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
cpp.append(
|
|
|
|
"const " + nativeType + ' ' + clsName + "::" + info.wrapperName +
|
|
|
|
"[] = " + getLiteral(val, info) + ";\n" +
|
|
|
|
"\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
// Fall back to using accessors if we encounter an exception.
|
2014-11-13 10:47:24 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
generateMember(info, field, uniqueName, type);
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final Class<?>[] getterArgs = EMPTY_CLASS_ARRAY;
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
header.append(
|
|
|
|
" " + generateDeclaration(info.wrapperName, getterArgs,
|
|
|
|
type, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
cpp.append(
|
|
|
|
generateDefinition(
|
|
|
|
"mozilla::jni::Field<" +
|
|
|
|
getTraitsName(uniqueName, /* includeScope */ false) + ">::Get",
|
|
|
|
info.wrapperName, getterArgs, type, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (isFinal) {
|
|
|
|
return;
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final Class<?>[] setterArgs = new Class<?>[] { type };
|
2013-09-09 05:57:37 -07:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
header.append(
|
|
|
|
" " + generateDeclaration(info.wrapperName, setterArgs,
|
|
|
|
void.class, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2014-11-13 10:47:24 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
cpp.append(
|
|
|
|
generateDefinition(
|
|
|
|
"mozilla::jni::Field<" +
|
|
|
|
getTraitsName(uniqueName, /* includeScope */ false) + ">::Set",
|
|
|
|
info.wrapperName, setterArgs, void.class, info, isStatic) + "\n" +
|
|
|
|
"\n");
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
public void generateConstructor(AnnotatableEntity annotatedConstructor) {
|
|
|
|
// Unpack the tuple and extract some useful fields from the Method..
|
|
|
|
final Constructor<?> method = annotatedConstructor.getConstructor();
|
|
|
|
final AnnotationInfo info = annotatedConstructor.mAnnotationInfo;
|
|
|
|
final String wrapperName = "New";
|
|
|
|
final String uniqueName = getUniqueMethodName(wrapperName);
|
|
|
|
final Class<?> returnType = cls;
|
2014-11-13 10:47:24 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (method.isSynthetic()) {
|
|
|
|
return;
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
generateMember(info, method, uniqueName, returnType);
|
2014-11-13 10:47:24 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final Class<?>[] argTypes = method.getParameterTypes();
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
header.append(
|
|
|
|
" " + generateDeclaration(wrapperName, argTypes,
|
|
|
|
returnType, info, /* isStatic */ true) + "\n" +
|
|
|
|
"\n");
|
2014-11-13 10:47:24 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
cpp.append(
|
|
|
|
generateDefinition(
|
|
|
|
"mozilla::jni::Constructor<" +
|
|
|
|
getTraitsName(uniqueName, /* includeScope */ false) + ">::Call",
|
|
|
|
wrapperName, argTypes, returnType, info, /* isStatic */ true) + "\n" +
|
|
|
|
"\n");
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
public void generateMembers(Member[] members) {
|
|
|
|
for (Member m : members) {
|
|
|
|
if (!Modifier.isPublic(m.getModifiers())) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
String name = Utils.getMemberName(m);
|
|
|
|
name = name.substring(0, 1).toUpperCase() + name.substring(1);
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
final AnnotationInfo info = new AnnotationInfo(name,
|
|
|
|
/* multithread */ true, /* nothrow */ false,
|
|
|
|
/* narrow */ false, /* catchException */ true);
|
|
|
|
final AnnotatableEntity entity = new AnnotatableEntity(m, info);
|
2013-11-21 12:41:28 -08:00
|
|
|
|
2015-01-09 16:33:57 -08:00
|
|
|
if (m instanceof Constructor) {
|
|
|
|
generateConstructor(entity);
|
|
|
|
} else if (m instanceof Method) {
|
|
|
|
generateMethod(entity);
|
|
|
|
} else if (m instanceof Field) {
|
|
|
|
generateField(entity);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"expected member to be Constructor, Method, or Field");
|
|
|
|
}
|
2013-11-21 12:41:28 -08:00
|
|
|
}
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the finalised bytes to go into the generated wrappers file.
|
|
|
|
*
|
|
|
|
* @return The bytes to be written to the wrappers file.
|
|
|
|
*/
|
2013-11-21 12:41:28 -08:00
|
|
|
public String getWrapperFileContents() {
|
2015-01-09 16:33:57 -08:00
|
|
|
return cpp.toString();
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the finalised bytes to go into the generated header file.
|
|
|
|
*
|
|
|
|
* @return The bytes to be written to the header file.
|
|
|
|
*/
|
2013-11-21 12:41:28 -08:00
|
|
|
public String getHeaderFileContents() {
|
2015-01-09 16:33:57 -08:00
|
|
|
header.append(
|
|
|
|
"};\n" +
|
|
|
|
"\n");
|
|
|
|
return header.toString();
|
2013-09-09 05:57:37 -07:00
|
|
|
}
|
|
|
|
}
|