Skip to content

Commit

Permalink
Static fields should not be included in toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 26, 2014
1 parent 78a89c7 commit 10fd146
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

/**
* Operations for manipulating JavaBeans methods of a class
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class FieldOperations
{
/**
* Removes the field, including its getters and setters and updating toString()
*
*
* @param targetClass The class, which field will be removed
* @param field The field to be removed
*/
Expand All @@ -42,35 +42,35 @@ public void removeField(final JavaClass targetClass, final Field<JavaClass> fiel

/**
* Adds the field with private visibility, including getter and setter, updating the toString()
*
*
* @param targetClass The class which the field will be added to
* @param fieldType The type of the field
* @param fieldName The name of the field
* @param annotations An optional list of annotations that will be added to the field
* @param annotations An optional list of annotations that will be added to the field
* @return The newly created field
*/
public Field<JavaClass> addFieldTo(final JavaClass targetClass, final String fieldType,
final String fieldName, String... annotations)

{
return addFieldTo(targetClass, fieldType, fieldName, Visibility.PRIVATE, true, true, annotations);
}

/**
* Adds the field, updating the toString(). If specified, adds a getter, a setter or both.
*
*
* @param targetClass The class which the field will be added to
* @param fieldType The type of the field
* @param fieldName The name of the field
* @param visibility The visibility of the newly created field
* @param withGetter Specifies whether accessor method should be created
* @param withSetter Specifies whether mutator method should be created
* @param annotations An optional list of annotations that will be added to the field
* @param annotations An optional list of annotations that will be added to the field
* @return The newly created field
*/
public Field<JavaClass> addFieldTo(final JavaClass targetClass, final String fieldType,
final String fieldName, Visibility visibility, boolean withGetter, boolean withSetter,
String... annotations)
final String fieldName, Visibility visibility, boolean withGetter, boolean withSetter,
String... annotations)
{
if (targetClass.hasField(fieldName))
{
Expand All @@ -89,23 +89,23 @@ public Field<JavaClass> addFieldTo(final JavaClass targetClass, final String fie
{
targetClass.addImport(fieldTypeForImport);
}

Refactory.createGetterAndSetter(targetClass, field);
if (!withGetter)

if (!withGetter)
{
removeAccessor(targetClass, field);
}

if (!withSetter)
{
removeMutator(targetClass, field);
}

updateToString(targetClass);
return field;
}

private JavaClass removeAccessor(final JavaClass targetClass, final Field<JavaClass> field)
{
String methodNameSuffix = Strings.capitalize(field.getName());
Expand All @@ -114,10 +114,10 @@ private JavaClass removeAccessor(final JavaClass targetClass, final Field<JavaCl
Method<JavaClass> method = targetClass.getMethod("get" + methodNameSuffix);
targetClass.removeMethod(method);
}

return targetClass;
}

private JavaClass removeMutator(final JavaClass targetClass, final Field<JavaClass> field)
{
String methodNameSuffix = Strings.capitalize(field.getName());
Expand All @@ -126,10 +126,10 @@ private JavaClass removeMutator(final JavaClass targetClass, final Field<JavaCla
Method<JavaClass> method = targetClass.getMethod("set" + methodNameSuffix, field.getQualifiedType());
targetClass.removeMethod(method);
}

return targetClass;
}

private void updateToString(final JavaClass targetEntity)
{
if (targetEntity.hasMethodSignature("toString"))
Expand All @@ -152,6 +152,6 @@ private void updateToString(final JavaClass targetEntity)

protected boolean canAddFieldToToString(Field<JavaClass> field)
{
return true;
return !field.isStatic();
}
}

0 comments on commit 10fd146

Please sign in to comment.