diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java index 7b482b8f..c393ea96 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java @@ -668,7 +668,10 @@ public ParameterSource addParameter(JavaType type, String name) @Override public ParameterSource addParameter(String type, String name) { - getOrigin().addImport(type); + if (!Types.isBasicType(type)) + { + getOrigin().addImport(type); + } String stub = "public class Stub { public void method( " + Types.toSimpleName(type) + " " + name + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List> methods = temp.getMethods(); diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/MethodSignatureTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/MethodSignatureTest.java index f296cea1..b4121912 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/MethodSignatureTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/MethodSignatureTest.java @@ -34,7 +34,8 @@ public void testEmptyMethodSignature() throws Exception @Test public void testMethodSignatureParams() throws Exception { - MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello(String foo, int bar)"); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod( + "public void hello(String foo, int bar)"); String signature = method.toSignature(); assertEquals("public hello(String, int) : void", signature); } @@ -42,7 +43,8 @@ public void testMethodSignatureParams() throws Exception @Test public void testMethodParams() throws Exception { - MethodSource method = Roaster.create(JavaClassSource.class).addMethod("public void hello(String foo, int bar)"); + MethodSource method = Roaster.create(JavaClassSource.class).addMethod( + "public void hello(String foo, int bar)"); List> parameters = method.getParameters(); Assert.assertEquals("String", parameters.get(0).getType().toString()); @@ -52,54 +54,73 @@ public void testMethodParams() throws Exception @Test(expected = UnsupportedOperationException.class) public void testUnmodifiableMethodParams() throws Exception { - Roaster.create(JavaClassSource.class).addMethod("public void hello(String foo, int bar)").getParameters().add(null); + Roaster.create(JavaClassSource.class).addMethod("public void hello(String foo, int bar)").getParameters() + .add(null); } @Test - public void testMethodVisibility() throws Exception { - JavaClassSource javaClass = Roaster.create(JavaClassSource.class); + public void testMethodVisibility() throws Exception + { + JavaClassSource javaClass = Roaster.create(JavaClassSource.class); - MethodSource method = javaClass.addMethod("public void hello()"); - assertVisibility(Visibility.PUBLIC, method); - assertVisibility("public", method); + MethodSource method = javaClass.addMethod("public void hello()"); + assertVisibility(Visibility.PUBLIC, method); + assertVisibility("public", method); - method = javaClass.addMethod("protected void hello()"); - assertVisibility(Visibility.PROTECTED, method); - assertVisibility("protected", method); + method = javaClass.addMethod("protected void hello()"); + assertVisibility(Visibility.PROTECTED, method); + assertVisibility("protected", method); - method = javaClass.addMethod("private void hello()"); - assertVisibility(Visibility.PRIVATE, method); - assertVisibility("private", method); + method = javaClass.addMethod("private void hello()"); + assertVisibility(Visibility.PRIVATE, method); + assertVisibility("private", method); - method = javaClass.addMethod("void hello()"); - assertVisibility(Visibility.PACKAGE_PRIVATE, method); - assertVisibility("", method); + method = javaClass.addMethod("void hello()"); + assertVisibility(Visibility.PACKAGE_PRIVATE, method); + assertVisibility("", method); } @Test - public void testMethodVisibilityWithSetter() throws Exception { - JavaClassSource javaClass = Roaster.create(JavaClassSource.class); - MethodSource method = javaClass.addMethod().setName("hello"); - assertVisibility("", method); + public void testMethodVisibilityWithSetter() throws Exception + { + JavaClassSource javaClass = Roaster.create(JavaClassSource.class); + MethodSource method = javaClass.addMethod().setName("hello"); + assertVisibility("", method); - method.setVisibility(Visibility.PUBLIC); - assertVisibility("public", method); + method.setVisibility(Visibility.PUBLIC); + assertVisibility("public", method); - method.setVisibility(Visibility.PROTECTED); - assertVisibility("protected", method); + method.setVisibility(Visibility.PROTECTED); + assertVisibility("protected", method); - method.setVisibility(Visibility.PRIVATE); - assertVisibility("private", method); + method.setVisibility(Visibility.PRIVATE); + assertVisibility("private", method); + + method.setVisibility(Visibility.PACKAGE_PRIVATE); + assertVisibility("", method); + } - method.setVisibility(Visibility.PACKAGE_PRIVATE); - assertVisibility("", method); + @Test + public void testMethodWithPrimitiveParameters() throws Exception + { + JavaClassSource javaClass = Roaster.create(JavaClassSource.class); + MethodSource method = javaClass.addMethod().setPublic().setName("doSomething").setReturnType(Integer.TYPE).setBody("return 0;"); + method.addParameter(Integer.TYPE, "initValue"); + method.addParameter(int.class,"intValueClass"); + Assert.assertEquals(1, javaClass.getMethods().size()); + List> parameters = javaClass.getMethods().get(0).getParameters(); + Assert.assertEquals(2, parameters.size()); + Assert.assertTrue(parameters.get(0).getType().isPrimitive()); + Assert.assertTrue(parameters.get(1).getType().isPrimitive()); } - private void assertVisibility(Visibility visibility, MethodSource method) { - Assert.assertEquals(visibility, method.getVisibility()); + private void assertVisibility(Visibility visibility, MethodSource method) + { + Assert.assertEquals(visibility, method.getVisibility()); } - private void assertVisibility(String visibility, MethodSource method) { - Assert.assertEquals(visibility, method.getVisibility().toString()); + private void assertVisibility(String visibility, MethodSource method) + { + Assert.assertEquals(visibility, method.getVisibility().toString()); } }