Skip to content

Commit 3781a72

Browse files
authored
Refactor dinky class loader (#2671)
* refactor: DinkyClassLoader Signed-off-by: licho <[email protected]> * Spotless Apply --------- Signed-off-by: licho <[email protected]> Co-authored-by: leechor <[email protected]>
1 parent 6c73913 commit 3781a72

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

dinky-common/src/main/java/org/dinky/classloader/DinkyClassLoader.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,34 @@ public class DinkyClassLoader extends URLClassLoader {
4242
FlinkUdfPathContextHolder udfPathContextHolder = new FlinkUdfPathContextHolder();
4343

4444
public DinkyClassLoader(URL[] urls, ClassLoader parent) {
45-
super(new URL[] {}, parent);
45+
this(urls, parent, null);
4646
}
4747

4848
public DinkyClassLoader(Collection<File> fileSet, ClassLoader parent) {
49-
super(new URL[] {}, parent);
50-
addURLs(fileSet);
49+
this(convertFilesToUrls(fileSet), parent, null);
5150
}
5251

5352
public DinkyClassLoader(URL[] urls) {
54-
super(new URL[] {});
53+
this(urls, Thread.currentThread().getContextClassLoader(), null);
5554
}
5655

5756
public DinkyClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
58-
super(new URL[] {}, parent, factory);
59-
}
60-
61-
// this class factory method
62-
public static DinkyClassLoader build() {
63-
return new DinkyClassLoader(new URL[] {});
57+
super(urls, parent, factory);
6458
}
6559

6660
// class factory method with urls parameters
6761
public static DinkyClassLoader build(URL... urls) {
6862
return new DinkyClassLoader(urls);
6963
}
7064

65+
public static DinkyClassLoader build(URL[] urls, ClassLoader parent) {
66+
return new DinkyClassLoader(urls, parent);
67+
}
68+
69+
public static DinkyClassLoader build(ClassLoader parent) {
70+
return new DinkyClassLoader(new URL[] {}, parent);
71+
}
72+
7173
// return udfPathContextHolder
7274
public FlinkUdfPathContextHolder getUdfPathContextHolder() {
7375
return udfPathContextHolder;
@@ -80,30 +82,23 @@ public void addURLs(URL... urls) {
8082
}
8183

8284
public void addURLs(Collection<File> fileSet) {
83-
URL[] urls = fileSet.stream()
84-
.map(x -> {
85-
try {
86-
return x.toURI().toURL();
87-
} catch (MalformedURLException e) {
88-
throw new RuntimeException(e);
89-
}
90-
})
91-
.toArray(URL[]::new);
85+
URL[] urls = convertFilesToUrls(fileSet);
9286
addURLs(urls);
9387
}
9488

95-
public void addURLs(List<File> files) {
96-
files.stream()
89+
private static URL[] convertFilesToUrls(Collection<File> fileSet) {
90+
return fileSet.stream()
9791
.map(x -> {
9892
try {
9993
return x.toURI().toURL();
10094
} catch (MalformedURLException e) {
10195
throw new RuntimeException(e);
10296
}
10397
})
104-
.forEach(this::addURL);
98+
.toArray(URL[]::new);
10599
}
106100

101+
@Override
107102
public void addURL(URL url) {
108103
super.addURL(url);
109104
}

0 commit comments

Comments
 (0)