diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java index a231cee68..dd1f510bb 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java @@ -169,7 +169,7 @@ public class ClassResolver { safeLoad(name); return true; } - catch (final ClassNotFoundException ignored) { + catch (final ClassNotFoundException | NoClassDefFoundError ignored) { return false; } } @@ -180,8 +180,9 @@ public class ClassResolver { * @param name name of the class to load * @return the {@code Class} for the specified class * @throws ClassNotFoundException if an error occurs + * @throws NoClassDefFoundError if an error occurs */ - public Class safeLoad(String name) throws ClassNotFoundException { + private Class safeLoad(String name) throws ClassNotFoundException, NoClassDefFoundError { // The next line will load the class using the specified class // loader. The magic is having the "false" parameter. This means the // class will not be initialised. Very, very important. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java index 7a3061f3d..bf1c2a471 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -150,4 +151,32 @@ public class ClassResolverTest { assertTrue(ex.getMessage().endsWith("expected exception")); } } + + /** + * This test exists to prevent any possible regression and let of + * https://github.com/checkstyle/checkstyle/issues/1192 to be persistent + * event is not very obvious + * + * @throws Exception when smth is not expected + */ + @Test + public void testIsLoadableWithNoClassDefFoundError() throws Exception { + final Set imports = Sets.newHashSet(); + imports.add("java.applet.someClass"); + + final ClassResolver classResolver = PowerMockito.spy(new ClassResolver(Thread + .currentThread().getContextClassLoader(), "", imports)); + + PowerMockito.doThrow(new NoClassDefFoundError("expected exception")) + .when(classResolver, "safeLoad", anyObject()); + + try { + final boolean result = classResolver.isLoadable("someClass"); + assertFalse("result should be false", result); + } + catch (NoClassDefFoundError ex) { + fail("NoClassDefFoundError is not expected"); + assertTrue(ex.getMessage().endsWith("expected exception")); + } + } }