parent
a1d903e6e8
commit
b003d5b082
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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<String> 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue