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

Catch LinkageError for ClassLoaderUtil.isClassPresent in case class is present but is failed to load #4097

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
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
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,53 @@
/*
* Copyright 2021 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
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");
}
}
}
}