From e4516e38226b28d321135311f5fe23723ea63bc6 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 1 Jan 2022 12:49:43 +0800 Subject: [PATCH] Catch LinkageError for ClassLoaderUtil.isClassPresent in case class is present but is failed to load --- CHANGES.md | 1 + .../apollo/core/utils/ClassLoaderUtil.java | 6 ++- .../core/utils/ClassLoaderUtilTest.java | 53 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtilTest.java diff --git a/CHANGES.md b/CHANGES.md index b75a02eadea..4411409bf67 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtil.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtil.java index 9d82d3d1efc..5ced2bd2c33 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtil.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtil.java @@ -61,7 +61,6 @@ public static ClassLoader getLoader() { return loader; } - public static String getClassPath() { return classPath; } @@ -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; } } diff --git a/apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtilTest.java b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtilTest.java new file mode 100644 index 00000000000..6c11051d1a2 --- /dev/null +++ b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/ClassLoaderUtilTest.java @@ -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"); + } + } + } +} \ No newline at end of file