RubyDescriptorPool.java
Go to the documentation of this file.
1 /*
2  * Protocol Buffers - Google's data interchange format
3  * Copyright 2014 Google Inc. All rights reserved.
4  * https://developers.google.com/protocol-buffers/
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.google.protobuf.jruby;
34 
37 import org.jruby.*;
38 import org.jruby.anno.JRubyClass;
39 import org.jruby.anno.JRubyMethod;
40 import org.jruby.runtime.*;
41 import org.jruby.runtime.builtin.IRubyObject;
42 
43 import java.util.HashMap;
44 import java.util.Map;
45 
46 @JRubyClass(name = "DescriptorPool")
47 public class RubyDescriptorPool extends RubyObject {
48  public static void createRubyDescriptorPool(Ruby runtime) {
49  RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
50  RubyClass cDescriptorPool = protobuf.defineClassUnder("DescriptorPool", runtime.getObject(), new ObjectAllocator() {
51  @Override
52  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
53  return new RubyDescriptorPool(runtime, klazz);
54  }
55  });
56 
57  cDescriptorPool.defineAnnotatedMethods(RubyDescriptorPool.class);
58  descriptorPool = (RubyDescriptorPool) cDescriptorPool.newInstance(runtime.getCurrentContext(), Block.NULL_BLOCK);
59  }
60 
61  public RubyDescriptorPool(Ruby ruby, RubyClass klazz) {
62  super(ruby, klazz);
63  }
64 
65  @JRubyMethod
66  public IRubyObject initialize(ThreadContext context) {
67  this.symtab = new HashMap<IRubyObject, IRubyObject>();
68  this.cBuilder = (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::Builder");
69  this.builder = DescriptorProtos.FileDescriptorProto.newBuilder();
70  return this;
71  }
72 
73  @JRubyMethod
74  public IRubyObject build(ThreadContext context, Block block) {
75  RubyBuilder ctx = (RubyBuilder) cBuilder.newInstance(context, Block.NULL_BLOCK);
76  if (block.arity() == Arity.ONE_ARGUMENT) {
77  block.yield(context, ctx);
78  } else {
79  Binding binding = block.getBinding();
80  binding.setSelf(ctx);
81  block.yieldSpecific(context);
82  }
83  ctx.finalizeToPool(context, this);
84  buildFileDescriptor(context);
85  return context.runtime.getNil();
86  }
87 
88  @JRubyMethod
89  public IRubyObject lookup(ThreadContext context, IRubyObject name) {
90  IRubyObject descriptor = this.symtab.get(name);
91  if (descriptor == null) {
92  return context.runtime.getNil();
93  }
94  return descriptor;
95  }
96 
97  /*
98  * call-seq:
99  * DescriptorPool.generated_pool => descriptor_pool
100  *
101  * Class method that returns the global DescriptorPool. This is a singleton into
102  * which generated-code message and enum types are registered. The user may also
103  * register types in this pool for convenience so that they do not have to hold
104  * a reference to a private pool instance.
105  */
106  @JRubyMethod(meta = true, name = "generated_pool")
107  public static IRubyObject generatedPool(ThreadContext context, IRubyObject recv) {
108  return descriptorPool;
109  }
110 
111  protected void addToSymtab(ThreadContext context, RubyDescriptor def) {
112  symtab.put(def.getName(context), def);
113  this.builder.addMessageType(def.getBuilder());
114  }
115 
116  protected void addToSymtab(ThreadContext context, RubyEnumDescriptor def) {
117  symtab.put(def.getName(context), def);
118  this.builder.addEnumType(def.getBuilder());
119  }
120 
121  private void buildFileDescriptor(ThreadContext context) {
122  Ruby runtime = context.runtime;
123  try {
124  this.builder.setSyntax("proto3");
126  this.builder.build(), new Descriptors.FileDescriptor[]{});
127 
128  for (Descriptors.EnumDescriptor enumDescriptor : fileDescriptor.getEnumTypes()) {
129  String enumName = Utils.unescapeIdentifier(enumDescriptor.getName());
130  if (enumDescriptor.findValueByNumber(0) == null) {
131  throw runtime.newTypeError("Enum definition " + enumName
132  + " does not contain a value for '0'");
133  }
134  ((RubyEnumDescriptor) symtab.get(runtime.newString(enumName)))
135  .setDescriptor(enumDescriptor);
136  }
137  for (Descriptors.Descriptor descriptor : fileDescriptor.getMessageTypes()) {
138  RubyDescriptor rubyDescriptor = ((RubyDescriptor)
139  symtab.get(runtime.newString(Utils.unescapeIdentifier(descriptor.getName()))));
140  for (Descriptors.FieldDescriptor fieldDescriptor : descriptor.getFields()) {
141  if (fieldDescriptor.isRequired()) {
142  throw runtime.newTypeError("Required fields are unsupported in proto3");
143  }
144  RubyFieldDescriptor rubyFieldDescriptor = rubyDescriptor.lookup(fieldDescriptor.getName());
145  rubyFieldDescriptor.setFieldDef(fieldDescriptor);
146  if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
147  RubyDescriptor subType = (RubyDescriptor) lookup(context,
148  runtime.newString(Utils.unescapeIdentifier(fieldDescriptor.getMessageType().getName())));
149  rubyFieldDescriptor.setSubType(subType);
150  }
151  if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.ENUM) {
152  RubyEnumDescriptor subType = (RubyEnumDescriptor) lookup(context,
153  runtime.newString(Utils.unescapeIdentifier(fieldDescriptor.getEnumType().getName())));
154  rubyFieldDescriptor.setSubType(subType);
155  }
156  }
157  rubyDescriptor.setDescriptor(descriptor);
158  }
160  throw runtime.newRuntimeError(e.getMessage());
161  }
162  }
163 
165 
166  private RubyClass cBuilder;
167  private Map<IRubyObject, IRubyObject> symtab;
168  private DescriptorProtos.FileDescriptorProto.Builder builder;
169 }
com.google.protobuf.jruby.RubyDescriptor.getBuilder
DescriptorProtos.DescriptorProto.Builder getBuilder()
Definition: RubyDescriptor.java:228
com.google.protobuf.Descriptors
Definition: Descriptors.java:80
cDescriptorPool
VALUE cDescriptorPool
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
com.google.protobuf.jruby.RubyDescriptorPool.build
IRubyObject build(ThreadContext context, Block block)
Definition: RubyDescriptorPool.java:74
lookup
static int16_t lookup[CNT]
Definition: floatTolin.h:6
com.google.protobuf.jruby.RubyBuilder
Definition: RubyBuilder.java:42
com.google.protobuf.jruby.RubyDescriptorPool.RubyDescriptorPool
RubyDescriptorPool(Ruby ruby, RubyClass klazz)
Definition: RubyDescriptorPool.java:61
cBuilder
VALUE cBuilder
com.google.protobuf.jruby.RubyDescriptorPool.addToSymtab
void addToSymtab(ThreadContext context, RubyDescriptor def)
Definition: RubyDescriptorPool.java:111
com.google.protobuf.jruby.Utils
Definition: Utils.java:49
com.google.protobuf.jruby.RubyDescriptor.setDescriptor
void setDescriptor(Descriptors.Descriptor descriptor)
Definition: RubyDescriptor.java:220
com.google.protobuf.jruby.RubyEnumDescriptor.getBuilder
DescriptorProtos.EnumDescriptorProto.Builder getBuilder()
Definition: RubyEnumDescriptor.java:163
com.google.protobuf.jruby.RubyFieldDescriptor.setSubType
void setSubType(IRubyObject rubyDescriptor)
Definition: RubyFieldDescriptor.java:237
com.google.protobuf.jruby.RubyDescriptorPool.buildFileDescriptor
void buildFileDescriptor(ThreadContext context)
Definition: RubyDescriptorPool.java:121
gen_gtest_pred_impl.Arity
def Arity(n)
Definition: gen_gtest_pred_impl.py:163
com.google.protobuf.Descriptors.FieldDescriptor.Type.MESSAGE
MESSAGE
Definition: Descriptors.java:1226
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.jruby.RubyDescriptorPool.lookup
IRubyObject lookup(ThreadContext context, IRubyObject name)
Definition: RubyDescriptorPool.java:89
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
com.google.protobuf.Descriptors.FieldDescriptor.Type
Definition: Descriptors.java:1215
com.google.protobuf.jruby.RubyDescriptorPool.initialize
IRubyObject initialize(ThreadContext context)
Definition: RubyDescriptorPool.java:66
com.google.protobuf.jruby.Utils.unescapeIdentifier
static String unescapeIdentifier(String name)
Definition: Utils.java:226
symtab
upb_symtab * symtab
Definition: php/ext/google/protobuf/protobuf.h:765
com.google.protobuf.jruby.RubyEnumDescriptor.getName
IRubyObject getName(ThreadContext context)
Definition: RubyEnumDescriptor.java:88
com.google.protobuf.jruby.RubyDescriptorPool.builder
DescriptorProtos.FileDescriptorProto.Builder builder
Definition: RubyDescriptorPool.java:168
com.google.protobuf.jruby.RubyDescriptorPool.cBuilder
RubyClass cBuilder
Definition: RubyDescriptorPool.java:166
com.google.protobuf.Descriptors.EnumDescriptor
Definition: Descriptors.java:1583
com.google.protobuf.jruby.RubyDescriptor.getName
IRubyObject getName(ThreadContext context)
Definition: RubyDescriptor.java:92
com.google.protobuf.jruby.RubyBuilder.finalizeToPool
IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool)
Definition: RubyBuilder.java:151
com.google.protobuf.Descriptors.FileDescriptor.buildFrom
static FileDescriptor buildFrom(final FileDescriptorProto proto, final FileDescriptor[] dependencies)
Definition: Descriptors.java:279
com.google.protobuf.jruby.RubyFieldDescriptor
Definition: RubyFieldDescriptor.java:45
com.google.protobuf.jruby.RubyEnumDescriptor
Definition: RubyEnumDescriptor.java:50
com.google.protobuf.jruby.RubyDescriptorPool.createRubyDescriptorPool
static void createRubyDescriptorPool(Ruby runtime)
Definition: RubyDescriptorPool.java:48
java
com.google.protobuf.Descriptors.Descriptor
Definition: Descriptors.java:629
com.google.protobuf.jruby.RubyDescriptorPool.descriptorPool
static RubyDescriptorPool descriptorPool
Definition: RubyDescriptorPool.java:164
com.google.protobuf.jruby.RubyDescriptor.lookup
IRubyObject lookup(ThreadContext context, IRubyObject fieldName)
Definition: RubyDescriptor.java:135
com.google
com
com.google.protobuf.jruby.RubyDescriptor
Definition: RubyDescriptor.java:50
com.google.protobuf.jruby.RubyDescriptorPool.addToSymtab
void addToSymtab(ThreadContext context, RubyEnumDescriptor def)
Definition: RubyDescriptorPool.java:116
com.google.protobuf::DescriptorProtos
com.google.protobuf.Descriptors.FileDescriptor
Definition: Descriptors.java:87
com.google.protobuf.jruby.RubyDescriptorPool
Definition: RubyDescriptorPool.java:47
com.google.protobuf.jruby.RubyDescriptorPool.symtab
Map< IRubyObject, IRubyObject > symtab
Definition: RubyDescriptorPool.java:167
com.google.protobuf.jruby.RubyFieldDescriptor.setFieldDef
void setFieldDef(Descriptors.FieldDescriptor fieldDescriptor)
Definition: RubyFieldDescriptor.java:241
com.google.protobuf.Descriptors.FieldDescriptor.Type.ENUM
ENUM
Definition: Descriptors.java:1229
com.google.protobuf.Descriptors.FieldDescriptor
Definition: Descriptors.java:949
com.google.protobuf.Descriptors.DescriptorValidationException
Definition: Descriptors.java:2138


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