<s> package org . eclipse . jdt . core ; import java . util . Hashtable ; import java . util . Map ; import org . eclipse . core . resources . * ; import org . eclipse . jdt . core . compiler . * ; import org . eclipse . jdt . internal . compiler . impl . CompilerOptions ; import org . eclipse . jdt . internal . compiler . parser . * ; import org . eclipse . jdt . internal . compiler . problem . ProblemReporter ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . Util ; public class CorrectionEngine { protected int correctionStart ; protected int correctionEnd ; protected int prefixLength ; protected ICompilationUnit compilationUnit ; protected ICorrectionRequestor correctionRequestor ; protected static final int CLASSES = 0x00000001 ; protected static final int INTERFACES = 0x00000002 ; protected static final int IMPORT = 0x00000004 ; protected static final int METHOD = 0x00000008 ; protected static final int FIELD = 0x00000010 ; protected static final int LOCAL = 0x00000020 ; protected int filter ; public CorrectionEngine ( Map setting ) { } public void computeCorrections ( IMarker marker , ICompilationUnit targetUnit , int positionOffset , ICorrectionRequestor requestor ) throws JavaModelException { IJavaElement element = targetUnit == null ? JavaCore . create ( marker . getResource ( ) ) : targetUnit ; if ( ! ( element instanceof ICompilationUnit ) ) return ; ICompilationUnit unit = ( ICompilationUnit ) element ; int id = marker . getAttribute ( IJavaModelMarker . ID , - 1 ) ; String [ ] args = Util . getProblemArgumentsFromMarker ( marker . getAttribute ( IJavaModelMarker . ARGUMENTS , "" ) ) ; int start = marker . getAttribute ( IMarker . CHAR_START , - 1 ) ; int end = marker . getAttribute ( IMarker . CHAR_END , - 1 ) ; computeCorrections ( unit , id , start + positionOffset , end + positionOffset , args , requestor ) ; } public void computeCorrections ( IProblem problem , ICompilationUnit targetUnit , ICorrectionRequestor requestor ) throws JavaModelException { if ( requestor == null ) { throw new IllegalArgumentException ( Messages . correction_nullUnit ) ; } this . computeCorrections ( targetUnit , problem . getID ( ) , problem . getSourceStart ( ) , problem . getSourceEnd ( ) , problem . getArguments ( ) , requestor ) ; } private void computeCorrections ( ICompilationUnit unit , int id , int start , int end , String [ ] arguments , ICorrectionRequestor requestor ) { if ( id == - 1 || arguments == null || start == - 1 || end == - 1 ) return ; if ( requestor == null ) { throw new IllegalArgumentException ( Messages . correction_nullRequestor ) ; } this . correctionRequestor = requestor ; this . correctionStart = start ; this . correctionEnd = end ; this . compilationUnit = unit ; String argument = null ; try { switch ( id ) { case IProblem . ImportNotFound : this . filter = IMPORT ; argument = arguments [ 0 ] ; break ; case IProblem . UndefinedType : this . filter = CLASSES | INTERFACES ; argument = arguments [ 0 ] ; break ; case IProblem . UndefinedMethod : this . filter = METHOD ; argument = arguments [ 1 ] ; break ; case IProblem . UndefinedField : this . filter = FIELD ; argument = arguments [ 0 ] ; break ; case IProblem . UndefinedName : case IProblem . UnresolvedVariable : this . filter = FIELD | LOCAL ; argument = arguments [ 0 ] ; break ; } } catch ( ArrayIndexOutOfBoundsException e ) { return ; } if ( argument != null ) { correct ( argument . toCharArray ( ) ) ; } } private void correct ( char [ ] argument ) { try { String source = this . compilationUnit . getSource ( ) ; Map currentProjectOptions = this . compilationUnit . getJavaProject ( ) . getOptions ( true ) ; long sourceLevel = CompilerOptions . versionToJdkLevel ( currentProjectOptions . get ( JavaCore . COMPILER_SOURCE ) ) ; long complianceLevel = CompilerOptions . versionToJdkLevel ( currentProjectOptions . get ( JavaCore . COMPILER_COMPLIANCE ) ) ; Scanner scanner = new Scanner ( false , false , false , sourceLevel , complianceLevel , null , null , true ) ; scanner . setSource ( source . toCharArray ( ) ) ; scanner . resetTo ( this . correctionStart , this . correctionEnd ) ; int token = 0 ; char [ ] argumentSource = CharOperation . NO_CHAR ; while ( true ) { token = scanner . getNextToken ( ) ; if ( token == TerminalTokens . TokenNameEOF ) return ; char [ ] tokenSource = scanner . getCurrentTokenSource ( ) ; argumentSource = CharOperation . concat ( argumentSource , tokenSource ) ; if ( ! CharOperation . prefixEquals ( argumentSource , argument ) ) return ; if ( CharOperation . equals ( argument , argumentSource ) ) { this . correctionStart = scanner . startPosition ; this . correctionEnd = scanner . currentPosition ; this . prefixLength = CharOperation . lastIndexOf ( '.' , argument ) + 1 ; break ; } } int completionPosition = this . correctionStart ; scanner . resetTo ( completionPosition , this . correctionEnd ) ; int position = completionPosition ; for ( int i = 0 ; i < 4 ; i ++ ) { if ( scanner . getNextCharAsJavaIdentifierPart ( ) ) { completionPosition = position ; position = scanner . currentPosition ; } else { break ; } } Hashtable oldOptions = JavaCore . getOptions ( ) ; try { Hashtable options = new Hashtable ( oldOptions ) ; options . put ( JavaCore . CODEASSIST_CAMEL_CASE_MATCH , JavaCore . DISABLED ) ; JavaCore . setOptions ( options ) ; this . compilationUnit . codeComplete ( completionPosition , this . completionRequestor ) ; } finally { JavaCore . setOptions ( oldOptions ) ; } } catch ( JavaModelException e ) { return ; } catch ( InvalidInputException e ) { return ; } } protected CompletionRequestor completionRequestor = new CompletionRequestor ( ) { public void accept ( CompletionProposal proposal ) { switch ( proposal . getKind ( ) ) { case CompletionProposal . TYPE_REF : int flags = proposal . getFlags ( ) ; if ( ! ( Flags . isEnum ( flags ) || Flags . isAnnotation ( flags ) ) ) { if ( ( CorrectionEngine . this . filter & ( CLASSES | INTERFACES ) ) != 0 ) { char [ ] completionName = proposal . getCompletion ( ) ; CorrectionEngine . this . correctionRequestor . acceptClass ( proposal . getDeclarationSignature ( ) , Signature . getSignatureSimpleName ( proposal . getSignature ( ) ) , CharOperation . subarray ( completionName , CorrectionEngine . this . prefixLength , completionName . length ) , proposal . getFlags ( ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } else if ( ( CorrectionEngine . this . filter & IMPORT ) != 0 ) { char [ ] packageName = proposal . getDeclarationSignature ( ) ; char [ ] className = Signature . getSignatureSimpleName ( proposal . getSignature ( ) ) ; char [ ] fullName = CharOperation . concat ( packageName , className , '.' ) ; CorrectionEngine . this . correctionRequestor . acceptClass ( packageName , className , CharOperation . subarray ( fullName , CorrectionEngine . this . prefixLength , fullName . length ) , proposal . getFlags ( ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } } break ; case CompletionProposal . FIELD_REF : if ( ( CorrectionEngine . this . filter & FIELD ) != 0 ) { char [ ] declaringSignature = proposal . getDeclarationSignature ( ) ; char [ ] signature = proposal . getSignature ( ) ; CorrectionEngine . this . correctionRequestor . acceptField ( Signature . getSignatureQualifier ( declaringSignature ) , Signature . getSignatureSimpleName ( declaringSignature ) , proposal . getName ( ) , Signature . getSignatureQualifier ( signature ) , Signature . getSignatureSimpleName ( signature ) , proposal . getName ( ) , proposal . getFlags ( ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } break ; case CompletionProposal . LOCAL_VARIABLE_REF : if ( ( CorrectionEngine . this . filter & LOCAL ) != 0 ) { char [ ] signature = proposal . getSignature ( ) ; CorrectionEngine . this . correctionRequestor . acceptLocalVariable ( proposal . getName ( ) , Signature . getSignatureQualifier ( signature ) , Signature . getSignatureSimpleName ( signature ) , proposal . getFlags ( ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } break ; case CompletionProposal . METHOD_REF : if ( ( CorrectionEngine . this . filter & METHOD ) != 0 ) { char [ ] declaringSignature = proposal . getDeclarationSignature ( ) ; char [ ] signature = proposal . getSignature ( ) ; char [ ] [ ] parameterTypeSignatures = Signature . getParameterTypes ( signature ) ; int length = parameterTypeSignatures . length ; char [ ] [ ] parameterPackageNames = new char [ length ] [ ] ; char [ ] [ ] parameterTypeNames = new char [ length ] [ ] ; for ( int i = 0 ; i < length ; i ++ ) { parameterPackageNames [ i ] = Signature . getSignatureQualifier ( parameterTypeSignatures [ i ] ) ; parameterTypeNames [ i ] = Signature . getSignatureSimpleName ( parameterTypeSignatures [ i ] ) ; } char [ ] returnTypeSignature = Signature . getReturnType ( signature ) ; CorrectionEngine . this . correctionRequestor . acceptMethod ( Signature . getSignatureQualifier ( declaringSignature ) , Signature . getSignatureSimpleName ( declaringSignature ) , proposal . getName ( ) , parameterPackageNames , parameterTypeNames , proposal . findParameterNames ( null ) , Signature . getSignatureQualifier ( returnTypeSignature ) , Signature . getSignatureSimpleName ( returnTypeSignature ) , proposal . getName ( ) , proposal . getFlags ( ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } break ; case CompletionProposal . PACKAGE_REF : if ( ( CorrectionEngine . this . filter & ( CLASSES | INTERFACES | IMPORT ) ) != 0 ) { char [ ] packageName = proposal . getDeclarationSignature ( ) ; CorrectionEngine . this . correctionRequestor . acceptPackage ( packageName , CharOperation . subarray ( packageName , CorrectionEngine . this . prefixLength , packageName . length ) , CorrectionEngine . this . correctionStart , CorrectionEngine . this . correctionEnd ) ; } break ; } } } ; public static String [ ] getAllWarningTokens ( ) { return CompilerOptions . warningTokens ; } public static String [ ] getProblemArguments ( IMarker problemMarker ) { String argumentsString = problemMarker . getAttribute ( IJavaModelMarker . ARGUMENTS , null ) ; return Util . getProblemArgumentsFromMarker ( argumentsString ) ; } public static String getWarningToken ( int problemID ) { int irritant = ProblemReporter . getIrritant ( problemID ) ; if ( irritant != 0 ) { return CompilerOptions . warningTokenFromIrritant ( irritant ) ; } return null ; } } </s>