Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2201.8.x] Fix CustomToolClassLoader linkage issue #42056

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@

package io.ballerina.cli.launcher;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
* Custom class loader used to load the tool implementation classes.
Expand All @@ -28,23 +34,71 @@
* @since 2201.8.0
*/
public class CustomToolClassLoader extends URLClassLoader {
private final ClassLoader system;

public CustomToolClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
system = getSystemClassLoader();

Check warning on line 41 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L41

Added line #L41 was not covered by tests
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
try {
// Load from parent if cli or picocli classes. This is to avoid SPI and class loading issues
if (name.startsWith("io.ballerina.cli") || name.startsWith("picocli")) {
return super.loadClass(name, resolve);
Class<?> loadedClass = findLoadedClass(name);

Check warning on line 46 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L46

Added line #L46 was not covered by tests
if (loadedClass == null) {
try {
// First, try to load the class from the URLs
loadedClass = findClass(name);
} catch (ClassNotFoundException e) {

Check warning on line 51 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L50-L51

Added lines #L50 - L51 were not covered by tests
try {
// If not found, delegate to the parent
loadedClass = super.loadClass(name, resolve);
} catch (ClassNotFoundException e2) {

Check warning on line 55 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L54-L55

Added lines #L54 - L55 were not covered by tests
// If not found, delegate to the system class loader
if (system != null) {
loadedClass = system.loadClass(name);

Check warning on line 58 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L58

Added line #L58 was not covered by tests
}
}

Check warning on line 60 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L60

Added line #L60 was not covered by tests
}
// First, try to load the class from the URLs
return findClass(name);
} catch (ClassNotFoundException e) {
// If not found, delegate to the parent
return super.loadClass(name, resolve);
}
if (resolve) {
resolveClass(Objects.requireNonNull(loadedClass));

Check warning on line 64 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L64

Added line #L64 was not covered by tests
}
return loadedClass;

Check warning on line 66 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L66

Added line #L66 was not covered by tests
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
List<URL> allResources = new LinkedList<>();

Check warning on line 71 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L71

Added line #L71 was not covered by tests
Enumeration<URL> sysResource;
if (system != null) {
sysResource = system.getResources(name);

Check warning on line 74 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L74

Added line #L74 was not covered by tests
while (sysResource.hasMoreElements()) {
allResources.add(sysResource.nextElement());

Check warning on line 76 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L76

Added line #L76 was not covered by tests
}
}
Enumeration<URL> thisResource = findResources(name);

Check warning on line 79 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L79

Added line #L79 was not covered by tests
while (thisResource.hasMoreElements()) {
allResources.add(thisResource.nextElement());

Check warning on line 81 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L81

Added line #L81 was not covered by tests
}
Enumeration<URL> parentResource;
if (getParent() != null) {
parentResource = getParent().getResources(name);

Check warning on line 85 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L85

Added line #L85 was not covered by tests
while (parentResource.hasMoreElements()) {
allResources.add(parentResource.nextElement());

Check warning on line 87 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L87

Added line #L87 was not covered by tests
}
}
return Collections.enumeration(allResources);

Check warning on line 90 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L90

Added line #L90 was not covered by tests
}

@Override
public URL getResource(String name) {
URL resource = findResource(name);

Check warning on line 95 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L95

Added line #L95 was not covered by tests
if (resource == null) {
resource = super.getResource(name);

Check warning on line 97 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L97

Added line #L97 was not covered by tests
}
if (resource == null && system != null) {
resource = system.getResource(name);

Check warning on line 100 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L100

Added line #L100 was not covered by tests
}
return resource;

Check warning on line 102 in cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/launcher/CustomToolClassLoader.java#L102

Added line #L102 was not covered by tests
}
}
Loading