org.eclipse.emf.codegen.merge.java.facade.ast
Class ASTJField

java.lang.Object
  extended by org.eclipse.emf.codegen.merge.java.facade.AbstractJNode
      extended by org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode<T>
          extended by org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
              extended by org.eclipse.emf.codegen.merge.java.facade.ast.ASTJField
All Implemented Interfaces:
JField, JMember, JNode

public class ASTJField
extends ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
implements JField

Represents a single variable in a field declaration.

If multiple variables are declared in the same field declaration, there is a unique ASTJField for each variable. Each ASTJField has reference to FieldDeclaration and VariableDeclarationFragment. Multiple ASTJField can share the same FieldDeclaration.

If the field declaration referenced by ASJField has more than 1 variable, calls to some set...() methods on the field and its children (e.g. annotations) will result in a separation of the variable referenced by ASTJField from the field declaration. The new declaration is inserted before the original declaration. After the separation, calls to get...() methods will no longer return the correct original content.

Since:
2.2.0

Field Summary
protected  java.lang.String initializer
          Cached value of initializer of this field.
protected  boolean splitPerformed
          Indicates whether the variable declaration fragment is the only fragment in the field declaration.
protected  java.lang.String type
          Cached type of the field
protected  org.eclipse.jdt.core.dom.VariableDeclarationFragment variableDeclarationFragment
          Variable declaration fragment that is wrapped by ASTJField and to be used by set...
protected  org.eclipse.jdt.core.dom.VariableDeclarationFragment wrappedVariableDeclarationFragment
          Variable declaration fragment that is wrapped by ASTJField and reflects current node in the rewritten tree that is used by this field.
 
Fields inherited from class org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMember
comment
 
Fields inherited from class org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode
isCommentedOut, name, rewriter, UNITIALIZED_STRING
 
Fields inherited from class org.eclipse.emf.codegen.merge.java.facade.AbstractJNode
EMPTY_STRING_ARRAY, qualifiedName
 
Constructor Summary
ASTJField(org.eclipse.jdt.core.dom.VariableDeclarationFragment variableDeclarationFragment, ASTFacadeHelper facadeHelper, org.eclipse.jdt.core.dom.rewrite.ASTRewrite rewriter)
          Sets wrapped object to VariableDeclarationFragment, and prepares variable separation if required
 
Method Summary
 boolean addChild(ASTJNode<?> child)
          Adds a child to this node.
protected  void childToBeChanged(ASTJNode<?> child)
          Notifies the node that the child will be changed.
 void commentOut()
          Commenting out a field results in splitting of the fields, and then commenting out only the field that is commented out.
 void dispose()
           
 java.lang.String getComment()
          Returns the comment associated with this member (including comment delimiters).
 java.lang.String getContents()
          Return the original declaration contents (including all variable declaration fragments in the declaration).
 int getFlags()
          Return original flags of the field declaration.
 java.lang.String getInitializer()
          Returns original initializer of variable declaration fragment.
 java.lang.String getName()
          Returns name of variable declaration fragment.
 org.eclipse.jdt.core.dom.FieldDeclaration getOriginalFieldDeclaration()
           
 java.lang.String getType()
          Returns original type of FieldDeclaration.
 org.eclipse.jdt.core.dom.VariableDeclarationFragment getVariableDeclarationFragment()
           
 org.eclipse.jdt.core.dom.VariableDeclarationFragment getWrappedVariableDeclarationFragment()
           
 boolean insertSibling(ASTJNode<?> node, ASTJNode<?> newSibling, boolean before)
          Inserts newSibling as a child of this node before or after the given node.
protected  void performSplit()
          If required, separates the variable declaration fragment into a new FieldDeclaration object.
protected  void prepareSplit()
          Ensures that the field wrapped by this ASTJField have only 1 variable in the declaration.
 boolean remove(ASTJNode<?> node)
          Removes a node.
protected  void revertPrepareSplit()
          Revert the changes made by prepareSplit().
 void setComment(java.lang.String comment)
          May split the declaration same way as setType(String).
 void setFlags(int flags)
          May split the declaration same way as setType(String) If the declaration has been split, getFlags() might not return the correct value.
 void setInitializer(java.lang.String initializer)
          Sets initializer of variable declaration fragment.
 void setName(java.lang.String name)
          Sets name of variable declaration fragment.
 void setType(java.lang.String type)
          Sets the type of FieldDeclaration.
protected  void setWrappedObject(org.eclipse.jdt.core.dom.ASTNode node)
          Sets wrapped object to be the given FieldDeclaration, and sets wrappedVariableDeclarationFragment attribute to be the first variable declaration fragment in the given FieldDeclaration.
 
Methods inherited from class org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMember
getAnnotationList, getChildren, insertLastAnnotation
 
