Bug 1192079 - Use unqualified names when possible; r=snorp

To reduce verbosity of the generated code, this patch makes the code
generator use unqualified names when possible, e.g. use State::Ref
instead of GeckoThread::State::Ref. To accomplish that, function
prototypes now use the C++11 -> syntax for return types.
This commit is contained in:
Jim Chen 2015-08-13 00:53:39 -04:00
Родитель 7595348845
Коммит 2bdb7be558
1 изменённых файлов: 30 добавлений и 24 удалений

Просмотреть файл

@ -34,20 +34,21 @@ public class CodeGenerator {
this.cls = annotatedClass.wrappedClass; this.cls = annotatedClass.wrappedClass;
this.clsName = annotatedClass.generatedName; this.clsName = annotatedClass.generatedName;
final String unqualifiedName = Utils.getUnqualifiedName(clsName);
header.append( header.append(
"class " + clsName + " : public mozilla::jni::Class<" + clsName + ">\n" + "class " + clsName + " : public mozilla::jni::Class<" + unqualifiedName + ">\n" +
"{\n" + "{\n" +
"public:\n" + "public:\n" +
" typedef mozilla::jni::Ref<" + clsName + "> Ref;\n" + " typedef mozilla::jni::Ref<" + unqualifiedName + "> Ref;\n" +
" typedef mozilla::jni::LocalRef<" + clsName + "> LocalRef;\n" + " typedef mozilla::jni::LocalRef<" + unqualifiedName + "> LocalRef;\n" +
" typedef mozilla::jni::GlobalRef<" + clsName + "> GlobalRef;\n" + " typedef mozilla::jni::GlobalRef<" + unqualifiedName + "> GlobalRef;\n" +
" typedef const mozilla::jni::Param<" + clsName + ">& Param;\n" + " typedef const mozilla::jni::Param<" + unqualifiedName + ">& Param;\n" +
"\n" + "\n" +
" static constexpr char name[] =\n" + " static constexpr char name[] =\n" +
" \"" + cls.getName().replace('.', '/') + "\";\n" + " \"" + cls.getName().replace('.', '/') + "\";\n" +
"\n" + "\n" +
"protected:\n" + "protected:\n" +
" " + clsName + "(jobject instance) : Class(instance) {}\n" + " using Class::Class;\n" +
"\n"); "\n");
cpp.append( cpp.append(
@ -57,7 +58,7 @@ public class CodeGenerator {
natives.append( natives.append(
"template<class Impl>\n" + "template<class Impl>\n" +
"class " + clsName + "::Natives : " + "class " + clsName + "::Natives : " +
"public mozilla::jni::NativeImpl<" + clsName + ", Impl>\n" + "public mozilla::jni::NativeImpl<" + unqualifiedName + ", Impl>\n" +
"{\n"); "{\n");
} }
@ -67,14 +68,14 @@ public class CodeGenerator {
private String getNativeParameterType(Class<?> type, AnnotationInfo info) { private String getNativeParameterType(Class<?> type, AnnotationInfo info) {
if (type == cls) { if (type == cls) {
return clsName + "::Param"; return Utils.getUnqualifiedName(clsName) + "::Param";
} }
return Utils.getNativeParameterType(type, info); return Utils.getNativeParameterType(type, info);
} }
private String getNativeReturnType(Class<?> type, AnnotationInfo info) { private String getNativeReturnType(Class<?> type, AnnotationInfo info) {
if (type == cls) { if (type == cls) {
return clsName + "::LocalRef"; return Utils.getUnqualifiedName(clsName) + "::LocalRef";
} }
return Utils.getNativeReturnType(type, info); return Utils.getNativeReturnType(type, info);
} }
@ -92,7 +93,7 @@ public class CodeGenerator {
header.append( header.append(
"public:\n" + "public:\n" +
" struct " + getTraitsName(uniqueName, /* includeScope */ false) + " {\n" + " struct " + getTraitsName(uniqueName, /* includeScope */ false) + " {\n" +
" typedef " + clsName + " Owner;\n" + " typedef " + Utils.getUnqualifiedName(clsName) + " Owner;\n" +
" typedef " + getNativeReturnType(type, info) + " ReturnType;\n" + " typedef " + getNativeReturnType(type, info) + " ReturnType;\n" +
" typedef " + getNativeParameterType(type, info) + " SetterType;\n" + " typedef " + getNativeParameterType(type, info) + " SetterType;\n" +
" typedef mozilla::jni::Args<" + args + "> Args;\n" + " typedef mozilla::jni::Args<" + args + "> Args;\n" +
@ -136,16 +137,13 @@ public class CodeGenerator {
*/ */
private String generatePrototype(String name, Class<?>[] argTypes, private String generatePrototype(String name, Class<?>[] argTypes,
Class<?> returnType, AnnotationInfo info, Class<?> returnType, AnnotationInfo info,
boolean includeScope, boolean includeArgName) { boolean includeScope, boolean includeArgName,
boolean isConst) {
final StringBuilder proto = new StringBuilder(); final StringBuilder proto = new StringBuilder();
int argIndex = 0; int argIndex = 0;
if (info.catchException) { proto.append("auto ");
proto.append("nsresult ");
} else {
proto.append(getNativeReturnType(returnType, info)).append(' ');
}
if (includeScope) { if (includeScope) {
proto.append(clsName).append("::"); proto.append(clsName).append("::");
@ -173,7 +171,18 @@ public class CodeGenerator {
proto.setLength(proto.length() - 2); proto.setLength(proto.length() - 2);
} }
return proto.append(')').toString(); proto.append(')');
if (isConst) {
proto.append(" const");
}
if (info.catchException) {
proto.append(" -> nsresult");
} else {
proto.append(" -> ").append(getNativeReturnType(returnType, info));
}
return proto.toString();
} }
/** /**
@ -186,8 +195,8 @@ public class CodeGenerator {
return (isStatic ? "static " : "") + return (isStatic ? "static " : "") +
generatePrototype(name, argTypes, returnType, info, generatePrototype(name, argTypes, returnType, info,
/* includeScope */ false, /* includeArgName */ false) + /* includeScope */ false, /* includeArgName */ false,
(isStatic ? ";" : " const;"); /* isConst */ !isStatic) + ';';
} }
/** /**
@ -199,11 +208,8 @@ public class CodeGenerator {
final StringBuilder def = new StringBuilder( final StringBuilder def = new StringBuilder(
generatePrototype(name, argTypes, returnType, info, generatePrototype(name, argTypes, returnType, info,
/* includeScope */ true, /* includeArgName */ true)); /* includeScope */ true, /* includeArgName */ true,
/* isConst */ !isStatic));
if (!isStatic) {
def.append(" const");
}
def.append("\n{\n"); def.append("\n{\n");