33 package com.google.protobuf.jruby;
38 import org.jruby.anno.JRubyClass;
39 import org.jruby.anno.JRubyMethod;
40 import org.jruby.runtime.ObjectAllocator;
41 import org.jruby.runtime.ThreadContext;
42 import org.jruby.runtime.builtin.IRubyObject;
44 @JRubyClass(
name =
"FieldDescriptor")
47 RubyModule mProtobuf = runtime.getClassFromPath(
"Google::Protobuf");
48 RubyClass
cFieldDescriptor = mProtobuf.defineClassUnder(
"FieldDescriptor", runtime.getObject(),
new ObjectAllocator() {
50 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
58 super(runtime, klazz);
70 builder = DescriptorProtos.FieldDescriptorProto.newBuilder();
80 @JRubyMethod(
name =
"label")
81 public IRubyObject getLabel(ThreadContext context) {
92 @JRubyMethod(
name =
"label=")
93 public IRubyObject setLabel(ThreadContext context, IRubyObject
value) {
94 String labelName =
value.asJavaString();
95 this.
label = context.runtime.newSymbol(labelName.toLowerCase());
96 this.builder.setLabel(
97 DescriptorProtos.FieldDescriptorProto.Label.valueOf(
"LABEL_" + labelName.toUpperCase()));
98 return context.runtime.getNil();
107 @JRubyMethod(
name =
"name")
108 public IRubyObject getName(ThreadContext context) {
119 @JRubyMethod(
name =
"name=")
120 public IRubyObject setName(ThreadContext context, IRubyObject
value) {
121 String nameStr =
value.asJavaString();
122 this.
name = context.runtime.newString(nameStr);
124 return context.runtime.getNil();
128 @JRubyMethod(
name =
"subtype")
129 public IRubyObject getSubType(ThreadContext context) {
143 @JRubyMethod(
name =
"type")
144 public IRubyObject getType(ThreadContext context) {
155 @JRubyMethod(
name =
"type=")
156 public IRubyObject setType(ThreadContext context, IRubyObject
value) {
157 this.builder.setType(DescriptorProtos.FieldDescriptorProto.Type.valueOf(
"TYPE_" +
value.asJavaString().toUpperCase()));
158 return context.runtime.getNil();
168 @JRubyMethod(
name =
"number")
169 public IRubyObject getnumber(ThreadContext context) {
180 @JRubyMethod(
name =
"number=")
181 public IRubyObject setNumber(ThreadContext context, IRubyObject
value) {
183 this.builder.setNumber(RubyNumeric.num2int(
value));
184 return context.runtime.getNil();
197 @JRubyMethod(
name =
"submsg_name=")
198 public IRubyObject setSubmsgName(ThreadContext context, IRubyObject
name) {
200 return context.runtime.getNil();
210 @JRubyMethod(
name =
"get")
211 public IRubyObject getValue(ThreadContext context, IRubyObject msgRb) {
213 if (
message.getDescriptor() != fieldDef.getContainingType()) {
214 throw context.runtime.newTypeError(
"set method called on wrong message type");
227 @JRubyMethod(
name =
"set")
228 public IRubyObject setValue(ThreadContext context, IRubyObject msgRb, IRubyObject
value) {
230 if (
message.getDescriptor() != fieldDef.getContainingType()) {
231 throw context.runtime.newTypeError(
"set method called on wrong message type");
234 return context.runtime.getNil();
238 this.subType = rubyDescriptor;
242 this.fieldDef = fieldDescriptor;
250 hasOneofIndex =
true;
262 protected DescriptorProtos.FieldDescriptorProto
build() {
264 builder.setOneofIndex(oneofIndex);
265 return this.builder.build();
268 private DescriptorProtos.FieldDescriptorProto.Builder
builder;
276 private boolean hasOneofIndex =
false;