From 1244425b26a4a5c31b5eac60fde30e9012835d2b Mon Sep 17 00:00:00 2001 From: Fokko Driesprong Date: Wed, 28 Oct 2026 23:55:49 +0100 Subject: [PATCH] GH-3897: Replace deprecated Class.newInstance() Class.newInstance() has been deprecated since Java 9. Use getDeclaredConstructor().newInstance() instead and widen the catch clauses to ReflectiveOperationException, keeping the existing wrapping exceptions and messages. As a result, checked exceptions thrown by a constructor now arrive wrapped in an InvocationTargetException instead of being sneaky-thrown. Co-authored-by: Isaac --- .../org/apache/parquet/filter2/predicate/Operators.java | 4 ++-- .../java/org/apache/parquet/format/event/Consumers.java | 4 ++-- .../parquet/crypto/DecryptionPropertiesFactory.java | 5 +++-- .../parquet/crypto/EncryptionPropertiesFactory.java | 5 +++-- .../org/apache/parquet/crypto/keytools/KeyToolkit.java | 4 ++-- .../org/apache/parquet/hadoop/ParquetInputFormat.java | 8 ++++---- .../org/apache/parquet/hadoop/ParquetOutputFormat.java | 3 ++- .../parquet/hadoop/thrift/ThriftBytesWriteSupport.java | 6 ++++-- .../org/apache/parquet/thrift/TBaseRecordConverter.java | 7 ++++--- .../apache/parquet/thrift/TestProtocolReadToWrite.java | 7 ++++--- 10 files changed, 30 insertions(+), 23 deletions(-) diff --git a/parquet-column/src/main/java/org/apache/parquet/filter2/predicate/Operators.java b/parquet-column/src/main/java/org/apache/parquet/filter2/predicate/Operators.java index 60dc80cd7b..2df9ac726b 100644 --- a/parquet-column/src/main/java/org/apache/parquet/filter2/predicate/Operators.java +++ b/parquet-column/src/main/java/org/apache/parquet/filter2/predicate/Operators.java @@ -662,8 +662,8 @@ public Class getUserDefinedPredicateClass() { @Override public U getUserDefinedPredicate() { try { - return udpClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return udpClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new RuntimeException(String.format(INSTANTIATION_ERROR_MESSAGE, udpClass), e); } } diff --git a/parquet-format-structures/src/main/java/org/apache/parquet/format/event/Consumers.java b/parquet-format-structures/src/main/java/org/apache/parquet/format/event/Consumers.java index 6e375c1c24..86f15112c7 100644 --- a/parquet-format-structures/src/main/java/org/apache/parquet/format/event/Consumers.java +++ b/parquet-format-structures/src/main/java/org/apache/parquet/format/event/Consumers.java @@ -186,8 +186,8 @@ public void consumeStruct(TProtocol protocol, EventBasedThriftReader reader) thr protected T newObject() { try { - return c.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return c.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new RuntimeException(c.getName(), e); } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/DecryptionPropertiesFactory.java b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/DecryptionPropertiesFactory.java index 621b91c0dc..2d5aa32f7a 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/DecryptionPropertiesFactory.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/DecryptionPropertiesFactory.java @@ -66,8 +66,9 @@ static DecryptionPropertiesFactory loadFactory(Configuration conf) { } try { - return (DecryptionPropertiesFactory) decryptionPropertiesFactoryClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return (DecryptionPropertiesFactory) + decryptionPropertiesFactoryClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new BadConfigurationException( "could not instantiate decryptionPropertiesFactoryClass class: " + decryptionPropertiesFactoryClass, e); diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/EncryptionPropertiesFactory.java b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/EncryptionPropertiesFactory.java index a49ae2db1b..77042214a6 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/EncryptionPropertiesFactory.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/EncryptionPropertiesFactory.java @@ -83,8 +83,9 @@ static EncryptionPropertiesFactory loadFactory(ParquetConfiguration conf) { } try { - return (EncryptionPropertiesFactory) encryptionPropertiesFactoryClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return (EncryptionPropertiesFactory) + encryptionPropertiesFactoryClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new BadConfigurationException( "could not instantiate encryptionPropertiesFactoryClass class: " + encryptionPropertiesFactoryClass, e); diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/keytools/KeyToolkit.java b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/keytools/KeyToolkit.java index 9d3da61ecd..f0124d0fc5 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/crypto/keytools/KeyToolkit.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/crypto/keytools/KeyToolkit.java @@ -562,8 +562,8 @@ private static KmsClient createAndInitKmsClient( if (null == kmsClientClass) { throw new ParquetCryptoRuntimeException("Unspecified " + KMS_CLIENT_CLASS_PROPERTY_NAME); } - kmsClient = (KmsClient) kmsClientClass.newInstance(); - } catch (InstantiationException | IllegalAccessException | BadConfigurationException e) { + kmsClient = (KmsClient) kmsClientClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException | BadConfigurationException e) { throw new ParquetCryptoRuntimeException("Could not instantiate KmsClient class: " + kmsClientClass, e); } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetInputFormat.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetInputFormat.java index 811e1f3833..288e1d241e 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetInputFormat.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetInputFormat.java @@ -210,8 +210,8 @@ private static UnboundRecordFilter getUnboundRecordFilterInstance(ParquetConfigu return null; } try { - return (UnboundRecordFilter) clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return (UnboundRecordFilter) clazz.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new BadConfigurationException("could not instantiate unbound record filter class", e); } } @@ -332,8 +332,8 @@ public static ReadSupport getReadSupportInstance(Configuration configurat @SuppressWarnings("unchecked") static ReadSupport getReadSupportInstance(Class> readSupportClass) { try { - return readSupportClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return readSupportClass.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { throw new BadConfigurationException("could not instantiate read support class", e); } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetOutputFormat.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetOutputFormat.java index 4db288f455..0e8823001d 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetOutputFormat.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetOutputFormat.java @@ -628,8 +628,9 @@ public WriteSupport getWriteSupport(Configuration configuration) { Class writeSupportClass = getWriteSupportClass(configuration); try { return (WriteSupport) Objects.requireNonNull(writeSupportClass, "writeSupportClass cannot be null") + .getDeclaredConstructor() .newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + } catch (ReflectiveOperationException e) { throw new BadConfigurationException("could not instantiate write support class: " + writeSupportClass, e); } } diff --git a/parquet-thrift/src/main/java/org/apache/parquet/hadoop/thrift/ThriftBytesWriteSupport.java b/parquet-thrift/src/main/java/org/apache/parquet/hadoop/thrift/ThriftBytesWriteSupport.java index 0ed3f0edf6..790e65de5b 100644 --- a/parquet-thrift/src/main/java/org/apache/parquet/hadoop/thrift/ThriftBytesWriteSupport.java +++ b/parquet-thrift/src/main/java/org/apache/parquet/hadoop/thrift/ThriftBytesWriteSupport.java @@ -154,8 +154,10 @@ public WriteContext init(ParquetConfiguration configuration) { this.configuration = configuration; if (this.protocolFactory == null) { try { - this.protocolFactory = getTProtocolFactoryClass(configuration).newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + this.protocolFactory = getTProtocolFactoryClass(configuration) + .getDeclaredConstructor() + .newInstance(); + } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } diff --git a/parquet-thrift/src/main/java/org/apache/parquet/thrift/TBaseRecordConverter.java b/parquet-thrift/src/main/java/org/apache/parquet/thrift/TBaseRecordConverter.java index 22c928137f..144b6e553d 100644 --- a/parquet-thrift/src/main/java/org/apache/parquet/thrift/TBaseRecordConverter.java +++ b/parquet-thrift/src/main/java/org/apache/parquet/thrift/TBaseRecordConverter.java @@ -59,14 +59,15 @@ public TBaseRecordConverter( @Override public T readOneRecord(TProtocol protocol) throws TException { try { - T thriftObject = thriftClass.newInstance(); + T thriftObject = + thriftClass.getDeclaredConstructor().newInstance(); thriftObject.read(protocol); return thriftObject; - } catch (InstantiationException e) { - throw new ParquetDecodingException("Could not instantiate Thrift " + thriftClass, e); } catch (IllegalAccessException e) { throw new ParquetDecodingException( "Thrift class or constructor not public " + thriftClass, e); + } catch (ReflectiveOperationException e) { + throw new ParquetDecodingException("Could not instantiate Thrift " + thriftClass, e); } } }, diff --git a/parquet-thrift/src/test/java/org/apache/parquet/thrift/TestProtocolReadToWrite.java b/parquet-thrift/src/test/java/org/apache/parquet/thrift/TestProtocolReadToWrite.java index a920276377..1918dd5ba1 100644 --- a/parquet-thrift/src/test/java/org/apache/parquet/thrift/TestProtocolReadToWrite.java +++ b/parquet-thrift/src/test/java/org/apache/parquet/thrift/TestProtocolReadToWrite.java @@ -124,7 +124,7 @@ public void testMapSet() throws Exception { writeReadCompare(a); } - private void writeReadCompare(TBase a) throws TException, InstantiationException, IllegalAccessException { + private void writeReadCompare(TBase a) throws TException, ReflectiveOperationException { ProtocolPipe[] pipes = { new ProtocolReadToWrite(), new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType((Class>) a.getClass())) @@ -134,7 +134,7 @@ private void writeReadCompare(TBase a) throws TException, InstantiationExc final ByteArrayOutputStream out = new ByteArrayOutputStream(); a.write(protocol(in)); p.readOne(protocol(new ByteArrayInputStream(in.toByteArray())), protocol(out)); - TBase b = a.getClass().newInstance(); + TBase b = a.getClass().getDeclaredConstructor().newInstance(); b.read(protocol(new ByteArrayInputStream(out.toByteArray()))); assertThat(b).as(p.getClass().getSimpleName()).isEqualTo(a); @@ -372,7 +372,8 @@ public void handleFieldIgnored(TField field) { assertThat(countingHandler.recordCountOfMissingFields).isEqualTo(1); assertThat(countingHandler.fieldIgnoredCount).isEqualTo(1); - StructV4WithExtracStructField b = StructV4WithExtracStructField.class.newInstance(); + StructV4WithExtracStructField b = + StructV4WithExtracStructField.class.getDeclaredConstructor().newInstance(); b.read(protocol(new ByteArrayInputStream(out.toByteArray()))); assertThat(b.getName()).isEqualTo(dataWithNewSchema.getName()); assertThat(b.getAge()).isEqualTo(dataWithNewSchema.getAge());