<s> package org . eclipse . jdt . internal . eval ; import java . util . Map ; import org . eclipse . jdt . core . compiler . * ; import org . eclipse . jdt . internal . compiler . ClassFile ; import org . eclipse . jdt . internal . compiler . Compiler ; import org . eclipse . jdt . internal . compiler . ICompilerRequestor ; import org . eclipse . jdt . internal . compiler . IProblemFactory ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFileReader ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFormatException ; import org . eclipse . jdt . internal . compiler . env . IBinaryType ; import org . eclipse . jdt . internal . compiler . env . INameEnvironment ; public class VariablesEvaluator extends Evaluator implements EvaluationConstants { VariablesEvaluator ( EvaluationContext context , INameEnvironment environment , Map options , IRequestor requestor , IProblemFactory problemFactory ) { super ( context , environment , options , requestor , problemFactory ) ; } protected void addEvaluationResultForCompilationProblem ( Map resultsByIDs , CategorizedProblem problem , char [ ] cuSource ) { char [ ] evaluationID = cuSource ; int evaluationType = EvaluationResult . T_INTERNAL ; int pbLine = problem . getSourceLineNumber ( ) ; int currentLine = 1 ; char [ ] packageName = getPackageName ( ) ; if ( packageName . length > 0 ) { if ( pbLine == 1 ) { evaluationID = packageName ; evaluationType = EvaluationResult . T_PACKAGE ; problem . setSourceLineNumber ( 1 ) ; problem . setSourceStart ( 0 ) ; problem . setSourceEnd ( evaluationID . length - 1 ) ; } currentLine ++ ; } char [ ] [ ] imports = this . context . imports ; if ( ( currentLine <= pbLine ) && ( pbLine < ( currentLine + imports . length ) ) ) { evaluationID = imports [ pbLine - currentLine ] ; evaluationType = EvaluationResult . T_IMPORT ; problem . setSourceLineNumber ( 1 ) ; problem . setSourceStart ( 0 ) ; problem . setSourceEnd ( evaluationID . length - 1 ) ; } currentLine += imports . length + 1 ; int varCount = this . context . variableCount ; if ( ( currentLine <= pbLine ) && ( pbLine < currentLine + varCount ) ) { GlobalVariable var = this . context . variables [ pbLine - currentLine ] ; evaluationID = var . getName ( ) ; evaluationType = EvaluationResult . T_VARIABLE ; int pbStart = problem . getSourceStart ( ) - var . declarationStart ; int pbEnd = problem . getSourceEnd ( ) - var . declarationStart ; int typeLength = var . getTypeName ( ) . length ; if ( ( 0 <= pbStart ) && ( pbEnd < typeLength ) ) { problem . setSourceLineNumber ( - 1 ) ; } else { pbStart -= typeLength + 1 ; pbEnd -= typeLength + 1 ; problem . setSourceLineNumber ( 0 ) ; } problem . setSourceStart ( pbStart ) ; problem . setSourceEnd ( pbEnd ) ; } currentLine = - 1 ; for ( int i = 0 ; i < varCount ; i ++ ) { GlobalVariable var = this . context . variables [ i ] ; char [ ] initializer = var . getInitializer ( ) ; int initializerLength = initializer == null ? 0 : initializer . length ; if ( ( var . initializerStart <= problem . getSourceStart ( ) ) && ( problem . getSourceEnd ( ) < var . initializerStart + var . name . length ) ) { return ; } else if ( ( var . initExpressionStart <= problem . getSourceStart ( ) ) && ( problem . getSourceEnd ( ) < var . initExpressionStart + initializerLength ) ) { evaluationID = var . name ; evaluationType = EvaluationResult . T_VARIABLE ; problem . setSourceLineNumber ( pbLine - var . initializerLineStart + 1 ) ; problem . setSourceStart ( problem . getSourceStart ( ) - var . initExpressionStart ) ; problem . setSourceEnd ( problem . getSourceEnd ( ) - var . initExpressionStart ) ; break ; } } EvaluationResult result = ( EvaluationResult ) resultsByIDs . get ( evaluationID ) ; if ( result == null ) { resultsByIDs . put ( evaluationID , new EvaluationResult ( evaluationID , evaluationType , new CategorizedProblem [ ] { problem } ) ) ; } else { result . addProblem ( problem ) ; } } protected char [ ] getClassName ( ) { return CharOperation . concat ( EvaluationConstants . GLOBAL_VARS_CLASS_NAME_PREFIX , Integer . toString ( EvaluationContext . VAR_CLASS_COUNTER + 1 ) . toCharArray ( ) ) ; } Compiler getCompiler ( ICompilerRequestor compilerRequestor ) { Compiler compiler = super . getCompiler ( compilerRequestor ) ; IBinaryType binaryType = this . context . getRootCodeSnippetBinary ( ) ; if ( binaryType != null ) { compiler . lookupEnvironment . cacheBinaryType ( binaryType , null ) ; } VariablesInfo installedVars = this . context . installedVars ; if ( installedVars != null ) { ClassFile [ ] classFiles = installedVars . classFiles ; for ( int i = 0 ; i < classFiles . length ; i ++ ) { ClassFile classFile = classFiles [ i ] ; IBinaryType binary = null ; try { binary = new ClassFileReader ( classFile . getBytes ( ) , null ) ; } catch ( ClassFormatException e ) { e . printStackTrace ( ) ; } compiler . lookupEnvironment . cacheBinaryType ( binary , null ) ; } } return compiler ; } protected char [ ] getPackageName ( ) { return this . context . packageName ; } protected char [ ] getSource ( ) { StringBuffer buffer = new StringBuffer ( ) ; int lineNumberOffset = 1 ; char [ ] packageName = getPackageName ( ) ; if ( packageName . length != 0 ) { buffer . append ( "package " ) ; buffer . append ( packageName ) ; buffer . append ( ';' ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; } char [ ] [ ] imports = this . context . imports ; for ( int i = 0 ; i < imports . length ; i ++ ) { buffer . append ( "import " ) ; buffer . append ( imports [ i ] ) ; buffer . append ( ';' ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; } buffer . append ( "" ) ; buffer . append ( getClassName ( ) ) ; buffer . append ( " extends " ) ; buffer . append ( PACKAGE_NAME ) ; buffer . append ( "." ) ; buffer . append ( ROOT_CLASS_NAME ) ; buffer . append ( " {" ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; GlobalVariable [ ] vars = this . context . variables ; VariablesInfo installedVars = this . context . installedVars ; for ( int i = 0 ; i < this . context . variableCount ; i ++ ) { GlobalVariable var = vars [ i ] ; buffer . append ( "" ) ; var . declarationStart = buffer . length ( ) ; buffer . append ( var . typeName ) ; buffer . append ( " " ) ; char [ ] varName = var . name ; buffer . append ( varName ) ; buffer . append ( ';' ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; } buffer . append ( "tstatic {" ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; for ( int i = 0 ; i < this . context . variableCount ; i ++ ) { GlobalVariable var = vars [ i ] ; char [ ] varName = var . name ; GlobalVariable installedVar = installedVars == null ? null : installedVars . varNamed ( varName ) ; if ( installedVar == null || ! CharOperation . equals ( installedVar . typeName , var . typeName ) ) { char [ ] initializer = var . initializer ; if ( initializer != null ) { buffer . append ( "tttry {" ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; var . initializerLineStart = lineNumberOffset ; buffer . append ( "ttt" ) ; var . initializerStart = buffer . length ( ) ; buffer . append ( varName ) ; buffer . append ( "= " ) ; var . initExpressionStart = buffer . length ( ) ; buffer . append ( initializer ) ; lineNumberOffset += numberOfCRs ( initializer ) ; buffer . append ( ';' ) . append ( this . context . lineSeparator ) ; buffer . append ( "" ) . append ( this . context . lineSeparator ) ; buffer . append ( "" ) . append ( this . context . lineSeparator ) ; buffer . append ( "tt}" ) . append ( this . context . lineSeparator ) ; lineNumberOffset += 4 ; } } else { buffer . append ( "tt" ) ; buffer . append ( varName ) ; buffer . append ( "= " ) ; char [ ] installedPackageName = installedVars . packageName ; if ( installedPackageName != null && installedPackageName . length != 0 ) { buffer . append ( installedPackageName ) ; buffer . append ( "." ) ; } buffer . append ( installedVars . className ) ; buffer . append ( "." ) ; buffer . append ( varName ) ; buffer . append ( ';' ) . append ( this . context . lineSeparator ) ; lineNumberOffset ++ ; } } buffer . append ( "t}" ) . append ( this . context . lineSeparator ) ; buffer . append ( '}' ) . append ( this . context . lineSeparator ) ; int length = buffer . length ( ) ; char [ ] result = new char [ length ] ; buffer . getChars ( 0 , length , result , 0 ) ; return result ; } private int numberOfCRs ( char [ ] source ) { int numberOfCRs = 0 ; boolean lastWasCR = false ; for ( int i = 0 ; i < source . length ; i ++ ) { char currentChar = source [ i ] ; switch ( currentChar ) { case '\r' : lastWasCR = true ; numberOfCRs ++ ; break ; case '\n' : if ( ! lastWasCR ) numberOfCRs ++ ; lastWasCR = false ; break ; default : lastWasCR = false ; } } return numberOfCRs ; } } </s>