<s> package org . eclipse . jdt . internal . core . jdom ; import org . eclipse . jdt . core . Flags ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . Signature ; 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 DOMMethod extends DOMMember implements IDOMMethod { protected String fReturnType ; protected int [ ] fReturnTypeRange ; protected char [ ] fParameterList ; protected int [ ] fParameterRange ; protected char [ ] fExceptionList ; protected int [ ] fExceptionRange ; protected String fBody ; protected int [ ] fBodyRange ; protected String [ ] fParameterNames ; protected String [ ] fParameterTypes ; protected String [ ] fExceptions ; protected String [ ] fTypeParameters = CharOperation . NO_STRINGS ; protected String fDefaultValue = null ; DOMMethod ( ) { } DOMMethod ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int [ ] commentRange , int flags , int [ ] modifierRange , boolean isConstructor , String returnType , int [ ] returnTypeRange , String [ ] parameterTypes , String [ ] parameterNames , int [ ] parameterRange , String [ ] exceptions , int [ ] exceptionRange , int [ ] bodyRange ) { super ( document , sourceRange , name , nameRange , commentRange , flags , modifierRange ) ; setMask ( MASK_IS_CONSTRUCTOR , isConstructor ) ; this . fReturnType = returnType ; this . fReturnTypeRange = returnTypeRange ; this . fParameterTypes = parameterTypes ; this . fParameterNames = parameterNames ; this . fParameterRange = parameterRange ; this . fExceptionRange = exceptionRange ; this . fExceptions = exceptions ; setHasBody ( true ) ; this . fBodyRange = bodyRange ; setMask ( MASK_DETAILED_SOURCE_INDEXES , true ) ; } DOMMethod ( char [ ] document , int [ ] sourceRange , String name , int [ ] nameRange , int flags , boolean isConstructor , String returnType , String [ ] parameterTypes , String [ ] parameterNames , String [ ] exceptions ) { this ( document , sourceRange , name , nameRange , new int [ ] { - 1 , - 1 } , flags , new int [ ] { - 1 , - 1 } , isConstructor , returnType , new int [ ] { - 1 , - 1 } , parameterTypes , parameterNames , new int [ ] { - 1 , - 1 } , exceptions , new int [ ] { - 1 , - 1 } , new int [ ] { - 1 , - 1 } ) ; setMask ( MASK_DETAILED_SOURCE_INDEXES , false ) ; } public void addException ( String name ) throws IllegalArgumentException { if ( name == null ) { throw new IllegalArgumentException ( Messages . dom_nullExceptionType ) ; } if ( this . fExceptions == null ) { this . fExceptions = new String [ 1 ] ; this . fExceptions [ 0 ] = name ; } else { this . fExceptions = appendString ( this . fExceptions , name ) ; } setExceptions ( this . fExceptions ) ; } public void addParameter ( String type , String name ) throws IllegalArgumentException { if ( type == null ) { throw new IllegalArgumentException ( Messages . dom_nullTypeParameter ) ; } if ( name == null ) { throw new IllegalArgumentException ( Messages . dom_nullNameParameter ) ; } if ( this . fParameterNames == null ) { this . fParameterNames = new String [ 1 ] ; this . fParameterNames [ 0 ] = name ; } else { this . fParameterNames = appendString ( this . fParameterNames , name ) ; } if ( this . fParameterTypes == null ) { this . fParameterTypes = new String [ 1 ] ; this . fParameterTypes [ 0 ] = type ; } else { this . fParameterTypes = appendString ( this . fParameterTypes , type ) ; } setParameters ( this . fParameterTypes , this . fParameterNames ) ; } protected void appendMemberBodyContents ( CharArrayBuffer buffer ) { if ( this . fBody != null ) { buffer . append ( this . fBody ) ; } else { buffer . append ( this . fDocument , this . fBodyRange [ 0 ] , this . fBodyRange [ 1 ] + 1 - this . fBodyRange [ 0 ] ) ; } } protected void appendMemberDeclarationContents ( CharArrayBuffer buffer ) { if ( isConstructor ( ) ) { buffer . append ( getConstructorName ( ) ) . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fParameterRange [ 0 ] - this . fNameRange [ 1 ] - 1 ) ; } else { buffer . append ( getReturnTypeContents ( ) ) ; if ( this . fReturnTypeRange [ 0 ] >= 0 ) { buffer . append ( this . fDocument , this . fReturnTypeRange [ 1 ] + 1 , this . fNameRange [ 0 ] - this . fReturnTypeRange [ 1 ] - 1 ) ; } else { buffer . append ( ' ' ) ; } buffer . append ( getNameContents ( ) ) . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fParameterRange [ 0 ] - this . fNameRange [ 1 ] - 1 ) ; } if ( this . fParameterList != null ) { buffer . append ( this . fParameterList ) ; } else { buffer . append ( this . fDocument , this . fParameterRange [ 0 ] , this . fParameterRange [ 1 ] + 1 - this . fParameterRange [ 0 ] ) ; } int start ; if ( hasTrailingArrayQualifier ( ) && isReturnTypeAltered ( ) ) { start = this . fReturnTypeRange [ 3 ] + 1 ; } else { start = this . fParameterRange [ 1 ] + 1 ; } if ( this . fExceptions != null ) { if ( this . fExceptionRange [ 0 ] >= 0 ) { buffer . append ( this . fDocument , start , this . fExceptionRange [ 0 ] - start ) ; } else { buffer . append ( " throws " ) ; } if ( this . fExceptionList != null ) { buffer . append ( this . fExceptionList ) ; if ( this . fExceptionRange [ 0 ] >= 0 ) { buffer . append ( this . fDocument , this . fExceptionRange [ 1 ] + 1 , this . fBodyRange [ 0 ] - this . fExceptionRange [ 1 ] - 1 ) ; } else { buffer . append ( this . fDocument , this . fParameterRange [ 1 ] + 1 , this . fBodyRange [ 0 ] - this . fParameterRange [ 1 ] - 1 ) ; } } else { buffer . append ( this . fDocument , this . fExceptionRange [ 0 ] , this . fBodyRange [ 0 ] - this . fExceptionRange [ 0 ] ) ; } } else { if ( this . fExceptionRange [ 0 ] >= 0 ) { buffer . append ( this . fDocument , this . fExceptionRange [ 1 ] + 1 , this . fBodyRange [ 0 ] - this . fExceptionRange [ 1 ] - 1 ) ; } else { buffer . append ( this . fDocument , start , this . fBodyRange [ 0 ] - start ) ; } } } protected void appendSimpleContents ( CharArrayBuffer buffer ) { buffer . append ( this . fDocument , this . fSourceRange [ 0 ] , this . fNameRange [ 0 ] - this . fSourceRange [ 0 ] ) ; if ( isConstructor ( ) ) { buffer . append ( getConstructorName ( ) ) ; } else { buffer . append ( this . fName ) ; } buffer . append ( this . fDocument , this . fNameRange [ 1 ] + 1 , this . fSourceRange [ 1 ] - this . fNameRange [ 1 ] ) ; } public String getBody ( ) { becomeDetailed ( ) ; if ( hasBody ( ) ) { if ( this . fBody != null ) { return this . fBody ; } else { return new String ( this . fDocument , this . fBodyRange [ 0 ] , this . fBodyRange [ 1 ] + 1 - this . fBodyRange [ 0 ] ) ; } } else { return null ; } } protected String getConstructorName ( ) { if ( isConstructor ( ) ) { if ( getParent ( ) != null ) { return getParent ( ) . getName ( ) ; } else { return new String ( getNameContents ( ) ) ; } } else { return null ; } } protected DOMNode getDetailedNode ( ) { return ( DOMNode ) getFactory ( ) . createMethod ( getContents ( ) ) ; } public String [ ] getExceptions ( ) { return this . fExceptions ; } protected char [ ] generateFlags ( ) { char [ ] flags = Flags . toString ( getFlags ( ) & ~ Flags . AccVarargs ) . toCharArray ( ) ; if ( flags . length == 0 ) { return flags ; } else { return CharOperation . concat ( flags , new char [ ] { ' ' } ) ; } } public IJavaElement getJavaElement ( IJavaElement parent ) throws IllegalArgumentException { if ( parent . getElementType ( ) == IJavaElement . TYPE ) { String [ ] sigs = null ; if ( this . fParameterTypes != null ) { sigs = new String [ this . fParameterTypes . length ] ; int i ; for ( i = 0 ; i < this . fParameterTypes . length ; i ++ ) { sigs [ i ] = Signature . createTypeSignature ( this . fParameterTypes [ i ] . toCharArray ( ) , false ) ; } } String name = null ; if ( isConstructor ( ) ) { name = getConstructorName ( ) ; } else { name = getName ( ) ; } return ( ( IType ) parent ) . getMethod ( name , sigs ) ; } else { throw new IllegalArgumentException ( Messages . element_illegalParent ) ; } } protected int getMemberDeclarationStartPosition ( ) { if ( this . fReturnTypeRange [ 0 ] >= 0 ) { return this . fReturnTypeRange [ 0 ] ; } else { return this . fNameRange [ 0 ] ; } } public String getName ( ) { if ( isConstructor ( ) ) { return null ; } else { return super . getName ( ) ; } } public int getNodeType ( ) { return IDOMNode . METHOD ; } public String [ ] getParameterNames ( ) { return this . fParameterNames ; } public String [ ] getParameterTypes ( ) { return this . fParameterTypes ; } public String getReturnType ( ) { if ( isConstructor ( ) ) { return null ; } else { return this . fReturnType ; } } protected char [ ] getReturnTypeContents ( ) { if ( isConstructor ( ) ) { return null ; } else { if ( isReturnTypeAltered ( ) ) { return this . fReturnType . toCharArray ( ) ; } else { return CharOperation . subarray ( this . fDocument , this . fReturnTypeRange [ 0 ] , this . fReturnTypeRange [ 1 ] + 1 ) ; } } } protected boolean hasTrailingArrayQualifier ( ) { return this . fReturnTypeRange . length > 2 ; } public boolean isConstructor ( ) { return getMask ( MASK_IS_CONSTRUCTOR ) ; } protected boolean isReturnTypeAltered ( ) { return getMask ( MASK_RETURN_TYPE_ALTERED ) ; } public boolean isSignatureEqual ( IDOMNode node ) { boolean ok = node . getNodeType ( ) == getNodeType ( ) ; if ( ok ) { IDOMMethod method = ( IDOMMethod ) node ; ok = ( isConstructor ( ) && method . isConstructor ( ) ) || ( ! isConstructor ( ) && ! method . isConstructor ( ) ) ; if ( ok && ! isConstructor ( ) ) { ok = getName ( ) . equals ( method . getName ( ) ) ; } if ( ! ok ) { return false ; } String [ ] types = method . getParameterTypes ( ) ; if ( this . fParameterTypes == null || this . fParameterTypes . length == 0 ) { if ( types == null || types . length == 0 ) { return true ; } } else { if ( types == null || types . length == 0 ) { return false ; } if ( this . fParameterTypes . length != types . length ) { return false ; } int i ; for ( i = 0 ; i < types . length ; i ++ ) { if ( ! this . fParameterTypes [ i ] . equals ( types [ i ] ) ) { return false ; } } return true ; } } return false ; } protected DOMNode newDOMNode ( ) { return new DOMMethod ( ) ; } protected void offset ( int offset ) { super . offset ( offset ) ; offsetRange ( this . fBodyRange , offset ) ; offsetRange ( this . fExceptionRange , offset ) ; offsetRange ( this . fParameterRange , offset ) ; offsetRange ( this . fReturnTypeRange , offset ) ; } public void setBody ( String body ) { becomeDetailed ( ) ; fragment ( ) ; this . fBody = body ; setHasBody ( body != null ) ; if ( ! hasBody ( ) ) { this . fBody = ";" + Util . getLineSeparator ( body , null ) ; } } void setBodyRangeEnd ( int end ) { this . fBodyRange [ 1 ] = end ; } public void setConstructor ( boolean b ) { becomeDetailed ( ) ; setMask ( MASK_IS_CONSTRUCTOR , b ) ; fragment ( ) ; } public void setExceptions ( String [ ] names ) { becomeDetailed ( ) ; if ( names == null || names . length == 0 ) { this . fExceptions = null ; } else { this . fExceptions = names ; CharArrayBuffer buffer = new CharArrayBuffer ( ) ; char [ ] comma = new char [ ] { ',' , ' ' } ; for ( int i = 0 , length = names . length ; i < length ; i ++ ) { if ( i > 0 ) buffer . append ( comma ) ; buffer . append ( names [ i ] ) ; } this . fExceptionList = buffer . getContents ( ) ; } fragment ( ) ; } public void setName ( String name ) { if ( name == null ) { throw new IllegalArgumentException ( Messages . element_nullName ) ; } else { super . setName ( name ) ; } } public void setParameters ( String [ ] types , String [ ] names ) throws IllegalArgumentException { becomeDetailed ( ) ; if ( types == null || names == null ) { if ( types == null && names == null ) { this . fParameterTypes = null ; this . fParameterNames = null ; this . fParameterList = new char [ ] { '(' , ')' } ; } else { throw new IllegalArgumentException ( Messages . dom_mismatchArgNamesAndTypes ) ; } } else if ( names . length != types . length ) { throw new IllegalArgumentException ( Messages . dom_mismatchArgNamesAndTypes ) ; } else if ( names . length == 0 ) { setParameters ( null , null ) ; } else { this . fParameterNames = names ; this . fParameterTypes = types ; CharArrayBuffer parametersBuffer = new CharArrayBuffer ( ) ; parametersBuffer . append ( "(" ) ; char [ ] comma = new char [ ] { ',' , ' ' } ; for ( int i = 0 ; i < names . length ; i ++ ) { if ( i > 0 ) { parametersBuffer . append ( comma ) ; } parametersBuffer . append ( types [ i ] ) . append ( ' ' ) . append ( names [ i ] ) ; } parametersBuffer . append ( ')' ) ; this . fParameterList = parametersBuffer . getContents ( ) ; } fragment ( ) ; } public void setReturnType ( String name ) throws IllegalArgumentException { if ( name == null ) { throw new IllegalArgumentException ( Messages . dom_nullReturnType ) ; } becomeDetailed ( ) ; fragment ( ) ; setReturnTypeAltered ( true ) ; this . fReturnType = name ; } protected void setReturnTypeAltered ( boolean typeAltered ) { setMask ( MASK_RETURN_TYPE_ALTERED , typeAltered ) ; } protected void setSourceRangeEnd ( int end ) { super . setSourceRangeEnd ( end ) ; this . fBodyRange [ 1 ] = end ; } protected void shareContents ( DOMNode node ) { super . shareContents ( node ) ; DOMMethod method = ( DOMMethod ) node ; this . fBody = method . fBody ; this . fBodyRange = rangeCopy ( method . fBodyRange ) ; this . fExceptionList = method . fExceptionList ; this . fExceptionRange = rangeCopy ( method . fExceptionRange ) ; this . fExceptions = method . fExceptions ; this . fParameterList = method . fParameterList ; this . fParameterNames = method . fParameterNames ; this . fParameterRange = rangeCopy ( method . fParameterRange ) ; this . fParameterTypes = method . fParameterTypes ; this . fReturnType = method . fReturnType ; this . fReturnTypeRange = rangeCopy ( method . fReturnTypeRange ) ; } public String toString ( ) { if ( isConstructor ( ) ) { return "CONSTRUCTOR" ; } else { return "METHOD: " + getName ( ) ; } } public void setDefault ( String defaultValue ) { this . fDefaultValue = defaultValue ; } public String getDefault ( ) { return this . fDefaultValue ; } public String [ ] getTypeParameters ( ) { return this . fTypeParameters ; } public void setTypeParameters ( String [ ] typeParameters ) { this . fTypeParameters = typeParameters ; } } </s>