Make TypeSet resolvers null-safe
This commit is contained in:
@ -99,6 +99,9 @@ public class TypeSet {
|
||||
}
|
||||
@Override
|
||||
public Class<?> resolve(String name) throws ClassNotFoundException {
|
||||
if (name == null) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
for (String importStmt : importStmts) {
|
||||
if (importStmt.endsWith(name)) {
|
||||
return pmdClassLoader.loadClass(importStmt);
|
||||
@ -210,7 +213,7 @@ public class TypeSet {
|
||||
public static class VoidResolver implements Resolver {
|
||||
@Override
|
||||
public Class<?> resolve(String name) throws ClassNotFoundException {
|
||||
if (name.equals("void")) {
|
||||
if ("void".equals(name)) {
|
||||
return void.class;
|
||||
}
|
||||
throw new ClassNotFoundException(name);
|
||||
@ -231,6 +234,9 @@ public class TypeSet {
|
||||
}
|
||||
@Override
|
||||
public Class<?> resolve(String name) throws ClassNotFoundException {
|
||||
if (name == null) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
return pmdClassLoader.loadClass(name);
|
||||
}
|
||||
}
|
||||
|
@ -90,12 +90,24 @@ public class TypeSetTest {
|
||||
assertEquals(long.class, r.resolve("long"));
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testPrimitiveTypeResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.PrimitiveTypeResolver();
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVoidTypeResolver() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.VoidResolver();
|
||||
assertEquals(void.class, r.resolve("void"));
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testVoidTypeResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.VoidResolver();
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitImportResolver() throws Throwable {
|
||||
Set<String> imports = new HashSet<String>();
|
||||
@ -104,6 +116,20 @@ public class TypeSetTest {
|
||||
assertEquals(File.class, r.resolve("File"));
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testExplicitImportResolverWithNull() throws Throwable {
|
||||
Set<String> imports = new HashSet<String>();
|
||||
imports.add("java.io.File");
|
||||
TypeSet.Resolver r = new TypeSet.ExplicitImportResolver(pmdClassLoader, imports);
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testExplicitImportResolverWithNullAndEmptyImports() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.ExplicitImportResolver(pmdClassLoader, new HashSet<String>());
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImplicitImportResolverPass() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.ImplicitImportResolver(pmdClassLoader);
|
||||
@ -116,12 +142,24 @@ public class TypeSetTest {
|
||||
r.resolve("PMD");
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testImplicitImportResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.ImplicitImportResolver(pmdClassLoader);
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCurrentPackageResolverPass() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.CurrentPackageResolver(pmdClassLoader, "net.sourceforge.pmd");
|
||||
assertEquals(PMD.class, r.resolve("PMD"));
|
||||
}
|
||||
|
||||
@Test(expected=ClassNotFoundException.class)
|
||||
public void testCurrentPackageResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.CurrentPackageResolver(pmdClassLoader, "net.sourceforge.pmd");
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportOnDemandResolverPass() throws Throwable {
|
||||
TypeSet.Resolver r = getResolver();
|
||||
@ -129,6 +167,12 @@ public class TypeSetTest {
|
||||
assertEquals(File.class, r.resolve("File"));
|
||||
}
|
||||
|
||||
@Test(expected = ClassNotFoundException.class)
|
||||
public void testImportOnDemandResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = getResolver();
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
@Test(expected = ClassNotFoundException.class)
|
||||
public void importOnDemandResolverFail1() throws Throwable {
|
||||
TypeSet.Resolver r = getResolver();
|
||||
@ -149,6 +193,12 @@ public class TypeSetTest {
|
||||
return r;
|
||||
}
|
||||
|
||||
@Test(expected = ClassNotFoundException.class)
|
||||
public void testFullyQualifiedNameResolverWithNull() throws Throwable {
|
||||
TypeSet.Resolver r = new TypeSet.FullyQualifiedNameResolver(pmdClassLoader);
|
||||
r.resolve(null);
|
||||
}
|
||||
|
||||
public static junit.framework.Test suite() {
|
||||
return new junit.framework.JUnit4TestAdapter(TypeSetTest.class);
|
||||
}
|
||||
|
Reference in New Issue
Block a user