Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a830bdfccc68f0e8226b1874c6d97e035b1ff917 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 */
package org.eclipse.emf.codegen.merge.java.facade.ast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.IExtendedModifier;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.emf.codegen.merge.java.facade.JMember;
import org.eclipse.emf.codegen.merge.java.facade.JNode;

/**
 * Each <code>ASTJMember</code> has a reference to {@link BodyDeclaration} as a wrapped object.
 * 
 * @param <T> wrapped body declaration type
 * 
 * @since 2.2.0
 */
public abstract class ASTJMember<T extends BodyDeclaration> extends ASTJNode<T> implements JMember
{
  /**
   * Cached value of member comment.
   * @see #getComment()
   * @see #setComment(String)
   */
  protected String comment = UNITIALIZED_STRING;

  /**
   * @param bodyDeclaration
   */
  public ASTJMember(T bodyDeclaration)
  {
    super(bodyDeclaration);
  }
  
  @Override
  public void dispose()
  {
    comment = null;
    super.dispose();
  }
  
  /**
   * Returns original flags of the member.
   * 
   * @see org.eclipse.emf.codegen.merge.java.facade.AbstractJNode#getFlags()
   */
  @Override
  public int getFlags()
  {
    return getASTNode().getModifiers();
  }
  
  /**
   * Sets the flags of the member.
   * <p>
   * Note that <code>getFlags()</code> will not return the new value.
   * 
   * @see org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode#setFlags(int)
   */
  @Override
  public void setFlags(int flags)
  {
    BodyDeclaration bodyDeclaration = getASTNode();
    ListRewrite listRewrite = rewriter.getListRewrite(bodyDeclaration, bodyDeclaration.getModifiersProperty());

    // remove all existing modifiers
    @SuppressWarnings("unchecked") 
    List<IExtendedModifier> existingModifiers = listRewrite.getRewrittenList();
    for (IExtendedModifier modifier : existingModifiers)
    {
      if (modifier.isModifier())
      {
        listRewrite.remove((ASTNode)modifier, null);
      }
    }
    
    // create new modifiers and add to rewrite
    @SuppressWarnings("unchecked")    
    List<Modifier> newModifiers = bodyDeclaration.getAST().newModifiers(flags);
    for (Modifier modifier : newModifiers)
    {
      listRewrite.insertLast(modifier, null);
    }    
  }
  
  public String getComment()
  {
    if (comment == UNITIALIZED_STRING)
    {
      comment = getFacadeHelper().toString(getASTNode().getJavadoc());
    }
    return comment;
  }

  public void setComment(String comment)
  {
    this.comment = comment;
    setTrackedNodeProperty(getASTNode(), comment, getASTNode().getJavadocProperty(), ASTNode.JAVADOC);
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode#remove(org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode)
   */
  @Override
  public boolean remove(ASTJNode<?> node)
  {
    if (node.getParent() != this)
    {
      return false;
    }
    
    if (node instanceof ASTJAnnotation)
    {
      remove(node, getASTNode().getModifiersProperty());
      return true;
    }
    
    return false;
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode#insertSibling(org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode, org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode, boolean)
   */
  @Override
  public boolean insertSibling(ASTJNode<?> node, ASTJNode<?> newSibling, boolean before)
  {
    if (newSibling.getParent() != null || node.getParent() != this)
    {
      return false;
    }
    
    if (newSibling instanceof ASTJAnnotation)
    {
      if (node instanceof ASTJAnnotation)
      {
        insert(newSibling, getASTNode().getModifiersProperty(), node, before);
      }
      else
      {
        insertLastAnnotation((ASTJAnnotation)newSibling);
      }
      return true;
    }
    
    return false;
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode#addChild(org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode)
   */
  @Override
  public boolean addChild(ASTJNode<?> child)
  {
    if (child.getParent() != null)
    {
      return false;
    }
    
    if (child instanceof ASTJAnnotation)
    {
      insertLastAnnotation((ASTJAnnotation)child);
      return true;
    }
    return false;
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.emf.codegen.merge.java.facade.AbstractJNode#getChildren()
   */
  @Override
  public List<JNode> getChildren()
  {
    if (!isDisposed())
    {
      List<JNode> children = new ArrayList<JNode>();
      children.addAll(getAnnotationList());
      if (!children.isEmpty())
      {
        return Collections.unmodifiableList(children);
      }
    }
    return Collections.emptyList();
  }

  protected List<JNode> getAnnotationList()
  {
    List<JNode> annotations = new ArrayList<JNode>();
    ListRewrite listRewrite = rewriter.getListRewrite(getASTNode(), getASTNode().getModifiersProperty());
    
    @SuppressWarnings("unchecked")
    List<IExtendedModifier> modifiers = listRewrite.getRewrittenList();
    
    for (IExtendedModifier extendedModifier : modifiers)
    {
      if (extendedModifier.isAnnotation())
      {
        JNode annotation = getFacadeHelper().convertToNode(extendedModifier);
        // specify the exact parent of annotations
        ((ASTJNode<?>)annotation).setParent(this);
        annotations.add(annotation);
      }
    }
    return annotations;    
  }
  
  /**
   * Insert annotation after the last annotation of the given body declaration.
   * <p>
   * Note that modifiers and annotations can alternate in the modifiers list.
   * This method inserts the new annotation after the last existing annotation.
   * 
   * @param annotation
   */
  protected void insertLastAnnotation(ASTJAnnotation annotation)
  {
    List<JNode> annotations = getAnnotationList();
    if (annotations.size() > 0)
    {
      ASTJNode<?> lastAnnotation = (ASTJNode<?>)annotations.get(annotations.size() - 1);
      insert(annotation, getASTNode().getModifiersProperty(), lastAnnotation, false);
    }
    else
    {
      insertFirst(annotation, getASTNode().getModifiersProperty());
    }
  }
}

Back to the top