Skip to content

Commit

Permalink
ROASTER-32: Supporting array types in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 7, 2014
1 parent 4a8729b commit 47e58d6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
22 changes: 13 additions & 9 deletions api/src/main/java/org/jboss/forge/roaster/model/util/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static boolean isSimpleName(final String name)
public static boolean isJavaLang(final String type)
{
final String javaLang = "java.lang.";

String check;
if (type.startsWith(javaLang))
{
Expand Down Expand Up @@ -252,16 +252,20 @@ public static String stripGenerics(final String type)
arrayDimensions = 0;
componentType = type;
}
final StringBuilder result = new StringBuilder();
if (isGeneric(componentType))
{
final StringBuilder result = new StringBuilder(componentType.replaceFirst("^([^<]*)<.*?>$", "$1"));
for (int i = 0; i < arrayDimensions; i++)
{
result.append("[]");
}
return result.toString();
result.append(componentType.replaceFirst("^([^<]*)<.*?>$", "$1"));
}
else
{
result.append(componentType);
}
for (int i = 0; i < arrayDimensions; i++)
{
result.append("[]");
}
return type;
return result.toString();
}

public static String getGenerics(final String type)
Expand Down Expand Up @@ -351,7 +355,7 @@ public static boolean isPrimitive(final String result)
* It simply counts the "[" from the string.
*
* @param name an array type, e.g.: byte[] or [Ljava.lang.Boolean;
* @return the array dimension. -1 if the type is not a valid array
* @return the array dimension. 0 if the type is not a valid array
*/
public static int getArrayDimension(String name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,11 @@ public ParameterSource<O> addParameter(JavaType<?> type, String name)
@Override
public ParameterSource<O> addParameter(String type, String name)
{
if (!Types.isBasicType(type))
if (getOrigin().requiresImport(type))
{
getOrigin().addImport(type);
}
String stub = "public class Stub { public void method( " + Types.toSimpleName(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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public void testMethodWithPrimitiveParameters() throws Exception
MethodSource<JavaClassSource> method = javaClass.addMethod().setPublic().setName("doSomething").setReturnType(Integer.TYPE).setBody("return 0;");
method.addParameter(Integer.TYPE, "initValue");
method.addParameter(int.class,"intValueClass");
method.addParameter(int[].class,"intValueClassArray");
Assert.assertEquals(1, javaClass.getMethods().size());
List<ParameterSource<JavaClassSource>> parameters = javaClass.getMethods().get(0).getParameters();
Assert.assertEquals(2, parameters.size());
Assert.assertEquals(3, parameters.size());
Assert.assertTrue(parameters.get(0).getType().isPrimitive());
Assert.assertTrue(parameters.get(1).getType().isPrimitive());
Assert.assertTrue(parameters.get(2).getType().isArray());
}

private void assertVisibility(Visibility visibility, MethodSource<JavaClassSource> method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void testGenerics()
assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class<LONG_TYPE_VARIABLE_NAME>[]"));
assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class<? extends Number>[]"));
assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class<E extends Enum<E>>[]"));
assertEquals("int[]", Types.stripGenerics(int[].class.getName()));
}

@Test
Expand Down

0 comments on commit 47e58d6

Please sign in to comment.