FieldType.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import java.lang.reflect.Field;
34 import java.lang.reflect.ParameterizedType;
35 import java.lang.reflect.Type;
36 import java.lang.reflect.TypeVariable;
37 import java.util.List;
38 
40 @ExperimentalApi
41 public enum FieldType {
93 
94  private final JavaType javaType;
95  private final int id;
96  private final Collection collection;
97  private final Class<?> elementType;
98  private final boolean primitiveScalar;
99 
100  FieldType(int id, Collection collection, JavaType javaType) {
101  this.id = id;
102  this.collection = collection;
103  this.javaType = javaType;
104 
105  switch (collection) {
106  case MAP:
107  elementType = javaType.getBoxedType();
108  break;
109  case VECTOR:
110  elementType = javaType.getBoxedType();
111  break;
112  case SCALAR:
113  default:
114  elementType = null;
115  break;
116  }
117 
118  boolean primitiveScalar = false;
119  if (collection == Collection.SCALAR) {
120  switch (javaType) {
121  case BYTE_STRING:
122  case MESSAGE:
123  case STRING:
124  break;
125  default:
126  primitiveScalar = true;
127  break;
128  }
129  }
130  this.primitiveScalar = primitiveScalar;
131  }
132 
134  public int id() {
135  return id;
136  }
137 
143  return javaType;
144  }
145 
147  public boolean isPacked() {
148  return Collection.PACKED_VECTOR.equals(collection);
149  }
150 
155  public boolean isPrimitiveScalar() {
156  return primitiveScalar;
157  }
158 
160  public boolean isScalar() {
161  return collection == Collection.SCALAR;
162  }
163 
165  public boolean isList() {
166  return collection.isList();
167  }
168 
170  public boolean isMap() {
171  return collection == Collection.MAP;
172  }
173 
175  public boolean isValidForField(Field field) {
176  if (Collection.VECTOR.equals(collection)) {
177  return isValidForList(field);
178  } else {
179  return javaType.getType().isAssignableFrom(field.getType());
180  }
181  }
182 
183  private boolean isValidForList(Field field) {
184  Class<?> clazz = field.getType();
185  if (!javaType.getType().isAssignableFrom(clazz)) {
186  // The field isn't a List type.
187  return false;
188  }
189  Type[] types = EMPTY_TYPES;
190  Type genericType = field.getGenericType();
191  if (genericType instanceof ParameterizedType) {
192  types = ((ParameterizedType) field.getGenericType()).getActualTypeArguments();
193  }
194  Type listParameter = getListParameter(clazz, types);
195  if (!(listParameter instanceof Class)) {
196  // It's a wildcard, we should allow anything in the list.
197  return true;
198  }
199  return elementType.isAssignableFrom((Class<?>) listParameter);
200  }
201 
207  /* @Nullable */
208  public static FieldType forId(int id) {
209  if (id < 0 || id >= VALUES.length) {
210  return null;
211  }
212  return VALUES[id];
213  }
214 
215  private static final FieldType[] VALUES;
216  private static final Type[] EMPTY_TYPES = new Type[0];
217 
218  static {
219  FieldType[] values = values();
220  VALUES = new FieldType[values.length];
221  for (FieldType type : values) {
222  VALUES[type.id] = type;
223  }
224  }
225 
231  /* @Nullable */
232  private static Type getGenericSuperList(Class<?> clazz) {
233  // First look at interfaces.
234  Type[] genericInterfaces = clazz.getGenericInterfaces();
235  for (Type genericInterface : genericInterfaces) {
236  if (genericInterface instanceof ParameterizedType) {
237  ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
238  Class<?> rawType = (Class<?>) parameterizedType.getRawType();
239  if (List.class.isAssignableFrom(rawType)) {
240  return genericInterface;
241  }
242  }
243  }
244 
245  // Try the subclass
246  Type type = clazz.getGenericSuperclass();
247  if (type instanceof ParameterizedType) {
248  ParameterizedType parameterizedType = (ParameterizedType) type;
249  Class<?> rawType = (Class<?>) parameterizedType.getRawType();
250  if (List.class.isAssignableFrom(rawType)) {
251  return type;
252  }
253  }
254 
255  // No super class/interface extends List.
256  return null;
257  }
258 
269  private static Type getListParameter(Class<?> clazz, Type[] realTypes) {
270  top:
271  while (clazz != List.class) {
272  // First look at generic subclass and interfaces.
273  Type genericType = getGenericSuperList(clazz);
274  if (genericType instanceof ParameterizedType) {
275  // Replace any generic parameters with the real values.
276  ParameterizedType parameterizedType = (ParameterizedType) genericType;
277  Type[] superArgs = parameterizedType.getActualTypeArguments();
278  for (int i = 0; i < superArgs.length; ++i) {
279  Type superArg = superArgs[i];
280  if (superArg instanceof TypeVariable) {
281  // Get the type variables for this class so that we can match them to the variables
282  // used on the super class.
283  TypeVariable<?>[] clazzParams = clazz.getTypeParameters();
284  if (realTypes.length != clazzParams.length) {
285  throw new RuntimeException("Type array mismatch");
286  }
287 
288  // Replace the variable parameter with the real type.
289  boolean foundReplacement = false;
290  for (int j = 0; j < clazzParams.length; ++j) {
291  if (superArg == clazzParams[j]) {
292  Type realType = realTypes[j];
293  superArgs[i] = realType;
294  foundReplacement = true;
295  break;
296  }
297  }
298  if (!foundReplacement) {
299  throw new RuntimeException("Unable to find replacement for " + superArg);
300  }
301  }
302  }
303 
304  Class<?> parent = (Class<?>) parameterizedType.getRawType();
305 
306  realTypes = superArgs;
307  clazz = parent;
308  continue;
309  }
310 
311  // None of the parameterized types inherit List. Just continue up the inheritance hierarchy
312  // toward the List interface until we can identify the parameters.
313  realTypes = EMPTY_TYPES;
314  for (Class<?> iface : clazz.getInterfaces()) {
315  if (List.class.isAssignableFrom(iface)) {
316  clazz = iface;
317  continue top;
318  }
319  }
320  clazz = clazz.getSuperclass();
321  }
322 
323  if (realTypes.length != 1) {
324  throw new RuntimeException("Unable to identify parameter type for List<T>");
325  }
326  return realTypes[0];
327  }
328 
329  enum Collection {
330  SCALAR(false),
331  VECTOR(true),
333  MAP(false);
334 
335  private final boolean isList;
336 
337  Collection(boolean isList) {
338  this.isList = isList;
339  }
340 
342  public boolean isList() {
343  return isList;
344  }
345  }
346 }
com.google.protobuf.FieldType.FIXED64
FIXED64
Definition: FieldType.java:47
com.google.protobuf.FieldType.UINT64_LIST_PACKED
UINT64_LIST_PACKED
Definition: FieldType.java:80
com.google.protobuf.FieldType.SINT32
SINT32
Definition: FieldType.java:57
com.google.protobuf.FieldType.GROUP
GROUP
Definition: FieldType.java:59
java::lang
com.google.protobuf.FieldType.STRING_LIST
STRING_LIST
Definition: FieldType.java:68
com.google.protobuf.FieldType.Collection.PACKED_VECTOR
PACKED_VECTOR
Definition: FieldType.java:332
com.google.protobuf.FieldType.javaType
final JavaType javaType
Definition: FieldType.java:94
com.google.protobuf.FieldType.VALUES
static final FieldType[] VALUES
Definition: FieldType.java:215
com.google.protobuf.FieldType.getJavaType
JavaType getJavaType()
Definition: FieldType.java:142
com.google.protobuf.JavaType.BYTE_STRING
BYTE_STRING
Definition: JavaType.java:43
com.google.protobuf.FieldType.Collection.Collection
Collection(boolean isList)
Definition: FieldType.java:337
com.google.protobuf.FieldType.SFIXED64
SFIXED64
Definition: FieldType.java:56
com.google.protobuf.FieldType.FLOAT
FLOAT
Definition: FieldType.java:43
com.google.protobuf.FieldType.BOOL
BOOL
Definition: FieldType.java:49
com.google.protobuf.JavaType.getBoxedType
Class<?> getBoxedType()
Definition: JavaType.java:68
types
GLsizei GLenum GLenum * types
Definition: glcorearb.h:4177
com.google.protobuf.FieldType.isMap
boolean isMap()
Definition: FieldType.java:170
com.google.protobuf.FieldType.DOUBLE
DOUBLE
Definition: FieldType.java:42
com.google.protobuf.FieldType.MAP
MAP
Definition: FieldType.java:92
com.google.protobuf.FieldType.GROUP_LIST
GROUP_LIST
Definition: FieldType.java:91
com.google.protobuf.FieldType.isValidForList
boolean isValidForList(Field field)
Definition: FieldType.java:183
com.google.protobuf.FieldType
Definition: FieldType.java:41
com.google.protobuf.FieldType.UINT64_LIST
UINT64_LIST
Definition: FieldType.java:63
java::lang::reflect
com.google.protobuf.FieldType.id
final int id
Definition: FieldType.java:95
com.google.protobuf.Descriptors.FieldDescriptor.Type
Definition: Descriptors.java:1215
com.google.protobuf.FieldType.ENUM
ENUM
Definition: FieldType.java:54
com.google.protobuf.FieldType.Collection.VECTOR
VECTOR
Definition: FieldType.java:331
com.google.protobuf.FieldType.FLOAT_LIST
FLOAT_LIST
Definition: FieldType.java:61
com.google.protobuf.FieldType.INT32
INT32
Definition: FieldType.java:46
values
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:3591
com.google.protobuf.FieldType.isScalar
boolean isScalar()
Definition: FieldType.java:160
com.google.protobuf.FieldType.isPacked
boolean isPacked()
Definition: FieldType.java:147
com.google.protobuf.FieldType.forId
static FieldType forId(int id)
Definition: FieldType.java:208
com.google.protobuf.Descriptors.FieldDescriptor.getType
Type getType()
Definition: Descriptors.java:1014
com.google.protobuf.JavaType.FLOAT
FLOAT
Definition: JavaType.java:39
com.google.protobuf.FieldType.INT64_LIST_PACKED
INT64_LIST_PACKED
Definition: FieldType.java:79
com.google.protobuf.FieldType.INT32_LIST
INT32_LIST
Definition: FieldType.java:64
com.google.protobuf.FieldType.collection
final Collection collection
Definition: FieldType.java:96
com.google.protobuf.FieldType.INT32_LIST_PACKED
INT32_LIST_PACKED
Definition: FieldType.java:81
com.google.protobuf.FieldType.Collection
Definition: FieldType.java:329
com.google.protobuf.FieldType.SFIXED32
SFIXED32
Definition: FieldType.java:55
id
GLenum GLuint id
Definition: glcorearb.h:2695
com.google.protobuf.FieldType.isList
boolean isList()
Definition: FieldType.java:165
com.google.protobuf.FieldType.INT64_LIST
INT64_LIST
Definition: FieldType.java:62
com.google.protobuf.FieldType.SFIXED32_LIST
SFIXED32_LIST
Definition: FieldType.java:73
com.google.protobuf.FieldType.SFIXED32_LIST_PACKED
SFIXED32_LIST_PACKED
Definition: FieldType.java:87
com.google.protobuf.FieldType.FLOAT_LIST_PACKED
FLOAT_LIST_PACKED
Definition: FieldType.java:78
com.google.protobuf.FieldType.MESSAGE_LIST
MESSAGE_LIST
Definition: FieldType.java:69
com.google.protobuf.FieldType.FIXED64_LIST_PACKED
FIXED64_LIST_PACKED
Definition: FieldType.java:82
com.google.protobuf.FieldType.STRING
STRING
Definition: FieldType.java:50
com.google.protobuf.FieldType.isPrimitiveScalar
boolean isPrimitiveScalar()
Definition: FieldType.java:155
com.google.protobuf.FieldType.SINT64_LIST_PACKED
SINT64_LIST_PACKED
Definition: FieldType.java:90
com.google.protobuf.JavaType.DOUBLE
DOUBLE
Definition: JavaType.java:40
com.google.protobuf.FieldType.SFIXED64_LIST
SFIXED64_LIST
Definition: FieldType.java:74
com.google.protobuf.FieldType.SINT64_LIST
SINT64_LIST
Definition: FieldType.java:76
com.google.protobuf.FieldType.isValidForField
boolean isValidForField(Field field)
Definition: FieldType.java:175
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
com.google.protobuf.JavaType.VOID
VOID
Definition: JavaType.java:36
com.google.protobuf.FieldType.UINT64
UINT64
Definition: FieldType.java:45
com.google.protobuf.FieldType.SINT32_LIST_PACKED
SINT32_LIST_PACKED
Definition: FieldType.java:89
com.google.protobuf.FieldType.UINT32_LIST
UINT32_LIST
Definition: FieldType.java:71
i
int i
Definition: gmock-matchers_test.cc:764
com.google.protobuf.FieldType.Collection.isList
final boolean isList
Definition: FieldType.java:335
java
Field
struct Field Field
Definition: php/ext/google/protobuf/protobuf.h:638
com.google.protobuf.JavaType.getType
Class<?> getType()
Definition: JavaType.java:63
type
GLenum type
Definition: glcorearb.h:2695
com.google.protobuf.FieldType.ENUM_LIST_PACKED
ENUM_LIST_PACKED
Definition: FieldType.java:86
com.google.protobuf.JavaType.MESSAGE
MESSAGE
Definition: JavaType.java:45
com.google.protobuf.FieldType.DOUBLE_LIST_PACKED
DOUBLE_LIST_PACKED
Definition: FieldType.java:77
com.google.protobuf.FieldType.MESSAGE
MESSAGE
Definition: FieldType.java:51
com.google.protobuf.FieldType.SFIXED64_LIST_PACKED
SFIXED64_LIST_PACKED
Definition: FieldType.java:88
com.google.protobuf.FieldType.DOUBLE_LIST
DOUBLE_LIST
Definition: FieldType.java:60
com.google.protobuf.FieldType.Collection.isList
boolean isList()
Definition: FieldType.java:342
com.google.protobuf.JavaType
Definition: JavaType.java:35
com.google.protobuf.FieldType.UINT32_LIST_PACKED
UINT32_LIST_PACKED
Definition: FieldType.java:85
com.google.protobuf.FieldType.getListParameter
static Type getListParameter(Class<?> clazz, Type[] realTypes)
Definition: FieldType.java:269
com.google.protobuf.FieldType.primitiveScalar
final boolean primitiveScalar
Definition: FieldType.java:98
com.google.protobuf.FieldType.Collection.SCALAR
SCALAR
Definition: FieldType.java:330
com.google.protobuf.FieldType.INT64
INT64
Definition: FieldType.java:44
com.google.protobuf.FieldType.FIXED32_LIST
FIXED32_LIST
Definition: FieldType.java:66
com.google.protobuf.FieldType.id
int id()
Definition: FieldType.java:134
com.google.protobuf.JavaType.LONG
LONG
Definition: JavaType.java:38
com.google.protobuf.FieldType.BYTES_LIST
BYTES_LIST
Definition: FieldType.java:70
com.google.protobuf.FieldType.UINT32
UINT32
Definition: FieldType.java:53
com.google.protobuf.FieldType.FIXED32
FIXED32
Definition: FieldType.java:48
com.google.protobuf.FieldType.BOOL_LIST
BOOL_LIST
Definition: FieldType.java:67
com.google.protobuf.JavaType.ENUM
ENUM
Definition: JavaType.java:44
com.google.protobuf.FieldType.BOOL_LIST_PACKED
BOOL_LIST_PACKED
Definition: FieldType.java:84
com.google.protobuf.FieldType.SINT32_LIST
SINT32_LIST
Definition: FieldType.java:75
com.google.protobuf.JavaType.INT
INT
Definition: JavaType.java:37
com.google.protobuf.FieldType.elementType
final Class<?> elementType
Definition: FieldType.java:97
top
static upb_pb_encoder_segment * top(upb_pb_encoder *e)
Definition: php/ext/google/protobuf/upb.c:7933
com.google.protobuf.FieldType.SINT64
SINT64
Definition: FieldType.java:58
com.google.protobuf.FieldType.FieldType
FieldType(int id, Collection collection, JavaType javaType)
Definition: FieldType.java:100
java::lang::reflect::Type
com.google.protobuf.FieldType.BYTES
BYTES
Definition: FieldType.java:52
com.google.protobuf.FieldType.FIXED64_LIST
FIXED64_LIST
Definition: FieldType.java:65
com.google.protobuf.FieldType.ENUM_LIST
ENUM_LIST
Definition: FieldType.java:72
com.google.protobuf.FieldType.Collection.MAP
MAP
Definition: FieldType.java:333
com.google.protobuf.JavaType.BOOLEAN
BOOLEAN
Definition: JavaType.java:41
com.google.protobuf.FieldType.FIXED32_LIST_PACKED
FIXED32_LIST_PACKED
Definition: FieldType.java:83
com.google.protobuf.JavaType.STRING
STRING
Definition: JavaType.java:42
com.google.protobuf.FieldType.getGenericSuperList
static Type getGenericSuperList(Class<?> clazz)
Definition: FieldType.java:232


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