diff options
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java | 29 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodecTest.java | 20 |
2 files changed, 23 insertions, 26 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java index 48bc8223c6..4128092f1e 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodec.java @@ -33,18 +33,18 @@ import sun.reflect.ReflectionFactory; * * <p>TODO(shahan): replace Unsafe with VarHandle once it's available. */ -public class DynamicCodec<T> implements ObjectCodec<T> { +public class DynamicCodec implements ObjectCodec<Object> { - private final Class<T> type; - private final Constructor<T> constructor; + private final Class<?> type; + private final Constructor<?> constructor; private final ImmutableSortedMap<Field, Long> offsets; private final ObjectCodec.MemoizationStrategy strategy; - public DynamicCodec(Class<T> type) throws ReflectiveOperationException { + public DynamicCodec(Class<?> type) throws ReflectiveOperationException { this(type, ObjectCodec.MemoizationStrategy.MEMOIZE_BEFORE); } - public DynamicCodec(Class<T> type, ObjectCodec.MemoizationStrategy strategy) + public DynamicCodec(Class<?> type, ObjectCodec.MemoizationStrategy strategy) throws ReflectiveOperationException { this.type = type; this.constructor = getConstructor(type); @@ -53,7 +53,7 @@ public class DynamicCodec<T> implements ObjectCodec<T> { } @Override - public Class<T> getEncodedClass() { + public Class<?> getEncodedClass() { return type; } @@ -63,7 +63,7 @@ public class DynamicCodec<T> implements ObjectCodec<T> { } @Override - public void serialize(SerializationContext context, T obj, CodedOutputStream codedOut) + public void serialize(SerializationContext context, Object obj, CodedOutputStream codedOut) throws SerializationException, IOException { for (Map.Entry<Field, Long> entry : offsets.entrySet()) { serializeField(context, codedOut, obj, entry.getKey().getType(), entry.getValue()); @@ -133,9 +133,9 @@ public class DynamicCodec<T> implements ObjectCodec<T> { } @Override - public T deserialize(DeserializationContext context, CodedInputStream codedIn) + public Object deserialize(DeserializationContext context, CodedInputStream codedIn) throws SerializationException, IOException { - T instance; + Object instance; try { instance = constructor.newInstance(); } catch (ReflectiveOperationException e) { @@ -225,13 +225,10 @@ public class DynamicCodec<T> implements ObjectCodec<T> { return offsets.build(); } - @SuppressWarnings("unchecked") - private static <T> Constructor<T> getConstructor(Class<T> type) - throws ReflectiveOperationException { - Constructor<T> constructor = - (Constructor<T>) - ReflectionFactory.getReflectionFactory() - .newConstructorForSerialization(type, Object.class.getDeclaredConstructor()); + private static Constructor<?> getConstructor(Class<?> type) throws ReflectiveOperationException { + Constructor<?> constructor = + ReflectionFactory.getReflectionFactory() + .newConstructorForSerialization(type, Object.class.getDeclaredConstructor()); constructor.setAccessible(true); return constructor; } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodecTest.java index 3e48781def..83e6dcee00 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodecTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/DynamicCodecTest.java @@ -52,7 +52,7 @@ public final class DynamicCodecTest { @Test public void testExample() throws Exception { new SerializationTester(new SimpleExample("a", "b", -5), new SimpleExample("a", null, 10)) - .addCodec(new DynamicCodec<>(SimpleExample.class)) + .addCodec(new DynamicCodec(SimpleExample.class)) .makeMemoizing() .runTests(); } @@ -83,7 +83,7 @@ public final class DynamicCodecTest { public void testExampleSubclass() throws Exception { new SerializationTester( new ExampleSubclass("a", "b", "c", 0), new ExampleSubclass("a", null, null, 15)) - .addCodec(new DynamicCodec<>(ExampleSubclass.class)) + .addCodec(new DynamicCodec(ExampleSubclass.class)) .makeMemoizing() .runTests(); } @@ -121,7 +121,7 @@ public final class DynamicCodecTest { new ExampleSmallPrimitives(false, (byte) 120, (short) 18000, 'x'), new ExampleSmallPrimitives(true, Byte.MIN_VALUE, Short.MIN_VALUE, Character.MIN_VALUE), new ExampleSmallPrimitives(true, Byte.MAX_VALUE, Short.MAX_VALUE, Character.MAX_VALUE)) - .addCodec(new DynamicCodec<>(ExampleSmallPrimitives.class)) + .addCodec(new DynamicCodec(ExampleSmallPrimitives.class)) .makeMemoizing() .runTests(); } @@ -153,7 +153,7 @@ public final class DynamicCodecTest { new ExampleMediumPrimitives(67890, -6e9f), new ExampleMediumPrimitives(Integer.MIN_VALUE, Float.MIN_VALUE), new ExampleMediumPrimitives(Integer.MAX_VALUE, Float.MAX_VALUE)) - .addCodec(new DynamicCodec<>(ExampleMediumPrimitives.class)) + .addCodec(new DynamicCodec(ExampleMediumPrimitives.class)) .makeMemoizing() .runTests(); } @@ -185,7 +185,7 @@ public final class DynamicCodecTest { new ExampleLargePrimitives(678900093045L, -9e180), new ExampleLargePrimitives(Long.MIN_VALUE, Double.MIN_VALUE), new ExampleLargePrimitives(Long.MAX_VALUE, Double.MAX_VALUE)) - .addCodec(new DynamicCodec<>(ExampleLargePrimitives.class)) + .addCodec(new DynamicCodec(ExampleLargePrimitives.class)) .makeMemoizing() .runTests(); } @@ -227,7 +227,7 @@ public final class DynamicCodecTest { new byte[] {-1, 0, 1}, new char[] {'a', 'b', 'c', 'x', 'y', 'z'}, new long[] {Long.MAX_VALUE, Long.MIN_VALUE, 27983741982341L, 52893748523495834L})) - .addCodec(new DynamicCodec<>(ArrayExample.class)) + .addCodec(new DynamicCodec(ArrayExample.class)) .makeMemoizing() .runTests(); } @@ -261,7 +261,7 @@ public final class DynamicCodecTest { {7} }), new NestedArrayExample(new int[][] {{1, 2, 3}, null, {7}})) - .addCodec(new DynamicCodec<>(NestedArrayExample.class)) + .addCodec(new DynamicCodec(NestedArrayExample.class)) .makeMemoizing() .runTests(); } @@ -312,8 +312,8 @@ public final class DynamicCodecTest { @Test public void testCyclic() throws Exception { new SerializationTester(createCycle(1, 2), createCycle(3, 4)) - .addCodec(new DynamicCodec<>(CycleA.class)) - .addCodec(new DynamicCodec<>(CycleB.class)) + .addCodec(new DynamicCodec(CycleA.class)) + .addCodec(new DynamicCodec(CycleB.class)) .makeMemoizing() .runTests(); } @@ -369,7 +369,7 @@ public final class DynamicCodecTest { new PrimitiveExample(true, 5, 20.0, EnumExample.THREE, null), new PrimitiveExample(true, 100, 100, null, "hello")) .addCodec( - new DynamicCodec<>( + new DynamicCodec( PrimitiveExample.class, ObjectCodec.MemoizationStrategy.DO_NOT_MEMOIZE)) .addCodec(new EnumCodec<>(EnumExample.class)) .setRepetitions(100000) |