public void checkUnsafe() { ReflectionUtil.getFieldAccess(); }
static void resetFieldAccess() { // load only one implementation of FieldAccess // so it is monomorphic and the JIT can inline FieldAccess access = null; try { if (null == System.getProperty("avro.disable.unsafe")) { FieldAccess unsafeAccess = load( "org.apache.avro.reflect.FieldAccessUnsafe", FieldAccess.class); if (validate(unsafeAccess)) { access = unsafeAccess; } } } catch (Throwable ignored) { } if (access == null) { try { FieldAccess reflectAccess = load( "org.apache.avro.reflect.FieldAccessReflect", FieldAccess.class); if (validate(reflectAccess)) { access = reflectAccess; } } catch (Throwable oops) { throw new AvroRuntimeException( "Unable to load a functional FieldAccess class!"); } } fieldAccess = access; }
@Test public void testDisableUnsafe() throws Exception { String saved = System.getProperty("avro.disable.unsafe"); try { System.setProperty("avro.disable.unsafe", "true"); ReflectData.ACCESSOR_CACHE.remove(multipleAnnotationRecord.class); ReflectData.ACCESSOR_CACHE.remove(AnotherSampleRecord.class); ReflectionUtil.resetFieldAccess(); testMultipleAnnotations(); testRecordWithNullIO(); } finally { if (saved == null) System.clearProperty("avro.disable.unsafe"); else System.setProperty("avro.disable.unsafe", saved); ReflectData.ACCESSOR_CACHE.remove(multipleAnnotationRecord.class); ReflectData.ACCESSOR_CACHE.remove(AnotherSampleRecord.class); ReflectionUtil.resetFieldAccess(); } }
private ClassAccessorData(Class<?> c) { clazz = c; for(Field f : getFields(c, false)) { if (f.isAnnotationPresent(AvroIgnore.class)) { continue; } FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); AvroName avroname = f.getAnnotation(AvroName.class); byName.put( (avroname != null ? avroname.value() : f.getName()) , accessor); } }
static void resetFieldAccess() { // load only one implementation of FieldAccess // so it is monomorphic and the JIT can inline FieldAccess access = null; try { if (null == System.getProperty("avro.disable.unsafe")) { FieldAccess unsafeAccess = load( "org.apache.avro.reflect.FieldAccessUnsafe", FieldAccess.class); if (validate(unsafeAccess)) { access = unsafeAccess; } } } catch (Throwable ignored) { } if (access == null) { try { FieldAccess reflectAccess = load( "org.apache.avro.reflect.FieldAccessReflect", FieldAccess.class); if (validate(reflectAccess)) { access = reflectAccess; } } catch (Throwable oops) { throw new AvroRuntimeException( "Unable to load a functional FieldAccess class!"); } } fieldAccess = access; }
private ClassAccessorData(Class<?> c) { clazz = c; for(Field f : getFields(c, false)) { if (f.isAnnotationPresent(AvroIgnore.class)) { continue; } FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); AvroName avroname = f.getAnnotation(AvroName.class); byName.put( (avroname != null ? avroname.value() : f.getName()) , accessor); } }
static void resetFieldAccess() { // load only one implementation of FieldAccess // so it is monomorphic and the JIT can inline FieldAccess access = null; try { if (null == System.getProperty("avro.disable.unsafe")) { FieldAccess unsafeAccess = load( "org.apache.avro.reflect.FieldAccessUnsafe", FieldAccess.class); if (validate(unsafeAccess)) { access = unsafeAccess; } } } catch (Throwable ignored) { } if (access == null) { try { FieldAccess reflectAccess = load( "org.apache.avro.reflect.FieldAccessReflect", FieldAccess.class); if (validate(reflectAccess)) { access = reflectAccess; } } catch (Throwable oops) { throw new AvroRuntimeException( "Unable to load a functional FieldAccess class!"); } } fieldAccess = access; }
@Test(expected = IllegalArgumentException.class) public void testReadUUIDMissingLogicalTypeReflect() throws IOException { String unsafeValue = System.getProperty("avro.disable.unsafe"); try { // only one FieldAccess can be set per JVM System.setProperty("avro.disable.unsafe", "true"); Assume.assumeTrue( ReflectionUtil.getFieldAccess() instanceof FieldAccessReflect); Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName()) .fields().requiredString("uuid").endRecord(); LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema()); UUID u1 = UUID.randomUUID(); RecordWithStringUUID r1 = new RecordWithStringUUID(); r1.uuid = u1.toString(); File test = write( ReflectData.get().getSchema(RecordWithStringUUID.class), r1); read(ReflectData.get().createDatumReader(uuidSchema), test).get(0); } finally { if (unsafeValue != null) { System.setProperty("avro.disable.unsafe", unsafeValue); } } }
@Test public void testReadUUIDMissingLogicalTypeUnsafe() throws IOException { String unsafeValue = System.getProperty("avro.disable.unsafe"); try { // only one FieldAccess can be set per JVM System.clearProperty("avro.disable.unsafe"); Assume.assumeTrue( ReflectionUtil.getFieldAccess() instanceof FieldAccessUnsafe); Schema uuidSchema = SchemaBuilder.record(RecordWithUUID.class.getName()) .fields().requiredString("uuid").endRecord(); LogicalTypes.uuid().addToSchema(uuidSchema.getField("uuid").schema()); UUID u1 = UUID.randomUUID(); RecordWithStringUUID r1 = new RecordWithStringUUID(); r1.uuid = u1.toString(); File test = write( ReflectData.get().getSchema(RecordWithStringUUID.class), r1); RecordWithUUID datum = (RecordWithUUID) read( ReflectData.get().createDatumReader(uuidSchema), test).get(0); Object uuid = datum.uuid; Assert.assertTrue("UUID should be a String (unsafe)", uuid instanceof String); } finally { if (unsafeValue != null) { System.setProperty("avro.disable.unsafe", unsafeValue); } } }
private ClassAccessorData(Class<?> c) { clazz = c; for(Field f : getFields(c, false)) { if (f.isAnnotationPresent(AvroIgnore.class)) { continue; } FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); AvroName avroname = f.getAnnotation(AvroName.class); byName.put( (avroname != null ? avroname.value() : f.getName()) , accessor); } }
private ClassAccessorData(Class<?> c) { clazz = c; for(Field f : getFields(c, false)) { if (f.isAnnotationPresent(AvroIgnore.class)) { continue; } FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); AvroName avroname = f.getAnnotation(AvroName.class); byName.put( (avroname != null ? avroname.value() : f.getName()) , accessor); } }