Skip to content

Commit

Permalink
Merge pull request #12 from forge/ROASTER-5
Browse files Browse the repository at this point in the history
ROASTER-5: Added add/removeParameter to MethodSource
  • Loading branch information
gastaldi committed Apr 7, 2014
2 parents c24f876 + ea46069 commit 828ddd4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

/**
* Represents a Java Method in source form.
*
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*
*/
public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodSource<O>>, AbstractableSource<MethodSource<O>>,
public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodSource<O>>,
AbstractableSource<MethodSource<O>>,
MemberSource<O, MethodSource<O>>, GenericCapableSource<O, MethodSource<O>>
{
/**
Expand All @@ -33,8 +34,8 @@ public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodS
MethodSource<O> setBody(final String body);

/**
* Toggle this method as a constructor. If true, and the name of the {@link Method} is not the same as the name
* of its parent {@link JavaClass} , update the name of the to match.
* Toggle this method as a constructor. If true, and the name of the {@link Method} is not the same as the name of
* its parent {@link JavaClass} , update the name of the to match.
*/
MethodSource<O> setConstructor(final boolean constructor);

Expand Down Expand Up @@ -84,4 +85,23 @@ public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodS
@Override
List<ParameterSource<O>> getParameters();

/**
* Add a parameter with the specified {@link Class} type and name to this method
*/
ParameterSource<O> addParameter(Class<?> type, String name);

/**
* Add a parameter with the specified type and name to this method
*/
ParameterSource<O> addParameter(String type, String name);

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

/**
* Remove a parameter from this method
*/
MethodSource<O> removeParameter(ParameterSource<O> parameter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Type<O> getReturnType()
}
return new TypeImpl<O>(parent, method.getReturnType2());
}

@Override
public boolean isReturnTypeVoid()
{
Expand Down Expand Up @@ -610,7 +610,7 @@ public TypeVariableSource<O> getTypeVariable(String name)
{
if (Strings.areEqual(name, typeParameter.getName().getIdentifier()))
{
return(new TypeVariableImpl<O>(parent, typeParameter));
return (new TypeVariableImpl<O>(parent, typeParameter));
}
}
return null;
Expand Down Expand Up @@ -646,4 +646,43 @@ public MethodSource<O> removeTypeVariable(TypeVariable<?> typeVariable)
{
return removeTypeVariable(typeVariable.getName());
}

@Override
public ParameterSource<O> addParameter(Class<?> type, String name)
{
return addParameter(type.getName(), name);
}

@Override
public ParameterSource<O> addParameter(JavaType<?> type, String name)
{
return addParameter(type.getQualifiedName(), name);
}

@SuppressWarnings("unchecked")
@Override
public ParameterSource<O> addParameter(String type, String name)
{
getOrigin().addImport(type);
String stub = "public class Stub { public void method( " + Types.toSimpleName(type) + " " + name + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();

ParameterSource<O> param = null;
for (VariableDeclaration declaration : astParameters)
{
VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration);
method.parameters().add(copy);
param = new ParameterImpl<O>(parent, copy);
}
return param;
}

@Override
public MethodSource<O> removeParameter(ParameterSource<O> parameter)
{
method.parameters().remove(parameter.getInternal());
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
Expand Down Expand Up @@ -118,9 +119,46 @@ public void testHasMethodZeroParameters() throws Exception
@Test
public void testGetMembers() throws Exception
{
JavaClassSource javaClass = Roaster.create(JavaClassSource.class).addMethod("public void doSomething();").getOrigin()
JavaClassSource javaClass = Roaster.create(JavaClassSource.class).addMethod("public void doSomething();")
.getOrigin()
.addField("private int id;").getOrigin();
List<MemberSource<JavaClassSource, ?>> members = javaClass.getMembers();
assertEquals(2, members.size());
}

@Test
public void testAddParameter() throws Exception
{
ParameterSource<JavaClassSource> param = method.addParameter(Number.class, "number");
assertNotNull(param);
assertEquals(3, method.getParameters().size());
}

@Test
public void testAddParameterStringType() throws Exception
{
ParameterSource<JavaClassSource> param = method.addParameter(Number.class.getName(), "number");
assertNotNull(param);
assertEquals(3, method.getParameters().size());
}

@Test
public void testAddParameterJavaType() throws Exception
{
JavaClassSource type = Roaster.create(JavaClassSource.class).setName("Mock").setPackage("mock.pkg");
ParameterSource<JavaClassSource> param = method.addParameter(type, "mock");
assertNotNull(param);
assertEquals(3, method.getParameters().size());
assertTrue(method.getOrigin().hasImport(type));
}

@Test
public void testRemoveParameter() throws Exception
{
ParameterSource<JavaClassSource> param = method.getParameters().get(0);
assertNotNull(param);
method.removeParameter(param);
assertEquals(1, method.getParameters().size());
}

}

0 comments on commit 828ddd4

Please sign in to comment.