Skip to content

Commit

Permalink
FORGE-741: add options to make new-annotation-type @documented, specify
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenson authored and gastaldi committed Jan 10, 2013
1 parent 250dabc commit 2c8a777
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
* Copyright 2012-2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
Expand All @@ -9,6 +9,9 @@

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
Expand Down Expand Up @@ -253,6 +256,14 @@ public void newAnnotationType(
description = "source package",
type = PromptType.JAVA_PACKAGE,
name = "package") final String pckg,
@Option(required = false,
help = "the @Retention policy for this annotation",
description = "retention policy",
name = "retention-policy") final RetentionPolicy retentionPolicy,
@Option(required = false,
help = "whether the annotation is @Documented",
description = "documented",
name = "documented") final boolean documented,
@Option(required = false,
help = "the annotation definition: surround with quotes",
description = "annotation definition") final String... def) throws FileNotFoundException
Expand All @@ -279,6 +290,14 @@ else if (in != null)
type.setPackage(pckg);
}

if (documented)
{
type.addAnnotation(Documented.class);
}
if (retentionPolicy != null)
{
type.addAnnotation(Retention.class).setEnumValue(retentionPolicy);
}
if (!type.hasSyntaxErrors())
{
java.saveJavaSource(type);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
* Copyright 2012-2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.dev.java;

import static junit.framework.Assert.assertNotNull;
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.assertTrue;

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

import org.jboss.forge.parser.java.AnnotationElement;
import org.jboss.forge.parser.java.EnumConstant;
import org.jboss.forge.parser.java.Field;
Expand All @@ -24,6 +29,7 @@
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 @@ -137,6 +143,22 @@ public void testCreateAnnotationType() throws Exception
.getJavaSource();
assertNotNull(source);
assertTrue(source.isAnnotation());
JavaAnnotation annotation = JavaAnnotation.class.cast(source);
assertFalse(annotation.hasAnnotation(Documented.class));
assertFalse(annotation.hasAnnotation(Retention.class));

getShell()
.execute(
"java new-annotation-type --package org.jboss.forge.test.types --documented --retention-policy RUNTIME \"public @interface TestingAnnotationTypeCreation{}\"");
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);
assertTrue(annotation.hasAnnotation(Documented.class));
assertTrue(annotation.hasAnnotation(Retention.class));
Assert.assertSame(RetentionPolicy.RUNTIME, annotation.getAnnotation(Retention.class).getEnumValue(RetentionPolicy.class));
}

@Test
Expand Down

0 comments on commit 2c8a777

Please sign in to comment.