@Option(name = "type", required = false, help = "the annotation element type; use with --name", description = "annotation element type") final String type,
@Option(required = false,
help = "the annotation element definition: surround with single quotes",
description = "annotation element definition") final String... def) throws FileNotFoundException
{
JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
String elementDef = null;
if (def != null)
{
elementDef = Strings.join(Arrays.asList(def), " ");
}
else if (in != null)
{
elementDef = in;
}
else if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(type))
{
throw new RuntimeException("arguments required");
}
JavaSource<?> source = resource.getJavaSource();
if (source.isAnnotation())
{
JavaAnnotation parent = JavaAnnotation.class.cast(source);
String addName;
if (elementDef != null)
{
addName = JavaParser.parse(JavaAnnotation.class, "public @interface Temp{}")
.addAnnotationElement(elementDef).getName();
}
else
{
addName = name;
}
if (parent.hasAnnotationElement(addName))
{
throw new IllegalStateException("Element named [" + addName + "] already exists.");
}
if (elementDef != null)
{
parent.addAnnotationElement(elementDef);
}
else
{
parent.addAnnotationElement().setName(name).setType(type);
}
java.saveJavaSource(source);
}
}