<s> package org . eclipse . jdt . internal . core . jdom ; import java . util . Enumeration ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . core . jdom . * ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . CharArrayBuffer ; import org . eclipse . jdt . internal . core . util . Util ; class DOMField extends DOMMember implements IDOMField { protected String fType ; protected int [ ] fTypeRange ; protected String fInitializer ; protected int [ ] fInitializerRange ; DOMField ( ) { } DOMField ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int [ ] commentRange , int flags , int [ ] modifierRange , int [ ] typeRange , String type , boolean hasInitializer , int [ ] initRange , boolean isVariableDeclarator ) { super ( document , sourceRange , name , nameRange , commentRange , flags , modifierRange ) ; this . fType = type ; this . fTypeRange = typeRange ; setHasInitializer ( hasInitializer ) ; this . fInitializerRange = initRange ; setIsVariableDeclarator ( isVariableDeclarator ) ; setMask ( MASK_DETAILED_SOURCE_INDEXES , true ) ; } DOMField ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int flags , String type , boolean isVariableDeclarator ) { this ( document , sourceRange , name , nameRange , new int [ ] { - 1 , - 1 } , flags , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , type , false , new int [ ] { - 1 , - 1 } , isVariableDeclarator ) ; setMask ( MASK_DETAILED_SOURCE_INDEXES , false ) ; } protected void appendMemberBodyContents ( CharArrayBuffer buffer ) { } protected void appendMemberDeclarationContents ( CharArrayBuffer buffer ) { if ( isVariableDeclarator ( ) ) { buffer . append ( this . fDocument , this . fSourceRange [ 0 ] , this . fNameRange [ 0 ] - this . fSourceRange [ 0 ] ) ; } else { buffer . append ( getTypeContents ( ) ) . append ( this . fDocument , this . fTypeRange [ 1 ] + 1 , this . fNameRange [ 0 ] - this . fTypeRange [ 1 ] - 1 ) ; } buffer . append ( getNameContents ( ) ) ; if ( hasInitializer ( ) ) { if ( this . fInitializerRange [ 0 ] < 0 ) { buffer . append ( '=' ) . append ( this . fInitializer ) . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fNameRange [ 1 ] ) ; } else { buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fInitializerRange [ 0 ] - this . fNameRange [ 1 ] - 1 ) . append ( getInitializer ( ) ) . append ( this . fDocument , this . fInitializerRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fInitializerRange [ 1 ] ) ; } } else { if ( this . fInitializerRange [ 0 ] < 0 ) { buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fNameRange [ 1 ] ) ; } else { buffer . append ( this . fDocument , this . fInitializerRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fInitializerRange [ 1 ] ) ; } } } protected void appendMemberHeaderFragment ( CharArrayBuffer buffer ) { if ( isVariableDeclarator ( ) ) { return ; } else { super . appendMemberHeaderFragment ( buffer ) ; } } protected void appendSimpleContents ( CharArrayBuffer buffer ) { buffer . append ( this . fDocument , this . fSourceRange [ 0 ] , this . fNameRange [ 0 ] - this . fSourceRange [ 0 ] ) ; buffer . append ( this . fName ) ; buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fNameRange [ 1 ] ) ; } protected void becomeDetailed ( ) throws DOMException { if ( ! isDetailed ( ) ) { if ( isVariableDeclarator ( ) || hasMultipleVariableDeclarators ( ) ) { DOMNode first = getFirstFieldDeclaration ( ) ; DOMNode last = getLastFieldDeclaration ( ) ; DOMNode node = first ; String source = first . getContents ( ) ; while ( node != last ) { node = node . fNextNode ; source += node . getContents ( ) ; } DOMBuilder builder = new DOMBuilder ( ) ; IDOMField [ ] details = builder . createFields ( source . toCharArray ( ) ) ; if ( details . length == 0 ) { throw new DOMException ( Messages . dom_cannotDetail ) ; } else { node = this ; for ( int i = 0 ; i < details . length ; i ++ ) { node . shareContents ( ( DOMNode ) details [ i ] ) ; node = node . fNextNode ; } } } else { super . becomeDetailed ( ) ; } } } public Object clone ( ) { if ( isVariableDeclarator ( ) || hasMultipleVariableDeclarators ( ) ) { return getFactory ( ) . createField ( new String ( getSingleVariableDeclaratorContents ( ) ) ) ; } else { return super . clone ( ) ; } } protected void expand ( ) { if ( isVariableDeclarator ( ) || hasMultipleVariableDeclarators ( ) ) { Enumeration siblings = new SiblingEnumeration ( getFirstFieldDeclaration ( ) ) ; DOMField field = ( DOMField ) siblings . nextElement ( ) ; DOMNode next = field . fNextNode ; while ( siblings . hasMoreElements ( ) && ( next instanceof DOMField ) && ( ( ( DOMField ) next ) . isVariableDeclarator ( ) ) ) { field . localizeContents ( ) ; if ( field . fParent != null ) { field . fParent . fragment ( ) ; } field = ( DOMField ) siblings . nextElement ( ) ; next = field . fNextNode ; } field . localizeContents ( ) ; } } protected DOMNode getDetailedNode ( ) { if ( isVariableDeclarator ( ) || hasMultipleVariableDeclarators ( ) ) { return ( DOMNode ) getFactory ( ) . createField ( new String ( getSingleVariableDeclaratorContents ( ) ) ) ; } else { return ( DOMNode ) getFactory ( ) . createField ( getContents ( ) ) ; } } protected DOMField getFirstFieldDeclaration ( ) { if ( isVariableDeclarator ( ) ) { return ( ( DOMField ) this . fPreviousNode ) . getFirstFieldDeclaration ( ) ; } else { return this ; } } public String getInitializer ( ) { becomeDetailed ( ) ; if ( hasInitializer ( ) ) { if ( this . fInitializer != null ) { return this . fInitializer ; } else { return new String ( this . fDocument , this . fInitializerRange [ 0 ] , this . fInitializerRange [ 1 ] + 1 - this . fInitializerRange [ 0 ] ) ; } } else { return null ; } } public IJavaElement getJavaElement ( IJavaElement parent ) throws IllegalArgumentException { if ( parent . getElementType ( ) == IJavaElement . TYPE ) { return ( ( IType ) parent ) . getField ( getName ( ) ) ; } else { throw new IllegalArgumentException ( Messages . element_illegalParent ) ; } } protected DOMField getLastFieldDeclaration ( ) { DOMField field = this ; while ( field . isVariableDeclarator ( ) || field . hasMultipleVariableDeclarators ( ) ) { if ( field . fNextNode instanceof DOMField && ( ( DOMField ) field . fNextNode ) . isVariableDeclarator ( ) ) { field = ( DOMField ) field . fNextNode ; } else { break ; } } return field ; } protected int getMemberDeclarationStartPosition ( ) { return this . fTypeRange [ 0 ] ; } public int getNodeType ( ) { return IDOMNode . FIELD ; } protected char [ ] getSingleVariableDeclaratorContents ( ) { CharArrayBuffer buffer = new CharArrayBuffer ( ) ; DOMField first = getFirstFieldDeclaration ( ) ; if ( first . isDetailed ( ) ) { first . appendMemberHeaderFragment ( buffer ) ; buffer . append ( getType ( ) ) ; if ( isVariableDeclarator ( ) ) { buffer . append ( ' ' ) ; } else { buffer . append ( this . fDocument , this . fTypeRange [ 1 ] + 1 , this . fNameRange [ 0 ] - this . fTypeRange [ 1 ] - 1 ) ; } } else { buffer . append ( first . fDocument , first . fSourceRange [ 0 ] , first . fNameRange [ 0 ] - first . fSourceRange [ 0 ] ) ; } buffer . append ( getName ( ) ) ; if ( hasInitializer ( ) ) { if ( this . fInitializerRange [ 0 ] < 0 ) { buffer . append ( '=' ) . append ( this . fInitializer ) . append ( ';' ) . append ( Util . getLineSeparator ( buffer . toString ( ) , null ) ) ; } else { buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fInitializerRange [ 0 ] - this . fNameRange [ 1 ] - 1 ) . append ( getInitializer ( ) ) . append ( ';' ) . append ( Util . getLineSeparator ( buffer . toString ( ) , null ) ) ; } } else { buffer . append ( ';' ) . append ( Util . getLineSeparator ( buffer . toString ( ) , null ) ) ; } return buffer . getContents ( ) ; } public String getType ( ) { return this . fType ; } protected char [ ] getTypeContents ( ) { if ( isTypeAltered ( ) ) { return this . fType . toCharArray ( ) ; } else { return CharOperation . subarray ( this . fDocument , this . fTypeRange [ 0 ] , this . fTypeRange [ 1 ] + 1 ) ; } } protected boolean hasInitializer ( ) { return getMask ( MASK_FIELD_HAS_INITIALIZER ) ; } protected boolean hasMultipleVariableDeclarators ( ) { return this . fNextNode != null && ( this . fNextNode instanceof DOMField ) && ( ( DOMField ) this . fNextNode ) . isVariableDeclarator ( ) ; } public void insertSibling ( IDOMNode sibling ) throws IllegalArgumentException , DOMException { if ( isVariableDeclarator ( ) ) { expand ( ) ; } super . insertSibling ( sibling ) ; } protected boolean isTypeAltered ( ) { return getMask ( MASK_FIELD_TYPE_ALTERED ) ; } protected boolean isVariableDeclarator ( ) { return getMask ( MASK_FIELD_IS_VARIABLE_DECLARATOR ) ; } protected DOMNode newDOMNode ( ) { return new DOMField ( ) ; } void normalizeEndPosition ( ILineStartFinder finder , DOMNode next ) { if ( next == null ) { DOMNode parent = ( DOMNode ) getParent ( ) ; if ( parent == null || parent instanceof DOMCompilationUnit ) { setSourceRangeEnd ( this . fDocument . length - 1 ) ; } else { int temp = ( ( DOMType ) parent ) . getCloseBodyPosition ( ) - 1 ; setSourceRangeEnd ( temp ) ; this . fInsertionPosition = Math . max ( finder . getLineStart ( temp + 1 ) , getEndPosition ( ) ) ; } } else { int temp = next . getStartPosition ( ) - 1 ; this . fInsertionPosition = Math . max ( finder . getLineStart ( temp + 1 ) , getEndPosition ( ) ) ; next . normalizeStartPosition ( getEndPosition ( ) , finder ) ; if ( next instanceof DOMField ) { DOMField field = ( DOMField ) next ; if ( field . isVariableDeclarator ( ) && this . fTypeRange [ 0 ] == field . fTypeRange [ 0 ] ) return ; } setSourceRangeEnd ( next . getStartPosition ( ) - 1 ) ; } } void normalizeStartPosition ( int endPosition , ILineStartFinder finder ) { if ( isVariableDeclarator ( ) ) { setStartPosition ( this . fPreviousNode . getEndPosition ( ) + 1 ) ; } else { super . normalizeStartPosition ( endPosition , finder ) ; } } protected void offset ( int offset ) { super . offset ( offset ) ; offsetRange ( this . fInitializerRange , offset ) ; offsetRange ( this . fTypeRange , offset ) ; } public void remove ( ) { expand ( ) ; super . remove ( ) ; } public void setComment ( String comment ) { expand ( ) ; super . setComment ( comment ) ; } public void setFlags ( int flags ) { expand ( ) ; super . setFlags ( flags ) ; } protected void setHasInitializer ( boolean hasInitializer ) { setMask ( MASK_FIELD_HAS_INITIALIZER , hasInitializer ) ; } public void setInitializer ( String initializer ) { becomeDetailed ( ) ; fragment ( ) ; setHasInitializer ( initializer != null ) ; this . fInitializer = initializer ; } void setInitializerRange ( int start , int end ) { this . fInitializerRange [ 0 ] = start ; this . fInitializerRange [ 1 ] = end ; } protected void setIsVariableDeclarator ( boolean isVariableDeclarator ) { setMask ( MASK_FIELD_IS_VARIABLE_DECLARATOR , isVariableDeclarator ) ; } public void setName ( String name ) throws IllegalArgumentException { if ( name == null ) { throw new IllegalArgumentException ( Messages . element_nullName ) ; } else { super . setName ( name ) ; setTypeAltered ( true ) ; } } public void setType ( String typeName ) throws IllegalArgumentException { if ( typeName == null ) { throw new IllegalArgumentException ( Messages . element_nullType ) ; } becomeDetailed ( ) ; expand ( ) ; fragment ( ) ; setTypeAltered ( true ) ; setNameAltered ( true ) ; this . fType = typeName ; } protected void setTypeAltered ( boolean typeAltered ) { setMask ( MASK_FIELD_TYPE_ALTERED , typeAltered ) ; } protected void shareContents ( DOMNode node ) { super . shareContents ( node ) ; DOMField field = ( DOMField ) node ; this . fInitializer = field . fInitializer ; this . fInitializerRange = rangeCopy ( field . fInitializerRange ) ; this . fType = field . fType ; this . fTypeRange = rangeCopy ( field . fTypeRange ) ; } public String toString ( ) { return "FIELD: " + getName ( ) ; } } </s>