1717import static com .google .common .collect .ImmutableMap .toImmutableMap ;
1818import static java .util .Arrays .stream ;
1919
20+ import com .google .common .collect .ImmutableCollection ;
2021import com .google .common .collect .ImmutableMap ;
22+ import com .google .common .collect .ImmutableMultimap ;
2123import com .google .protobuf .Any ;
2224import com .google .protobuf .BoolValue ;
2325import com .google .protobuf .BytesValue ;
4648 */
4749@ Internal
4850public enum WellKnownProto {
49- ANY_VALUE ("google.protobuf.Any" , Any .class ),
50- DURATION ("google.protobuf.Duration" , Duration .class ),
51- JSON_LIST_VALUE ("google.protobuf.ListValue" , ListValue .class ),
52- JSON_STRUCT_VALUE ("google.protobuf.Struct" , Struct .class ),
53- JSON_VALUE ("google.protobuf.Value" , Value .class ),
54- TIMESTAMP ("google.protobuf.Timestamp" , Timestamp .class ),
51+ ANY_VALUE ("google.protobuf.Any" , "google/protobuf/any.proto" , Any .class ),
52+ DURATION ("google.protobuf.Duration" , "google/protobuf/duration.proto" , Duration .class ),
53+ JSON_LIST_VALUE ("google.protobuf.ListValue" , "google/protobuf/struct.proto" , ListValue .class ),
54+ JSON_STRUCT_VALUE ("google.protobuf.Struct" , "google/protobuf/struct.proto" , Struct .class ),
55+ JSON_VALUE ("google.protobuf.Value" , "google/protobuf/struct.proto" , Value .class ),
56+ TIMESTAMP ("google.protobuf.Timestamp" , "google/protobuf/timestamp.proto" , Timestamp .class ),
5557 // Wrapper types
56- FLOAT_VALUE ("google.protobuf.FloatValue" , FloatValue .class , /* isWrapperType= */ true ),
57- INT32_VALUE ("google.protobuf.Int32Value" , Int32Value .class , /* isWrapperType= */ true ),
58- INT64_VALUE ("google.protobuf.Int64Value" , Int64Value .class , /* isWrapperType= */ true ),
59- STRING_VALUE ("google.protobuf.StringValue" , StringValue .class , /* isWrapperType= */ true ),
60- BOOL_VALUE ("google.protobuf.BoolValue" , BoolValue .class , /* isWrapperType= */ true ),
61- BYTES_VALUE ("google.protobuf.BytesValue" , BytesValue .class , /* isWrapperType= */ true ),
62- DOUBLE_VALUE ("google.protobuf.DoubleValue" , DoubleValue .class , /* isWrapperType= */ true ),
63- UINT32_VALUE ("google.protobuf.UInt32Value" , UInt32Value .class , /* isWrapperType= */ true ),
64- UINT64_VALUE ("google.protobuf.UInt64Value" , UInt64Value .class , /* isWrapperType= */ true ),
58+ FLOAT_VALUE (
59+ "google.protobuf.FloatValue" ,
60+ "google/protobuf/wrappers.proto" ,
61+ FloatValue .class ,
62+ /* isWrapperType= */ true ),
63+ INT32_VALUE (
64+ "google.protobuf.Int32Value" ,
65+ "google/protobuf/wrappers.proto" ,
66+ Int32Value .class ,
67+ /* isWrapperType= */ true ),
68+ INT64_VALUE (
69+ "google.protobuf.Int64Value" ,
70+ "google/protobuf/wrappers.proto" ,
71+ Int64Value .class ,
72+ /* isWrapperType= */ true ),
73+ STRING_VALUE (
74+ "google.protobuf.StringValue" ,
75+ "google/protobuf/wrappers.proto" ,
76+ StringValue .class ,
77+ /* isWrapperType= */ true ),
78+ BOOL_VALUE (
79+ "google.protobuf.BoolValue" ,
80+ "google/protobuf/wrappers.proto" ,
81+ BoolValue .class ,
82+ /* isWrapperType= */ true ),
83+ BYTES_VALUE (
84+ "google.protobuf.BytesValue" ,
85+ "google/protobuf/wrappers.proto" ,
86+ BytesValue .class ,
87+ /* isWrapperType= */ true ),
88+ DOUBLE_VALUE (
89+ "google.protobuf.DoubleValue" ,
90+ "google/protobuf/wrappers.proto" ,
91+ DoubleValue .class ,
92+ /* isWrapperType= */ true ),
93+ UINT32_VALUE (
94+ "google.protobuf.UInt32Value" ,
95+ "google/protobuf/wrappers.proto" ,
96+ UInt32Value .class ,
97+ /* isWrapperType= */ true ),
98+ UINT64_VALUE (
99+ "google.protobuf.UInt64Value" ,
100+ "google/protobuf/wrappers.proto" ,
101+ UInt64Value .class ,
102+ /* isWrapperType= */ true ),
65103 // These aren't explicitly called out as wrapper types in the spec, but behave like one, because
66104 // they are still converted into an equivalent primitive type.
67105
68- EMPTY ("google.protobuf.Empty" , Empty .class , /* isWrapperType= */ true ),
69- FIELD_MASK ("google.protobuf.FieldMask" , FieldMask .class , /* isWrapperType= */ true ),
70- ;
106+ EMPTY (
107+ "google.protobuf.Empty" ,
108+ "google/protobuf/empty.proto" ,
109+ Empty .class ,
110+ /* isWrapperType= */ true ),
111+ FIELD_MASK (
112+ "google.protobuf.FieldMask" ,
113+ "google/protobuf/field_mask.proto" ,
114+ FieldMask .class ,
115+ /* isWrapperType= */ true );
71116
72117 private static final ImmutableMap <String , WellKnownProto > TYPE_NAME_TO_WELL_KNOWN_PROTO_MAP =
73118 stream (WellKnownProto .values ())
@@ -78,10 +123,27 @@ public enum WellKnownProto {
78123 stream (WellKnownProto .values ())
79124 .collect (toImmutableMap (WellKnownProto ::messageClass , Function .identity ()));
80125
126+ private static final ImmutableMultimap <String , WellKnownProto > PATH_NAME_TO_WELL_KNOWN_PROTO_MAP =
127+ initPathNameMap ();
128+
129+ private static ImmutableMultimap <String , WellKnownProto > initPathNameMap () {
130+ ImmutableMultimap .Builder <String , WellKnownProto > builder = ImmutableMultimap .builder ();
131+ for (WellKnownProto proto : values ()) {
132+ builder .put (proto .pathName (), proto );
133+ }
134+ return builder .build ();
135+ }
136+
81137 private final String wellKnownProtoTypeName ;
138+ private final String pathName ;
82139 private final Class <?> clazz ;
83140 private final boolean isWrapperType ;
84141
142+ /** Gets the full proto path name (ex: google/protobuf/any.proto) */
143+ public String pathName () {
144+ return pathName ;
145+ }
146+
85147 /** Gets the fully qualified prototype name (ex: google.protobuf.FloatValue) */
86148 public String typeName () {
87149 return wellKnownProtoTypeName ;
@@ -92,6 +154,14 @@ public Class<?> messageClass() {
92154 return clazz ;
93155 }
94156
157+ /**
158+ * Returns the well known proto given the full proto path (example:
159+ * google/protobuf/timestamp.proto)
160+ */
161+ public static ImmutableCollection <WellKnownProto > getByPathName (String typeName ) {
162+ return PATH_NAME_TO_WELL_KNOWN_PROTO_MAP .get (typeName );
163+ }
164+
95165 public static Optional <WellKnownProto > getByTypeName (String typeName ) {
96166 return Optional .ofNullable (TYPE_NAME_TO_WELL_KNOWN_PROTO_MAP .get (typeName ));
97167 }
@@ -112,12 +182,14 @@ public boolean isWrapperType() {
112182 return isWrapperType ;
113183 }
114184
115- WellKnownProto (String wellKnownProtoTypeName , Class <?> clazz ) {
116- this (wellKnownProtoTypeName , clazz , /* isWrapperType= */ false );
185+ WellKnownProto (String wellKnownProtoTypeName , String pathName , Class <?> clazz ) {
186+ this (wellKnownProtoTypeName , pathName , clazz , /* isWrapperType= */ false );
117187 }
118188
119- WellKnownProto (String wellKnownProtoFullName , Class <?> clazz , boolean isWrapperType ) {
189+ WellKnownProto (
190+ String wellKnownProtoFullName , String pathName , Class <?> clazz , boolean isWrapperType ) {
120191 this .wellKnownProtoTypeName = wellKnownProtoFullName ;
192+ this .pathName = pathName ;
121193 this .clazz = clazz ;
122194 this .isWrapperType = isWrapperType ;
123195 }
0 commit comments