You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2017/03/11 18:44:04 UTC

[jira] [Comment Edited] (GROOVY-8116) Property generation for enum is incorrect

    [ https://issues.apache.org/jira/browse/GROOVY-8116?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15906277#comment-15906277 ] 

Eric Milles edited comment on GROOVY-8116 at 3/11/17 6:44 PM:
--------------------------------------------------------------

It looks like AntParserPlugin could be modified.  It already has special case handling for fields in interfaces.
{code}
    protected void fieldDef(AST fieldDef) {
        List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
        AST node = fieldDef.getFirstChild();

        int modifiers = 0;
        if (isType(MODIFIERS, node)) {
            modifiers = modifiers(node, annotations, modifiers);
            node = node.getNextSibling();
        }

        // maybe this?
        if (classNode.isEnum()) {
            modifiers |= Opcodes.ACC_FINAL;
        } else
        // end
        if (classNode.isInterface()) {
            modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
            if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
                modifiers |= Opcodes.ACC_PUBLIC;
            }
        }
{code}


was (Author: emilles):
It looks like AntParserPlugin could be modified.  It already has special case handling for fields in interfaces.
{code}
    protected void fieldDef(AST fieldDef) {
        List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
        AST node = fieldDef.getFirstChild();

        int modifiers = 0;
        if (isType(MODIFIERS, node)) {
            modifiers = modifiers(node, annotations, modifiers);
            node = node.getNextSibling();
        }

        if (classNode.isInterface()) {
            modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
            if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
                modifiers |= Opcodes.ACC_PUBLIC;
            }
        }
{code}

> Property generation for enum is incorrect
> -----------------------------------------
>
>                 Key: GROOVY-8116
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8116
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Eric Milles
>
> When creating a simple enum, I noticed that content assist was giving some strange results.  It appears as though there is no special case handling for properties in an enum.
> {code}
> enum Suit {
>   CLUBS(1),  SPADES(1), HEARTS(2), DIAMONDS(2);
>   int color
>   Suit(int color) {
>     this.color = color
>   }
> }
> {code}
> Since this is an enum, the final modifier on color should not be necessary -- it is implied.  Java does not require it.  However the generated field does not indicate final in the AST and a setter setColor is created.  I checked this in the Groovy Console.
> {code}
> final public class Suit implements groovy.lang.GroovyObject extends java.lang.Enum<Suit> { 
>     final public static Suit HEARTS 
>     final public static Suit DIAMONDS 
>     final public static Suit SPADES 
>     final public static Suit CLUBS 
>     private int color
>     ...
>     public int getColor() {
>     }
>     public void setColor(int value) {
>     }
> }
> {code}
> The method setColor is coming up in my IDE's content assist, since it is generated by the Verifier.visitProperty method.  I think if the generated fields are indicated as final for enums then Verifier will not generate setters.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)