/** * Creates a {@link DatumWriter} that is able to encode given data type with the given {@link Schema}. * The instance created is thread safe and reusable. * * @param type Type information of the data type to be encoded. * @param schema Schema of the data type. * @param <T> Type of the data type. * @return A {@link DatumWriter} instance. */ @SuppressWarnings("unchecked") @Override public <T> DatumWriter<T> create(TypeToken<T> type, Schema schema) { try { Class<DatumWriter<?>> writerClass = datumWriterClasses.getUnchecked(new CacheKey(schema, type)); return (DatumWriter<T>) writerClass.getConstructor(Schema.class, FieldAccessorFactory.class) .newInstance(schema, fieldAccessorFactory); } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Creates a {@link DatumWriter} that is able to encode given data type with the given {@link Schema}. * The instance created is thread safe and reusable. * * @param type Type information of the data type to be encoded. * @param schema Schema of the data type. * @param <T> Type of the data type. * @return A {@link DatumWriter} instance. */ @SuppressWarnings("unchecked") @Override public <T> DatumWriter<T> create(TypeToken<T> type, Schema schema) { try { Class<DatumWriter<?>> writerClass = datumWriterClasses.getUnchecked(new CacheKey(schema, type)); return (DatumWriter<T>) writerClass.getConstructor(Schema.class, FieldAccessorFactory.class) .newInstance(schema, fieldAccessorFactory); } catch (Exception e) { throw Throwables.propagate(e); } }
@SuppressWarnings("unchecked") @Override public Class<DatumWriter<?>> load(CacheKey key) throws Exception { ClassDefinition classDef = new DatumWriterGenerator().generate(key.getType(), key.getSchema()); ByteCodeClassLoader classloader = classloaders.get(key.getType()); if (classloader == null) { // The ClassLoader of the generated DatumWriter has CDAP system ClassLoader as parent. // The ClassDefinition contains list of classes that should not be loaded by the generated class ClassLoader classloader = new ByteCodeClassLoader(ASMDatumWriterFactory.class.getClassLoader()); classloaders.put(key.getType(), classloader); } return (Class<DatumWriter<?>>) classloader.addClass(classDef).loadClass(classDef.getClassName()); } }
@SuppressWarnings("unchecked") @Override public Class<DatumWriter<?>> load(CacheKey key) throws Exception { ClassDefinition classDef = new DatumWriterGenerator().generate(key.getType(), key.getSchema()); ByteCodeClassLoader classloader = classloaders.get(key.getType()); if (classloader == null) { // The ClassLoader of the generated DatumWriter has CDAP system ClassLoader as parent. // The ClassDefinition contains list of classes that should not be loaded by the generated class ClassLoader classloader = new ByteCodeClassLoader(ASMDatumWriterFactory.class.getClassLoader()); classloaders.put(key.getType(), classloader); } return (Class<DatumWriter<?>>) classloader.addClass(classDef).loadClass(classDef.getClassName()); } }