Implementation:Risingwavelabs Risingwave DynConstructors
| Property | Value |
|---|---|
| File | java/connector-node/risingwave-sink-iceberg/src/main/java/org/apache/iceberg/common/DynConstructors.java
|
| Language | Java |
| Lines | 298 |
| Category | Utility |
| Package | org.apache.iceberg.common
|
| Origin | Ported from Apache Parquet / Apache Iceberg project |
Overview
DynConstructors is a utility class providing a builder pattern for dynamically locating and invoking Java constructors via reflection. Originally copied from parquet-common and ported into the Apache Iceberg project, it enables dynamic instantiation of classes without compile-time constructor dependencies. The class provides a Builder for searching constructors across multiple classes and a Ctor wrapper that extends DynMethods.UnboundMethod for unified invocation semantics.
Code Reference
Source Location
Signature
public class DynConstructors {
private DynConstructors() {}
public static Builder builder();
public static Builder builder(Class<?> baseClass);
public static class Ctor<C> extends DynMethods.UnboundMethod {
public Class<? extends C> getConstructedClass();
public C newInstanceChecked(Object... args) throws Exception;
public C newInstance(Object... args);
public <R> R invoke(Object target, Object... args);
public <R> R invokeChecked(Object target, Object... args) throws Exception;
}
public static class Builder {
public Builder loader(ClassLoader newLoader);
public Builder impl(String className, Class<?>... types);
public <T> Builder impl(Class<T> targetClass, Class<?>... types);
public Builder hiddenImpl(Class<?>... types);
public Builder hiddenImpl(String className, Class<?>... types);
public <T> Builder hiddenImpl(Class<T> targetClass, Class<?>... types);
public <C> Ctor<C> buildChecked() throws NoSuchMethodException;
public <C> Ctor<C> build();
}
}
Imports
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.base.Throwables;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Map;
I/O Contract
Ctor (Inner Class)
The Ctor<C> class wraps a Java Constructor<C> and provides:
| Method | Description |
|---|---|
| getConstructedClass() | Returns the Class that this constructor creates
|
| newInstanceChecked(Object... args) | Creates a new instance; if more args are provided than the constructor accepts, truncates to match parameter count. Propagates checked exceptions. |
| newInstance(Object... args) | Creates a new instance; wraps checked exceptions as RuntimeException
|
| invoke(Object target, Object... args) | Delegates to newInstance(); target must be null (enforced by precondition)
|
| invokeChecked(Object target, Object... args) | Delegates to newInstanceChecked(); target must be null
|
| bind(Object receiver) | Always throws IllegalStateException (constructors cannot be bound)
|
| isStatic() | Always returns true
|
Builder Methods
| Method | Description |
|---|---|
| loader(ClassLoader) | Sets the ClassLoader for class lookup (currently a no-op in this implementation) |
| impl(String className, Class<?>... types) | Tries to find a public constructor on the named class matching the given parameter types |
| impl(Class<T> targetClass, Class<?>... types) | Tries to find a public constructor on the given class matching the parameter types |
| hiddenImpl(Class<?>... types) | Tries to find a hidden (non-public) constructor on the base class |
| hiddenImpl(String className, Class<?>... types) | Tries to find a hidden constructor on the named class matching the parameter types |
| hiddenImpl(Class<T> targetClass, Class<?>... types) | Tries to find a hidden constructor on the given class; uses AccessController.doPrivileged() to make it accessible
|
| buildChecked() | Returns the found Ctor; throws NoSuchMethodException with detailed problem descriptions if none found
|
| build() | Returns the found Ctor; throws RuntimeException with detailed problem descriptions if none found
|
Error Reporting
The builder maintains a Map<String, Throwable> of problems encountered during constructor searches. When build() or buildChecked() fails, the error message includes all attempted methods and their failure reasons, with each original exception added as a suppressed exception.
Usage Examples
// Find a public constructor by class name and parameter types
DynConstructors.Ctor<MyCatalog> ctor = DynConstructors.builder()
.impl("org.apache.iceberg.aws.glue.GlueCatalog")
.impl("org.apache.iceberg.hive.HiveCatalog")
.buildChecked();
MyCatalog catalog = ctor.newInstance();
// Find a hidden constructor with specific parameter types
DynConstructors.Ctor<MyClass> ctor = DynConstructors.builder(MyClass.class)
.hiddenImpl(MyClass.class, String.class, int.class)
.build();
MyClass instance = ctor.newInstance("arg1", 42);
// Using with a base class constraint
DynConstructors.Ctor<Catalog> ctor = DynConstructors.builder(Catalog.class)
.impl("com.example.CustomCatalog", Map.class)
.build();
Catalog catalog = ctor.newInstance(configMap);
Related Pages
- DynClasses - Companion utility for dynamically loading classes by name
- JniCatalogWrapper LoadTable - Iceberg catalog operations that may use dynamic constructor invocation