Skip to content

Commit

Permalink
enumConstants can host annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenson committed Aug 5, 2013
1 parent e31de8d commit 816c028
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Expand Up @@ -9,7 +9,8 @@
import org.jboss.forge.parser.Internal;
import org.jboss.forge.parser.Origin;

public interface EnumConstant<O extends JavaSource<O>> extends Internal, Origin<O>
public interface EnumConstant<O extends JavaSource<O>> extends Internal, Origin<O>,
AnnotationTarget<O, EnumConstant<O>>
{
/**
* Get this enum constant name.
Expand Down
Expand Up @@ -12,12 +12,15 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.jboss.forge.parser.JavaParser;
import org.jboss.forge.parser.java.Annotation;
import org.jboss.forge.parser.java.EnumConstant;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.ast.AnnotationAccessor;

public class EnumConstantImpl<O extends JavaSource<O>> implements EnumConstant<O>
{
private final AnnotationAccessor<O, EnumConstant<O>> annotations = new AnnotationAccessor<O, EnumConstant<O>>();
private O parent;
private AST ast;
private EnumConstantDeclaration enumConstant;
Expand Down Expand Up @@ -75,4 +78,72 @@ public O getOrigin()
{
return parent;
}

/*
* Annotation<O> Modifiers
*/

@Override
public Annotation<O> addAnnotation()
{
return annotations.addAnnotation(this, enumConstant);
}

@Override
public Annotation<O> addAnnotation(final Class<? extends java.lang.annotation.Annotation> clazz)
{
if (!parent.hasImport(clazz))
{
parent.addImport(clazz);
}
return annotations.addAnnotation(this, enumConstant, clazz.getSimpleName());
}

@Override
public Annotation<O> addAnnotation(final String className)
{
return annotations.addAnnotation(this, enumConstant, className);
}

@Override
public List<Annotation<O>> getAnnotations()
{
return annotations.getAnnotations(this, enumConstant);
}

@Override
public boolean hasAnnotation(final Class<? extends java.lang.annotation.Annotation> type)
{
return annotations.hasAnnotation(this, enumConstant, type.getName());
}

@Override
public boolean hasAnnotation(final String type)
{
return annotations.hasAnnotation(this, enumConstant, type);
}

@Override
public EnumConstant<O> removeAnnotation(final Annotation<O> annotation)
{
return annotations.removeAnnotation(this, enumConstant, annotation);
}

@Override
public Annotation<O> getAnnotation(final Class<? extends java.lang.annotation.Annotation> type)
{
return annotations.getAnnotation(this, enumConstant, type);
}

@Override
public Annotation<O> getAnnotation(final String type)
{
return annotations.getAnnotation(this, enumConstant, type);
}

@Override
public String toString()
{
return enumConstant.toString();
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2012 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.test.parser.java;

import java.io.InputStream;

import org.jboss.forge.parser.JavaParser;
import org.jboss.forge.parser.java.EnumConstant;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.test.parser.java.common.AnnotationTest;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class EnumConstantAnnotationTest extends AnnotationTest<JavaEnum, EnumConstant<JavaEnum>>
{
@SuppressWarnings("resource")
@Override
public void resetTests()
{
InputStream stream = EnumConstantAnnotationTest.class
.getResourceAsStream("/org/jboss/forge/grammar/java/MockAnnotatedEnumConstant.java");
EnumConstant<JavaEnum> field = JavaParser.parse(JavaEnum.class, stream).getEnumConstants().get(0);
setTarget(field);
}

}
@@ -0,0 +1,17 @@
package org.jboss.forge.grammar.java;

import org.jboss.forge.test.grammar.java.common.MockAnnotation;
import org.jboss.forge.test.grammar.java.common.MockNestingAnnotation.MockNestedAnnotation;
import org.jboss.forge.test.grammar.java.common.MockNestingAnnotation;

import static org.jboss.forge.test.grammar.java.common.MockEnum.FOO;

public enum MockAnnotatedEnumConstant
{
@Deprecated
@SuppressWarnings("deprecation")
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
SINGLE_CONSTANT;
}

0 comments on commit 816c028

Please sign in to comment.