wangzhao created FLINK-39687:
--------------------------------
Summary: URI used as URL in SessionContext causes
ArrayStoreException
Key: FLINK-39687
URL: https://issues.apache.org/jira/browse/FLINK-39687
Project: Flink
Issue Type: Bug
Components: Table SQL / Gateway
Reporter: wangzhao
## Location
`flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/context/SessionContext.java`
line 306
## Faulty Code
```java
defaultContext.getDependencies().toArray(new URL[0])
```
## Root Cause
`DefaultContext.getDependencies()` returns `List<URI>` (`java.net.URI`), but
the code calls `.toArray(new URL[0])`.
`java.net.URI` and `java.net.URL` are **completely unrelated classes** (no
inheritance relationship), so a `URL[]` array cannot hold `URI` objects.
Java's `List.toArray(T[])` generic method does not check element type
compatibility at compile time, so this compiles successfully. However, it will
**always throw `ArrayStoreException` at runtime** when the `toArray`
implementation attempts to store a `URI` into a `URL[]`.
## When It Triggers
This bug triggers when `defaultContext.getDependencies()` returns a non-empty
list. It currently goes unnoticed because:
- `DefaultContext.load()` creates a DefaultContext with an empty dependency
list (the `dependencies` parameter is always empty in current callers)
- `ScriptRunner` passes `Collections.emptyList()` directly (empty array,
`toArray` never needs to store elements, so no exception)
Once anyone creates a DefaultContext with non-empty dependencies, the exception
will fire.
## Fix
Convert each `URI` to `URL` individually:
```java
defaultContext.getDependencies().stream()
.map(uri -> uri.toURL())
.toArray(URL[]::new)
```
## Introduced In
FLINK 2.2.0 (commit: 5a336892424)
--
This message was sent by Atlassian Jira
(v8.20.10#820010)