33 package com.google.protobuf.jruby;
38 import org.jruby.anno.JRubyClass;
39 import org.jruby.anno.JRubyMethod;
40 import org.jruby.runtime.Block;
41 import org.jruby.runtime.ObjectAllocator;
42 import org.jruby.runtime.ThreadContext;
43 import org.jruby.runtime.builtin.IRubyObject;
45 import java.util.HashMap;
49 @JRubyClass(
name =
"Descriptor", include =
"Enumerable")
52 RubyModule protobuf = runtime.getClassFromPath(
"Google::Protobuf");
53 RubyClass
cDescriptor = protobuf.defineClassUnder(
"Descriptor", runtime.getObject(),
new ObjectAllocator() {
55 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
64 super(runtime, klazz);
78 this.builder = DescriptorProtos.DescriptorProto.newBuilder();
79 this.fieldDefMap =
new HashMap<String, RubyFieldDescriptor>();
80 this.oneofDefs =
new HashMap<IRubyObject, RubyOneofDescriptor>();
91 @JRubyMethod(
name =
"name")
92 public IRubyObject getName(ThreadContext context) {
103 @JRubyMethod(
name =
"name=")
104 public IRubyObject setName(ThreadContext context, IRubyObject
name) {
107 return context.runtime.getNil();
119 @JRubyMethod(
name =
"add_field")
120 public IRubyObject addField(ThreadContext context, IRubyObject
obj) {
122 this.fieldDefMap.put(fieldDef.
getName(context).asJavaString(), fieldDef);
123 this.builder.addField(fieldDef.
build());
124 return context.runtime.getNil();
135 public IRubyObject
lookup(ThreadContext context, IRubyObject fieldName) {
136 return this.fieldDefMap.get(fieldName.asJavaString());
147 public IRubyObject
msgclass(ThreadContext context) {
148 if (this.klazz ==
null) {
149 this.klazz = buildClassFromDescriptor(context);
161 public IRubyObject
each(ThreadContext context, Block block) {
163 block.yield(context, entry.getValue());
165 return context.runtime.getNil();
179 @JRubyMethod(
name =
"add_oneof")
180 public IRubyObject addOneof(ThreadContext context, IRubyObject
obj) {
182 builder.addOneofDecl(def.
build(builder.getOneofDeclCount()));
184 addField(context, fieldDescriptor);
186 oneofDefs.put(def.
getName(context), def);
187 return context.runtime.getNil();
197 @JRubyMethod(
name =
"each_oneof")
198 public IRubyObject eachOneof(ThreadContext context, Block block) {
200 block.yieldSpecific(context, oneofDescriptor);
202 return context.runtime.getNil();
212 @JRubyMethod(
name =
"lookup_oneof")
213 public IRubyObject lookupOneof(ThreadContext context, IRubyObject
name) {
214 if (
name instanceof RubySymbol) {
215 name = ((RubySymbol)
name).id2name();
217 return oneofDefs.containsKey(
name) ? oneofDefs.get(
name) : context.runtime.getNil();
228 public DescriptorProtos.DescriptorProto.Builder
getBuilder() {
233 this.builder.setOptions(DescriptorProtos.MessageOptions.newBuilder().setMapEntry(isMapEntry));
237 Ruby runtime = context.runtime;
239 ObjectAllocator allocator =
new ObjectAllocator() {
241 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
247 RubyClass
klass = RubyClass.newClass(runtime, runtime.getObject());
248 klass.setAllocator(allocator);
249 klass.makeMetaClass(runtime.getObject().getMetaClass());
250 klass.inherit(runtime.getObject());
251 RubyModule messageExts = runtime.getClassFromPath(
"Google::Protobuf::MessageExts");
252 klass.include(
new IRubyObject[] {messageExts});
265 private DescriptorProtos.DescriptorProto.Builder
builder;
268 private Map<IRubyObject, RubyOneofDescriptor>
oneofDefs;