if (collectionClass != null) return collectionClass; Class elementClass = getClass(schema.getElementType()); if(elementClass.isPrimitive()) {
@Test public void testDollarTerminatedNamespaceCompatibility() { ReflectData data = ReflectData.get(); Schema s = new Schema.Parser().parse ("{\"type\":\"record\",\"name\":\"Z\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[]}"); assertEquals(data.getSchema(data.getClass(s)).toString(), "{\"type\":\"record\",\"name\":\"Z\",\"namespace\":\"org.apache.avro.reflect.TestReflect\",\"fields\":[]}"); }
if (collectionClass != null) return collectionClass; Class elementClass = getClass(schema.getElementType()); if(elementClass.isPrimitive()) {
@Test public void testP0() throws Exception { Protocol p0 = ReflectData.get().getProtocol(P0.class); Protocol.Message message = p0.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
elementClass = data.getClass(schema.getElementType());
@Test public void testP1() throws Exception { Protocol p1 = ReflectData.get().getProtocol(P1.class); Protocol.Message message = p1.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
elementClass = data.getClass(schema.getElementType());
@SuppressWarnings("unchecked") public static Object getEnumValue(Schema schema, String symbol) { return Enum.valueOf(ReflectData.get().getClass(schema), symbol); }
@Override @SuppressWarnings(value="unchecked") protected Object newArray(Object old, int size, Schema schema) { ReflectData data = ReflectData.get(); Class collectionClass = ReflectData.getClassProp(schema, ReflectData.CLASS_PROP); if (collectionClass != null) { if (old instanceof Collection) { ((Collection)old).clear(); return old; } if (collectionClass.isAssignableFrom(ArrayList.class)) return new ArrayList(); return newInstance(collectionClass, schema); } Class elementClass = ReflectData.getClassProp(schema, ReflectData.ELEMENT_PROP); if (elementClass == null) elementClass = data.getClass(schema.getElementType()); return Array.newInstance(elementClass, size); }
@SuppressWarnings("unchecked") public static Object getEnumValue(Schema schema, String symbol) { return Enum.valueOf(ReflectData.get().getClass(schema), symbol); }
@Override @SuppressWarnings(value="unchecked") protected Object newArray(Object old, int size, Schema schema) { ReflectData data = ReflectData.get(); Class collectionClass = ReflectData.getClassProp(schema, ReflectData.CLASS_PROP); if (collectionClass != null) { if (old instanceof Collection) { ((Collection)old).clear(); return old; } if (collectionClass.isAssignableFrom(ArrayList.class)) return new ArrayList(); return newInstance(collectionClass); } Class elementClass = ReflectData.getClassProp(schema, ReflectData.ELEMENT_PROP); if (elementClass == null) elementClass = data.getClass(schema.getElementType()); return Array.newInstance(elementClass, size); }
/** * Resolves the type based on the given schema. In most cases, the type should * stay as is. However, if the type is Object, then that means that the old * default behavior of determining the class from ReflectData#getClass(Schema) * should be used. If a class can't be found, it will default to * GenericData.Record. * * @param <E> The entity type * @param type The Java class of the entity type * @param schema The {@link Schema} for the entity * @return The resolved Java class object */ @SuppressWarnings("unchecked") public static <E> Class<E> resolveType(Class<E> type, Schema schema) { if (type == Object.class) { type = ReflectData.get().getClass(schema); } if (type == null) { type = (Class<E>) GenericData.Record.class; } return type; }
@Override public Class getClass(Schema schema) { switch (schema.getType()) { case ARRAY: Class collectionClass = getClassProp(schema, CLASS_PROP); if (collectionClass != null) return collectionClass; return java.lang.reflect.Array.newInstance(getClass(schema.getElementType()),0).getClass(); case STRING: return String.class; case BYTES: return BYTES_CLASS; case INT: if (Short.class.getName().equals(schema.getProp(CLASS_PROP))) return Short.TYPE; default: return super.getClass(schema); } }
@SuppressWarnings("unchecked") public static <T> T fromBytes( byte[] val, Schema schema , SpecificDatumReader<T> datumReader, T object) throws IOException { Type type = schema.getType(); switch (type) { case ENUM: String symbol = schema.getEnumSymbols().get(val[0]); return (T)Enum.valueOf(ReflectData.get().getClass(schema), symbol); case STRING: return (T)new Utf8(toString(val)); case BYTES: return (T)ByteBuffer.wrap(val); case INT: return (T)Integer.valueOf(bytesToVint(val)); case LONG: return (T)Long.valueOf(bytesToVlong(val)); case FLOAT: return (T)Float.valueOf(toFloat(val)); case DOUBLE: return (T)Double.valueOf(toDouble(val)); case BOOLEAN: return (T)Boolean.valueOf(val[0] != 0); case RECORD: //fall case MAP: case ARRAY: return (T)IOUtils.deserialize(val, (SpecificDatumReader<SpecificRecord>)datumReader, (SpecificRecord)object); default: throw new RuntimeException("Unknown type: "+type); } }
@Override public Class getClass(Schema schema) { switch (schema.getType()) { case ARRAY: Class collectionClass = getClassProp(schema, CLASS_PROP); if (collectionClass != null) return collectionClass; return java.lang.reflect.Array.newInstance(getClass(schema.getElementType()),0).getClass(); case STRING: return String.class; case BYTES: return BYTES_CLASS; case INT: if (Short.class.getName().equals(schema.getProp(CLASS_PROP))) return Short.TYPE; default: return super.getClass(schema); } }
private void checkUnion(String context, TypeDescriptor<?> type, Schema schema) { final List<Schema> unionTypes = schema.getTypes(); if (!type.getRawType().isAnnotationPresent(Union.class)) { // First check for @Nullable field, which shows up as a union of field type and null. if (unionTypes.size() == 2 && unionTypes.contains(AVRO_NULL_SCHEMA)) { // Find the Schema that is not NULL and recursively check that it is deterministic. Schema nullableFieldSchema = unionTypes.get(0).equals(AVRO_NULL_SCHEMA) ? unionTypes.get(1) : unionTypes.get(0); doCheck(context, type, nullableFieldSchema); return; } // Otherwise report a schema error. reportError(context, "Expected type %s to have @Union annotation", type); return; } // Errors associated with this union will use the base class as their context. String baseClassContext = type.getRawType().getName(); // For a union, we need to make sure that each possible instantiation is deterministic. for (Schema concrete : unionTypes) { @SuppressWarnings("unchecked") TypeDescriptor<?> unionType = TypeDescriptor.of(ReflectData.get().getClass(concrete)); recurse(baseClassContext, unionType, concrete); } }
@Override public Class getClass(Schema schema) { switch (schema.getType()) { case ARRAY: Class collectionClass = getClassProp(schema, CLASS_PROP); if (collectionClass != null) return collectionClass; Class elementClass = getClass(schema.getElementType()); if(elementClass.isPrimitive()) { // avoid expensive translation to array type when primitive return ARRAY_CLASSES.get(elementClass); } else { return java.lang.reflect.Array.newInstance(elementClass, 0).getClass(); } case STRING: Class stringClass = getClassProp(schema, CLASS_PROP); if (stringClass != null) return stringClass; return String.class; case BYTES: return BYTES_CLASS; case INT: String intClass = schema.getProp(CLASS_PROP); if (Byte.class.getName().equals(intClass)) return Byte.TYPE; if (Short.class.getName().equals(intClass)) return Short.TYPE; if (Character.class.getName().equals(intClass)) return Character.TYPE; default: return super.getClass(schema); } }
@SuppressWarnings("unchecked") public static <T> T fromBytes( byte[] val, Schema schema , SpecificDatumReader<T> datumReader, T object) throws IOException { Type type = schema.getType(); switch (type) { case ENUM: String symbol = schema.getEnumSymbols().get(val[0]); return (T)Enum.valueOf(ReflectData.get().getClass(schema), symbol); case STRING: return (T)new Utf8(toString(val)); case BYTES: return (T)ByteBuffer.wrap(val); case INT: return (T)Integer.valueOf(bytesToVint(val)); case LONG: return (T)Long.valueOf(bytesToVlong(val)); case FLOAT: return (T)Float.valueOf(toFloat(val)); case DOUBLE: return (T)Double.valueOf(toDouble(val)); case BOOLEAN: return (T)Boolean.valueOf(val[0] != 0); case RECORD: //fall case MAP: case ARRAY: return (T)IOUtils.deserialize(val, (SpecificDatumReader<SpecificRecord>)datumReader, (SpecificRecord)object); default: throw new RuntimeException("Unknown type: "+type); } }
@Override protected Object newArray(Object old, int size, Schema schema) { Class<?> collectionClass = ReflectData.getClassProp(schema, SpecificData.CLASS_PROP); Class<?> elementClass = ReflectData.getClassProp(schema, SpecificData.ELEMENT_PROP); if (collectionClass == null && elementClass == null) return super.newArray(old, size, schema); // use specific/generic if (collectionClass != null && !collectionClass.isArray()) { if (old instanceof Collection) { ((Collection<?>)old).clear(); return old; } if (collectionClass.isAssignableFrom(ArrayList.class)) return new ArrayList<Object>(); return SpecificData.newInstance(collectionClass, schema); } if (elementClass == null) { elementClass = collectionClass.getComponentType(); } if (elementClass == null) { ReflectData data = (ReflectData)getData(); elementClass = data.getClass(schema.getElementType()); } return Array.newInstance(elementClass, size); }
elementClass = data.getClass(schema.getElementType());