Skip to content

Commit

Permalink
ROASTER-62: addThrows should not import java.lang types
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 28, 2015
1 parent f305a0f commit 2d577bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Expand Up @@ -621,22 +621,22 @@ else if (!method.equals(other.method))
@Override
public MethodSource<O> addThrows(final Class<? extends Exception> type)
{
return addThrows(type.getName());
return addThrows(type.getCanonicalName());
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public MethodSource<O> addThrows(final String type)
{
String packg = Types.getPackage(type);
String name = Types.toSimpleName(type);
String simpleTypeName = Types.toSimpleName(type);

if (!packg.isEmpty())
O origin = getOrigin();
if (!Strings.areEqual(type, simpleTypeName) && origin.requiresImport(type))
{
getOrigin().addImport(type);
origin.addImport(type);
}

SimpleName simpleName = method.getAST().newSimpleName(name);
SimpleName simpleName = method.getAST().newSimpleName(simpleTypeName);

List list = (List) method.getStructuralProperty(MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
list.add(method.getAST().newSimpleType(simpleName));
Expand Down
Expand Up @@ -9,6 +9,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ConcurrentModificationException;

import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.forge.roaster.model.source.MethodSource;
Expand Down Expand Up @@ -46,9 +48,9 @@ public void testParseThrowsMany() throws Exception
public void testAddThrowsOne() throws Exception, RuntimeException
{
MethodSource<JavaClassSource> method = Roaster.create(JavaClassSource.class).addMethod(
"public void hello(String foo, int bar)").addThrows(Exception.class);
"public void hello(String foo, int bar)").addThrows(ConcurrentModificationException.class);
assertEquals(1, method.getThrownExceptions().size());
assertTrue(method.getOrigin().hasImport(Exception.class));
assertTrue(method.getOrigin().hasImport(ConcurrentModificationException.class));
}

@Test
Expand Down Expand Up @@ -83,4 +85,12 @@ public void testRemoveThrowsMany() throws Exception
.removeThrows(Exception.class);
assertEquals(1, method.getThrownExceptions().size());
}

@Test
public void testAddThrowsExceptionShouldNotImportJavaLang() throws Exception
{
JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
javaClass.addMethod().setName("hello").addThrows(Exception.class);
assertEquals(0, javaClass.getImports().size());
}
}

0 comments on commit 2d577bd

Please sign in to comment.