29
29
30
30
/** Manages the resolution of protovalidate constraints. */
31
31
class ConstraintResolver {
32
+ private static final ExtensionRegistry EXTENSION_REGISTRY = ExtensionRegistry .newInstance ();
33
+
34
+ static {
35
+ EXTENSION_REGISTRY .add (ValidateProto .message );
36
+ EXTENSION_REGISTRY .add (ValidateProto .oneof );
37
+ EXTENSION_REGISTRY .add (ValidateProto .field );
38
+ }
32
39
33
40
/**
34
41
* Resolves the constraints for a message descriptor.
@@ -41,9 +48,8 @@ MessageConstraints resolveMessageConstraints(Descriptor desc)
41
48
DescriptorProtos .MessageOptions options = desc .getOptions ();
42
49
// If the protovalidate message extension is unknown, reparse using extension registry.
43
50
if (options .getUnknownFields ().hasField (ValidateProto .message .getNumber ())) {
44
- ExtensionRegistry registry = ExtensionRegistry .newInstance ();
45
- registry .add (ValidateProto .message );
46
- options = DescriptorProtos .MessageOptions .parseFrom (options .toByteString (), registry );
51
+ options =
52
+ DescriptorProtos .MessageOptions .parseFrom (options .toByteString (), EXTENSION_REGISTRY );
47
53
}
48
54
if (!options .hasExtension (ValidateProto .message )) {
49
55
return MessageConstraints .getDefaultInstance ();
@@ -73,9 +79,7 @@ OneofConstraints resolveOneofConstraints(OneofDescriptor desc)
73
79
DescriptorProtos .OneofOptions options = desc .getOptions ();
74
80
// If the protovalidate oneof extension is unknown, reparse using extension registry.
75
81
if (options .getUnknownFields ().hasField (ValidateProto .oneof .getNumber ())) {
76
- ExtensionRegistry registry = ExtensionRegistry .newInstance ();
77
- registry .add (ValidateProto .oneof );
78
- options = DescriptorProtos .OneofOptions .parseFrom (options .toByteString (), registry );
82
+ options = DescriptorProtos .OneofOptions .parseFrom (options .toByteString (), EXTENSION_REGISTRY );
79
83
}
80
84
if (!options .hasExtension (ValidateProto .oneof )) {
81
85
return OneofConstraints .getDefaultInstance ();
@@ -105,9 +109,7 @@ FieldConstraints resolveFieldConstraints(FieldDescriptor desc)
105
109
DescriptorProtos .FieldOptions options = desc .getOptions ();
106
110
// If the protovalidate field option is unknown, reparse using extension registry.
107
111
if (options .getUnknownFields ().hasField (ValidateProto .field .getNumber ())) {
108
- ExtensionRegistry registry = ExtensionRegistry .newInstance ();
109
- registry .add (ValidateProto .field );
110
- options = DescriptorProtos .FieldOptions .parseFrom (options .toByteString (), registry );
112
+ options = DescriptorProtos .FieldOptions .parseFrom (options .toByteString (), EXTENSION_REGISTRY );
111
113
}
112
114
if (!options .hasExtension (ValidateProto .field )) {
113
115
return FieldConstraints .getDefaultInstance ();
0 commit comments