Skip to content

Commit

Permalink
Add a java-new-field command
Browse files Browse the repository at this point in the history
You can specify the field name, its type, visibility (public, protected, etc.) and also whether you want getter and setter generated.
  • Loading branch information
Ivan St. Ivanov authored and ivannov committed Mar 23, 2014
1 parent f2db6d3 commit 02281e6
Show file tree
Hide file tree
Showing 9 changed files with 698 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.jboss.forge.addon.parser.java.beans.FieldOperations;
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
import org.jboss.forge.addon.projects.Project;
Expand All @@ -31,7 +32,6 @@
import org.jboss.forge.parser.java.Field;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.parser.java.util.Refactory;
import org.jboss.forge.parser.java.util.Types;

Expand All @@ -41,58 +41,8 @@
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public class FieldOperations
public class JPAFieldOperations extends FieldOperations
{
/**
* Remove the field, including its getters and setters and updating toString()
*
* @param targetEntity
* @param field
*/
public void removeField(final JavaClass targetEntity, final Field<JavaClass> field)
{
targetEntity.removeField(field);
String methodNameSuffix = Strings.capitalize(field.getName());
// Condition to remove getField()
if (targetEntity.hasMethodSignature("get" + methodNameSuffix))
{
Method<JavaClass> method = targetEntity.getMethod("get" + methodNameSuffix);
targetEntity.removeMethod(method);
}
// Condition to remove setField()
if (targetEntity.hasMethodSignature("set" + methodNameSuffix, field.getQualifiedType()))
{
Method<JavaClass> method = targetEntity.getMethod("set" + methodNameSuffix, field.getQualifiedType());
targetEntity.removeMethod(method);
}
updateToString(targetEntity);
}

public Field<JavaClass> addFieldTo(final JavaClass targetEntity, final String fieldType,
final String fieldName, String... annotations)
throws FileNotFoundException
{
if (targetEntity.hasField(fieldName))
{
throw new IllegalStateException("Entity already has a field named [" + fieldName + "]");
}
Field<JavaClass> field = targetEntity.addField();
field.setName(fieldName).setPrivate().setType(fieldType);
for (String annotation : annotations)
{
field.addAnnotation(annotation);
}

String fieldTypeForImport = Types.stripArray(fieldType);
if (!fieldTypeForImport.startsWith("java.lang.") && fieldTypeForImport.contains(".")
&& !fieldTypeForImport.equals(targetEntity.getCanonicalName()))
{
targetEntity.addImport(fieldTypeForImport);
}
Refactory.createGetterAndSetter(targetEntity, field);
updateToString(targetEntity);
return field;
}

/**
* Creates a One-to-One relationship
Expand Down Expand Up @@ -393,28 +343,6 @@ public void newManyToManyRelationship(
java.saveJavaSource(entity);
}

private void updateToString(final JavaClass targetEntity)
{
if (targetEntity.hasMethodSignature("toString"))
{
targetEntity.removeMethod(targetEntity.getMethod("toString"));
}
List<Field<JavaClass>> fields = new ArrayList<Field<JavaClass>>();
for (Field<JavaClass> f : targetEntity.getFields())
{
if (!"id".equals(f.getName()) && !"version".equals(f.getName())
&& (f.getTypeInspector().isPrimitive() || Types.isJavaLang(f.getType()))
&& !f.hasAnnotation(Transient.class))
{
fields.add(f);
}
}
if (!fields.isEmpty())
{
Refactory.createToStringFromFields(targetEntity, fields);
}
}

private JavaClass findEntity(Project project, final String entity) throws FileNotFoundException
{
JavaClass result = null;
Expand Down Expand Up @@ -462,4 +390,15 @@ private boolean areTypesSame(String from, String to)
String toCompare = to.endsWith(".java") ? to.substring(0, to.length() - 5) : to;
return fromCompare.equals(toCompare);
}

@Override
protected boolean canAddFieldToToString(Field<JavaClass> field)
{
return super.canAddFieldToToString(field)
&& (field.getTypeInspector().isPrimitive() || Types.isJavaLang(field.getType()))
&& !"id".equals(field.getName())
&& !"version".equals(field.getName())
&& !field.hasAnnotation(Transient.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import javax.persistence.CascadeType;
import javax.persistence.FetchType;

import org.jboss.forge.addon.javaee.jpa.FieldOperations;
import org.jboss.forge.addon.javaee.jpa.JPAFieldOperations;
import org.jboss.forge.addon.javaee.ui.AbstractJavaEECommand;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
import org.jboss.forge.addon.projects.Project;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class NewFieldRelationshipWizardStep extends AbstractJavaEECommand implem
private UISelectMany<CascadeType> cascadeType;

@Inject
private FieldOperations persistenceOperations;
private JPAFieldOperations persistenceOperations;

@Override
public void initializeUI(UIBuilder builder) throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import javax.persistence.Transient;

import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.javaee.jpa.FieldOperations;
import org.jboss.forge.addon.javaee.jpa.JPAFieldOperations;
import org.jboss.forge.addon.javaee.jpa.JPAFacet;
import org.jboss.forge.addon.javaee.jpa.ui.setup.JPASetupWizard;
import org.jboss.forge.addon.javaee.ui.AbstractJavaEECommand;
Expand Down Expand Up @@ -98,7 +98,7 @@ public class NewFieldWizard extends AbstractJavaEECommand implements UIWizard, P
private UIInput<String> columnName;

@Inject
private FieldOperations fieldOperations;
private JPAFieldOperations fieldOperations;

@Inject
private InputComponentFactory inputFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import static org.junit.Assert.assertThat;

/**
* Unit tests to verify the behavior of the {@link FieldOperations} class.
* Unit tests to verify the behavior of the {@link JPAFieldOperations} class.
*/
@RunWith(Arquillian.class)
public class FieldOperationsTest
public class JPAFieldOperationsTest
{

@Deployment
Expand Down Expand Up @@ -68,7 +68,7 @@ public static ForgeArchive getDeployment()
private JavaClass entityClass;

@Inject
private FieldOperations fieldOperations;
private JPAFieldOperations jpaFieldOperations;

@Before
public void setUp()
Expand All @@ -86,42 +86,6 @@ public void setUp()
}
}

