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 Jan 1, 2022
1 parent 9f2655c commit e4516e3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Apollo 1.9.2
* [Fix the issue that property placeholder doesn't work for dubbo reference beans](https://github.com/apolloconfig/apollo/pull/4161)
* [Update xstream version to 1.4.18](https://github.com/apolloconfig/apollo/pull/4177)
* [Fix the NPE occurred when using EnableApolloConfig with Spring 3.1.1](https://github.com/apolloconfig/apollo/pull/4179)
* [Catch LinkageError for ClassLoaderUtil.isClassPresent in case class is present but is failed to load](https://github.com/apolloconfig/apollo/pull/4187)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/10?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");
}
}
}
}

0 comments on commit e4516e3

Please sign in to comment.