Skip to content

Commit

Permalink
ROASTER-34: Created removeParameter methods by type and name
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 11, 2014
1 parent 2714f84 commit 1bfdf1a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,21 @@ public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodS
* Remove a parameter from this method
*/
MethodSource<O> removeParameter(ParameterSource<O> parameter);

/**
* Remove a parameter with the specified {@link Class} type and name from this method
*/
MethodSource<O> removeParameter(Class<?> type, String name);

/**
* Remove a parameter with the specified type and name from this method
*/
MethodSource<O> removeParameter(String type, String name);

/**
* Remove a parameter with the specified {@link JavaType} type and name to this method
*/
MethodSource<O> removeParameter(JavaType<?> type, String name);


}
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ public ParameterSource<O> 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<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();
Expand All @@ -693,4 +694,62 @@ public MethodSource<O> removeParameter(ParameterSource<O> parameter)
method.parameters().remove(parameter.getInternal());
return this;
}

@Override
public MethodSource<O> removeParameter(Class<?> type, String name)
{
ParameterSource<O> parameter = null;
for (ParameterSource<O> param : getParameters())
{
if (param.getType().isType(type) && param.getName().equals(name))
{
parameter = param;
break;
}
}
if (parameter != null)
{
removeParameter(parameter);
}
return this;
}

@Override
public MethodSource<O> removeParameter(JavaType<?> type, String name)
{
ParameterSource<O> parameter = null;
for (ParameterSource<O> 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<O> removeParameter(String type, String name)
{
ParameterSource<O> parameter = null;
for (ParameterSource<O> param : getParameters())
{
if (param.getType().isType(type) && param.getName().equals(name))
{
parameter = param;
break;
}
}
if (parameter != null)
{
removeParameter(parameter);
}
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit 1bfdf1a

Please sign in to comment.