Skip to content

Commit

Permalink
Catch LinkageError for ClassLoaderUtil.isClassPresent in case class i…
Browse files Browse the repository at this point in the history
…s present but is failed to load
  • Loading branch information
nobodyiam committed Nov 14, 2021
1 parent 02fff62 commit 7db3f7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Apollo 2.0.0
* [Support Java 17](https://github.com/apolloconfig/apollo/pull/4060)
* [Optimize top navbar style](https://github.com/apolloconfig/apollo/pull/4073)
* [Support export/import configs by apollo env](https://github.com/apolloconfig/apollo/pull/3947)
* [Catch LinkageError for ClassLoaderUtil.isClassPresent in case class is present but is failed to load](https://github.com/apolloconfig/apollo/pull/4097)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public static ClassLoader getLoader() {
return loader;
}


public static String getClassPath() {
return classPath;
}
Expand All @@ -71,6 +70,11 @@ public static boolean isClassPresent(String className) {
Class.forName(className);
return true;
} catch (ClassNotFoundException ex) {
// ignore expected exception
return false;
} catch (LinkageError ex) {
// unexpected error, need to let the user know the actual error
logger.error("Failed to load class: {}", className, ex);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ctrip.framework.apollo.core.utils;

import static org.junit.Assert.*;

import org.junit.Test;

public class ClassLoaderUtilTest {
private static boolean shouldFailInInitialization = false;
@Test
public void testGetClassLoader() {
assertNotNull(ClassLoaderUtil.getLoader());
}

@Test
public void testIsClassPresent() {
assertTrue(ClassLoaderUtil.isClassPresent("java.lang.String"));
}

@Test
public void testIsClassPresentWithClassNotFound() {
assertFalse(ClassLoaderUtil.isClassPresent("java.lang.StringNotFound"));
}

@Test
public void testIsClassPresentWithLinkageError() {
shouldFailInInitialization = true;
assertFalse(ClassLoaderUtil.isClassPresent(ClassWithInitializationError.class.getName()));
}

public static class ClassWithInitializationError {
static {
if (ClassLoaderUtilTest.shouldFailInInitialization) {
throw new RuntimeException("Some initialization exception");
}
}
}
}

0 comments on commit 7db3f7e

Please sign in to comment.