Skip to content

Commit

Permalink
FORGE-765: JavaPlugin new-annotation-type: multi-select @target Eleme…
Browse files Browse the repository at this point in the history
…ntTypes
  • Loading branch information
mbenson committed Jan 23, 2013
1 parent 1109de2 commit e1dac80
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
32 changes: 32 additions & 0 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 @@ -10,11 +10,15 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.enterprise.event.Event;
import javax.inject.Inject;
Expand All @@ -35,6 +39,7 @@
import org.jboss.forge.project.facets.JavaSourceFacet;
import org.jboss.forge.resources.java.JavaResource;
import org.jboss.forge.shell.PromptType;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.ShellColor;
import org.jboss.forge.shell.ShellPrintWriter;
import org.jboss.forge.shell.ShellPrompt;
Expand Down Expand Up @@ -75,6 +80,9 @@ public class JavaPlugin implements Plugin
@Inject
private Event<PickupResource> pickUp;

@Inject
private Shell shell;

@DefaultCommand(help = "Prints all Java system property information.")
public void info(final PipeOut out)
{
Expand Down Expand Up @@ -264,6 +272,10 @@ public void newAnnotationType(
help = "whether the annotation is @Documented",
description = "documented",
name = "documented") final boolean documented,
@Option(required = false,
help = "whether to omit the @Target annotation (if omitted the user will be prompted for the target types)",
description = "omit @Target",
name = "no-target") final boolean noTarget,
@Option(required = false,
help = "the annotation definition: surround with quotes",
description = "annotation definition") final String... def) throws FileNotFoundException
Expand Down Expand Up @@ -298,6 +310,25 @@ else if (in != null)
{
type.addAnnotation(Retention.class).setEnumValue(retentionPolicy);
}
final Set<ElementType> targetTypes;
if (noTarget)
{
targetTypes = Collections.emptySet();
}
else
{
targetTypes = shell.promptMultiSelectWithWildcard("*",
"Select target element types", ElementType.values());
}
if (targetTypes.isEmpty())
{
shell.printlnVerbose("Skipping @Target annotation");
}
else
{
type.addAnnotation(Target.class).setEnumValue(targetTypes.toArray(new ElementType[targetTypes.size()]));
}

if (!type.hasSyntaxErrors())
{
java.saveJavaSource(type);
Expand Down Expand Up @@ -506,4 +537,5 @@ else if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(type))
java.saveJavaSource(source);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@

package org.jboss.forge.dev.java;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jboss.forge.parser.java.AnnotationElement;
import org.jboss.forge.parser.java.EnumConstant;
Expand All @@ -29,7 +33,6 @@
import org.jboss.forge.project.facets.JavaSourceFacet;
import org.jboss.forge.shell.util.Packages;
import org.jboss.forge.test.AbstractShellTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -133,6 +136,7 @@ public void testCreateEnumConst() throws Exception
@Test
public void testCreateAnnotationType() throws Exception
{
queueInputLines("");
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation{}\"");
Expand All @@ -146,7 +150,26 @@ public void testCreateAnnotationType() throws Exception
JavaAnnotation annotation = JavaAnnotation.class.cast(source);
assertFalse(annotation.hasAnnotation(Documented.class));
assertFalse(annotation.hasAnnotation(Retention.class));
assertFalse(annotation.hasAnnotation(Target.class));

resetInputQueue();
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types --no-target \"public @interface TestingAnnotationTypeCreation{}\"");
getShell().execute("ls");
getShell().execute("build");
source = getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.types.TestingAnnotationTypeCreation"))
.getJavaSource();
assertNotNull(source);
assertTrue(source.isAnnotation());
annotation = JavaAnnotation.class.cast(source);
assertFalse(annotation.hasAnnotation(Documented.class));
assertFalse(annotation.hasAnnotation(Retention.class));
assertFalse(annotation.hasAnnotation(Target.class));

resetInputQueue();
queueInputLines("*");
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types --documented --retention-policy RUNTIME \"public @interface TestingAnnotationTypeCreation{}\"");
Expand All @@ -158,12 +181,16 @@ public void testCreateAnnotationType() throws Exception
annotation = JavaAnnotation.class.cast(source);
assertTrue(annotation.hasAnnotation(Documented.class));
assertTrue(annotation.hasAnnotation(Retention.class));
Assert.assertSame(RetentionPolicy.RUNTIME, annotation.getAnnotation(Retention.class).getEnumValue(RetentionPolicy.class));
assertTrue(annotation.hasAnnotation(Target.class));
assertArrayEquals(ElementType.values(),
annotation.getAnnotation(Target.class).getEnumArrayValue(ElementType.class));
assertSame(RetentionPolicy.RUNTIME, annotation.getAnnotation(Retention.class).getEnumValue(RetentionPolicy.class));
}

@Test
public void testCreateAnnotationElement() throws Exception
{
queueInputLines("");
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation {}\"");
Expand Down Expand Up @@ -191,6 +218,7 @@ public void testCreateAnnotationElement() throws Exception
@Test
public void testCreateAnnotationElementFromTypeAndName() throws Exception
{
queueInputLines("");
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation {}\"");
Expand All @@ -217,6 +245,7 @@ public void testCreateAnnotationElementFromTypeAndName() throws Exception
@Test(expected = RuntimeException.class)
public void testCreateAnnotationElementMissingOptions() throws Exception
{
queueInputLines("");
getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types \"public @interface TestingAnnotationTypeCreation {}\"");
Expand Down

0 comments on commit e1dac80

Please sign in to comment.