RubyOneofDescriptor.java
Go to the documentation of this file.
1 package com.google.protobuf.jruby;
2 
5 import org.jruby.Ruby;
6 import org.jruby.RubyClass;
7 import org.jruby.RubyModule;
8 import org.jruby.RubyObject;
9 import org.jruby.anno.JRubyClass;
10 import org.jruby.anno.JRubyMethod;
11 import org.jruby.runtime.Block;
12 import org.jruby.runtime.ObjectAllocator;
13 import org.jruby.runtime.ThreadContext;
14 import org.jruby.runtime.builtin.IRubyObject;
15 
16 import java.util.*;
17 
18 @JRubyClass(name = "OneofDescriptor", include = "Enumerable")
19 public class RubyOneofDescriptor extends RubyObject {
20 
21  public static void createRubyOneofDescriptor(Ruby runtime) {
22  RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
23  RubyClass cRubyOneofDescriptor = protobuf.defineClassUnder("OneofDescriptor", runtime.getObject(), new ObjectAllocator() {
24  @Override
25  public IRubyObject allocate(Ruby ruby, RubyClass rubyClass) {
26  return new RubyOneofDescriptor(ruby, rubyClass);
27  }
28  });
29  cRubyOneofDescriptor.defineAnnotatedMethods(RubyOneofDescriptor.class);
30  cRubyOneofDescriptor.includeModule(runtime.getEnumerable());
31  }
32 
33  public RubyOneofDescriptor(Ruby ruby, RubyClass rubyClass) {
34  super(ruby, rubyClass);
35  }
36 
37  @JRubyMethod
38  public IRubyObject initialize(ThreadContext context) {
39  builder = DescriptorProtos.OneofDescriptorProto.newBuilder();
40  fields = new ArrayList<RubyFieldDescriptor>();
41  return this;
42  }
43 
44  /*
45  * call-seq:
46  * OneofDescriptor.name => name
47  *
48  * Returns the name of this oneof.
49  */
50  @JRubyMethod(name = "name")
51  public IRubyObject getName(ThreadContext context) {
52  return name;
53  }
54 
55  /*
56  * call-seq:
57  * OneofDescriptor.name = name
58  *
59  * Sets a new name for this oneof. The oneof must not have been added to a
60  * message descriptor yet.
61  */
62  @JRubyMethod(name = "name=")
63  public IRubyObject setName(ThreadContext context, IRubyObject name) {
64  this.name = context.runtime.newString(name.asJavaString());
65  this.builder.setName(name.asJavaString());
66  return context.runtime.getNil();
67  }
68 
69  /*
70  * call-seq:
71  * OneofDescriptor.add_field(field) => nil
72  *
73  * Adds a field to this oneof. The field may have been added to this oneof in
74  * the past, or the message to which this oneof belongs (if any), but may not
75  * have already been added to any other oneof or message. Otherwise, an
76  * exception is raised.
77  *
78  * All fields added to the oneof via this method will be automatically added to
79  * the message to which this oneof belongs, if it belongs to one currently, or
80  * else will be added to any message to which the oneof is later added at the
81  * time that it is added.
82  */
83  @JRubyMethod(name = "add_field")
84  public IRubyObject addField(ThreadContext context, IRubyObject obj) {
85  RubyFieldDescriptor fieldDescriptor = (RubyFieldDescriptor) obj;
86  fieldDescriptor.setOneofName(this.name);
87  fields.add(fieldDescriptor);
88  return context.runtime.getNil();
89  }
90 
91  /*
92  * call-seq:
93  * OneofDescriptor.each(&block) => nil
94  *
95  * Iterates through fields in this oneof, yielding to the block on each one.
96  */
97  @JRubyMethod
98  public IRubyObject each(ThreadContext context, Block block) {
100  block.yieldSpecific(context, field);
101  }
102  return context.runtime.getNil();
103  }
104 
105  public DescriptorProtos.OneofDescriptorProto build(int index) {
107  field.setOneofIndex(index);
108  }
109  return this.builder.build();
110  }
111 
112  protected Collection<RubyFieldDescriptor> getFields() {
113  return fields;
114  }
115 
117  RubyFieldDescriptor fieldDescriptor = fields.get(0);
118  return fieldDescriptor.getFieldDef().getContainingOneof();
119  }
120 
121  private IRubyObject name;
122  private DescriptorProtos.OneofDescriptorProto.Builder builder;
123  private List<RubyFieldDescriptor> fields;
124 }
com.google.protobuf.Descriptors
Definition: Descriptors.java:80
com.google.protobuf.jruby.RubyOneofDescriptor.name
IRubyObject name
Definition: RubyOneofDescriptor.java:121
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
com.google.protobuf.jruby.RubyOneofDescriptor.each
IRubyObject each(ThreadContext context, Block block)
Definition: RubyOneofDescriptor.java:98
com.google.protobuf.jruby.RubyOneofDescriptor
Definition: RubyOneofDescriptor.java:19
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.jruby.RubyOneofDescriptor.builder
DescriptorProtos.OneofDescriptorProto.Builder builder
Definition: RubyOneofDescriptor.java:122
com.google.protobuf.jruby.RubyOneofDescriptor.RubyOneofDescriptor
RubyOneofDescriptor(Ruby ruby, RubyClass rubyClass)
Definition: RubyOneofDescriptor.java:33
obj
GLsizei GLsizei GLuint * obj
Definition: glcorearb.h:3066
com.google.protobuf.jruby.RubyOneofDescriptor.fields
List< RubyFieldDescriptor > fields
Definition: RubyOneofDescriptor.java:123
com.google.protobuf.Descriptors.OneofDescriptor
Definition: Descriptors.java:2597
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
com.google.protobuf.jruby.RubyOneofDescriptor.getOneofDescriptor
Descriptors.OneofDescriptor getOneofDescriptor()
Definition: RubyOneofDescriptor.java:116
com.google.protobuf.jruby.RubyFieldDescriptor
Definition: RubyFieldDescriptor.java:45
java
fields
static const upb_fielddef fields[107]
Definition: ruby/ext/google/protobuf_c/upb.c:7671
com.google.protobuf.jruby.RubyFieldDescriptor.getFieldDef
Descriptors.FieldDescriptor getFieldDef()
Definition: RubyFieldDescriptor.java:258
com.google.protobuf.jruby.RubyOneofDescriptor.createRubyOneofDescriptor
static void createRubyOneofDescriptor(Ruby runtime)
Definition: RubyOneofDescriptor.java:21
com.google.protobuf.jruby.RubyOneofDescriptor.build
DescriptorProtos.OneofDescriptorProto build(int index)
Definition: RubyOneofDescriptor.java:105
com.google
com
com.google.protobuf.jruby.RubyFieldDescriptor.setOneofName
void setOneofName(IRubyObject name)
Definition: RubyFieldDescriptor.java:245
com.google.protobuf::DescriptorProtos
index
GLuint index
Definition: glcorearb.h:3055
com.google.protobuf.jruby.RubyOneofDescriptor.getFields
Collection< RubyFieldDescriptor > getFields()
Definition: RubyOneofDescriptor.java:112
com.google.protobuf.jruby.RubyOneofDescriptor.initialize
IRubyObject initialize(ThreadContext context)
Definition: RubyOneofDescriptor.java:38


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58