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 1 commit
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,13 @@

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;

/**
* Custom class loader used to load the tool implementation classes.
Expand All @@ -28,23 +33,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 40 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#L40

Added line #L40 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 45 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#L45

Added line #L45 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 50 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#L49-L50

Added lines #L49 - L50 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 54 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#L53-L54

Added lines #L53 - L54 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 57 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#L57

Added line #L57 was not covered by tests
}
}

Check warning on line 59 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#L59

Added line #L59 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(loadedClass);

Check warning on line 63 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#L63

Added line #L63 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be improved as resolveClass(Objects.requireNonNull(loadedClass));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with ecdf19e

}
return loadedClass;

Check warning on line 65 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#L65

Added line #L65 was not covered by tests
}

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

Check warning on line 70 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#L70

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

Check warning on line 73 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#L73

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

Check warning on line 75 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#L75

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

Check warning on line 78 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#L78

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

Check warning on line 80 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#L80

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

Check warning on line 84 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#L84

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

Check warning on line 86 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#L86

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

Check warning on line 89 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#L89

Added line #L89 was not covered by tests
}

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

Check warning on line 94 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#L94

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

Check warning on line 96 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#L96

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

Check warning on line 99 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#L99

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

Check warning on line 101 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#L101

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