outerClassname
This commit is contained in:
parent
a1be7d2e27
commit
8aca726a4d
3 changed files with 23 additions and 48 deletions
|
@ -56,7 +56,7 @@
|
||||||
namespace capnp {
|
namespace capnp {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
static constexpr uint64_t NAMESPACE_ANNOTATION_ID = 0xb9c6f99ebf805f2cull;
|
static constexpr uint64_t OUTER_CLASSNAME_ANNOTATION_ID = 0x9b066bb4881f7cd3;
|
||||||
static constexpr uint64_t PACKAGE_ANNOTATION_ID = 0x9ee4c8f803b3b596ull;
|
static constexpr uint64_t PACKAGE_ANNOTATION_ID = 0x9ee4c8f803b3b596ull;
|
||||||
|
|
||||||
static constexpr const char* FIELD_SIZE_NAMES[] = {
|
static constexpr const char* FIELD_SIZE_NAMES[] = {
|
||||||
|
@ -194,18 +194,16 @@ private:
|
||||||
std::unordered_set<uint64_t> usedImports;
|
std::unordered_set<uint64_t> usedImports;
|
||||||
bool hasInterfaces = false;
|
bool hasInterfaces = false;
|
||||||
|
|
||||||
kj::String outerClassName;
|
|
||||||
|
|
||||||
kj::StringTree javaFullName(Schema schema) {
|
kj::StringTree javaFullName(Schema schema) {
|
||||||
auto node = schema.getProto();
|
auto node = schema.getProto();
|
||||||
if (node.getScopeId() == 0) {
|
if (node.getScopeId() == 0) {
|
||||||
usedImports.insert(node.getId());
|
usedImports.insert(node.getId());
|
||||||
for (auto annotation: node.getAnnotations()) {
|
for (auto annotation: node.getAnnotations()) {
|
||||||
/* if (annotation.getId() == NAMESPACE_ANNOTATION_ID) {
|
if (annotation.getId() == OUTER_CLASSNAME_ANNOTATION_ID) {
|
||||||
return kj::strTree("", annotation.getValue().getText());
|
return kj::strTree("", toTitleCase(annotation.getValue().getText()));
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
return kj::strTree(outerClassName);
|
return kj::strTree(" ");//kj::strTree(outerClassName);
|
||||||
} else {
|
} else {
|
||||||
Schema parent = schemaLoader.get(node.getScopeId());
|
Schema parent = schemaLoader.get(node.getScopeId());
|
||||||
for (auto nested: parent.getProto().getNestedNodes()) {
|
for (auto nested: parent.getProto().getNestedNodes()) {
|
||||||
|
@ -1324,7 +1322,7 @@ private:
|
||||||
kj::StringTree sourceFileDefs;
|
kj::StringTree sourceFileDefs;
|
||||||
};
|
};
|
||||||
|
|
||||||
NodeText makeNodeText(kj::StringPtr namespace_, kj::StringPtr scope,
|
NodeText makeNodeText(kj::StringPtr scope,
|
||||||
kj::StringPtr name, Schema schema,
|
kj::StringPtr name, Schema schema,
|
||||||
int indent) {
|
int indent) {
|
||||||
auto proto = schema.getProto();
|
auto proto = schema.getProto();
|
||||||
|
@ -1336,15 +1334,13 @@ private:
|
||||||
kj::Vector<NodeText> nestedTexts(proto.getNestedNodes().size());
|
kj::Vector<NodeText> nestedTexts(proto.getNestedNodes().size());
|
||||||
for (auto nested: proto.getNestedNodes()) {
|
for (auto nested: proto.getNestedNodes()) {
|
||||||
nestedTexts.add(makeNodeText(
|
nestedTexts.add(makeNodeText(
|
||||||
namespace_,
|
|
||||||
subScope, nested.getName(), schemaLoader.get(nested.getId()), indent + 1));
|
subScope, nested.getName(), schemaLoader.get(nested.getId()), indent + 1));
|
||||||
};
|
};
|
||||||
|
|
||||||
if (proto.isStruct()) {
|
if (proto.isStruct()) {
|
||||||
for (auto field: proto.getStruct().getFields()) {
|
for (auto field: proto.getStruct().getFields()) {
|
||||||
if (field.isGroup()) {
|
if (field.isGroup()) {
|
||||||
nestedTexts.add(makeNodeText(
|
nestedTexts.add(makeNodeText(subScope, toTitleCase(field.getName()),
|
||||||
namespace_, subScope, toTitleCase(field.getName()),
|
|
||||||
schemaLoader.get(field.getGroup().getTypeId()), indent + 1));
|
schemaLoader.get(field.getGroup().getTypeId()), indent + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1423,7 +1419,7 @@ private:
|
||||||
"};\n");
|
"};\n");
|
||||||
|
|
||||||
NodeTextNoSchema top = makeNodeTextWithoutNested(
|
NodeTextNoSchema top = makeNodeTextWithoutNested(
|
||||||
namespace_, scope, name, schema,
|
scope, name, schema,
|
||||||
KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }, indent);
|
KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }, indent);
|
||||||
|
|
||||||
return NodeText {
|
return NodeText {
|
||||||
|
@ -1463,7 +1459,7 @@ private:
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeTextNoSchema makeNodeTextWithoutNested(kj::StringPtr namespace_, kj::StringPtr scope,
|
NodeTextNoSchema makeNodeTextWithoutNested(kj::StringPtr scope,
|
||||||
kj::StringPtr name, Schema schema,
|
kj::StringPtr name, Schema schema,
|
||||||
kj::Array<kj::StringTree> nestedTypeDecls,
|
kj::Array<kj::StringTree> nestedTypeDecls,
|
||||||
int indent) {
|
int indent) {
|
||||||
|
@ -1559,6 +1555,7 @@ private:
|
||||||
// -----------------------------------------------------------------
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
struct FileText {
|
struct FileText {
|
||||||
|
kj::String outerClassname;
|
||||||
kj::StringTree source;
|
kj::StringTree source;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1569,47 +1566,26 @@ private:
|
||||||
auto node = schema.getProto();
|
auto node = schema.getProto();
|
||||||
auto displayName = node.getDisplayName();
|
auto displayName = node.getDisplayName();
|
||||||
|
|
||||||
kj::Vector<kj::ArrayPtr<const char>> namespaceParts;
|
|
||||||
kj::String namespacePrefix;
|
|
||||||
kj::StringPtr packageName;
|
kj::StringPtr packageName;
|
||||||
|
kj::StringPtr outerClassname;
|
||||||
for (auto annotation: node.getAnnotations()) {
|
|
||||||
if (annotation.getId() == NAMESPACE_ANNOTATION_ID) {
|
|
||||||
kj::StringPtr ns = annotation.getValue().getText();
|
|
||||||
kj::StringPtr ns2 = ns;
|
|
||||||
namespacePrefix = kj::str("::", ns);
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
KJ_IF_MAYBE(colonPos, ns.findFirst(':')) {
|
|
||||||
namespaceParts.add(ns.slice(0, *colonPos));
|
|
||||||
ns = ns.slice(*colonPos);
|
|
||||||
if (!ns.startsWith("::")) {
|
|
||||||
context.exitError(kj::str(displayName, ": invalid namespace spec: ", ns2));
|
|
||||||
}
|
|
||||||
ns = ns.slice(2);
|
|
||||||
} else {
|
|
||||||
namespaceParts.add(ns);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto annotation: node.getAnnotations()) {
|
for (auto annotation: node.getAnnotations()) {
|
||||||
if (annotation.getId() == PACKAGE_ANNOTATION_ID) {
|
if (annotation.getId() == PACKAGE_ANNOTATION_ID) {
|
||||||
packageName = annotation.getValue().getText();
|
packageName = annotation.getValue().getText();
|
||||||
break;
|
} else if (annotation.getId() == OUTER_CLASSNAME_ANNOTATION_ID) {
|
||||||
|
outerClassname = annotation.getValue().getText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packageName.size() == 0) {
|
if (packageName.size() == 0) {
|
||||||
context.exitError(kj::str(displayName, ": must provide a Java package name."));
|
context.exitError(kj::str(displayName, ": must provide a Java package name."));
|
||||||
}
|
}
|
||||||
|
if (outerClassname.size() == 0) {
|
||||||
|
context.exitError(kj::str(displayName, ": must provide a Java outer classname."));
|
||||||
|
}
|
||||||
|
|
||||||
auto nodeTexts = KJ_MAP(nested, node.getNestedNodes()) {
|
auto nodeTexts = KJ_MAP(nested, node.getNestedNodes()) {
|
||||||
return makeNodeText(namespacePrefix, "", nested.getName(), schemaLoader.get(nested.getId()), 1);
|
return makeNodeText("", nested.getName(), schemaLoader.get(nested.getId()), 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
kj::String separator = kj::str("// ", kj::repeat('=', 87), "\n");
|
kj::String separator = kj::str("// ", kj::repeat('=', 87), "\n");
|
||||||
|
@ -1625,15 +1601,14 @@ private:
|
||||||
KJ_MAP(n, nodeTexts) { return kj::mv(n.sourceFileDefs); });
|
KJ_MAP(n, nodeTexts) { return kj::mv(n.sourceFileDefs); });
|
||||||
|
|
||||||
return FileText {
|
return FileText {
|
||||||
|
kj::str(outerClassname),
|
||||||
kj::strTree(
|
kj::strTree(
|
||||||
"// Generated by Cap'n Proto compiler, DO NOT EDIT\n"
|
"// Generated by Cap'n Proto compiler, DO NOT EDIT\n"
|
||||||
"// source: ", baseName(displayName), "\n\n",
|
"// source: ", baseName(displayName), "\n\n",
|
||||||
"package ", packageName, ";\n\n",
|
"package ", packageName, ";\n\n",
|
||||||
//"import org.capnproto;\n",
|
//"import org.capnproto;\n",
|
||||||
// KJ_MAP(n, namespaceParts) { return kj::strTree("namespace ", n, " {\n"); }, "\n",
|
"public class ", outerClassname, " {\n",
|
||||||
"public class ", outerClassName, " {\n",
|
|
||||||
KJ_MAP(n, nodeTexts) { return kj::mv(n.outerTypeDef); },
|
KJ_MAP(n, nodeTexts) { return kj::mv(n.outerTypeDef); },
|
||||||
//KJ_MAP(n, namespaceParts) { return kj::strTree("}\n"); },
|
|
||||||
"}\n",
|
"}\n",
|
||||||
"\n")
|
"\n")
|
||||||
};
|
};
|
||||||
|
@ -1698,13 +1673,10 @@ private:
|
||||||
KJ_IF_MAYBE(dotpos, filename.findLast('.')) {
|
KJ_IF_MAYBE(dotpos, filename.findLast('.')) {
|
||||||
stemend = *dotpos;
|
stemend = *dotpos;
|
||||||
}
|
}
|
||||||
outerClassName = toTitleCase(kj::str(filename.slice(stemstart, stemend)));
|
|
||||||
|
|
||||||
auto genFileName = kj::str(filename.slice(0, stemstart), outerClassName, ".java");
|
|
||||||
|
|
||||||
auto fileText = makeFileText(schema, requestedFile);
|
auto fileText = makeFileText(schema, requestedFile);
|
||||||
|
|
||||||
writeFile(genFileName, fileText.source);
|
writeFile(kj::str(filename.slice(0, stemstart), fileText.outerClassname, ".java"), fileText.source);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
@0xc5f1af96651f70ea;
|
@0xc5f1af96651f70ea;
|
||||||
|
|
||||||
annotation package(file): Text;
|
annotation package(file): Text;
|
||||||
|
|
||||||
|
annotation outerClassname(file): Text;
|
|
@ -28,6 +28,7 @@ $Cxx.namespace("addressbook");
|
||||||
|
|
||||||
using Java = import "/java_support/java.capnp";
|
using Java = import "/java_support/java.capnp";
|
||||||
$Java.package("org.capnproto.examples");
|
$Java.package("org.capnproto.examples");
|
||||||
|
$Java.outerClassname("Addressbook");
|
||||||
|
|
||||||
struct Person {
|
struct Person {
|
||||||
id @0 :UInt32;
|
id @0 :UInt32;
|
||||||
|
|
Loading…
Reference in a new issue