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 35 import com.google.protobuf.CodedInputStream; 36 import com.google.protobuf.Descriptors.FileDescriptor; 37 import com.google.protobuf.Descriptors.GenericDescriptor; 38 import org.jruby.*; 39 import org.jruby.anno.JRubyClass; 40 import org.jruby.anno.JRubyMethod; 41 import org.jruby.runtime.Block; 42 import org.jruby.runtime.ObjectAllocator; 43 import org.jruby.runtime.ThreadContext; 44 import org.jruby.runtime.builtin.IRubyObject; 45 46 @JRubyClass(name = "FileDescriptor") 47 public class RubyFileDescriptor extends RubyObject { createRubyFileDescriptor(Ruby runtime)48 public static void createRubyFileDescriptor(Ruby runtime) { 49 RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf"); 50 cFileDescriptor = 51 mProtobuf.defineClassUnder( 52 "FileDescriptor", 53 runtime.getObject(), 54 new ObjectAllocator() { 55 @Override 56 public IRubyObject allocate(Ruby runtime, RubyClass klazz) { 57 return new RubyFileDescriptor(runtime, klazz); 58 } 59 }); 60 cFileDescriptor.defineAnnotatedMethods(RubyFileDescriptor.class); 61 } 62 getRubyFileDescriptor( ThreadContext context, GenericDescriptor descriptor)63 public static RubyFileDescriptor getRubyFileDescriptor( 64 ThreadContext context, GenericDescriptor descriptor) { 65 RubyFileDescriptor rfd = 66 (RubyFileDescriptor) cFileDescriptor.newInstance(context, Block.NULL_BLOCK); 67 rfd.fileDescriptor = descriptor.getFile(); 68 return rfd; 69 } 70 RubyFileDescriptor(Ruby runtime, RubyClass klazz)71 public RubyFileDescriptor(Ruby runtime, RubyClass klazz) { 72 super(runtime, klazz); 73 } 74 75 /* 76 * call-seq: 77 * FileDescriptor.name => name 78 * 79 * Returns the name of the file. 80 */ 81 @JRubyMethod(name = "name") getName(ThreadContext context)82 public IRubyObject getName(ThreadContext context) { 83 String name = fileDescriptor.getName(); 84 return name == null ? context.nil : context.runtime.newString(name); 85 } 86 87 @JRubyMethod options(ThreadContext context)88 public IRubyObject options(ThreadContext context) { 89 RubyDescriptorPool pool = (RubyDescriptorPool) RubyDescriptorPool.generatedPool(null, null); 90 RubyDescriptor fileOptionsDescriptor = 91 (RubyDescriptor) 92 pool.lookup(context, context.runtime.newString("google.protobuf.FileOptions")); 93 RubyClass fileOptionsClass = (RubyClass) fileOptionsDescriptor.msgclass(context); 94 RubyMessage msg = (RubyMessage) fileOptionsClass.newInstance(context, Block.NULL_BLOCK); 95 return msg.decodeBytes( 96 context, 97 msg, 98 CodedInputStream.newInstance( 99 fileDescriptor.getOptions().toByteString().toByteArray()), /*freeze*/ 100 true); 101 } 102 103 private static RubyClass cFileDescriptor; 104 105 private FileDescriptor fileDescriptor; 106 } 107