{
if (targetEntity.hasField(fieldName))
{
throw new IllegalStateException("Entity already has a field named [" + fieldName + "]");
}
JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
Field<JavaClass> field = targetEntity.addField();
field.setName(fieldName).setPrivate().setType(fieldType).addAnnotation(annotation);
Class<?> fieldTypeForImport = fieldType;
if (fieldType.getComponentType() != null)
{
fieldTypeForImport = fieldType.getComponentType();
}
if (!fieldTypeForImport.getCanonicalName().startsWith("java.lang.") && !fieldTypeForImport.isPrimitive()
&& !fieldTypeForImport.getCanonicalName().equals(targetEntity.getCanonicalName()))
{
targetEntity.addImport(fieldTypeForImport);
}
Refactory.createGetterAndSetter(targetEntity, field);
updateToString(targetEntity);
java.saveJavaSource(targetEntity);
shell.println("Added field to " + targetEntity.getQualifiedName() + ": " + field);
return field;
}