Skip to content

Commit

Permalink
ROASTER-105: Transform imports when adding property with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jun 28, 2016
1 parent a211a7d commit 5e3f803
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private List<WildcardImportResolver> getImportResolvers()

private boolean validImport(final String type)
{
return !Strings.isNullOrEmpty(type) && !Types.isPrimitive(type);
return !Strings.isNullOrEmpty(type) && !Types.isPrimitive(type) && !Strings.isNullOrEmpty(Types.getPackage(type));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public abstract class AbstractJavaSourceMemberHolder<O extends JavaSource<O> & PropertyHolderSource<O>>
extends AbstractJavaSource<O>
public abstract class AbstractJavaSourceMemberHolder<O extends JavaSource<O> & PropertyHolderSource<O>> extends AbstractJavaSource<O>
implements InterfaceCapableSource<O>, PropertyHolderSource<O>
{
private static final Pattern GET_SET_PATTERN = Pattern.compile("^[gs]et.+$");
Expand Down Expand Up @@ -541,7 +540,20 @@ public final PropertySource<O> addProperty(String type, String name)
{
Assert.isFalse(hasProperty(name), "Cannot create existing property " + name);

final org.jboss.forge.roaster.model.Type<O> typeObject = new TypeImpl<O>(getOrigin(), null, type);
O origin = getOrigin();
if (origin.requiresImport(type))
{
origin.addImport(type);
}
for (String genericType : Types.splitGenerics(type))
{
if (origin.requiresImport(genericType))
{
origin.addImport(genericType);
}
}
final org.jboss.forge.roaster.model.Type<O> typeObject = new TypeImpl<O>(getOrigin(), null,
Types.toSimpleName(type));
final PropertySource<O> result = new PropertyImpl<O>(name, getOrigin())
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ public boolean hasTypeVariable(String name)
}
}
return false;

}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.test.roaster.model;

import static org.junit.Assert.assertEquals;

import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.Type;
import org.jboss.forge.roaster.model.source.FieldSource;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.forge.roaster.model.source.PropertySource;
import org.junit.Test;

/**
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class PropertiesGenericsTest
{
@Test
public void addGenericAndGetPropertyType()
{
JavaClassSource classSource = Roaster.create(JavaClassSource.class);
classSource.addProperty("java.util.List<java.lang.String>", "list");
PropertySource<JavaClassSource> property = classSource.getProperty("list");
Type<JavaClassSource> propertyType = property.getType();
assertEquals("List<String>", propertyType.toString());
}

@Test
public void addGenericAndGetFieldType()
{
JavaClassSource classSource = Roaster.create(JavaClassSource.class);
classSource.addProperty("java.util.List<java.lang.String>", "list");
PropertySource<JavaClassSource> property = classSource.getProperty("list");
FieldSource<JavaClassSource> field = property.getField();
Type<JavaClassSource> fieldType = field.getType();
assertEquals("List<String>", fieldType.toString());
}
}

0 comments on commit 5e3f803

Please sign in to comment.