From 47e58d6211e245dcde9d95352cf897625ffd1a55 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Sun, 7 Sep 2014 18:49:12 -0300 Subject: [PATCH] ROASTER-32: Supporting array types in parameters --- .../jboss/forge/roaster/model/util/Types.java | 22 +++++++++++-------- .../forge/roaster/model/impl/MethodImpl.java | 4 ++-- .../roaster/model/MethodSignatureTest.java | 4 +++- .../test/roaster/model/util/TypesTest.java | 1 + 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/org/jboss/forge/roaster/model/util/Types.java b/api/src/main/java/org/jboss/forge/roaster/model/util/Types.java index 9be04fa9..3cc67319 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/util/Types.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/util/Types.java @@ -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)) { @@ -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) @@ -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) { 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 c393ea96..614bd52e 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,11 +668,11 @@ public ParameterSource addParameter(JavaType type, String name) @Override public ParameterSource 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> methods = temp.getMethods(); List astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); 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 b4121912..21ff0210 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 @@ -107,11 +107,13 @@ public void testMethodWithPrimitiveParameters() throws Exception MethodSource 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> 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 method) diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/util/TypesTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/util/TypesTest.java index fb89a62f..74282bc2 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/util/TypesTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/util/TypesTest.java @@ -128,6 +128,7 @@ public void testGenerics() assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class[]")); assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class[]")); assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class>[]")); + assertEquals("int[]", Types.stripGenerics(int[].class.getName())); } @Test