Methods inherited from class org.eclipse.emf.codegen.merge.java.facade.ast.ASTJNode
addValueToListProperty, addValueToListProperty, ancestorInserted, ancestorToBeRemoved, combineArrayAndList, convertASTNodeListToStringArray, disableTrackAndReplace, enableTrackAndReplace, getASTNode, getFacadeHelper, getParent, getRemovedASTNode, getRewriter, getWrappedObject, insert, insertFirst, insertLast, isDisposed, nodeToBeMoved, nodeToBeRemoved, remove, removeNodeFromListProperty, removeTrackAndReplace, setASTNode, setFacadeHelper, setListNodeProperty, setNodeProperty, setNodeProperty, setParent, setRemovedASTNode, setRewriter, setTrackedNodeProperty, trackAndReplace
 
Methods inherited from class org.eclipse.emf.codegen.merge.java.facade.AbstractJNode
computeQualifiedName, computeQualifiedName, computeQualifiedName, computeQualifiedName, getName, getQualifiedName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.eclipse.emf.codegen.merge.java.facade.JNode
getChildren, getParent, getQualifiedName
 

Field Detail

initializer

protected java.lang.String initializer
Cached value of initializer of this field.

Note that change in initializer should not result in splitting variables in the field declaration.

If setInitializer() has been called, and performSplit() is called after, then performSplit() should not overwrite the initializer.


type

protected java.lang.String type
Cached type of the field

See Also:
getType(), setType(String)

splitPerformed

protected boolean splitPerformed
Indicates whether the variable declaration fragment is the only fragment in the field declaration.


variableDeclarationFragment

protected org.eclipse.jdt.core.dom.VariableDeclarationFragment variableDeclarationFragment
Variable declaration fragment that is wrapped by ASTJField and to be used by set... and get... methods. Note that when field is removed, this variable is not changed. On the other hand, wrappedVariableDeclarationFragment is updated to reflect current node in the tree.

Since the same FieldDeclaration can have multiple variables declared in it, but JField object must be unique for 1 variable, each ASTJField has reference to FieldDeclaration and VariableDeclarationFragment.

See Also:
VariableDeclarationFragment

wrappedVariableDeclarationFragment

protected org.eclipse.jdt.core.dom.VariableDeclarationFragment wrappedVariableDeclarationFragment
Variable declaration fragment that is wrapped by ASTJField and reflects current node in the rewritten tree that is used by this field.

Actual wrapped object is FieldDeclaration object, while VariableDeclarationFragment is stored as an attribute of ASTJField and returned by ASTJNode.getWrappedObject().

See Also:
VariableDeclarationFragment
Constructor Detail

ASTJField

public ASTJField(org.eclipse.jdt.core.dom.VariableDeclarationFragment variableDeclarationFragment,
                 ASTFacadeHelper facadeHelper,
                 org.eclipse.jdt.core.dom.rewrite.ASTRewrite rewriter)
Sets wrapped object to VariableDeclarationFragment, and prepares variable separation if required

Parameters:
variableDeclarationFragment - must have parent of type FieldDeclaration
facadeHelper - facade helper to use for this field, must not be null
rewriter - to use, must not be null
See Also:
prepareSplit()
Method Detail

dispose

public void dispose()
Overrides:
dispose in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>

getOriginalFieldDeclaration

public org.eclipse.jdt.core.dom.FieldDeclaration getOriginalFieldDeclaration()

getWrappedVariableDeclarationFragment

public org.eclipse.jdt.core.dom.VariableDeclarationFragment getWrappedVariableDeclarationFragment()

getVariableDeclarationFragment

public org.eclipse.jdt.core.dom.VariableDeclarationFragment getVariableDeclarationFragment()

addChild

public boolean addChild(ASTJNode<?> child)
Description copied from class: ASTJNode
Adds a child to this node.

Default implementation does nothing and returns false.

Overrides:
addChild in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
child - to add
Returns:
true if operation successful, false otherwise

getComment

public java.lang.String getComment()
Description copied from interface: JMember
Returns the comment associated with this member (including comment delimiters).

Specified by:
getComment in interface JMember
Overrides:
getComment in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
Returns:
the comment, or null if this member has no associated comment

setComment

public void setComment(java.lang.String comment)
May split the declaration same way as setType(String).

If the declaration has been split, getComment() might not return the correct value.

Specified by:
setComment in interface JMember
Overrides:
setComment in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
comment - the comment, including comment delimiters, or null indicating this member should have no associated comment
See Also:
ASTJMember.setComment(java.lang.String)

getContents

public java.lang.String getContents()
Return the original declaration contents (including all variable declaration fragments in the declaration).

If the declaration has been split, the returned value will not be correct.

Specified by:
getContents in interface JNode
Overrides:
getContents in class ASTJNode<org.eclipse.jdt.core.dom.FieldDeclaration>
Returns:
the contents, or null if this node has no contents
See Also:
ASTJNode.getContents()

getFlags

public int getFlags()
Return original flags of the field declaration.

Specified by:
getFlags in interface JNode
Overrides:
getFlags in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
See Also:
ASTJMember.getFlags()

setFlags

public void setFlags(int flags)
May split the declaration same way as setType(String) If the declaration has been split, getFlags() might not return the correct value.

Specified by:
setFlags in interface JNode
Overrides:
setFlags in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
flags - the flags
See Also:
ASTJMember.setFlags(int)

