An evaluation context supports evaluating code snippets.
A code snippet is pretty much any valid piece of Java code that could be pasted into the body of a method and compiled. However, there are two areas where the rules are slightly more liberal.
First, a code snippet can return heterogeneous types. Inside the same code snippet an int
could be returned on one line, and a String
on the next, etc. For example, the following would be considered a valid code snippet:
char c = '3'; switch (c) { case '1': return 1; case '2': return '2'; case '3': return "3"; default: return null; }
Second, if the last statement is only an expression, the return
keyword is implied. For example, the following returns false
:
int i = 1; i == 2
Global variables are an additional feature of evaluation contexts. Within an evaluation context, global variables maintain their value across evaluations. These variables are particularly useful for storing the result of an evaluation for use in subsequent evaluations.
The evaluation context remembers the name of the package in which code snippets are run. The user can set this to any package, thereby gaining access to types that are normally only visible within that package.
Finally, the evaluation context remembers a list of import declarations. The user can import any packages and types so that the code snippets may refer to types by their shorter simple names.
Example of use:
IJavaProject project = getJavaProject(); IEvaluationContext context = project.newEvaluationContext(); String codeSnippet = "int i= 0; i++"; ICodeSnippetRequestor requestor = ...; context.evaluateCodeSnippet(codeSnippet, requestor, progressMonitor);
IJavaProject.newEvaluationContext
can be used to obtain an instance.
@see IJavaProject#newEvaluationContext()
@noimplement This interface is not intended to be implemented by clients.