diff --git a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java index d6ef1cf7..c54597e2 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/source/MethodSource.java @@ -104,4 +104,21 @@ public interface MethodSource> extends Method removeParameter(ParameterSource parameter); + + /** + * Remove a parameter with the specified {@link Class} type and name from this method + */ + MethodSource removeParameter(Class type, String name); + + /** + * Remove a parameter with the specified type and name from this method + */ + MethodSource removeParameter(String type, String name); + + /** + * Remove a parameter with the specified {@link JavaType} type and name to this method + */ + MethodSource removeParameter(JavaType type, String name); + + } \ No newline at end of file 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 614bd52e..bb7bc32d 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 @@ -672,7 +672,8 @@ public ParameterSource addParameter(String type, String name) { getOrigin().addImport(type); } - String stub = "public class Stub { public void method( " + Types.toSimpleName(Types.stripGenerics(type)) + " " + name + " ) {} }"; + String stub = "public class Stub { public void method( " + Types.toSimpleName(Types.stripGenerics(type)) + " " + + name + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List> methods = temp.getMethods(); List astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); @@ -693,4 +694,62 @@ public MethodSource removeParameter(ParameterSource parameter) method.parameters().remove(parameter.getInternal()); return this; } + + @Override + public MethodSource removeParameter(Class type, String name) + { + ParameterSource parameter = null; + for (ParameterSource param : getParameters()) + { + if (param.getType().isType(type) && param.getName().equals(name)) + { + parameter = param; + break; + } + } + if (parameter != null) + { + removeParameter(parameter); + } + return this; + } + + @Override + public MethodSource removeParameter(JavaType type, String name) + { + ParameterSource parameter = null; + for (ParameterSource param : getParameters()) + { + if (param.getType().isType(type.getCanonicalName()) && param.getName().equals(name)) + { + parameter = param; + break; + } + } + if (parameter != null) + { + removeParameter(parameter); + } + return this; + } + + @Override + public MethodSource removeParameter(String type, String name) + { + ParameterSource parameter = null; + for (ParameterSource param : getParameters()) + { + if (param.getType().isType(type) && param.getName().equals(name)) + { + parameter = param; + break; + } + } + if (parameter != null) + { + removeParameter(parameter); + } + return this; + } + } diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassMethodTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassMethodTest.java index 471c354e..012e1d22 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassMethodTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassMethodTest.java @@ -161,4 +161,26 @@ public void testRemoveParameter() throws Exception assertEquals(1, method.getParameters().size()); } + @Test + public void testRemoveParameterByClassType() throws Exception + { + method.removeParameter(String.class, "pattern"); + assertEquals(1, method.getParameters().size()); + } + + @Test + public void testRemoveParameterByStringType() throws Exception + { + method.removeParameter("String", "pattern"); + assertEquals(1, method.getParameters().size()); + } + + @Test + public void testRemoveParameterByJavaType() throws Exception + { + JavaClassSource type = Roaster.create(JavaClassSource.class).setName("String").setPackage("java.lang"); + method.removeParameter(type, "pattern"); + assertEquals(1, method.getParameters().size()); + } + }