getInitializer

public java.lang.String getInitializer()
Returns original initializer of variable declaration fragment.

Specified by:
getInitializer in interface JField
Returns:
the initializer expression, or null if this field does not have an initializer
See Also:
JField.getInitializer()

setInitializer

public void setInitializer(java.lang.String initializer)
Sets initializer of variable declaration fragment. This operation does not result in splitting fields.

Specified by:
setInitializer in interface JField
Parameters:
initializer - the initializer expression, or null indicating the field does not have an initializer
See Also:
JField.setInitializer(java.lang.String)

getName

public java.lang.String getName()
Returns name of variable declaration fragment.

Specified by:
getName in interface JNode
Returns:
the name, or null if it has no name
See Also:
JNode.getName()

setName

public void setName(java.lang.String name)
Sets name of variable declaration fragment.

Specified by:
setName in interface JNode
See Also:
JNode.setName(java.lang.String)

getType

public java.lang.String getType()
Returns original type of FieldDeclaration. The dimensions declared after variable name are appended to the type.

Specified by:
getType in interface JField
Returns:
the name of the type
See Also:
JField.getType()

setType

public void setType(java.lang.String type)
Sets the type of FieldDeclaration.

If there is only one VariableDeclarationFragment in this declaration, only the type of the FieldDeclaration is changed.

If there are multiple VariableDeclarationFragments in this declaration, variable declaration fragment of this ASTJField is moved to a new FieldDeclaration, and the type of new declaration is set.

Note that if field declaration has been split, getType(), getContents(), getComment(), getInitializer() will not return the original content.

Specified by:
setType in interface JField
Parameters:
type - the type name
See Also:
JField.setType(String)

insertSibling

public boolean insertSibling(ASTJNode<?> node,
                             ASTJNode<?> newSibling,
                             boolean before)
Description copied from class: ASTJNode
Inserts newSibling as a child of this node before or after the given node.

Default implementation does nothing and returns false.

Overrides:
insertSibling in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
newSibling - to insert
before - true if newSibling must be before node, false if after
Returns:
true if operation successful, false otherwise

childToBeChanged

protected void childToBeChanged(ASTJNode<?> child)
Description copied from class: ASTJNode
Notifies the node that the child will be changed.

Parents that are interested in changes to children must override this method. Children that need to notify the parents about changes have to call this method.

Default implementation does nothing.

Overrides:
childToBeChanged in class ASTJNode<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
child - that will be changed

prepareSplit

protected void prepareSplit()
Ensures that the field wrapped by this ASTJField have only 1 variable in the declaration.

If required, this method creates a new field declaration, and sets rewritten AST node to it. The ASTNode of all annotations of the new field is updated to use the original node.

Note that the no changes are added to the rewriter or wrapped object until performSplit() is called.

This method must be called when field is created to ensure that annotations are unique for each ASTJField.

See Also:
performSplit()

revertPrepareSplit

protected void revertPrepareSplit()
Revert the changes made by prepareSplit().

See Also:
prepareSplit()

performSplit

protected void performSplit()
If required, separates the variable declaration fragment into a new FieldDeclaration object. If this declaration does not need to be split, reverts the changes made by prepareSplit().

New field declaration will have only one variable declaration fragment. New declaration is added to the ASTRewrite. The attributes of this ASTJField are updated to reference elements of the new declaration. Only the javadoc, variable initializer, and annotations are copied as String, all other attributes are copied using ASTNode.copySubtree(org.eclipse.jdt.core.dom.AST, ASTNode). All formatting except for Javadoc, initializer, and annotations is lost. If field declaration wrapped by ASTJField has only one variable declaration fragment left, no changes are made.

Note that this method must be called after prepareSplit() and before any changes are made to the nodes.

See Also:
prepareSplit()

remove

public boolean remove(ASTJNode<?> node)
Description copied from class: ASTJNode
Removes a node.

Default implementation does nothing and returns false.

Overrides:
remove in class ASTJMember<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
node - must be a child of this node
Returns:
true if operation successful, false otherwise

setWrappedObject

protected void setWrappedObject(org.eclipse.jdt.core.dom.ASTNode node)
Sets wrapped object to be the given FieldDeclaration, and sets wrappedVariableDeclarationFragment attribute to be the first variable declaration fragment in the given FieldDeclaration.

This method is mainly used by remove(ASTJNode) to create a move target to allow insertion of the nodes later.

Overrides:
setWrappedObject in class ASTJNode<org.eclipse.jdt.core.dom.FieldDeclaration>
Parameters:
node - must be of type FieldDeclaration, ignored otherwise
See Also:
ASTJNode.setWrappedObject(org.eclipse.jdt.core.dom.ASTNode)

commentOut

public void commentOut()
Commenting out a field results in splitting of the fields, and then commenting out only the field that is commented out.

Overrides:
commentOut in class ASTJNode<org.eclipse.jdt.core.dom.FieldDeclaration>
See Also:
ASTJNode.commentOut()

Copyright 2001-2006 IBM Corporation and others.
All Rights Reserved.