Skip to content

Commit

Permalink
Merge pull request #268 from mbenson/new-annotation-element
Browse files Browse the repository at this point in the history
add alternate new-annotation-element options for ease-of-use
  • Loading branch information
gastaldi committed Jan 10, 2013
2 parents e2d7d1d + 19bf87d commit 600b4aa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
30 changes: 24 additions & 6 deletions dev-plugins/src/main/java/org/jboss/forge/dev/java/JavaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ else if (in != null)
public void newAnnotationElement(
@PipeIn final String in,
final PipeOut out,
@Option(name = "name", required = false, help = "the annotation element name; use with --type", description = "annotation element name") final String name,
@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
Expand All @@ -468,23 +470,39 @@ else if (in != null)
{
elementDef = in;
}
else
else if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(type))
{
throw new RuntimeException("arguments required");
}

JavaSource<?> source = resource.getJavaSource();
if (source.isAnnotation())
{
JavaAnnotation type = JavaAnnotation.class.cast(source);
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;
}

String name = JavaParser.parse(JavaAnnotation.class, "public @interface Temp{}").addAnnotationElement(elementDef).getName();
if (type.hasAnnotationElement(name))
if (parent.hasAnnotationElement(addName))
{
throw new IllegalStateException("Element named [" + name + "] already exists.");
throw new IllegalStateException("Element named [" + addName + "] already exists.");
}

type.addAnnotationElement(elementDef);
if (elementDef != null)
{
parent.addAnnotationElement(elementDef);
}
else
{
parent.addAnnotationElement().setName(name).setType(type);
}
java.saveJavaSource(source);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,38 @@ public void testCreateAnnotationElement() throws Exception
assertEquals("{\"\"}", testStrings.getDefaultValue().getLiteral());
}

@Test
public void testCreateAnnotationElementFromTypeAndName() throws Exception
{
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation {}\"");
getShell().execute("java new-annotation-element --type int --name testInt");
getShell().execute("java new-annotation-element --type String[] --name testStrings");
getShell().execute("ls");
getShell().execute("build");
JavaSource<?> source = getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.types.TestingAnnotationTypeCreation"))
.getJavaSource();
assertTrue(source.isAnnotation());
AnnotationElement testInt = JavaAnnotation.class.cast(source).getAnnotationElement("testInt");
assertNotNull(testInt);
assertEquals("testInt", testInt.getName());
assertEquals("int", testInt.getType());
assertNull(testInt.getDefaultValue().getLiteral());

AnnotationElement testStrings = JavaAnnotation.class.cast(source).getAnnotationElement("testStrings");
assertNotNull(testStrings);
assertEquals("testStrings", testStrings.getName());
assertNull(testStrings.getDefaultValue().getLiteral());
}

@Test(expected = RuntimeException.class)
public void testCreateAnnotationElementMissingOptions() throws Exception
{
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation {}\"");
getShell().execute("java new-annotation-element");
}
}

0 comments on commit 600b4aa

Please sign in to comment.