/**
* Verifies that simple fields can be added to a class.
*/
@Test
public void testAddNewSimpleField() throws Exception
{
String simpleTypeName = String.class.getSimpleName();
String qualifiedTypeName = String.class.getCanonicalName();

// Add a String field with a qualified type.
String fieldName = "firstName";
fieldOperations.addFieldTo(entityClass, qualifiedTypeName, fieldName);

// Is the field present? Is the name and type of the field correct?
assertThat(entityClass.hasField(fieldName), is(true));
assertThat(entityClass.getField(fieldName).getName(), equalTo(fieldName));
assertThat(entityClass.getField(fieldName).getType(), equalTo(simpleTypeName));
// Is the type from java.lang imported?
assertThat(entityClass.hasImport(qualifiedTypeName), is(false));
// Syntax errors?
assertThat(entityClass.hasSyntaxErrors(), is(false));

// Add a String field with a simple type.
fieldName = "lastName";
fieldOperations.addFieldTo(entityClass, simpleTypeName, fieldName);

// Is the field present? Is the name and type of the field correct?
assertThat(entityClass.hasField(fieldName), is(true));
assertThat(entityClass.getField(fieldName).getName(), equalTo(fieldName));
assertThat(entityClass.getField(fieldName).getType(), equalTo(simpleTypeName));
// Is the type from java.lang imported?
assertThat(entityClass.hasImport(qualifiedTypeName), is(false));
// Syntax errors?
assertThat(entityClass.hasSyntaxErrors(), is(false));
}

@Test
public void testAddOneToManyRel() throws Exception
{
Expand All @@ -132,7 +96,7 @@ public void testAddOneToManyRel() throws Exception
String qualifiedCollectionType = Set.class.getCanonicalName();

String fieldName = "accounts";
fieldOperations.newOneToManyRelationship(project, entity, fieldName, qualifiedRhsType, null,
jpaFieldOperations.newOneToManyRelationship(project, entity, fieldName, qualifiedRhsType, null,
FetchType.LAZY, Lists.<CascadeType> newArrayList());
entityClass = (JavaClass) entity.getJavaSource();

Expand Down Expand Up @@ -163,7 +127,7 @@ public void testAddManyToManyRel() throws Exception
String qualifiedCollectionType = Set.class.getCanonicalName();

String fieldName = "accounts";
fieldOperations.newManyToManyRelationship(project, entity, fieldName, qualifiedRhsType, null,
jpaFieldOperations.newManyToManyRelationship(project, entity, fieldName, qualifiedRhsType, null,
FetchType.LAZY, Lists.<CascadeType> newArrayList());
entityClass = (JavaClass) entity.getJavaSource();

Expand Down Expand Up @@ -192,7 +156,7 @@ public void testAddManyToOneRel() throws Exception
String simpleRhsType = rhsClass.getJavaSource().getName();

String fieldName = "store";
fieldOperations.newManyToOneRelationship(project, entity, fieldName, qualifiedRhsType, null,
jpaFieldOperations.newManyToOneRelationship(project, entity, fieldName, qualifiedRhsType, null,
FetchType.LAZY, false, Lists.<CascadeType> newArrayList());
entityClass = (JavaClass) entity.getJavaSource();

Expand Down Expand Up @@ -220,7 +184,7 @@ public void testAddOneToOneRel() throws Exception
String simpleRhsType = rhsClass.getJavaSource().getName();

String fieldName = "store";
fieldOperations.newOneToOneRelationship(project, entity, fieldName, qualifiedRhsType, null,
jpaFieldOperations.newOneToOneRelationship(project, entity, fieldName, qualifiedRhsType, null,
FetchType.LAZY, false, Lists.<CascadeType> newArrayList());
entityClass = (JavaClass) entity.getJavaSource();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static ForgeArchive getDeployment()
private Project project;

@Inject
private FieldOperations fieldOperations;
private JPAFieldOperations fieldOperations;

@Before
public void setUp()
Expand Down
Loading

0 comments on commit 02281e6

Please sign in to comment.