<s> package org . eclipse . jdt . internal . core . jdom ; import java . util . Enumeration ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . core . compiler . InvalidInputException ; import org . eclipse . jdt . core . jdom . * ; import org . eclipse . jdt . internal . compiler . parser . Scanner ; import org . eclipse . jdt . internal . compiler . parser . TerminalTokens ; import org . eclipse . jdt . internal . core . util . CharArrayBuffer ; import org . eclipse . jdt . internal . core . util . Messages ; class DOMType extends DOMMember implements IDOMType { protected String fTypeKeyword ; protected int [ ] fTypeRange ; protected String fSuperclass ; protected int [ ] fSuperclassRange ; protected int [ ] fExtendsRange ; protected int [ ] fImplementsRange ; protected char [ ] fInterfaces ; protected int [ ] fInterfacesRange ; protected int [ ] fOpenBodyRange ; protected int [ ] fCloseBodyRange ; protected String [ ] fSuperInterfaces = CharOperation . NO_STRINGS ; protected String [ ] fTypeParameters = CharOperation . NO_STRINGS ; protected boolean fIsEnum = false ; protected boolean fIsAnnotation = false ; DOMType ( ) { } DOMType ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int [ ] commentRange , int flags , int [ ] modifierRange , int [ ] typeRange , int [ ] superclassRange , int [ ] extendsRange , String [ ] implementsList , int [ ] implementsRange , int [ ] implementsKeywordRange , int [ ] openBodyRange , int [ ] closeBodyRange , boolean isClass ) { super ( document , sourceRange , name , nameRange , commentRange , flags , modifierRange ) ; this . fTypeRange = typeRange ; setMask ( MASK_TYPE_IS_CLASS , isClass ) ; this . fExtendsRange = extendsRange ; this . fImplementsRange = implementsKeywordRange ; this . fSuperclassRange = superclassRange ; this . fInterfacesRange = implementsRange ; this . fCloseBodyRange = closeBodyRange ; setMask ( MASK_TYPE_HAS_SUPERCLASS , superclassRange [ 0 ] > 0 ) ; setMask ( MASK_TYPE_HAS_INTERFACES , implementsList != null ) ; this . fSuperInterfaces = implementsList ; this . fOpenBodyRange = openBodyRange ; this . fCloseBodyRange = closeBodyRange ; setMask ( MASK_DETAILED_SOURCE_INDEXES , true ) ; } DOMType ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int flags , String [ ] implementsList , boolean isClass ) { this ( document , sourceRange , name , nameRange , new int [ ] { - 1 , - 1 } , flags , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , implementsList , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } , new int [ ] { sourceRange [ 1 ] , sourceRange [ 1 ] } , isClass ) ; setMask ( MASK_DETAILED_SOURCE_INDEXES , false ) ; } public void addSuperInterface ( String name ) throws IllegalArgumentException { if ( name == null ) { throw new IllegalArgumentException ( Messages . dom_addNullInterface ) ; } if ( this . fSuperInterfaces == null ) { this . fSuperInterfaces = new String [ 1 ] ; this . fSuperInterfaces [ 0 ] = name ; } else { this . fSuperInterfaces = appendString ( this . fSuperInterfaces , name ) ; } setSuperInterfaces ( this . fSuperInterfaces ) ; } protected void appendMemberBodyContents ( CharArrayBuffer buffer ) { buffer . append ( this . fDocument , this . fOpenBodyRange [ 0 ] , this . fOpenBodyRange [ 1 ] + 1 - this . fOpenBodyRange [ 0 ] ) ; appendContentsOfChildren ( buffer ) ; buffer . append ( this . fDocument , this . fCloseBodyRange [ 0 ] , this . fCloseBodyRange [ 1 ] + 1 - this . fCloseBodyRange [ 0 ] ) ; buffer . append ( this . fDocument , this . fCloseBodyRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fCloseBodyRange [ 1 ] ) ; } protected void appendMemberDeclarationContents ( CharArrayBuffer buffer ) { if ( this . fTypeKeyword != null ) { buffer . append ( this . fTypeKeyword ) ; buffer . append ( this . fDocument , this . fTypeRange [ 1 ] , this . fNameRange [ 0 ] - this . fTypeRange [ 1 ] ) ; } else { buffer . append ( this . fDocument , this . fTypeRange [ 0 ] , this . fTypeRange [ 1 ] + 1 - this . fTypeRange [ 0 ] ) ; } buffer . append ( getName ( ) ) ; if ( isClass ( ) ) { boolean hasInterfaces = false ; if ( getMask ( MASK_TYPE_HAS_SUPERCLASS ) ) { if ( this . fExtendsRange [ 0 ] < 0 ) { buffer . append ( " extends " ) ; } else { buffer . append ( this . fDocument , this . fExtendsRange [ 0 ] , this . fExtendsRange [ 1 ] + 1 - this . fExtendsRange [ 0 ] ) ; } if ( this . fSuperclass != null ) { buffer . append ( this . fSuperclass ) ; } else { buffer . append ( this . fDocument , this . fSuperclassRange [ 0 ] , this . fSuperclassRange [ 1 ] + 1 - this . fSuperclassRange [ 0 ] ) ; } } if ( getMask ( MASK_TYPE_HAS_INTERFACES ) ) { hasInterfaces = true ; if ( this . fImplementsRange [ 0 ] < 0 ) { buffer . append ( " implements " ) ; } else { buffer . append ( this . fDocument , this . fImplementsRange [ 0 ] , this . fImplementsRange [ 1 ] + 1 - this . fImplementsRange [ 0 ] ) ; } if ( this . fInterfaces != null ) { buffer . append ( this . fInterfaces ) ; } else { buffer . append ( this . fDocument , this . fInterfacesRange [ 0 ] , this . fInterfacesRange [ 1 ] + 1 - this . fInterfacesRange [ 0 ] ) ; } } if ( hasInterfaces ) { if ( this . fImplementsRange [ 0 ] < 0 ) { buffer . append ( ' ' ) ; } else { buffer . append ( this . fDocument , this . fInterfacesRange [ 1 ] + 1 , this . fOpenBodyRange [ 0 ] - this . fInterfacesRange [ 1 ] - 1 ) ; } } else { if ( this . fSuperclassRange [ 0 ] < 0 ) { buffer . append ( ' ' ) ; } else if ( this . fImplementsRange [ 0 ] > 0 ) { buffer . append ( this . fDocument , this . fSuperclassRange [ 1 ] + 1 , this . fImplementsRange [ 0 ] - this . fSuperclassRange [ 1 ] - 1 ) ; buffer . append ( this . fDocument , this . fInterfacesRange [ 1 ] + 1 , this . fOpenBodyRange [ 0 ] - this . fInterfacesRange [ 1 ] - 1 ) ; } else { buffer . append ( this . fDocument , this . fSuperclassRange [ 1 ] + 1 , this . fOpenBodyRange [ 0 ] - this . fSuperclassRange [ 1 ] - 1 ) ; } } } else { if ( getMask ( MASK_TYPE_HAS_INTERFACES ) ) { if ( this . fExtendsRange [ 0 ] < 0 ) { buffer . append ( " extends " ) ; } else { buffer . append ( this . fDocument , this . fExtendsRange [ 0 ] , this . fExtendsRange [ 1 ] + 1 - this . fExtendsRange [ 0 ] ) ; } if ( this . fInterfaces != null ) { buffer . append ( this . fInterfaces ) ; buffer . append ( ' ' ) ; } else { buffer . append ( this . fDocument , this . fInterfacesRange [ 0 ] , this . fInterfacesRange [ 1 ] + 1 - this . fInterfacesRange [ 0 ] ) ; } } else { if ( this . fImplementsRange [ 0 ] < 0 ) { buffer . append ( ' ' ) ; } else { buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fOpenBodyRange [ 0 ] - this . fNameRange [ 1 ] - 1 ) ; } } } } 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 . fOpenBodyRange [ 1 ] - this . fNameRange [ 1 ] ) ; appendContentsOfChildren ( buffer ) ; buffer . append ( this . fDocument , this . fCloseBodyRange [ 0 ] , this . fSourceRange [ 1 ] - this . fCloseBodyRange [ 0 ] + 1 ) ; } public boolean canHaveChildren ( ) { return true ; } int getCloseBodyPosition ( ) { return this . fCloseBodyRange [ 0 ] ; } protected DOMNode getDetailedNode ( ) { return ( DOMNode ) getFactory ( ) . createType ( getContents ( ) ) ; } public int getInsertionPosition ( ) { return this . fInsertionPosition ; } public IJavaElement getJavaElement ( IJavaElement parent ) throws IllegalArgumentException { switch ( parent . getElementType ( ) ) { case IJavaElement . COMPILATION_UNIT : return ( ( ICompilationUnit ) parent ) . getType ( getName ( ) ) ; case IJavaElement . TYPE : return ( ( IType ) parent ) . getType ( getName ( ) ) ; default : throw new IllegalArgumentException ( Messages . element_illegalParent ) ; } } protected int getMemberDeclarationStartPosition ( ) { return this . fTypeRange [ 0 ] ; } public int getNodeType ( ) { return IDOMNode . TYPE ; } int getOpenBodyEnd ( ) { return this . fOpenBodyRange [ 1 ] ; } public String getSuperclass ( ) { becomeDetailed ( ) ; if ( getMask ( MASK_TYPE_HAS_SUPERCLASS ) ) { if ( this . fSuperclass != null ) { return this . fSuperclass ; } else { return new String ( this . fDocument , this . fSuperclassRange [ 0 ] , this . fSuperclassRange [ 1 ] + 1 - this . fSuperclassRange [ 0 ] ) ; } } else { return null ; } } public String [ ] getSuperInterfaces ( ) { return this . fSuperInterfaces ; } public boolean isAllowableChild ( IDOMNode node ) { if ( node != null ) { int type = node . getNodeType ( ) ; return type == IDOMNode . TYPE || type == IDOMNode . FIELD || type == IDOMNode . METHOD || type == IDOMNode . INITIALIZER ; } else { return false ; } } public boolean isClass ( ) { return getMask ( MASK_TYPE_IS_CLASS ) ; } protected DOMNode newDOMNode ( ) { return new DOMType ( ) ; } void normalize ( ILineStartFinder finder ) { int openBodyEnd , openBodyStart , closeBodyStart , closeBodyEnd ; DOMNode first = ( DOMNode ) getFirstChild ( ) ; DOMNode lastNode = null ; Scanner scanner = new Scanner ( ) ; scanner . setSource ( this . fDocument ) ; scanner . resetTo ( this . fNameRange [ 1 ] + 1 , this . fDocument . length ) ; try { int currentToken = scanner . getNextToken ( ) ; while ( currentToken != TerminalTokens . TokenNameLBRACE && currentToken != TerminalTokens . TokenNameEOF ) { currentToken = scanner . getNextToken ( ) ; } if ( currentToken == TerminalTokens . TokenNameLBRACE ) { openBodyEnd = scanner . currentPosition - 1 ; openBodyStart = scanner . startPosition ; } else { openBodyEnd = this . fDocument . length ; openBodyStart = this . fDocument . length ; } } catch ( InvalidInputException e ) { openBodyEnd = this . fDocument . length ; openBodyStart = this . fDocument . length ; } if ( first != null ) { int lineStart = finder . getLineStart ( first . getStartPosition ( ) ) ; if ( lineStart > openBodyEnd ) { openBodyEnd = lineStart - 1 ; } else { openBodyEnd = first . getStartPosition ( ) - 1 ; } lastNode = ( DOMNode ) first . getNextNode ( ) ; if ( lastNode == null ) { lastNode = first ; } else { while ( lastNode . getNextNode ( ) != null ) { lastNode = ( DOMNode ) lastNode . getNextNode ( ) ; } } scanner . setSource ( this . fDocument ) ; scanner . resetTo ( lastNode . getEndPosition ( ) + 1 , this . fDocument . length ) ; try { int currentToken = scanner . getNextToken ( ) ; while ( currentToken != TerminalTokens . TokenNameRBRACE && currentToken != TerminalTokens . TokenNameEOF ) { currentToken = scanner . getNextToken ( ) ; } if ( currentToken == TerminalTokens . TokenNameRBRACE ) { closeBodyStart = scanner . startPosition ; closeBodyEnd = scanner . currentPosition - 1 ; } else { closeBodyStart = this . fDocument . length ; closeBodyEnd = this . fDocument . length ; } } catch ( InvalidInputException e ) { closeBodyStart = this . fDocument . length ; closeBodyEnd = this . fDocument . length ; } } else { scanner . resetTo ( openBodyEnd , this . fDocument . length ) ; try { int currentToken = scanner . getNextToken ( ) ; while ( currentToken != TerminalTokens . TokenNameRBRACE && currentToken != TerminalTokens . TokenNameEOF ) { currentToken = scanner . getNextToken ( ) ; } if ( currentToken == TerminalTokens . TokenNameRBRACE ) { closeBodyStart = scanner . startPosition ; closeBodyEnd = scanner . currentPosition - 1 ; } else { closeBodyStart = this . fDocument . length ; closeBodyEnd = this . fDocument . length ; } } catch ( InvalidInputException e ) { closeBodyStart = this . fDocument . length ; closeBodyEnd = this . fDocument . length ; } openBodyEnd = closeBodyEnd - 1 ; } setOpenBodyRangeEnd ( openBodyEnd ) ; setOpenBodyRangeStart ( openBodyStart ) ; setCloseBodyRangeStart ( closeBodyStart ) ; setCloseBodyRangeEnd ( closeBodyEnd ) ; this . fInsertionPosition = finder . getLineStart ( closeBodyStart ) ; if ( lastNode != null && this . fInsertionPosition < lastNode . getEndPosition ( ) ) { this . fInsertionPosition = getCloseBodyPosition ( ) ; } if ( this . fInsertionPosition <= openBodyEnd ) { this . fInsertionPosition = getCloseBodyPosition ( ) ; } super . normalize ( finder ) ; } 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 { setSourceRangeEnd ( ( ( DOMType ) parent ) . getCloseBodyPosition ( ) - 1 ) ; } } else { next . normalizeStartPosition ( getEndPosition ( ) , finder ) ; setSourceRangeEnd ( next . getStartPosition ( ) - 1 ) ; } } protected void offset ( int offset ) { super . offset ( offset ) ; offsetRange ( this . fCloseBodyRange , offset ) ; offsetRange ( this . fExtendsRange , offset ) ; offsetRange ( this . fImplementsRange , offset ) ; offsetRange ( this . fInterfacesRange , offset ) ; offsetRange ( this . fOpenBodyRange , offset ) ; offsetRange ( this . fSuperclassRange , offset ) ; offsetRange ( this . fTypeRange , offset ) ; } public void setClass ( boolean b ) { becomeDetailed ( ) ; fragment ( ) ; setMask ( MASK_TYPE_IS_CLASS , b ) ; if ( b ) { this . fTypeKeyword = "class" ; } else { this . fTypeKeyword = "interface" ; setSuperclass ( null ) ; } } void setCloseBodyRangeEnd ( int end ) { this . fCloseBodyRange [ 1 ] = end ; } void setCloseBodyRangeStart ( int start ) { this . fCloseBodyRange [ 0 ] = start ; } public void setName ( String name ) throws IllegalArgumentException { if ( name == null ) { throw new IllegalArgumentException ( Messages . element_nullName ) ; } super . setName ( name ) ; Enumeration children = getChildren ( ) ; while ( children . hasMoreElements ( ) ) { IDOMNode child = ( IDOMNode ) children . nextElement ( ) ; if ( child . getNodeType ( ) == IDOMNode . METHOD && ( ( IDOMMethod ) child ) . isConstructor ( ) ) { ( ( DOMNode ) child ) . fragment ( ) ; } } } void setOpenBodyRangeEnd ( int end ) { this . fOpenBodyRange [ 1 ] = end ; } void setOpenBodyRangeStart ( int start ) { this . fOpenBodyRange [ 0 ] = start ; } public void setSuperclass ( String superclassName ) { becomeDetailed ( ) ; fragment ( ) ; this . fSuperclass = superclassName ; setMask ( MASK_TYPE_HAS_SUPERCLASS , superclassName != null ) ; } public void setSuperInterfaces ( String [ ] names ) { becomeDetailed ( ) ; if ( names == null ) { throw new IllegalArgumentException ( Messages . dom_nullInterfaces ) ; } fragment ( ) ; this . fSuperInterfaces = names ; if ( names . length == 0 ) { this . fInterfaces = null ; this . fSuperInterfaces = CharOperation . NO_STRINGS ; setMask ( MASK_TYPE_HAS_INTERFACES , false ) ; } else { setMask ( MASK_TYPE_HAS_INTERFACES , true ) ; CharArrayBuffer buffer = new CharArrayBuffer ( ) ; for ( int i = 0 ; i < names . length ; i ++ ) { if ( i > 0 ) { buffer . append ( ", " ) ; } buffer . append ( names [ i ] ) ; } this . fInterfaces = buffer . getContents ( ) ; } } void setTypeKeyword ( String keyword ) { this . fTypeKeyword = keyword ; } protected void shareContents ( DOMNode node ) { super . shareContents ( node ) ; DOMType type = ( DOMType ) node ; this . fCloseBodyRange = rangeCopy ( type . fCloseBodyRange ) ; this . fExtendsRange = type . fExtendsRange ; this . fImplementsRange = rangeCopy ( type . fImplementsRange ) ; this . fInterfaces = type . fInterfaces ; this . fInterfacesRange = rangeCopy ( type . fInterfacesRange ) ; this . fOpenBodyRange = rangeCopy ( type . fOpenBodyRange ) ; this . fSuperclass = type . fSuperclass ; this . fSuperclassRange = rangeCopy ( type . fSuperclassRange ) ; this . fSuperInterfaces = type . fSuperInterfaces ; this . fTypeKeyword = type . fTypeKeyword ; this . fTypeRange = rangeCopy ( type . fTypeRange ) ; } public String toString ( ) { return "TYPE: " + getName ( ) ; } public String [ ] getTypeParameters ( ) { return this . fTypeParameters ; } public boolean isEnum ( ) { return this . fIsEnum ; } public boolean isAnnotation ( ) { return this . fIsAnnotation ; } public void setEnum ( boolean b ) { this . fIsEnum = b ; if ( this . fIsEnum ) { setClass ( true ) ; setSuperclass ( null ) ; } } public void setAnnotation ( boolean b ) { this . fIsAnnotation = b ; if ( this . fIsAnnotation ) { setClass ( false ) ; setSuperclass ( null ) ; setSuperInterfaces ( CharOperation . NO_STRINGS ) ; } } public void setTypeParameters ( String [ ] typeParameters ) { this . fTypeParameters = typeParameters ; } } </s>