Merge pull request #9690 from JasonLunn/format_jruby_with_google-java-format

Run all JRuby source files through `google-java-format.`
pull/9697/head
Jason Lunn 3 years ago committed by GitHub
commit 3dee6efa54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java
  2. 45
      ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptorPool.java
  3. 27
      ruby/src/main/java/com/google/protobuf/jruby/RubyEnumDescriptor.java
  4. 17
      ruby/src/main/java/com/google/protobuf/jruby/RubyFieldDescriptor.java
  5. 13
      ruby/src/main/java/com/google/protobuf/jruby/RubyFileDescriptor.java
  6. 69
      ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
  7. 364
      ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
  8. 20
      ruby/src/main/java/com/google/protobuf/jruby/RubyOneofDescriptor.java
  9. 3
      ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java
  10. 49
      ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java
  11. 403
      ruby/src/main/java/com/google/protobuf/jruby/SentinelOuterClass.java
  12. 150
      ruby/src/main/java/com/google/protobuf/jruby/Utils.java
  13. 3
      ruby/src/main/java/google/ProtobufJavaService.java

@ -35,6 +35,8 @@ package com.google.protobuf.jruby;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import java.util.HashMap;
import java.util.Map;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
@ -44,15 +46,15 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.util.HashMap;
import java.util.Map;
@JRubyClass(name = "Descriptor", include = "Enumerable")
public class RubyDescriptor extends RubyObject {
public static void createRubyDescriptor(Ruby runtime) {
RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cDescriptor = protobuf.defineClassUnder("Descriptor", runtime.getObject(), new ObjectAllocator() {
RubyClass cDescriptor =
protobuf.defineClassUnder(
"Descriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyDescriptor(runtime, klazz);
@ -160,7 +162,8 @@ public class RubyDescriptor extends RubyObject {
return descriptor.findFieldByName(name);
}
protected void setDescriptor(ThreadContext context, Descriptor descriptor, RubyDescriptorPool pool) {
protected void setDescriptor(
ThreadContext context, Descriptor descriptor, RubyDescriptorPool pool) {
Ruby runtime = context.runtime;
Map<FieldDescriptor, RubyFieldDescriptor> cache = new HashMap();
this.descriptor = descriptor;
@ -170,14 +173,16 @@ public class RubyDescriptor extends RubyObject {
oneofDescriptors = new HashMap<IRubyObject, RubyOneofDescriptor>();
for (FieldDescriptor fieldDescriptor : descriptor.getFields()) {
RubyFieldDescriptor fd = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK);
RubyFieldDescriptor fd =
(RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK);
fd.setDescriptor(context, fieldDescriptor, pool);
fieldDescriptors.put(runtime.newString(fieldDescriptor.getName()), fd);
cache.put(fieldDescriptor, fd);
}
for (OneofDescriptor oneofDescriptor : descriptor.getRealOneofs()) {
RubyOneofDescriptor ood = (RubyOneofDescriptor) cOneofDescriptor.newInstance(context, Block.NULL_BLOCK);
RubyOneofDescriptor ood =
(RubyOneofDescriptor) cOneofDescriptor.newInstance(context, Block.NULL_BLOCK);
ood.setDescriptor(context, oneofDescriptor, cache);
oneofDescriptors.put(runtime.newString(oneofDescriptor.getName()), ood);
}
@ -193,7 +198,8 @@ public class RubyDescriptor extends RubyObject {
private RubyClass buildClassFromDescriptor(ThreadContext context) {
Ruby runtime = context.runtime;
ObjectAllocator allocator = new ObjectAllocator() {
ObjectAllocator allocator =
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyMessage(runtime, klazz, descriptor);

@ -38,6 +38,10 @@ import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
@ -45,16 +49,15 @@ import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.*;
import org.jruby.runtime.builtin.IRubyObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@JRubyClass(name = "DescriptorPool")
public class RubyDescriptorPool extends RubyObject {
public static void createRubyDescriptorPool(Ruby runtime) {
RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cDescriptorPool = protobuf.defineClassUnder("DescriptorPool", runtime.getObject(), new ObjectAllocator() {
RubyClass cDescriptorPool =
protobuf.defineClassUnder(
"DescriptorPool",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyDescriptorPool(runtime, klazz);
@ -62,7 +65,9 @@ public class RubyDescriptorPool extends RubyObject {
});
cDescriptorPool.defineAnnotatedMethods(RubyDescriptorPool.class);
descriptorPool = (RubyDescriptorPool) cDescriptorPool.newInstance(runtime.getCurrentContext(), Block.NULL_BLOCK);
descriptorPool =
(RubyDescriptorPool)
cDescriptorPool.newInstance(runtime.getCurrentContext(), Block.NULL_BLOCK);
cDescriptor = (RubyClass) runtime.getClassFromPath("Google::Protobuf::Descriptor");
cEnumDescriptor = (RubyClass) runtime.getClassFromPath("Google::Protobuf::EnumDescriptor");
}
@ -75,7 +80,8 @@ public class RubyDescriptorPool extends RubyObject {
@JRubyMethod
public IRubyObject build(ThreadContext context, Block block) {
RubyClass cBuilder = (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::Internal::Builder");
RubyClass cBuilder =
(RubyClass) context.runtime.getClassFromPath("Google::Protobuf::Internal::Builder");
RubyBasicObject ctx = (RubyBasicObject) cBuilder.newInstance(context, this, Block.NULL_BLOCK);
ctx.instance_eval(context, block);
ctx.callMethod(context, "build"); // Needs to be called to support the deprecated syntax
@ -118,12 +124,16 @@ public class RubyDescriptorPool extends RubyObject {
FileDescriptorProto.Builder builder = FileDescriptorProto.newBuilder().mergeFrom(bin);
registerFileDescriptor(context, builder);
} catch (InvalidProtocolBufferException e) {
throw RaiseException.from(context.runtime, (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError"), e.getMessage());
throw RaiseException.from(
context.runtime,
(RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError"),
e.getMessage());
}
return context.nil;
}
protected void registerFileDescriptor(ThreadContext context, FileDescriptorProto.Builder builder) {
protected void registerFileDescriptor(
ThreadContext context, FileDescriptorProto.Builder builder) {
final FileDescriptor fd;
try {
fd = FileDescriptor.buildFrom(builder.build(), existingFileDescriptors());
@ -138,7 +148,8 @@ public class RubyDescriptorPool extends RubyObject {
// Need to make sure enums are registered first in case anything references them
for (EnumDescriptor ed : fd.getEnumTypes()) registerEnumDescriptor(context, ed, packageName);
for (Descriptor message : fd.getMessageTypes()) registerDescriptor(context, message, packageName);
for (Descriptor message : fd.getMessageTypes())
registerDescriptor(context, message, packageName);
// Mark this as a loaded file
fileDescriptors.add(fd);
@ -155,13 +166,17 @@ public class RubyDescriptorPool extends RubyObject {
symtab.put(name, des);
// Need to make sure enums are registered first in case anything references them
for (EnumDescriptor ed : descriptor.getEnumTypes()) registerEnumDescriptor(context, ed, fullPath);
for (Descriptor message : descriptor.getNestedTypes()) registerDescriptor(context, message, fullPath);
for (EnumDescriptor ed : descriptor.getEnumTypes())
registerEnumDescriptor(context, ed, fullPath);
for (Descriptor message : descriptor.getNestedTypes())
registerDescriptor(context, message, fullPath);
}
private void registerEnumDescriptor(ThreadContext context, EnumDescriptor descriptor, String parentPath) {
private void registerEnumDescriptor(
ThreadContext context, EnumDescriptor descriptor, String parentPath) {
RubyString name = context.runtime.newString(parentPath + descriptor.getName());
RubyEnumDescriptor des = (RubyEnumDescriptor) cEnumDescriptor.newInstance(context, Block.NULL_BLOCK);
RubyEnumDescriptor des =
(RubyEnumDescriptor) cEnumDescriptor.newInstance(context, Block.NULL_BLOCK);
des.setName(name);
des.setDescriptor(context, descriptor);
symtab.put(name, des);

@ -39,8 +39,8 @@ import com.google.protobuf.Descriptors.FileDescriptor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.RubyNumeric;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
@ -52,7 +52,11 @@ import org.jruby.runtime.builtin.IRubyObject;
public class RubyEnumDescriptor extends RubyObject {
public static void createRubyEnumDescriptor(Ruby runtime) {
RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cEnumDescriptor = mProtobuf.defineClassUnder("EnumDescriptor", runtime.getObject(), new ObjectAllocator() {
RubyClass cEnumDescriptor =
mProtobuf.defineClassUnder(
"EnumDescriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyEnumDescriptor(runtime, klazz);
@ -88,7 +92,10 @@ public class RubyEnumDescriptor extends RubyObject {
public IRubyObject each(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
for (EnumValueDescriptor enumValueDescriptor : descriptor.getValues()) {
block.yield(context, runtime.newArray(runtime.newSymbol(enumValueDescriptor.getName()),
block.yield(
context,
runtime.newArray(
runtime.newSymbol(enumValueDescriptor.getName()),
runtime.newFixnum(enumValueDescriptor.getNumber())));
}
return context.nil;
@ -152,14 +159,21 @@ public class RubyEnumDescriptor extends RubyObject {
Ruby runtime = context.runtime;
RubyModule enumModule = RubyModule.newModule(runtime);
boolean defaultValueRequiredButNotFound = descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3;
boolean defaultValueRequiredButNotFound =
descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3;
for (EnumValueDescriptor value : descriptor.getValues()) {
String name = value.getName();
// Make sure its a valid constant name before trying to create it
if (Character.isUpperCase(name.codePointAt(0))) {
enumModule.defineConstant(name, runtime.newFixnum(value.getNumber()));
} else {
runtime.getWarnings().warn("Enum value " + name + " does not start with an uppercase letter as is required for Ruby constants.");
runtime
.getWarnings()
.warn(
"Enum value "
+ name
+ " does not start with an uppercase letter as is required for Ruby"
+ " constants.");
}
if (value.getNumber() == 0) {
defaultValueRequiredButNotFound = false;
@ -167,7 +181,8 @@ public class RubyEnumDescriptor extends RubyObject {
}
if (defaultValueRequiredButNotFound) {
throw Utils.createTypeError(context, "Enum definition " + name + " does not contain a value for '0'");
throw Utils.createTypeError(
context, "Enum definition " + name + " does not contain a value for '0'");
}
enumModule.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
enumModule.defineAnnotatedMethods(RubyEnum.class);

@ -45,7 +45,11 @@ import org.jruby.runtime.builtin.IRubyObject;
public class RubyFieldDescriptor extends RubyObject {
public static void createRubyFieldDescriptor(Ruby runtime) {
RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cFieldDescriptor = mProtobuf.defineClassUnder("FieldDescriptor", runtime.getObject(), new ObjectAllocator() {
RubyClass cFieldDescriptor =
mProtobuf.defineClassUnder(
"FieldDescriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyFieldDescriptor(runtime, klazz);
@ -227,9 +231,14 @@ public class RubyFieldDescriptor extends RubyObject {
return context.nil;
}
protected void setDescriptor(ThreadContext context, FieldDescriptor descriptor, RubyDescriptorPool pool) {
if (descriptor.isRequired() && descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) {
throw Utils.createTypeError(context, descriptor.getName() + " is labeled required but required fields are unsupported in proto3");
protected void setDescriptor(
ThreadContext context, FieldDescriptor descriptor, RubyDescriptorPool pool) {
if (descriptor.isRequired()
&& descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) {
throw Utils.createTypeError(
context,
descriptor.getName()
+ " is labeled required but required fields are unsupported in proto3");
}
this.descriptor = descriptor;
this.name = context.runtime.newString(descriptor.getName());

@ -32,7 +32,6 @@
package com.google.protobuf.jruby;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor.Syntax.*;
import com.google.protobuf.Descriptors.GenericDescriptor;
@ -48,7 +47,11 @@ import org.jruby.runtime.builtin.IRubyObject;
public class RubyFileDescriptor extends RubyObject {
public static void createRubyFileDescriptor(Ruby runtime) {
RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
cFileDescriptor = mProtobuf.defineClassUnder("FileDescriptor", runtime.getObject(), new ObjectAllocator() {
cFileDescriptor =
mProtobuf.defineClassUnder(
"FileDescriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyFileDescriptor(runtime, klazz);
@ -57,8 +60,10 @@ public class RubyFileDescriptor extends RubyObject {
cFileDescriptor.defineAnnotatedMethods(RubyFileDescriptor.class);
}
public static RubyFileDescriptor getRubyFileDescriptor(ThreadContext context, GenericDescriptor descriptor) {
RubyFileDescriptor rfd = (RubyFileDescriptor) cFileDescriptor.newInstance(context, Block.NULL_BLOCK);
public static RubyFileDescriptor getRubyFileDescriptor(
ThreadContext context, GenericDescriptor descriptor) {
RubyFileDescriptor rfd =
(RubyFileDescriptor) cFileDescriptor.newInstance(context, Block.NULL_BLOCK);
rfd.fileDescriptor = descriptor.getFile();
return rfd;
}

@ -34,6 +34,13 @@ package com.google.protobuf.jruby;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.DynamicMessage;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
@ -43,19 +50,15 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@JRubyClass(name = "Map", include = "Enumerable")
public class RubyMap extends RubyObject {
public static void createRubyMap(Ruby runtime) {
RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cMap = protobuf.defineClassUnder("Map", runtime.getObject(), new ObjectAllocator() {
RubyClass cMap =
protobuf.defineClassUnder(
"Map",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby ruby, RubyClass rubyClass) {
return new RubyMap(ruby, rubyClass);
@ -165,9 +168,9 @@ public class RubyMap extends RubyObject {
key = Utils.checkType(context, keyType, "key", key, (RubyModule) valueTypeClass);
value = Utils.checkType(context, valueType, "value", value, (RubyModule) valueTypeClass);
IRubyObject symbol;
if (valueType == FieldDescriptor.Type.ENUM &&
Utils.isRubyNum(value) &&
! (symbol = RubyEnum.lookup(context, valueTypeClass, value)).isNil()) {
if (valueType == FieldDescriptor.Type.ENUM
&& Utils.isRubyNum(value)
&& !(symbol = RubyEnum.lookup(context, valueTypeClass, value)).isNil()) {
value = symbol;
}
this.table.put(key, value);
@ -202,17 +205,14 @@ public class RubyMap extends RubyObject {
*/
@JRubyMethod(name = "==")
public IRubyObject eq(ThreadContext context, IRubyObject _other) {
if (_other instanceof RubyHash)
return singleLevelHash(context).op_equal(context, _other);
if (_other instanceof RubyHash) return singleLevelHash(context).op_equal(context, _other);
RubyMap other = (RubyMap) _other;
if (this == other) return context.runtime.getTrue();
if (!typeCompatible(other) || this.table.size() != other.table.size())
return context.runtime.getFalse();
for (IRubyObject key : table.keySet()) {
if (! other.table.containsKey(key))
return context.runtime.getFalse();
if (! other.table.get(key).equals(table.get(key)))
return context.runtime.getFalse();
if (!other.table.containsKey(key)) return context.runtime.getFalse();
if (!other.table.get(key).equals(table.get(key))) return context.runtime.getFalse();
}
return context.runtime.getTrue();
}
@ -357,7 +357,8 @@ public class RubyMap extends RubyObject {
public RubyHash toHash(ThreadContext context) {
Map<IRubyObject, IRubyObject> mapForHash = new HashMap();
table.forEach((key, value) -> {
table.forEach(
(key, value) -> {
if (!value.isNil()) {
if (value.respondsTo("to_h")) {
value = Helpers.invoke(context, value, "to_h");
@ -389,7 +390,8 @@ public class RubyMap extends RubyObject {
return newMap;
}
protected List<DynamicMessage> build(ThreadContext context, RubyDescriptor descriptor, int depth, int recursionLimit) {
protected List<DynamicMessage> build(
ThreadContext context, RubyDescriptor descriptor, int depth, int recursionLimit) {
List<DynamicMessage> list = new ArrayList<DynamicMessage>();
RubyClass rubyClass = (RubyClass) descriptor.msgclass(context);
FieldDescriptor keyField = descriptor.getField("key");
@ -405,7 +407,10 @@ public class RubyMap extends RubyObject {
protected RubyMap mergeIntoSelf(final ThreadContext context, IRubyObject hashmap) {
if (hashmap instanceof RubyHash) {
((RubyHash) hashmap).visitAll(context, new RubyHash.Visitor() {
((RubyHash) hashmap)
.visitAll(
context,
new RubyHash.Visitor() {
@Override
public void visit(IRubyObject key, IRubyObject val) {
if (val instanceof RubyHash && !valueTypeClass.isNil()) {
@ -413,7 +418,8 @@ public class RubyMap extends RubyObject {
}
indexSet(context, key, val);
}
}, null);
},
null);
} else if (hashmap instanceof RubyMap) {
RubyMap other = (RubyMap) hashmap;
if (!typeCompatible(other)) {
@ -426,20 +432,27 @@ public class RubyMap extends RubyObject {
}
protected boolean typeCompatible(RubyMap other) {
return this.keyType == other.keyType &&
this.valueType == other.valueType &&
this.valueTypeClass == other.valueTypeClass;
return this.keyType == other.keyType
&& this.valueType == other.valueType
&& this.valueTypeClass == other.valueTypeClass;
}
private RubyMap newThisType(ThreadContext context) {
RubyMap newMap;
if (needTypeclass(valueType)) {
newMap = (RubyMap) metaClass.newInstance(context,
newMap =
(RubyMap)
metaClass.newInstance(
context,
Utils.fieldTypeToRuby(context, keyType),
Utils.fieldTypeToRuby(context, valueType),
valueTypeClass, Block.NULL_BLOCK);
valueTypeClass,
Block.NULL_BLOCK);
} else {
newMap = (RubyMap) metaClass.newInstance(context,
newMap =
(RubyMap)
metaClass.newInstance(
context,
Utils.fieldTypeToRuby(context, keyType),
Utils.fieldTypeToRuby(context, valueType),
Block.NULL_BLOCK);

@ -32,19 +32,25 @@
package com.google.protobuf.jruby;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.UnknownFieldSet;
import com.google.protobuf.util.JsonFormat;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jruby.*;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
@ -54,13 +60,6 @@ import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RubyMessage extends RubyObject {
private final String DEFAULT_VALUE = "google.protobuf.FieldDescriptorProto.default_value";
private final String TYPE = "type";
@ -97,27 +96,40 @@ public class RubyMessage extends RubyObject {
throw runtime.newArgumentError("expected Hash arguments.");
}
RubyHash hash = args[0].convertToHash();
hash.visitAll(context, new RubyHash.Visitor() {
hash.visitAll(
context,
new RubyHash.Visitor() {
@Override
public void visit(IRubyObject key, IRubyObject value) {
if (!(key instanceof RubySymbol) && !(key instanceof RubyString)) {
throw Utils.createTypeError(context,
"Expected string or symbols as hash keys in initialization map.");
throw Utils.createTypeError(
context, "Expected string or symbols as hash keys in initialization map.");
}
final FieldDescriptor fieldDescriptor = findField(context, key, ignoreUnknownFieldsOnInit);
final FieldDescriptor fieldDescriptor =
findField(context, key, ignoreUnknownFieldsOnInit);
if (value == null || value.isNil()) return;
if (fieldDescriptor.isMapField()) {
if (!(value instanceof RubyHash))
throw runtime.newArgumentError("Expected Hash object as initializer value for map field '" + key.asJavaString() + "' (given " + value.getMetaClass() + ").");
throw runtime.newArgumentError(
"Expected Hash object as initializer value for map field '"
+ key.asJavaString()
+ "' (given "
+ value.getMetaClass()
+ ").");
final RubyMap map = newMapForField(context, fieldDescriptor);
map.mergeIntoSelf(context, value);
fields.put(fieldDescriptor, map);
} else if (fieldDescriptor.isRepeated()) {
if (!(value instanceof RubyArray))
throw runtime.newArgumentError("Expected array as initializer value for repeated field '" + key.asJavaString() + "' (given " + value.getMetaClass() + ").");
throw runtime.newArgumentError(
"Expected array as initializer value for repeated field '"
+ key.asJavaString()
+ "' (given "
+ value.getMetaClass()
+ ").");
fields.put(fieldDescriptor, rubyToRepeatedField(context, fieldDescriptor, value));
} else {
OneofDescriptor oneof = fieldDescriptor.getContainingOneof();
@ -125,18 +137,20 @@ public class RubyMessage extends RubyObject {
oneofCases.put(oneof, fieldDescriptor);
}
if (value instanceof RubyHash && fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE) {
RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
if (value instanceof RubyHash
&& fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE) {
RubyDescriptor descriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
RubyClass typeClass = (RubyClass) descriptor.msgclass(context);
value = (IRubyObject) typeClass.newInstance(context, value, Block.NULL_BLOCK);
fields.put(fieldDescriptor, value);
} else {
indexSet(context, key, value);
}
}
}
}, null);
},
null);
}
return this;
}
@ -242,8 +256,7 @@ public class RubyMessage extends RubyObject {
@JRubyMethod(name = {"==", "eql?"})
public IRubyObject eq(ThreadContext context, IRubyObject other) {
Ruby runtime = context.runtime;
if (!(other instanceof RubyMessage))
return runtime.getFalse();
if (!(other instanceof RubyMessage)) return runtime.getFalse();
RubyMessage message = (RubyMessage) other;
if (descriptor != message.descriptor) {
return runtime.getFalse();
@ -280,7 +293,8 @@ public class RubyMessage extends RubyObject {
}
if (methodName.startsWith(CLEAR_PREFIX)) {
String strippedMethodName = methodName.substring(6);
oneofDescriptor = rubyDescriptor.lookupOneof(context, context.runtime.newSymbol(strippedMethodName));
oneofDescriptor =
rubyDescriptor.lookupOneof(context, context.runtime.newSymbol(strippedMethodName));
if (!oneofDescriptor.isNil()) {
return context.runtime.getTrue();
}
@ -292,27 +306,30 @@ public class RubyMessage extends RubyObject {
if (methodName.startsWith(HAS_PREFIX) && methodName.endsWith(QUESTION_MARK)) {
String strippedMethodName = methodName.substring(4, methodName.length() - 1);
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(strippedMethodName);
if (fieldDescriptor != null &&
(!proto3 || fieldDescriptor.getContainingOneof() == null || fieldDescriptor
.getContainingOneof().isSynthetic()) &&
fieldDescriptor.hasPresence()) {
if (fieldDescriptor != null
&& (!proto3
|| fieldDescriptor.getContainingOneof() == null
|| fieldDescriptor.getContainingOneof().isSynthetic())
&& fieldDescriptor.hasPresence()) {
return context.runtime.getTrue();
}
oneofDescriptor = rubyDescriptor.lookupOneof(context, RubyString.newString(context.runtime, strippedMethodName));
oneofDescriptor =
rubyDescriptor.lookupOneof(
context, RubyString.newString(context.runtime, strippedMethodName));
if (!oneofDescriptor.isNil()) {
return context.runtime.getTrue();
}
}
if (methodName.endsWith(AS_VALUE_SUFFIX)) {
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(
methodName.substring(0, methodName.length() - 9));
FieldDescriptor fieldDescriptor =
descriptor.findFieldByName(methodName.substring(0, methodName.length() - 9));
if (fieldDescriptor != null && isWrappable(fieldDescriptor)) {
return context.runtime.getTrue();
}
}
if (methodName.endsWith(CONST_SUFFIX)) {
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(
methodName.substring(0, methodName.length() - 6));
FieldDescriptor fieldDescriptor =
descriptor.findFieldByName(methodName.substring(0, methodName.length() - 6));
if (fieldDescriptor != null) {
if (fieldDescriptor.getType() == FieldDescriptor.Type.ENUM) {
return context.runtime.getTrue();
@ -337,7 +354,9 @@ public class RubyMessage extends RubyObject {
if (args.length == 2) {
includePrivate = context.runtime.getTrue().equals(args[1]);
}
return metaClass.respondsToMethod(methodName, includePrivate) ? context.runtime.getTrue() : context.runtime.getFalse();
return metaClass.respondsToMethod(methodName, includePrivate)
? context.runtime.getTrue()
: context.runtime.getFalse();
}
/*
@ -427,21 +446,25 @@ public class RubyMessage extends RubyObject {
}
} else if (methodName.startsWith(HAS_PREFIX) && methodName.endsWith(QUESTION_MARK)) {
methodName = methodName.substring(4, methodName.length() - 1); // Trim "has_" and "?" off the field name
methodName =
methodName.substring(
4, methodName.length() - 1); // Trim "has_" and "?" off the field name
oneofDescriptor = rubyDescriptor.lookupOneof(context, runtime.newSymbol(methodName));
if (!oneofDescriptor.isNil()) {
RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor;
return oneofCases.containsKey(rubyOneofDescriptor.getDescriptor()) ? runtime.getTrue() : runtime.getFalse();
return oneofCases.containsKey(rubyOneofDescriptor.getDescriptor())
? runtime.getTrue()
: runtime.getFalse();
}
fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null &&
(!proto3 || fieldDescriptor.getContainingOneof() == null || fieldDescriptor
.getContainingOneof().isSynthetic()) &&
fieldDescriptor.hasPresence()) {
return fields.containsKey(fieldDescriptor) ? runtime.getTrue()
: runtime.getFalse();
if (fieldDescriptor != null
&& (!proto3
|| fieldDescriptor.getContainingOneof() == null
|| fieldDescriptor.getContainingOneof().isSynthetic())
&& fieldDescriptor.hasPresence()) {
return fields.containsKey(fieldDescriptor) ? runtime.getTrue() : runtime.getFalse();
}
} else if (methodName.endsWith(AS_VALUE_SUFFIX)) {
@ -471,12 +494,14 @@ public class RubyMessage extends RubyObject {
RubyArray retValues = runtime.newArray(values.getLength());
for (int i = 0; i < values.getLength(); i++) {
String val = values.eltInternal(i).toString();
retValues.store((long) i, runtime.newFixnum(enumDescriptor.findValueByName(val).getNumber()));
retValues.store(
(long) i, runtime.newFixnum(enumDescriptor.findValueByName(val).getNumber()));
}
return retValues;
}
return runtime.newFixnum(enumDescriptor.findValueByName(enumValue.asJavaString()).getNumber());
return runtime.newFixnum(
enumDescriptor.findValueByName(enumValue.asJavaString()).getNumber());
}
}
}
@ -489,7 +514,8 @@ public class RubyMessage extends RubyObject {
return setFieldInternal(context, fieldDescriptor, args[1]);
}
IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, RubyString.newString(context.runtime, methodName));
IRubyObject oneofDescriptor =
rubyDescriptor.lookupOneof(context, RubyString.newString(context.runtime, methodName));
if (!oneofDescriptor.isNil()) {
throw runtime.newRuntimeError("Oneof accessors are read-only.");
}
@ -504,22 +530,23 @@ public class RubyMessage extends RubyObject {
return setFieldInternal(context, fieldDescriptor, args[1]);
}
RubyClass typeClass = (RubyClass) ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
RubyClass typeClass =
(RubyClass)
((RubyDescriptor) getDescriptorForField(context, fieldDescriptor))
.msgclass(context);
RubyMessage msg = (RubyMessage) typeClass.newInstance(context, Block.NULL_BLOCK);
msg.indexSet(context, runtime.newString("value"), args[1]);
return setFieldInternal(context, fieldDescriptor, msg);
}
}
}
return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK);
}
/**
* call-seq:
* Message.dup => new_message
* Performs a shallow copy of this message and returns the new copy.
* call-seq: Message.dup => new_message Performs a shallow copy of this message and returns the
* new copy.
*/
@JRubyMethod
public IRubyObject dup(ThreadContext context) {
@ -530,7 +557,9 @@ public class RubyMessage extends RubyObject {
} else if (fields.containsKey(fieldDescriptor)) {
dup.setFieldInternal(context, fieldDescriptor, fields.get(fieldDescriptor));
} else if (this.builder.hasField(fieldDescriptor)) {
dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor)));
dup.fields.put(
fieldDescriptor,
wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor)));
}
}
return dup;
@ -560,7 +589,8 @@ public class RubyMessage extends RubyObject {
@JRubyMethod(required = 1, optional = 1, meta = true)
public static IRubyObject encode(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
if (recv != args[0].getMetaClass()) {
throw context.runtime.newArgumentError("Tried to encode a " + args[0].getMetaClass() + " message with " + recv);
throw context.runtime.newArgumentError(
"Tried to encode a " + args[0].getMetaClass() + " message with " + recv);
}
RubyMessage message = (RubyMessage) args[0];
int recursionLimitInt = SINK_MAXIMUM_NESTING;
@ -573,7 +603,8 @@ public class RubyMessage extends RubyObject {
recursionLimitInt = ((RubyNumeric) recursionLimit).getIntValue();
}
}
return context.runtime.newString(new ByteList(message.build(context, 0, recursionLimitInt).toByteArray()));
return context.runtime.newString(
new ByteList(message.build(context, 0, recursionLimitInt).toByteArray()));
}
/*
@ -598,7 +629,8 @@ public class RubyMessage extends RubyObject {
throw context.runtime.newArgumentError("Expected hash arguments.");
}
IRubyObject recursionLimit = ((RubyHash) args[1]).fastARef(context.runtime.newSymbol("recursion_limit"));
IRubyObject recursionLimit =
((RubyHash) args[1]).fastARef(context.runtime.newSymbol("recursion_limit"));
if (recursionLimit != null) {
input.setRecursionLimit(((RubyNumeric) recursionLimit).getIntValue());
}
@ -607,17 +639,28 @@ public class RubyMessage extends RubyObject {
try {
ret.builder.mergeFrom(input);
} catch (Exception e) {
throw RaiseException.from(context.runtime, (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError"), e.getMessage());
throw RaiseException.from(
context.runtime,
(RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError"),
e.getMessage());
}
if (!ret.proto3) {
// Need to reset unknown values in repeated enum fields
ret.builder.getUnknownFields().asMap().forEach((i, values) -> {
ret.builder
.getUnknownFields()
.asMap()
.forEach(
(i, values) -> {
FieldDescriptor fd = ret.builder.getDescriptorForType().findFieldByNumber(i);
if (fd != null && fd.isRepeated() && fd.getType() == FieldDescriptor.Type.ENUM) {
EnumDescriptor ed = fd.getEnumType();
values.getVarintList().forEach(value -> {
ret.builder.addRepeatedField(fd, ed.findValueByNumberCreatingIfUnknown(value.intValue()));
values
.getVarintList()
.forEach(
value -> {
ret.builder.addRepeatedField(
fd, ed.findValueByNumberCreatingIfUnknown(value.intValue()));
});
}
});
@ -636,7 +679,8 @@ public class RubyMessage extends RubyObject {
* emit_defaults: set true to emit 0/false values (default is to omit them)
*/
@JRubyMethod(name = "encode_json", required = 1, optional = 1, meta = true)
public static IRubyObject encodeJson(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
public static IRubyObject encodeJson(
ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
RubyMessage message = (RubyMessage) args[0];
JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
@ -663,7 +707,9 @@ public class RubyMessage extends RubyObject {
printer = printer.preservingProtoFieldNames();
}
}
printer = printer.usingTypeRegistry(JsonFormat.TypeRegistry.newBuilder().add(message.descriptor).build());
printer =
printer.usingTypeRegistry(
JsonFormat.TypeRegistry.newBuilder().add(message.descriptor).build());
try {
result = printer.print(message.build(context, 0, SINK_MAXIMUM_NESTING));
@ -689,7 +735,8 @@ public class RubyMessage extends RubyObject {
* raise an error)
*/
@JRubyMethod(name = "decode_json", required = 1, optional = 1, meta = true)
public static IRubyObject decodeJson(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
public static IRubyObject decodeJson(
ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
boolean ignoreUnknownFields = false;
IRubyObject data = args[0];
@ -700,7 +747,8 @@ public class RubyMessage extends RubyObject {
throw runtime.newArgumentError("Expected hash arguments.");
}
IRubyObject ignoreSetting = ((RubyHash) args[1]).fastARef(runtime.newSymbol("ignore_unknown_fields"));
IRubyObject ignoreSetting =
((RubyHash) args[1]).fastARef(runtime.newSymbol("ignore_unknown_fields"));
if (ignoreSetting != null && ignoreSetting.isTrue()) {
parser = parser.ignoringUnknownFields();
}
@ -711,7 +759,8 @@ public class RubyMessage extends RubyObject {
}
RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK);
parser = parser.usingTypeRegistry(JsonFormat.TypeRegistry.newBuilder().add(ret.descriptor).build());
parser =
parser.usingTypeRegistry(JsonFormat.TypeRegistry.newBuilder().add(ret.descriptor).build());
try {
parser.merge(data.asJavaString(), ret.builder);
@ -720,7 +769,8 @@ public class RubyMessage extends RubyObject {
}
if (isWrapper(ret.descriptor)) {
throw runtime.newRuntimeError("Parsing a wrapper type from JSON at the top level does not work.");
throw runtime.newRuntimeError(
"Parsing a wrapper type from JSON at the top level does not work.");
}
return ret;
@ -735,7 +785,8 @@ public class RubyMessage extends RubyObject {
if (!value.isNil()) {
if (fdef.isRepeated() && !fdef.isMapField()) {
if (!proto3 && ((RubyRepeatedField) value).size() == 0) continue; // Don't output empty repeated fields for proto2
if (!proto3 && ((RubyRepeatedField) value).size() == 0)
continue; // Don't output empty repeated fields for proto2
if (fdef.getType() != FieldDescriptor.Type.MESSAGE) {
value = Helpers.invoke(context, value, "to_a");
} else {
@ -773,8 +824,10 @@ public class RubyMessage extends RubyObject {
if (value instanceof RubyMap) {
builder.clearField(fieldDescriptor);
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
RubyDescriptor mapDescriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
for (DynamicMessage kv :
((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
builder.addRepeatedField(fieldDescriptor, kv);
}
@ -783,18 +836,23 @@ public class RubyMessage extends RubyObject {
builder.clearField(fieldDescriptor);
for (int i = 0; i < repeatedField.size(); i++) {
Object item = convert(context, fieldDescriptor, repeatedField.get(i), depth, recursionLimit,
Object item =
convert(
context,
fieldDescriptor,
repeatedField.get(i),
depth,
recursionLimit,
/*isDefaultValueForBytes*/ false);
builder.addRepeatedField(fieldDescriptor, item);
}
} else if (!value.isNil()) {
/**
* Detect the special case where default_value strings are provided for byte fields.
* If so, disable normal string encoding behavior within convert.
* For a more detailed explanation of other possible workarounds, see the comments
* above {@code com.google.protobuf.Internal#stringDefaultValue()
* stringDefaultValue}.
* Detect the special case where default_value strings are provided for byte fields. If so,
* disable normal string encoding behavior within convert. For a more detailed explanation
* of other possible workarounds, see the comments above {@code
* com.google.protobuf.Internal#stringDefaultValue() stringDefaultValue}.
*/
boolean isDefaultStringForBytes = false;
if (DEFAULT_VALUE.equals(fieldDescriptor.getFullName())) {
@ -804,7 +862,10 @@ public class RubyMessage extends RubyObject {
isDefaultStringForBytes = true;
}
}
builder.setField(fieldDescriptor, convert(context, fieldDescriptor, value, depth, recursionLimit, isDefaultStringForBytes));
builder.setField(
fieldDescriptor,
convert(
context, fieldDescriptor, value, depth, recursionLimit, isDefaultStringForBytes));
}
}
@ -822,9 +883,10 @@ public class RubyMessage extends RubyObject {
value = getFieldInternal(context, fieldDescriptor);
if (value instanceof RubyMap) {
builder.clearField(fieldDescriptor);
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context,
fieldDescriptor);
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
RubyDescriptor mapDescriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
for (DynamicMessage kv :
((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
builder.addRepeatedField(fieldDescriptor, kv);
}
}
@ -866,22 +928,30 @@ public class RubyMessage extends RubyObject {
if (!fieldDescriptor.hasPresence()) {
throw context.runtime.newArgumentError("does not track presence");
}
return fields.containsKey(fieldDescriptor) ? context.runtime.getTrue() : context.runtime.getFalse();
return fields.containsKey(fieldDescriptor)
? context.runtime.getTrue()
: context.runtime.getFalse();
}
protected IRubyObject setField(ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
protected IRubyObject setField(
ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
validateMessageType(context, fieldDescriptor, "set");
return setFieldInternal(context, fieldDescriptor, value);
}
private RubyRepeatedField getRepeatedField(ThreadContext context, FieldDescriptor fieldDescriptor) {
private RubyRepeatedField getRepeatedField(
ThreadContext context, FieldDescriptor fieldDescriptor) {
if (fields.containsKey(fieldDescriptor)) {
return (RubyRepeatedField) fields.get(fieldDescriptor);
}
int count = this.builder.getRepeatedFieldCount(fieldDescriptor);
RubyRepeatedField ret = repeatedFieldForFieldDescriptor(context, fieldDescriptor);
for (int i = 0; i < count; i++) {
ret.push(context, new IRubyObject[] {wrapField(context, fieldDescriptor, this.builder.getRepeatedField(fieldDescriptor, i))});
ret.push(
context,
new IRubyObject[] {
wrapField(context, fieldDescriptor, this.builder.getRepeatedField(fieldDescriptor, i))
});
}
fields.put(fieldDescriptor, ret);
return ret;
@ -902,11 +972,16 @@ public class RubyMessage extends RubyObject {
private void discardUnknownFields(ThreadContext context, Message.Builder messageBuilder) {
messageBuilder.setUnknownFields(UnknownFieldSet.getDefaultInstance());
messageBuilder.getAllFields().forEach((fd, value) -> {
messageBuilder
.getAllFields()
.forEach(
(fd, value) -> {
if (fd.getType() == FieldDescriptor.Type.MESSAGE) {
if (fd.isRepeated()) {
messageBuilder.clearField(fd);
((List) value).forEach((val) -> {
((List) value)
.forEach(
(val) -> {
Message.Builder submessageBuilder = ((DynamicMessage) val).toBuilder();
discardUnknownFields(context, submessageBuilder);
messageBuilder.addRepeatedField(fd, submessageBuilder.build());
@ -924,7 +999,8 @@ public class RubyMessage extends RubyObject {
return findField(context, fieldName, false);
}
private FieldDescriptor findField(ThreadContext context, IRubyObject fieldName, boolean ignoreUnknownField) {
private FieldDescriptor findField(
ThreadContext context, IRubyObject fieldName, boolean ignoreUnknownField) {
String nameStr = fieldName.asJavaString();
FieldDescriptor ret = this.descriptor.findFieldByName(nameStr);
if (ret == null && !ignoreUnknownField) {
@ -934,9 +1010,12 @@ public class RubyMessage extends RubyObject {
}
// convert a ruby object to protobuf type, skip type check since it is checked on the way in
private Object convert(ThreadContext context,
private Object convert(
ThreadContext context,
FieldDescriptor fieldDescriptor,
IRubyObject value, int depth, int recursionLimit,
IRubyObject value,
int depth,
int recursionLimit,
boolean isDefaultStringForBytes) {
Object val = null;
switch (fieldDescriptor.getType()) {
@ -997,16 +1076,19 @@ public class RubyMessage extends RubyObject {
private static RaiseException createParseError(ThreadContext context, String message) {
if (parseErrorClass == null) {
parseErrorClass = (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError");
parseErrorClass =
(RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError");
}
return RaiseException.from(context.runtime, parseErrorClass, message);
}
private IRubyObject wrapField(ThreadContext context, FieldDescriptor fieldDescriptor, Object value) {
private IRubyObject wrapField(
ThreadContext context, FieldDescriptor fieldDescriptor, Object value) {
return wrapField(context, fieldDescriptor, value, false);
}
private IRubyObject wrapField(ThreadContext context, FieldDescriptor fieldDescriptor, Object value, boolean encodeBytes) {
private IRubyObject wrapField(
ThreadContext context, FieldDescriptor fieldDescriptor, Object value, boolean encodeBytes) {
if (value == null) {
return context.runtime.getNil();
}
@ -1030,7 +1112,10 @@ public class RubyMessage extends RubyObject {
case STRING:
return Utils.wrapPrimaryValue(context, fieldDescriptor.getType(), value, encodeBytes);
case MESSAGE:
RubyClass typeClass = (RubyClass) ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
RubyClass typeClass =
(RubyClass)
((RubyDescriptor) getDescriptorForField(context, fieldDescriptor))
.msgclass(context);
RubyMessage msg = (RubyMessage) typeClass.newInstance(context, Block.NULL_BLOCK);
return msg.buildFrom(context, (DynamicMessage) value);
case ENUM:
@ -1044,7 +1129,8 @@ public class RubyMessage extends RubyObject {
}
}
private RubyRepeatedField repeatedFieldForFieldDescriptor(ThreadContext context, FieldDescriptor fieldDescriptor) {
private RubyRepeatedField repeatedFieldForFieldDescriptor(
ThreadContext context, FieldDescriptor fieldDescriptor) {
IRubyObject typeClass = context.runtime.getNilClass();
IRubyObject descriptor = getDescriptorForField(context, fieldDescriptor);
FieldDescriptor.Type type = fieldDescriptor.getType();
@ -1056,7 +1142,8 @@ public class RubyMessage extends RubyObject {
typeClass = ((RubyEnumDescriptor) descriptor).enummodule(context);
}
RubyRepeatedField field = new RubyRepeatedField(context.runtime, cRepeatedField, type, typeClass);
RubyRepeatedField field =
new RubyRepeatedField(context.runtime, cRepeatedField, type, typeClass);
field.setName(fieldDescriptor.getName());
return field;
@ -1066,8 +1153,8 @@ public class RubyMessage extends RubyObject {
return getFieldInternal(context, fieldDescriptor, true);
}
private IRubyObject getFieldInternal(ThreadContext context, FieldDescriptor fieldDescriptor,
boolean returnDefaults) {
private IRubyObject getFieldInternal(
ThreadContext context, FieldDescriptor fieldDescriptor, boolean returnDefaults) {
OneofDescriptor oneofDescriptor = fieldDescriptor.getContainingOneof();
if (oneofDescriptor != null) {
if (oneofCases.get(oneofDescriptor) == fieldDescriptor) {
@ -1077,7 +1164,8 @@ public class RubyMessage extends RubyObject {
if (oneofCase != null) {
Object builderValue = builder.getField(oneofCase);
if (builderValue != null) {
boolean encodeBytes = oneofCase.hasDefaultValue() && builderValue.equals(oneofCase.getDefaultValue());
boolean encodeBytes =
oneofCase.hasDefaultValue() && builderValue.equals(oneofCase.getDefaultValue());
value = wrapField(context, oneofCase, builderValue, encodeBytes);
}
}
@ -1092,17 +1180,16 @@ public class RubyMessage extends RubyObject {
} else {
FieldDescriptor oneofCase = builder.getOneofFieldDescriptor(oneofDescriptor);
if (oneofCase != fieldDescriptor) {
if (fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE
|| !returnDefaults) {
if (fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE || !returnDefaults) {
return context.nil;
} else {
return wrapField(context, fieldDescriptor,
fieldDescriptor.getDefaultValue(), true);
return wrapField(context, fieldDescriptor, fieldDescriptor.getDefaultValue(), true);
}
}
if (returnDefaults || builder.hasField(fieldDescriptor)) {
Object rawValue = builder.getField(oneofCase);
boolean encodeBytes = oneofCase.hasDefaultValue() && rawValue.equals(oneofCase.getDefaultValue());
boolean encodeBytes =
oneofCase.hasDefaultValue() && rawValue.equals(oneofCase.getDefaultValue());
IRubyObject value = wrapField(context, oneofCase, rawValue, encodeBytes);
fields.put(fieldDescriptor, value);
return value;
@ -1119,13 +1206,18 @@ public class RubyMessage extends RubyObject {
int mapSize = this.builder.getRepeatedFieldCount(fieldDescriptor);
FieldDescriptor keyField = fieldDescriptor.getMessageType().findFieldByNumber(1);
FieldDescriptor valueField = fieldDescriptor.getMessageType().findFieldByNumber(2);
RubyDescriptor kvDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
RubyDescriptor kvDescriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
RubyClass kvClass = (RubyClass) kvDescriptor.msgclass(context);
for (int i = 0; i < mapSize; i++) {
RubyMessage kvMessage = (RubyMessage) kvClass.newInstance(context, Block.NULL_BLOCK);
DynamicMessage message = (DynamicMessage) this.builder.getRepeatedField(fieldDescriptor, i);
DynamicMessage message =
(DynamicMessage) this.builder.getRepeatedField(fieldDescriptor, i);
kvMessage.buildFrom(context, message);
map.indexSet(context, kvMessage.getField(context, keyField), kvMessage.getField(context, valueField));
map.indexSet(
context,
kvMessage.getField(context, keyField),
kvMessage.getField(context, valueField));
}
fields.put(fieldDescriptor, map);
}
@ -1136,13 +1228,15 @@ public class RubyMessage extends RubyObject {
return getRepeatedField(context, fieldDescriptor);
}
if (fieldDescriptor.getType() != FieldDescriptor.Type.MESSAGE ||
builder.hasField(fieldDescriptor) || fields.containsKey(fieldDescriptor)) {
if (fieldDescriptor.getType() != FieldDescriptor.Type.MESSAGE
|| builder.hasField(fieldDescriptor)
|| fields.containsKey(fieldDescriptor)) {
if (fields.containsKey(fieldDescriptor)) {
return fields.get(fieldDescriptor);
} else if (returnDefaults || builder.hasField(fieldDescriptor)) {
Object rawValue = builder.getField(fieldDescriptor);
boolean encodeBytes = fieldDescriptor.hasDefaultValue() && rawValue.equals(fieldDescriptor.getDefaultValue());
boolean encodeBytes =
fieldDescriptor.hasDefaultValue() && rawValue.equals(fieldDescriptor.getDefaultValue());
IRubyObject value = wrapField(context, fieldDescriptor, rawValue, encodeBytes);
if (builder.hasField(fieldDescriptor)) {
fields.put(fieldDescriptor, value);
@ -1153,7 +1247,8 @@ public class RubyMessage extends RubyObject {
return context.nil;
}
private IRubyObject setFieldInternal(ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
private IRubyObject setFieldInternal(
ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
testFrozen("can't modify frozen " + getMetaClass());
if (fieldDescriptor.isMapField()) {
@ -1178,12 +1273,15 @@ public class RubyMessage extends RubyObject {
// Determine the typeclass, if any
IRubyObject typeClass = context.runtime.getObject();
if (fieldType == FieldDescriptor.Type.MESSAGE) {
typeClass = ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
typeClass =
((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
if (value.isNil()) {
addValue = false;
}
} else if (fieldType == FieldDescriptor.Type.ENUM) {
typeClass = ((RubyEnumDescriptor) getDescriptorForField(context, fieldDescriptor)).enummodule(context);
typeClass =
((RubyEnumDescriptor) getDescriptorForField(context, fieldDescriptor))
.enummodule(context);
value = enumToSymbol(context, fieldDescriptor.getEnumType(), value);
}
@ -1207,7 +1305,9 @@ public class RubyMessage extends RubyObject {
}
if (addValue) {
value = Utils.checkType(context, fieldType, fieldDescriptor.getName(), value, (RubyModule) typeClass);
value =
Utils.checkType(
context, fieldType, fieldDescriptor.getName(), value, (RubyModule) typeClass);
fields.put(fieldDescriptor, value);
} else {
fields.remove(fieldDescriptor);
@ -1216,17 +1316,22 @@ public class RubyMessage extends RubyObject {
return context.nil;
}
private IRubyObject getDescriptorForField(ThreadContext context, FieldDescriptor fieldDescriptor) {
private IRubyObject getDescriptorForField(
ThreadContext context, FieldDescriptor fieldDescriptor) {
RubyDescriptor thisRbDescriptor = (RubyDescriptor) getDescriptor(context, metaClass);
RubyFieldDescriptor fd = (RubyFieldDescriptor) thisRbDescriptor.lookup(context, context.runtime.newString(fieldDescriptor.getName()));
RubyFieldDescriptor fd =
(RubyFieldDescriptor)
thisRbDescriptor.lookup(context, context.runtime.newString(fieldDescriptor.getName()));
return fd.getSubtype(context);
}
private IRubyObject enumToSymbol(ThreadContext context, EnumDescriptor enumDescriptor, IRubyObject value) {
private IRubyObject enumToSymbol(
ThreadContext context, EnumDescriptor enumDescriptor, IRubyObject value) {
if (value instanceof RubySymbol) {
return (RubySymbol) value;
} else if (Utils.isRubyNum(value)) {
EnumValueDescriptor enumValue = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value));
EnumValueDescriptor enumValue =
enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value));
if (enumValue.getIndex() != -1) {
return context.runtime.newSymbol(enumValue.getName());
} else {
@ -1239,8 +1344,8 @@ public class RubyMessage extends RubyObject {
return context.runtime.newSymbol("UNKNOWN");
}
private RubyRepeatedField rubyToRepeatedField(ThreadContext context,
FieldDescriptor fieldDescriptor, IRubyObject value) {
private RubyRepeatedField rubyToRepeatedField(
ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
RubyArray arr = value.convertToArray();
RubyRepeatedField repeatedField = repeatedFieldForFieldDescriptor(context, fieldDescriptor);
IRubyObject[] values = new IRubyObject[arr.size()];
@ -1251,7 +1356,8 @@ public class RubyMessage extends RubyObject {
RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
typeClass = (RubyModule) descriptor.msgclass(context);
} else if (fieldType == FieldDescriptor.Type.ENUM) {
RubyEnumDescriptor enumDescriptor = (RubyEnumDescriptor) getDescriptorForField(context, fieldDescriptor);
RubyEnumDescriptor enumDescriptor =
(RubyEnumDescriptor) getDescriptorForField(context, fieldDescriptor);
typeClass = (RubyModule) enumDescriptor.enummodule(context);
}
@ -1283,18 +1389,25 @@ public class RubyMessage extends RubyObject {
IRubyObject valueType = RubySymbol.newSymbol(context.runtime, valueField.getType().name());
if (valueField.getType() == FieldDescriptor.Type.MESSAGE) {
RubyFieldDescriptor rubyFieldDescriptor = (RubyFieldDescriptor) mapDescriptor.lookup(context,
context.runtime.newString("value"));
RubyFieldDescriptor rubyFieldDescriptor =
(RubyFieldDescriptor) mapDescriptor.lookup(context, context.runtime.newString("value"));
RubyDescriptor rubyDescriptor = (RubyDescriptor) rubyFieldDescriptor.getSubtype(context);
return (RubyMap) cMap.newInstance(context, keyType, valueType,
rubyDescriptor.msgclass(context), Block.NULL_BLOCK);
return (RubyMap)
cMap.newInstance(
context, keyType, valueType, rubyDescriptor.msgclass(context), Block.NULL_BLOCK);
} else if (valueField.getType() == FieldDescriptor.Type.ENUM) {
RubyFieldDescriptor rubyFieldDescriptor = (RubyFieldDescriptor) mapDescriptor.lookup(context,
context.runtime.newString("value"));
RubyEnumDescriptor rubyEnumDescriptor = (RubyEnumDescriptor) rubyFieldDescriptor.getSubtype(context);
return (RubyMap) cMap.newInstance(context, keyType, valueType,
rubyEnumDescriptor.enummodule(context), Block.NULL_BLOCK);
RubyFieldDescriptor rubyFieldDescriptor =
(RubyFieldDescriptor) mapDescriptor.lookup(context, context.runtime.newString("value"));
RubyEnumDescriptor rubyEnumDescriptor =
(RubyEnumDescriptor) rubyFieldDescriptor.getSubtype(context);
return (RubyMap)
cMap.newInstance(
context,
keyType,
valueType,
rubyEnumDescriptor.enummodule(context),
Block.NULL_BLOCK);
} else {
return (RubyMap) cMap.newInstance(context, keyType, valueType, Block.NULL_BLOCK);
@ -1324,7 +1437,8 @@ public class RubyMessage extends RubyObject {
}
}
private void validateMessageType(ThreadContext context, FieldDescriptor fieldDescriptor, String methodName) {
private void validateMessageType(
ThreadContext context, FieldDescriptor fieldDescriptor, String methodName) {
if (descriptor != fieldDescriptor.getContainingType()) {
throw Utils.createTypeError(context, methodName + " method called on wrong message type");
}

@ -2,6 +2,10 @@ package com.google.protobuf.jruby;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
@ -13,17 +17,16 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@JRubyClass(name = "OneofDescriptor", include = "Enumerable")
public class RubyOneofDescriptor extends RubyObject {
public static void createRubyOneofDescriptor(Ruby runtime) {
RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cRubyOneofDescriptor = protobuf.defineClassUnder("OneofDescriptor", runtime.getObject(), new ObjectAllocator() {
RubyClass cRubyOneofDescriptor =
protobuf.defineClassUnder(
"OneofDescriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby ruby, RubyClass rubyClass) {
return new RubyOneofDescriptor(ruby, rubyClass);
@ -71,7 +74,10 @@ public class RubyOneofDescriptor extends RubyObject {
return descriptor;
}
protected void setDescriptor(ThreadContext context, OneofDescriptor descriptor, Map<FieldDescriptor, RubyFieldDescriptor> fieldCache) {
protected void setDescriptor(
ThreadContext context,
OneofDescriptor descriptor,
Map<FieldDescriptor, RubyFieldDescriptor> fieldCache) {
this.descriptor = descriptor;
this.name = context.runtime.newString(descriptor.getName());

@ -75,7 +75,8 @@ public class RubyProtobuf {
* unknown fields in submessages.
*/
@JRubyMethod(name = "discard_unknown", meta = true)
public static IRubyObject discardUnknown(ThreadContext context, IRubyObject self, IRubyObject message) {
public static IRubyObject discardUnknown(
ThreadContext context, IRubyObject self, IRubyObject message) {
((RubyMessage) message).discardUnknownFields(context);
return context.nil;
}

@ -40,13 +40,15 @@ import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.util.Arrays;
@JRubyClass(name = "RepeatedClass", include = "Enumerable")
public class RubyRepeatedField extends RubyObject {
public static void createRubyRepeatedField(Ruby runtime) {
RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cRepeatedField = mProtobuf.defineClassUnder("RepeatedField", runtime.getObject(),
RubyClass cRepeatedField =
mProtobuf.defineClassUnder(
"RepeatedField",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
@ -61,7 +63,8 @@ public class RubyRepeatedField extends RubyObject {
super(runtime, klazz);
}
public RubyRepeatedField(Ruby runtime, RubyClass klazz, FieldDescriptor.Type fieldType, IRubyObject typeClass) {
public RubyRepeatedField(
Ruby runtime, RubyClass klazz, FieldDescriptor.Type fieldType, IRubyObject typeClass) {
this(runtime, klazz);
this.fieldType = fieldType;
this.storage = runtime.newArray();
@ -77,19 +80,15 @@ public class RubyRepeatedField extends RubyObject {
throw runtime.newArgumentError("Expected Symbol for type name");
}
this.fieldType = Utils.rubyToFieldType(args[0]);
if (fieldType == FieldDescriptor.Type.MESSAGE
|| fieldType == FieldDescriptor.Type.ENUM) {
if (fieldType == FieldDescriptor.Type.MESSAGE || fieldType == FieldDescriptor.Type.ENUM) {
if (args.length < 2)
throw runtime.newArgumentError("Expected at least 2 arguments for message/enum");
typeClass = args[1];
if (args.length > 2)
ary = args[2];
if (args.length > 2) ary = args[2];
Utils.validateTypeClass(context, fieldType, typeClass);
} else {
if (args.length > 2)
throw runtime.newArgumentError("Too many arguments: expected 1 or 2");
if (args.length > 1)
ary = args[1];
if (args.length > 2) throw runtime.newArgumentError("Too many arguments: expected 1 or 2");
if (args.length > 1) ary = args[1];
}
if (ary != null) {
RubyArray arr = ary.convertToArray();
@ -125,7 +124,10 @@ public class RubyRepeatedField extends RubyObject {
*
* Accesses the element at the given index. Returns nil on out-of-bounds
*/
@JRubyMethod(required=1, optional=1, name = {"at", "[]"})
@JRubyMethod(
required = 1,
optional = 1,
name = {"at", "[]"})
public IRubyObject index(ThreadContext context, IRubyObject[] args) {
if (args.length == 1) {
IRubyObject arg = args[0];
@ -165,7 +167,10 @@ public class RubyRepeatedField extends RubyObject {
*
* Adds a new element to the repeated field.
*/
@JRubyMethod(name = {"push", "<<"}, required = 1, rest = true)
@JRubyMethod(
name = {"push", "<<"},
required = 1,
rest = true)
public IRubyObject push(ThreadContext context, IRubyObject[] args) {
for (int i = 0; i < args.length; i++) {
IRubyObject val = args[i];
@ -241,9 +246,10 @@ public class RubyRepeatedField extends RubyObject {
dup.storage.addAll((RubyArray) list);
} else {
RubyRepeatedField repeatedField = (RubyRepeatedField) list;
if (! fieldType.equals(repeatedField.fieldType) || (typeClass != null && !
typeClass.equals(repeatedField.typeClass)))
throw context.runtime.newArgumentError("Attempt to append RepeatedField with different element type.");
if (!fieldType.equals(repeatedField.fieldType)
|| (typeClass != null && !typeClass.equals(repeatedField.typeClass)))
throw context.runtime.newArgumentError(
"Attempt to append RepeatedField with different element type.");
dup.storage.addAll((RubyArray) repeatedField.toArray(context));
}
return dup;
@ -262,9 +268,10 @@ public class RubyRepeatedField extends RubyObject {
this.storage.addAll((RubyArray) list);
} else {
RubyRepeatedField repeatedField = (RubyRepeatedField) list;
if (! fieldType.equals(repeatedField.fieldType) || (typeClass != null && !
typeClass.equals(repeatedField.typeClass)))
throw context.runtime.newArgumentError("Attempt to append RepeatedField with different element type.");
if (!fieldType.equals(repeatedField.fieldType)
|| (typeClass != null && !typeClass.equals(repeatedField.typeClass)))
throw context.runtime.newArgumentError(
"Attempt to append RepeatedField with different element type.");
this.storage.addAll((RubyArray) repeatedField.toArray(context));
}
return this;
@ -310,7 +317,6 @@ public class RubyRepeatedField extends RubyObject {
return this;
}
@JRubyMethod(name = {"to_ary", "to_a"})
public IRubyObject toArray(ThreadContext context) {
return this.storage;
@ -341,7 +347,8 @@ public class RubyRepeatedField extends RubyObject {
}
protected RubyRepeatedField deepCopy(ThreadContext context) {
RubyRepeatedField copy = new RubyRepeatedField(context.runtime, metaClass, fieldType, typeClass);
RubyRepeatedField copy =
new RubyRepeatedField(context.runtime, metaClass, fieldType, typeClass);
for (int i = 0; i < size(); i++) {
IRubyObject value = storage.eltInternal(i);
if (fieldType == FieldDescriptor.Type.MESSAGE) {

@ -37,74 +37,53 @@ package com.google.protobuf.jruby;
public final class SentinelOuterClass {
private SentinelOuterClass() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface SentinelOrBuilder extends
public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) {}
public interface SentinelOrBuilder
extends
// @@protoc_insertion_point(interface_extends:com.google.protobuf.jruby.Sentinel)
com.google.protobuf.MessageOrBuilder {
/**
* <code>optional int32 default_int32 = 1;</code>
*/
/** <code>optional int32 default_int32 = 1;</code> */
int getDefaultInt32();
/**
* <code>optional int64 default_int64 = 2;</code>
*/
/** <code>optional int64 default_int64 = 2;</code> */
long getDefaultInt64();
/**
* <code>optional uint32 default_unit32 = 3;</code>
*/
/** <code>optional uint32 default_unit32 = 3;</code> */
int getDefaultUnit32();
/**
* <code>optional uint64 default_uint64 = 4;</code>
*/
/** <code>optional uint64 default_uint64 = 4;</code> */
long getDefaultUint64();
/**
* <code>optional string default_string = 5;</code>
*/
/** <code>optional string default_string = 5;</code> */
java.lang.String getDefaultString();
/**
* <code>optional string default_string = 5;</code>
*/
com.google.protobuf.ByteString
getDefaultStringBytes();
/** <code>optional string default_string = 5;</code> */
com.google.protobuf.ByteString getDefaultStringBytes();
/**
* <code>optional bool default_bool = 6;</code>
*/
/** <code>optional bool default_bool = 6;</code> */
boolean getDefaultBool();
/**
* <code>optional float default_float = 7;</code>
*/
/** <code>optional float default_float = 7;</code> */
float getDefaultFloat();
/**
* <code>optional double default_double = 8;</code>
*/
/** <code>optional double default_double = 8;</code> */
double getDefaultDouble();
/**
* <code>optional bytes default_bytes = 9;</code>
*/
/** <code>optional bytes default_bytes = 9;</code> */
com.google.protobuf.ByteString getDefaultBytes();
}
/**
* Protobuf type {@code com.google.protobuf.jruby.Sentinel}
*/
public static final class Sentinel extends
com.google.protobuf.GeneratedMessage implements
/** Protobuf type {@code com.google.protobuf.jruby.Sentinel} */
public static final class Sentinel extends com.google.protobuf.GeneratedMessage
implements
// @@protoc_insertion_point(message_implements:com.google.protobuf.jruby.Sentinel)
SentinelOrBuilder {
// Use Sentinel.newBuilder() to construct.
private Sentinel(com.google.protobuf.GeneratedMessage.Builder builder) {
super(builder);
}
private Sentinel() {
defaultInt32_ = 0;
defaultInt64_ = 0L;
@ -118,20 +97,22 @@ public final class SentinelOuterClass {
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
public final com.google.protobuf.UnknownFieldSet getUnknownFields() {
return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.google.protobuf.jruby.SentinelOuterClass.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
return com.google.protobuf.jruby.SentinelOuterClass
.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.google.protobuf.jruby.SentinelOuterClass.internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable
return com.google.protobuf.jruby.SentinelOuterClass
.internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable
.ensureFieldAccessorsInitialized(
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.class, com.google.protobuf.jruby.SentinelOuterClass.Sentinel.Builder.class);
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.class,
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.Builder.class);
}
public static final com.google.protobuf.Parser<Sentinel> PARSER =
@ -146,8 +127,8 @@ public final class SentinelOuterClass {
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(builder.buildPartial());
throw new com.google.protobuf.InvalidProtocolBufferException(e.getMessage())
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
@ -160,52 +141,41 @@ public final class SentinelOuterClass {
public static final int DEFAULT_INT32_FIELD_NUMBER = 1;
private int defaultInt32_;
/**
* <code>optional int32 default_int32 = 1;</code>
*/
/** <code>optional int32 default_int32 = 1;</code> */
public int getDefaultInt32() {
return defaultInt32_;
}
public static final int DEFAULT_INT64_FIELD_NUMBER = 2;
private long defaultInt64_;
/**
* <code>optional int64 default_int64 = 2;</code>
*/
/** <code>optional int64 default_int64 = 2;</code> */
public long getDefaultInt64() {
return defaultInt64_;
}
public static final int DEFAULT_UNIT32_FIELD_NUMBER = 3;
private int defaultUnit32_;
/**
* <code>optional uint32 default_unit32 = 3;</code>
*/
/** <code>optional uint32 default_unit32 = 3;</code> */
public int getDefaultUnit32() {
return defaultUnit32_;
}
public static final int DEFAULT_UINT64_FIELD_NUMBER = 4;
private long defaultUint64_;
/**
* <code>optional uint64 default_uint64 = 4;</code>
*/
/** <code>optional uint64 default_uint64 = 4;</code> */
public long getDefaultUint64() {
return defaultUint64_;
}
public static final int DEFAULT_STRING_FIELD_NUMBER = 5;
private java.lang.Object defaultString_;
/**
* <code>optional string default_string = 5;</code>
*/
/** <code>optional string default_string = 5;</code> */
public java.lang.String getDefaultString() {
java.lang.Object ref = defaultString_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
defaultString_ = s;
@ -213,16 +183,12 @@ public final class SentinelOuterClass {
return s;
}
}
/**
* <code>optional string default_string = 5;</code>
*/
public com.google.protobuf.ByteString
getDefaultStringBytes() {
/** <code>optional string default_string = 5;</code> */
public com.google.protobuf.ByteString getDefaultStringBytes() {
java.lang.Object ref = defaultString_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
defaultString_ = b;
return b;
} else {
@ -232,36 +198,28 @@ public final class SentinelOuterClass {
public static final int DEFAULT_BOOL_FIELD_NUMBER = 6;
private boolean defaultBool_;
/**
* <code>optional bool default_bool = 6;</code>
*/
/** <code>optional bool default_bool = 6;</code> */
public boolean getDefaultBool() {
return defaultBool_;
}
public static final int DEFAULT_FLOAT_FIELD_NUMBER = 7;
private float defaultFloat_;
/**
* <code>optional float default_float = 7;</code>
*/
/** <code>optional float default_float = 7;</code> */
public float getDefaultFloat() {
return defaultFloat_;
}
public static final int DEFAULT_DOUBLE_FIELD_NUMBER = 8;
private double defaultDouble_;
/**
* <code>optional double default_double = 8;</code>
*/
/** <code>optional double default_double = 8;</code> */
public double getDefaultDouble() {
return defaultDouble_;
}
public static final int DEFAULT_BYTES_FIELD_NUMBER = 9;
private com.google.protobuf.ByteString defaultBytes_;
/**
* <code>optional bytes default_bytes = 9;</code>
*/
/** <code>optional bytes default_bytes = 9;</code> */
public com.google.protobuf.ByteString getDefaultBytes() {
return defaultBytes_;
}
@ -271,47 +229,52 @@ public final class SentinelOuterClass {
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(java.io.InputStream input)
throws java.io.IOException {
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
java.io.InputStream input) throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseDelimitedFrom(
java.io.InputStream input) throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
com.google.protobuf.CodedInputStream input) throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static com.google.protobuf.jruby.SentinelOuterClass.Sentinel parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
@ -319,36 +282,45 @@ public final class SentinelOuterClass {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return new Builder(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(com.google.protobuf.jruby.SentinelOuterClass.Sentinel prototype) {
public static Builder newBuilder() {
return new Builder();
}
public Builder newBuilderForType() {
return newBuilder();
}
public static Builder newBuilder(
com.google.protobuf.jruby.SentinelOuterClass.Sentinel prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
public Builder toBuilder() {
return newBuilder(this);
}
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
protected Builder newBuilderForType(com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code com.google.protobuf.jruby.Sentinel}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
/** Protobuf type {@code com.google.protobuf.jruby.Sentinel} */
public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder>
implements
// @@protoc_insertion_point(builder_implements:com.google.protobuf.jruby.Sentinel)
com.google.protobuf.jruby.SentinelOuterClass.SentinelOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return com.google.protobuf.jruby.SentinelOuterClass.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
return com.google.protobuf.jruby.SentinelOuterClass
.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return com.google.protobuf.jruby.SentinelOuterClass.internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable
return com.google.protobuf.jruby.SentinelOuterClass
.internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable
.ensureFieldAccessorsInitialized(
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.class, com.google.protobuf.jruby.SentinelOuterClass.Sentinel.Builder.class);
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.class,
com.google.protobuf.jruby.SentinelOuterClass.Sentinel.Builder.class);
}
// Construct using com.google.protobuf.jruby.SentinelOuterClass.Sentinel.newBuilder()
@ -356,15 +328,15 @@ public final class SentinelOuterClass {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
private Builder(com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {}
}
public Builder clear() {
super.clear();
defaultInt32_ = 0;
@ -388,9 +360,9 @@ public final class SentinelOuterClass {
return this;
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return com.google.protobuf.jruby.SentinelOuterClass.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() {
return com.google.protobuf.jruby.SentinelOuterClass
.internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
}
public com.google.protobuf.jruby.SentinelOuterClass.Sentinel getDefaultInstanceForType() {
@ -406,7 +378,8 @@ public final class SentinelOuterClass {
}
public com.google.protobuf.jruby.SentinelOuterClass.Sentinel buildPartial() {
com.google.protobuf.jruby.SentinelOuterClass.Sentinel result = new com.google.protobuf.jruby.SentinelOuterClass.Sentinel(this);
com.google.protobuf.jruby.SentinelOuterClass.Sentinel result =
new com.google.protobuf.jruby.SentinelOuterClass.Sentinel(this);
result.defaultInt32_ = defaultInt32_;
result.defaultInt64_ = defaultInt64_;
result.defaultUnit32_ = defaultUnit32_;
@ -420,26 +393,19 @@ public final class SentinelOuterClass {
return result;
}
private int defaultInt32_;
/**
* <code>optional int32 default_int32 = 1;</code>
*/
/** <code>optional int32 default_int32 = 1;</code> */
public int getDefaultInt32() {
return defaultInt32_;
}
/**
* <code>optional int32 default_int32 = 1;</code>
*/
/** <code>optional int32 default_int32 = 1;</code> */
public Builder setDefaultInt32(int value) {
defaultInt32_ = value;
onChanged();
return this;
}
/**
* <code>optional int32 default_int32 = 1;</code>
*/
/** <code>optional int32 default_int32 = 1;</code> */
public Builder clearDefaultInt32() {
defaultInt32_ = 0;
@ -448,24 +414,18 @@ public final class SentinelOuterClass {
}
private long defaultInt64_;
/**
* <code>optional int64 default_int64 = 2;</code>
*/
/** <code>optional int64 default_int64 = 2;</code> */
public long getDefaultInt64() {
return defaultInt64_;
}
/**
* <code>optional int64 default_int64 = 2;</code>
*/
/** <code>optional int64 default_int64 = 2;</code> */
public Builder setDefaultInt64(long value) {
defaultInt64_ = value;
onChanged();
return this;
}
/**
* <code>optional int64 default_int64 = 2;</code>
*/
/** <code>optional int64 default_int64 = 2;</code> */
public Builder clearDefaultInt64() {
defaultInt64_ = 0L;
@ -474,24 +434,18 @@ public final class SentinelOuterClass {
}
private int defaultUnit32_;
/**
* <code>optional uint32 default_unit32 = 3;</code>
*/
/** <code>optional uint32 default_unit32 = 3;</code> */
public int getDefaultUnit32() {
return defaultUnit32_;
}
/**
* <code>optional uint32 default_unit32 = 3;</code>
*/
/** <code>optional uint32 default_unit32 = 3;</code> */
public Builder setDefaultUnit32(int value) {
defaultUnit32_ = value;
onChanged();
return this;
}
/**
* <code>optional uint32 default_unit32 = 3;</code>
*/
/** <code>optional uint32 default_unit32 = 3;</code> */
public Builder clearDefaultUnit32() {
defaultUnit32_ = 0;
@ -500,24 +454,18 @@ public final class SentinelOuterClass {
}
private long defaultUint64_;
/**
* <code>optional uint64 default_uint64 = 4;</code>
*/
/** <code>optional uint64 default_uint64 = 4;</code> */
public long getDefaultUint64() {
return defaultUint64_;
}
/**
* <code>optional uint64 default_uint64 = 4;</code>
*/
/** <code>optional uint64 default_uint64 = 4;</code> */
public Builder setDefaultUint64(long value) {
defaultUint64_ = value;
onChanged();
return this;
}
/**
* <code>optional uint64 default_uint64 = 4;</code>
*/
/** <code>optional uint64 default_uint64 = 4;</code> */
public Builder clearDefaultUint64() {
defaultUint64_ = 0L;
@ -526,14 +474,11 @@ public final class SentinelOuterClass {
}
private java.lang.Object defaultString_ = "";
/**
* <code>optional string default_string = 5;</code>
*/
/** <code>optional string default_string = 5;</code> */
public java.lang.String getDefaultString() {
java.lang.Object ref = defaultString_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
defaultString_ = s;
@ -543,27 +488,20 @@ public final class SentinelOuterClass {
return (java.lang.String) ref;
}
}
/**
* <code>optional string default_string = 5;</code>
*/
public com.google.protobuf.ByteString
getDefaultStringBytes() {
/** <code>optional string default_string = 5;</code> */
public com.google.protobuf.ByteString getDefaultStringBytes() {
java.lang.Object ref = defaultString_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
defaultString_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string default_string = 5;</code>
*/
public Builder setDefaultString(
java.lang.String value) {
/** <code>optional string default_string = 5;</code> */
public Builder setDefaultString(java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
@ -572,20 +510,15 @@ public final class SentinelOuterClass {
onChanged();
return this;
}
/**
* <code>optional string default_string = 5;</code>
*/
/** <code>optional string default_string = 5;</code> */
public Builder clearDefaultString() {
defaultString_ = getDefaultInstance().getDefaultString();
onChanged();
return this;
}
/**
* <code>optional string default_string = 5;</code>
*/
public Builder setDefaultStringBytes(
com.google.protobuf.ByteString value) {
/** <code>optional string default_string = 5;</code> */
public Builder setDefaultStringBytes(com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
@ -596,24 +529,18 @@ public final class SentinelOuterClass {
}
private boolean defaultBool_;
/**
* <code>optional bool default_bool = 6;</code>
*/
/** <code>optional bool default_bool = 6;</code> */
public boolean getDefaultBool() {
return defaultBool_;
}
/**
* <code>optional bool default_bool = 6;</code>
*/
/** <code>optional bool default_bool = 6;</code> */
public Builder setDefaultBool(boolean value) {
defaultBool_ = value;
onChanged();
return this;
}
/**
* <code>optional bool default_bool = 6;</code>
*/
/** <code>optional bool default_bool = 6;</code> */
public Builder clearDefaultBool() {
defaultBool_ = false;
@ -622,24 +549,18 @@ public final class SentinelOuterClass {
}
private float defaultFloat_;
/**
* <code>optional float default_float = 7;</code>
*/
/** <code>optional float default_float = 7;</code> */
public float getDefaultFloat() {
return defaultFloat_;
}
/**
* <code>optional float default_float = 7;</code>
*/
/** <code>optional float default_float = 7;</code> */
public Builder setDefaultFloat(float value) {
defaultFloat_ = value;
onChanged();
return this;
}
/**
* <code>optional float default_float = 7;</code>
*/
/** <code>optional float default_float = 7;</code> */
public Builder clearDefaultFloat() {
defaultFloat_ = 0F;
@ -648,24 +569,18 @@ public final class SentinelOuterClass {
}
private double defaultDouble_;
/**
* <code>optional double default_double = 8;</code>
*/
/** <code>optional double default_double = 8;</code> */
public double getDefaultDouble() {
return defaultDouble_;
}
/**
* <code>optional double default_double = 8;</code>
*/
/** <code>optional double default_double = 8;</code> */
public Builder setDefaultDouble(double value) {
defaultDouble_ = value;
onChanged();
return this;
}
/**
* <code>optional double default_double = 8;</code>
*/
/** <code>optional double default_double = 8;</code> */
public Builder clearDefaultDouble() {
defaultDouble_ = 0D;
@ -674,15 +589,11 @@ public final class SentinelOuterClass {
}
private com.google.protobuf.ByteString defaultBytes_ = com.google.protobuf.ByteString.EMPTY;
/**
* <code>optional bytes default_bytes = 9;</code>
*/
/** <code>optional bytes default_bytes = 9;</code> */
public com.google.protobuf.ByteString getDefaultBytes() {
return defaultBytes_;
}
/**
* <code>optional bytes default_bytes = 9;</code>
*/
/** <code>optional bytes default_bytes = 9;</code> */
public Builder setDefaultBytes(com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
@ -692,15 +603,14 @@ public final class SentinelOuterClass {
onChanged();
return this;
}
/**
* <code>optional bytes default_bytes = 9;</code>
*/
/** <code>optional bytes default_bytes = 9;</code> */
public Builder clearDefaultBytes() {
defaultBytes_ = getDefaultInstance().getDefaultBytes();
onChanged();
return this;
}
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return this;
@ -711,12 +621,13 @@ public final class SentinelOuterClass {
return this;
}
// @@protoc_insertion_point(builder_scope:com.google.protobuf.jruby.Sentinel)
}
// @@protoc_insertion_point(class_scope:com.google.protobuf.jruby.Sentinel)
private static final com.google.protobuf.jruby.SentinelOuterClass.Sentinel defaultInstance;static {
private static final com.google.protobuf.jruby.SentinelOuterClass.Sentinel defaultInstance;
static {
defaultInstance = new com.google.protobuf.jruby.SentinelOuterClass.Sentinel();
}
@ -727,30 +638,28 @@ public final class SentinelOuterClass {
public com.google.protobuf.jruby.SentinelOuterClass.Sentinel getDefaultInstanceForType() {
return defaultInstance;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_com_google_protobuf_jruby_Sentinel_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
private static com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
private static com.google.protobuf.Descriptors.FileDescriptor descriptor;
static {
java.lang.String[] descriptorData = {
"\n\016sentinel.proto\022\031com.google.protobuf.jr" +
"uby\"\334\001\n\010Sentinel\022\025\n\rdefault_int32\030\001 \001(\005\022" +
"\025\n\rdefault_int64\030\002 \001(\003\022\026\n\016default_unit32" +
"\030\003 \001(\r\022\026\n\016default_uint64\030\004 \001(\004\022\026\n\016defaul" +
"t_string\030\005 \001(\t\022\024\n\014default_bool\030\006 \001(\010\022\025\n\r" +
"default_float\030\007 \001(\002\022\026\n\016default_double\030\010 " +
"\001(\001\022\025\n\rdefault_bytes\030\t \001(\014B\002H\002b\006proto3"
"\n\016sentinel.proto\022\031com.google.protobuf.jr"
+ "uby\"\334\001\n\010Sentinel\022\025\n\rdefault_int32\030\001 \001(\005\022"
+ "\025\n\rdefault_int64\030\002 \001(\003\022\026\n\016default_unit32"
+ "\030\003 \001(\r\022\026\n\016default_uint64\030\004 \001(\004\022\026\n\016defaul"
+ "t_string\030\005 \001(\t\022\024\n\014default_bool\030\006 \001(\010\022\025\n\r"
+ "default_float\030\007 \001(\002\022\026\n\016default_double\030\010 "
+ "\001(\001\022\025\n\rdefault_bytes\030\t \001(\014B\002H\002b\006proto3"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
@ -760,16 +669,24 @@ public final class SentinelOuterClass {
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
com.google.protobuf.Descriptors.FileDescriptor.internalBuildGeneratedFileFrom(
descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] {}, assigner);
internal_static_com_google_protobuf_jruby_Sentinel_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_com_google_protobuf_jruby_Sentinel_fieldAccessorTable =
new com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_com_google_protobuf_jruby_Sentinel_descriptor,
new java.lang.String[] { "DefaultInt32", "DefaultInt64", "DefaultUnit32", "DefaultUint64", "DefaultString", "DefaultBool", "DefaultFloat", "DefaultDouble", "DefaultBytes", });
new java.lang.String[] {
"DefaultInt32",
"DefaultInt64",
"DefaultUnit32",
"DefaultUint64",
"DefaultString",
"DefaultBool",
"DefaultFloat",
"DefaultDouble",
"DefaultBytes",
});
}
// @@protoc_insertion_point(outer_class_scope)

@ -35,6 +35,7 @@ package com.google.protobuf.jruby;
import com.google.protobuf.ByteString;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
import com.google.protobuf.Descriptors.FieldDescriptor;
import java.math.BigInteger;
import org.jcodings.specific.ASCIIEncoding;
import org.jruby.*;
import org.jruby.exceptions.RaiseException;
@ -44,8 +45,6 @@ import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import java.math.BigInteger;
public class Utils {
public static FieldDescriptor.Type rubyToFieldType(IRubyObject typeClass) {
return FieldDescriptor.Type.valueOf(typeClass.asJavaString().toUpperCase());
@ -64,8 +63,12 @@ public class Utils {
return context.runtime.newSymbol(typeName.replace("TYPE_", "").toLowerCase());
}
public static IRubyObject checkType(ThreadContext context, FieldDescriptor.Type fieldType,
String fieldName, IRubyObject value, RubyModule typeClass) {
public static IRubyObject checkType(
ThreadContext context,
FieldDescriptor.Type fieldType,
String fieldName,
IRubyObject value,
RubyModule typeClass) {
Ruby runtime = context.runtime;
switch (fieldType) {
@ -85,13 +88,25 @@ public class Utils {
if (value instanceof RubyFloat) {
double doubleVal = RubyNumeric.num2dbl(value);
if (Math.floor(doubleVal) != doubleVal) {
throw runtime.newRangeError("Non-integral floating point value assigned to integer field '" + fieldName + "' (given " + value.getMetaClass() + ").");
}
}
if (fieldType == FieldDescriptor.Type.UINT32 || fieldType == FieldDescriptor.Type.UINT64 ||
fieldType == FieldDescriptor.Type.FIXED32 || fieldType == FieldDescriptor.Type.FIXED64) {
throw runtime.newRangeError(
"Non-integral floating point value assigned to integer field '"
+ fieldName
+ "' (given "
+ value.getMetaClass()
+ ").");
}
}
if (fieldType == FieldDescriptor.Type.UINT32
|| fieldType == FieldDescriptor.Type.UINT64
|| fieldType == FieldDescriptor.Type.FIXED32
|| fieldType == FieldDescriptor.Type.FIXED64) {
if (((RubyNumeric) value).isNegative()) {
throw runtime.newRangeError("Assigning negative value to unsigned integer field '" + fieldName + "' (given " + value.getMetaClass() + ").");
throw runtime.newRangeError(
"Assigning negative value to unsigned integer field '"
+ fieldName
+ "' (given "
+ value.getMetaClass()
+ ").");
}
}
@ -128,7 +143,9 @@ public class Utils {
value = validateAndEncodeString(context, "bytes", fieldName, value, "Encoding::ASCII_8BIT");
break;
case STRING:
value = validateAndEncodeString(context, "string", fieldName, symToString(value), "Encoding::UTF_8");
value =
validateAndEncodeString(
context, "string", fieldName, symToString(value), "Encoding::UTF_8");
break;
case MESSAGE:
if (value.getMetaClass() != typeClass) {
@ -138,12 +155,18 @@ public class Utils {
if (className.equals("Google::Protobuf::Timestamp") && value instanceof RubyTime) {
RubyTime rt = (RubyTime) value;
RubyHash timestampArgs =
Helpers.constructHash(runtime,
runtime.newString("nanos"), rt.nsec(), false,
runtime.newString("seconds"), rt.to_i(), false);
Helpers.constructHash(
runtime,
runtime.newString("nanos"),
rt.nsec(),
false,
runtime.newString("seconds"),
rt.to_i(),
false);
return ((RubyClass) typeClass).newInstance(context, timestampArgs, Block.NULL_BLOCK);
} else if (className.equals("Google::Protobuf::Duration") && value instanceof RubyNumeric) {
} else if (className.equals("Google::Protobuf::Duration")
&& value instanceof RubyNumeric) {
IRubyObject seconds;
if (value instanceof RubyFloat) {
seconds = ((RubyFloat) value).truncate(context);
@ -167,19 +190,32 @@ public class Utils {
}
RubyHash durationArgs =
Helpers.constructHash(runtime,
runtime.newString("nanos"), ((RubyNumeric) nanos).round(context), false,
runtime.newString("seconds"), seconds, false);
Helpers.constructHash(
runtime,
runtime.newString("nanos"),
((RubyNumeric) nanos).round(context),
false,
runtime.newString("seconds"),
seconds,
false);
return ((RubyClass) typeClass).newInstance(context, durationArgs, Block.NULL_BLOCK);
}
// Not able to convert so flag as invalid
throw createTypeError(context, "Invalid type " + value.getMetaClass() + " to assign to submessage field '" + fieldName + "'.");
throw createTypeError(
context,
"Invalid type "
+ value.getMetaClass()
+ " to assign to submessage field '"
+ fieldName
+ "'.");
}
break;
case ENUM:
boolean isValid = ((RubyEnumDescriptor) typeClass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR)).isValidValue(context, value);
boolean isValid =
((RubyEnumDescriptor) typeClass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR))
.isValidValue(context, value);
if (!isValid) {
throw runtime.newRangeError("Unknown symbol value for enum field '" + fieldName + "'.");
}
@ -190,11 +226,13 @@ public class Utils {
return value;
}
public static IRubyObject wrapPrimaryValue(ThreadContext context, FieldDescriptor.Type fieldType, Object value) {
public static IRubyObject wrapPrimaryValue(
ThreadContext context, FieldDescriptor.Type fieldType, Object value) {
return wrapPrimaryValue(context, fieldType, value, false);
}
public static IRubyObject wrapPrimaryValue(ThreadContext context, FieldDescriptor.Type fieldType, Object value, boolean encodeBytes) {
public static IRubyObject wrapPrimaryValue(
ThreadContext context, FieldDescriptor.Type fieldType, Object value, boolean encodeBytes) {
Ruby runtime = context.runtime;
switch (fieldType) {
case INT32:
@ -211,22 +249,27 @@ public class Utils {
case FIXED64:
case UINT64:
long ret = (Long) value;
return ret >= 0 ? runtime.newFixnum(ret) :
RubyBignum.newBignum(runtime, UINT64_COMPLEMENTARY.add(new BigInteger(ret + "")));
return ret >= 0
? runtime.newFixnum(ret)
: RubyBignum.newBignum(runtime, UINT64_COMPLEMENTARY.add(new BigInteger(ret + "")));
case FLOAT:
return runtime.newFloat((Float) value);
case DOUBLE:
return runtime.newFloat((Double) value);
case BOOL:
return (Boolean) value ? runtime.getTrue() : runtime.getFalse();
case BYTES: {
IRubyObject wrapped = encodeBytes ?
RubyString.newString(runtime, ((ByteString) value).toStringUtf8(), ASCIIEncoding.INSTANCE) :
RubyString.newString(runtime, ((ByteString) value).toByteArray());
case BYTES:
{
IRubyObject wrapped =
encodeBytes
? RubyString.newString(
runtime, ((ByteString) value).toStringUtf8(), ASCIIEncoding.INSTANCE)
: RubyString.newString(runtime, ((ByteString) value).toByteArray());
wrapped.setFrozen(true);
return wrapped;
}
case STRING: {
case STRING:
{
IRubyObject wrapped = runtime.newString(value.toString());
wrapped.setFrozen(true);
return wrapped;
@ -239,7 +282,9 @@ public class Utils {
public static int num2uint(IRubyObject value) {
long longVal = RubyNumeric.num2long(value);
if (longVal > UINT_MAX)
throw value.getRuntime().newRangeError("Integer " + longVal + " too big to convert to 'unsigned int'");
throw value
.getRuntime()
.newRangeError("Integer " + longVal + " too big to convert to 'unsigned int'");
long num = longVal;
if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE)
// encode to UINT32
@ -275,9 +320,9 @@ public class Utils {
}
public static boolean isMapEntry(FieldDescriptor fieldDescriptor) {
return fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE &&
fieldDescriptor.isRepeated() &&
fieldDescriptor.getMessageType().getOptions().getMapEntry();
return fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE
&& fieldDescriptor.isRepeated()
&& fieldDescriptor.getMessageType().getOptions().getMapEntry();
}
public static RaiseException createTypeError(ThreadContext context, String message) {
@ -287,19 +332,27 @@ public class Utils {
return RaiseException.from(context.runtime, cTypeError, message);
}
public static RaiseException createExpectedTypeError(ThreadContext context, String type, String fieldType, String fieldName, IRubyObject value) {
return createTypeError(context, String.format(EXPECTED_TYPE_ERROR_FORMAT, type, fieldType, fieldName, value.getMetaClass()));
public static RaiseException createExpectedTypeError(
ThreadContext context, String type, String fieldType, String fieldName, IRubyObject value) {
return createTypeError(
context,
String.format(
EXPECTED_TYPE_ERROR_FORMAT, type, fieldType, fieldName, value.getMetaClass()));
}
public static RaiseException createInvalidTypeError(ThreadContext context, String fieldType, String fieldName, IRubyObject value) {
return createTypeError(context, String.format(INVALID_TYPE_ERROR_FORMAT, fieldType, fieldName, value.getMetaClass()));
public static RaiseException createInvalidTypeError(
ThreadContext context, String fieldType, String fieldName, IRubyObject value) {
return createTypeError(
context,
String.format(INVALID_TYPE_ERROR_FORMAT, fieldType, fieldName, value.getMetaClass()));
}
protected static boolean isRubyNum(Object value) {
return value instanceof RubyFixnum || value instanceof RubyFloat || value instanceof RubyBignum;
}
protected static void validateTypeClass(ThreadContext context, FieldDescriptor.Type type, IRubyObject value) {
protected static void validateTypeClass(
ThreadContext context, FieldDescriptor.Type type, IRubyObject value) {
Ruby runtime = context.runtime;
if (!(value instanceof RubyModule)) {
throw runtime.newArgumentError("TypeClass has incorrect type");
@ -307,8 +360,9 @@ public class Utils {
RubyModule klass = (RubyModule) value;
IRubyObject descriptor = klass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR);
if (descriptor.isNil()) {
throw runtime.newArgumentError("Type class has no descriptor. Please pass a " +
"class or enum as returned by the DescriptorPool.");
throw runtime.newArgumentError(
"Type class has no descriptor. Please pass a "
+ "class or enum as returned by the DescriptorPool.");
}
if (type == FieldDescriptor.Type.MESSAGE) {
if (!(descriptor instanceof RubyDescriptor)) {
@ -321,7 +375,12 @@ public class Utils {
}
}
private static IRubyObject validateAndEncodeString(ThreadContext context, String fieldType, String fieldName, IRubyObject value, String encoding) {
private static IRubyObject validateAndEncodeString(
ThreadContext context,
String fieldType,
String fieldName,
IRubyObject value,
String encoding) {
if (!(value instanceof RubyString))
throw createInvalidTypeError(context, fieldType, fieldName, value);
@ -334,10 +393,13 @@ public class Utils {
public static final String EQUAL_SIGN = "=";
private static final BigInteger UINT64_COMPLEMENTARY = new BigInteger("18446744073709551616"); //Math.pow(2, 64)
private static final BigInteger UINT64_COMPLEMENTARY =
new BigInteger("18446744073709551616"); // Math.pow(2, 64)
private static final String EXPECTED_TYPE_ERROR_FORMAT = "Expected %s type for %s field '%s' (given %s).";
private static final String INVALID_TYPE_ERROR_FORMAT = "Invalid argument for %s field '%s' (given %s).";
private static final String EXPECTED_TYPE_ERROR_FORMAT =
"Expected %s type for %s field '%s' (given %s).";
private static final String INVALID_TYPE_ERROR_FORMAT =
"Invalid argument for %s field '%s' (given %s).";
private static final long UINT_MAX = 0xffffffffl;

@ -33,11 +33,10 @@
package google;
import com.google.protobuf.jruby.*;
import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.runtime.load.BasicLibraryService;
import java.io.IOException;
public class ProtobufJavaService implements BasicLibraryService {
@Override
public boolean basicLoad(Ruby ruby) throws IOException {

Loading…
Cancel
Save