Skip to content

Commit

Permalink
ROASTER-48 and ROASTER-50
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed May 30, 2016
1 parent 32fc928 commit 1441ae0
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public interface GenericCapable<O extends JavaType<O>>
* @return TypeVariable or {@code null}
*/
TypeVariable<O> getTypeVariable(String name);
}

/**
* Returns if the value is declared as a {@link TypeVariable}
*
* @param name the type name
* @return <code>true</code> if the type name is declared as a type variable, <code>false</code>
*/
boolean hasTypeVariable(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jboss.forge.roaster.model.JavaClass;
import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.Method;
import org.jboss.forge.roaster.model.Type;

/**
* Represents a Java Method in source form.
Expand Down Expand Up @@ -61,6 +62,11 @@ public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodS
*/
MethodSource<O> setReturnType(JavaType<?> type);

/**
* Set this {@link Method} to return the given {@link Type}
*/
MethodSource<O> setReturnType(Type<?> type);

/**
* Set this {@link Method} to return 'void'
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ public TypeVariableSource<O> getTypeVariable(String name)
return null;
}

@Override
public boolean hasTypeVariable(String name)
{
TypeDeclaration type = (TypeDeclaration) body;
List<TypeParameter> typeParameters = type.typeParameters();
for (TypeParameter typeParameter : typeParameters)
{
if (Strings.areEqual(name, typeParameter.getName().getIdentifier()))
{
return true;
}
}
return false;
}

@Override
public TypeVariableSource<O> addTypeVariable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ public boolean isReturnTypeVoid()
return getReturnType() == null || getReturnType().isType(Void.TYPE);
}

@Override
public MethodSource<O> setReturnType(Type<?> type)
{
return setReturnType(type.getQualifiedNameWithGenerics());
}

@Override
public MethodSource<O> setReturnType(final Class<?> type)
{
Expand All @@ -322,13 +328,14 @@ public MethodSource<O> setReturnType(final String typeName)
String simpleName = Types.toSimpleName(typeName);

O origin = getOrigin();
if (!Strings.areEqual(typeName, simpleName) && origin.requiresImport(typeName))
if (!hasTypeVariable(typeName) && !Strings.areEqual(typeName, simpleName)
&& origin.requiresImport(typeName))
{
origin.addImport(typeName);
}
for (String genericType : Types.splitGenerics(typeName))
{
if (origin.requiresImport(genericType))
if (!hasTypeVariable(genericType) && origin.requiresImport(genericType))
{
origin.addImport(genericType);
}
Expand Down Expand Up @@ -730,10 +737,10 @@ else if (!getOrigin().hasImport(type) && !getOrigin().hasImport(thrown))
}

@Override
@SuppressWarnings("unchecked")
public List<TypeVariableSource<O>> getTypeVariables()
{
List<TypeVariableSource<O>> result = new ArrayList<TypeVariableSource<O>>();
@SuppressWarnings("unchecked")
List<TypeParameter> typeParameters = method.typeParameters();
if (typeParameters != null)
{
Expand All @@ -746,9 +753,9 @@ public List<TypeVariableSource<O>> getTypeVariables()
}

@Override
@SuppressWarnings("unchecked")
public TypeVariableSource<O> getTypeVariable(String name)
{
@SuppressWarnings("unchecked")
List<TypeParameter> typeParameters = method.typeParameters();
for (TypeParameter typeParameter : typeParameters)
{
Expand All @@ -760,6 +767,22 @@ public TypeVariableSource<O> getTypeVariable(String name)
return null;
}

@SuppressWarnings("unchecked")
@Override
public boolean hasTypeVariable(String name)
{
List<TypeParameter> typeParameters = method.typeParameters();
for (TypeParameter typeParameter : typeParameters)
{
if (Strings.areEqual(name, typeParameter.getName().getIdentifier()))
{
return true;
}
}
return false;

}

@SuppressWarnings("unchecked")
@Override
public TypeVariableSource<O> addTypeVariable()
Expand Down Expand Up @@ -813,10 +836,14 @@ public ParameterSource<O> addParameter(JavaType<?> type, String name)
@Override
public ParameterSource<O> addParameter(String type, String name)
{
Type<?> innerType = new TypeImpl<O>(getOrigin(), null, type);
Import imprt = getOrigin().addImport(innerType);
String resolvedType = imprt != null ? Types.rebuildGenericNameWithArrays(imprt.getSimpleName(), innerType)
: Types.toSimpleName(type);
String resolvedType = type;
if (!hasTypeVariable(type) && getOrigin().requiresImport(type))
{
Type<?> innerType = new TypeImpl<O>(getOrigin(), null, type);
Import imprt = getOrigin().addImport(innerType);
resolvedType = imprt != null ? Types.rebuildGenericNameWithArrays(imprt.getSimpleName(), innerType)
: Types.toSimpleName(type);
}

String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.jboss.forge.test.roaster.model;

import static org.hamcrest.CoreMatchers.containsString;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -115,18 +117,61 @@ public void fullyQualifiedArrayArguments() throws ClassNotFoundException
MethodSource<JavaClassSource> method = javaClass.addMethod();
method.addTypeVariable().setName("T").setBounds(CharSequence.class);
method.addParameter("java.util.Map<org.foo.String[],T>[]", "complexMap");
Type type = method.getParameters().get(0).getType();
Type<?> type = method.getParameters().get(0).getType();
Assert.assertEquals("Map<org.foo.String[],T>[]", type.toString());
}

@Test
public void nestedTypedParameter()
{
JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> method = javaClass.addMethod();
method.addParameter("java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>>", "map");
Type type = method.getParameters().get(0).getType();
Assert.assertEquals("java.util.Map", type.getQualifiedName());
JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> method = javaClass.addMethod();
method.addParameter("java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>>", "map");
Type<?> type = method.getParameters().get(0).getType();
Assert.assertEquals("java.util.Map", type.getQualifiedName());
}

@Test
public void testSetReturnTypeWithGenerics()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> srcMethod = source.addMethod();
srcMethod.setName("name");
srcMethod.setPublic();
srcMethod.addTypeVariable("T");
srcMethod.setReturnType("List");
Assert.assertThat(srcMethod.toString(), containsString("public <T>List name()"));
srcMethod.setReturnType("List<T>");
Assert.assertThat(srcMethod.toString(), containsString("public <T>List<T> name()"));
}

@Test
public void testTransferableReturnType()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> srcMethod = source.addMethod();
srcMethod.setName("name");
srcMethod.addTypeVariable("T");
srcMethod.setReturnType("List<T>");
JavaClassSource dest = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> destMethod = dest.addMethod().setName("name");
destMethod.addTypeVariable("T");
destMethod.setReturnType(srcMethod.getReturnType());
Assert.assertThat(destMethod.toString(), containsString("List<T> name()"));
}

@Test
public void testTransferableReturnTypeString()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> srcMethod = source.addMethod();
srcMethod.setName("name");
srcMethod.addTypeVariable("T");
srcMethod.setReturnType("List<T>");
JavaClassSource dest = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> destMethod = dest.addMethod().setName("name");
destMethod.addTypeVariable("T");
destMethod.setReturnType(srcMethod.getReturnType().getQualifiedNameWithGenerics());
Assert.assertThat(destMethod.toString(), containsString("List<T> name()"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.jboss.forge.test.roaster.model;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;

import java.io.InputStream;
Expand Down Expand Up @@ -61,4 +62,28 @@ public void testParameterShouldBeVarargs() throws Exception
Assert.assertThat(parameter.isVarArgs(), is(true));
}

@Test
public void testGenericTypeParameterShouldNotBeImported() throws Exception
{
JavaClassSource clazz = Roaster.create(JavaClassSource.class).setName("TestClass");
MethodSource<JavaClassSource> method = clazz.addMethod().setReturnTypeVoid().setName("myMethod");
method.addTypeVariable("T");
method.addParameter("T", "name");
Assert.assertThat(method.toString(), containsString("<T>void myMethod(T name)"));
}

@Test
public void testPreserveParameterGenericTypes() throws Exception
{
JavaClassSource clazz = Roaster.create(JavaClassSource.class).setName("TestClass");
final MethodSource<JavaClassSource> newMethod = clazz.addMethod()
.setName("name")
.setPublic()
.setFinal(true);
newMethod.addTypeVariable("T");
newMethod.setReturnType("T").setBody("String.class.as(((Class<T>) as));");
newMethod.addParameter(int.class, "index");
newMethod.addParameter("Class<T>", "as");
Assert.assertThat(clazz.toString(), containsString("public final <T> T name(int index, Class<T> as)"));
}
}

0 comments on commit 1441ae0

Please sign in to comment.