Skip to content

Commit f859462

Browse files
X-czhhuwh
authored andcommitted
[FLINK-37735][serializer] Replace class.newInstance with constructor.newInstance
1 parent 160433d commit f859462

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializer.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.io.ObjectInputStream;
3333
import java.io.ObjectOutputStream;
34+
import java.lang.reflect.Constructor;
3435
import java.lang.reflect.Field;
3536
import java.lang.reflect.Modifier;
3637
import java.util.Arrays;
@@ -61,6 +62,11 @@ public final class PojoSerializer<T> extends TypeSerializer<T> {
6162
/** The POJO type class. */
6263
private final Class<T> clazz;
6364

65+
/**
66+
* The cached constructor, which is not serializable and kept as a separate transient member.
67+
*/
68+
private transient Constructor<T> constructor;
69+
6470
/**
6571
* Fields of the POJO and their serializers.
6672
*
@@ -212,14 +218,26 @@ public T createInstance() {
212218
return null;
213219
}
214220
try {
215-
T t = clazz.newInstance();
221+
T t = instantiateRaw();
216222
initializeFields(t);
217223
return t;
218224
} catch (Exception e) {
219225
throw new RuntimeException("Cannot instantiate class.", e);
220226
}
221227
}
222228

229+
private T instantiateRaw() {
230+
try {
231+
if (constructor == null) {
232+
constructor = clazz.getDeclaredConstructor();
233+
constructor.setAccessible(true);
234+
}
235+
return constructor.newInstance();
236+
} catch (Exception e) {
237+
throw new RuntimeException("Cannot instantiate class.", e);
238+
}
239+
}
240+
223241
protected void initializeFields(T t) {
224242
for (int i = 0; i < numFields; i++) {
225243
if (fields[i] != null) {
@@ -256,7 +274,7 @@ public T copy(T from) {
256274
} else if (actualType == clazz) {
257275
T target;
258276
try {
259-
target = (T) from.getClass().newInstance();
277+
target = instantiateRaw();
260278
} catch (Throwable t) {
261279
throw new RuntimeException("Cannot instantiate class.", t);
262280
}

flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/TupleSerializer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@
2727
import org.apache.flink.types.NullFieldException;
2828

2929
import java.io.IOException;
30+
import java.lang.reflect.Constructor;
3031

3132
@Internal
3233
public class TupleSerializer<T extends Tuple> extends TupleSerializerBase<T> {
3334

3435
private static final long serialVersionUID = 1L;
3536

37+
/**
38+
* The cached constructor, which is not serializable and kept as a separate transient member.
39+
*/
40+
private transient Constructor<T> constructor;
41+
3642
public TupleSerializer(Class<T> tupleClass, TypeSerializer<?>[] fieldSerializers) {
3743
super(tupleClass, fieldSerializers);
3844
}
@@ -61,7 +67,7 @@ public TupleSerializer<T> duplicate() {
6167
@Override
6268
public T createInstance() {
6369
try {
64-
T t = tupleClass.newInstance();
70+
T t = instantiateRaw();
6571

6672
for (int i = 0; i < arity; i++) {
6773
t.setField(fieldSerializers[i].createInstance(), i);
@@ -77,7 +83,7 @@ public T createInstance() {
7783
public T createInstance(Object[] fields) {
7884

7985
try {
80-
T t = tupleClass.newInstance();
86+
T t = instantiateRaw();
8187

8288
for (int i = 0; i < arity; i++) {
8389
t.setField(fields[i], i);
@@ -163,7 +169,11 @@ public TypeSerializerSnapshot<T> snapshotConfiguration() {
163169

164170
private T instantiateRaw() {
165171
try {
166-
return tupleClass.newInstance();
172+
if (constructor == null) {
173+
constructor = tupleClass.getDeclaredConstructor();
174+
constructor.setAccessible(true);
175+
}
176+
return constructor.newInstance();
167177
} catch (Exception e) {
168178
throw new RuntimeException("Cannot instantiate tuple.", e);
169179
}

0 commit comments

Comments
 (0)