Skip to content

Commit

Permalink
Support Javadoc in AnnotationElement (#272)
Browse files Browse the repository at this point in the history
- Fixes #271
  • Loading branch information
gastaldi authored Nov 9, 2022
1 parent 6f765db commit ab36db6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* @author Matt Benson
*/
public interface AnnotationElementSource extends AnnotationElement<JavaAnnotationSource>,
AnnotationTargetSource<JavaAnnotationSource, AnnotationElementSource>, NamedSource<AnnotationElementSource>
AnnotationTargetSource<JavaAnnotationSource, AnnotationElementSource>, NamedSource<AnnotationElementSource>,
JavaDocCapableSource<AnnotationElementSource>
{
/**
* Represents the default value of an {@link AnnotationElementSource} and provides mechanisms to set that value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
Expand All @@ -31,6 +32,7 @@
import org.jboss.forge.roaster.model.source.AnnotationElementSource;
import org.jboss.forge.roaster.model.source.AnnotationSource;
import org.jboss.forge.roaster.model.source.JavaAnnotationSource;
import org.jboss.forge.roaster.model.source.JavaDocSource;
import org.jboss.forge.roaster.model.util.Types;

/**
Expand All @@ -39,6 +41,7 @@
*/
public class AnnotationElementImpl implements AnnotationElementSource
{

private class AnnotationValue extends AnnotationImpl<JavaAnnotationSource, JavaAnnotationSource>
{

Expand Down Expand Up @@ -509,41 +512,46 @@ public DefaultValue getDefaultValue()
}

@Override
public int hashCode()
public boolean hasJavaDoc()
{
final int prime = 31;
int result = 1;
result = (prime * result) + ((member == null) ? 0 : member.hashCode());
return result;
return member.getJavadoc() != null;
}

@SuppressWarnings("unchecked")
@Override
public boolean equals(final Object obj)
public JavaDocSource<AnnotationElementSource> getJavaDoc()
{
if (this == obj)
{
return true;
}
if (obj == null)
Javadoc javadoc = member.getJavadoc();
if (javadoc == null)
{
return false;
javadoc = ast.newJavadoc();
member.setJavadoc(javadoc);
}
if (getClass() != obj.getClass())
{
return false;
}
AnnotationElementImpl other = (AnnotationElementImpl) obj;
if (member == null)
{
if (other.member != null)
{
return false;
}
}
else if (!member.equals(other.member))
{
return new JavaDocImpl<>(this, javadoc);
}

@SuppressWarnings("unchecked")
@Override
public AnnotationElementSource removeJavaDoc()
{
member.setJavadoc(null);
return this;
}

@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
}
return true;
AnnotationElementImpl that = (AnnotationElementImpl) o;
return member.equals(that.member);
}

@Override
public int hashCode()
{
return Objects.hash(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,12 @@ public void testAnnotationGetStringArrayValueShouldNotReturnValueIfEmpty()
assertEquals(0, arrayValue.length);
}

@Test
public void testJavadocInAnnotations()
{
AnnotationElementSource annotationElement = javaAnnotation.getAnnotationElement("value");
annotationElement.getJavaDoc().setText("This is a javadoc");
assertEquals("This is a javadoc", annotationElement.getJavaDoc().getText());
}

}

0 comments on commit ab36db6

Please sign in to comment.