Skip to content

Commit 8dd15bd

Browse files
committed
Try to mitigate evaluation problems with old project settings
If there are some projects lurking in the workspace that somehow use unsupported Java compiler versions, evaluation could find the type from such a project and try to use the project options for snippet evaluation. Let simply disregard such option values and use first supported Java version for evaluation. Fixes #668
1 parent 91f9022 commit 8dd15bd

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/EvaluationSourceGenerator.java

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
*******************************************************************************/
1515
package org.eclipse.jdt.internal.debug.eval.ast.engine;
1616

17+
import static org.eclipse.jdt.core.JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM;
18+
import static org.eclipse.jdt.core.JavaCore.COMPILER_COMPLIANCE;
19+
import static org.eclipse.jdt.core.JavaCore.COMPILER_RELEASE;
20+
import static org.eclipse.jdt.core.JavaCore.COMPILER_SOURCE;
21+
import static org.eclipse.jdt.core.JavaCore.DISABLED;
22+
23+
import java.util.HashMap;
1724
import java.util.Map;
25+
import java.util.SortedSet;
1826

1927
import org.eclipse.core.runtime.CoreException;
2028
import org.eclipse.core.runtime.IStatus;
@@ -183,7 +191,9 @@ private boolean needsReturn(String codeSnippet){
183191
if ( codeSnippet.length() == 0) {
184192
return false;
185193
}
186-
IScanner scanner = ToolFactory.createScanner(false, false, false, fJavaProject.getOption(JavaCore.COMPILER_SOURCE, true), fJavaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true), true);
194+
String sourceLevel = toSupportedVersion(fJavaProject.getOption(COMPILER_SOURCE, true));
195+
String complianceLevel = toSupportedVersion(fJavaProject.getOption(COMPILER_COMPLIANCE, true));
196+
IScanner scanner = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel, true);
187197
scanner.setSource(codeSnippet.toCharArray());
188198
int token;
189199
try {
@@ -314,7 +324,7 @@ private void createEvaluationSourceFromSource(String source, IType type,
314324
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
315325
parser.setSource(source.toCharArray());
316326
Map<String, String> options = getCompilerOptions(project);
317-
String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
327+
String sourceLevel = toSupportedVersion(project.getOption(COMPILER_SOURCE, true));
318328
parser.setCompilerOptions(options);
319329
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
320330
SourceBasedSourceGenerator visitor = new SourceBasedSourceGenerator(
@@ -342,25 +352,76 @@ private void createEvaluationSourceFromSource(String source, IType type,
342352
/**
343353
* Returns the compiler options used for compiling the expression.
344354
* <p>
345-
* Turns all errors and warnings into ignore and disables task tags. The customizable set of compiler options only contains additional Eclipse
346-
* options. The standard JDK compiler options can't be changed anyway.
355+
* Turns all errors and warnings into ignore and disables task tags.
356+
* The customizable set of compiler options only contains additional Eclipse
357+
* options. The standard JDK compiler options will be changed only if they
358+
* are not supported anymore but required by compiler.
347359
*
348360
* @param project
349361
* the IJavaProject
350362
* @return compiler options
351363
*/
352364
public static Map<String, String> getCompilerOptions(IJavaProject project) {
353-
Map<String, String> options = project.getOptions(true);
365+
Map<String, String> options = new HashMap<>(project.getOptions(true));
354366
for (String key : options.keySet()) {
355367
String value = options.get(key);
356368
if (JavaCore.ERROR.equals(value) || JavaCore.WARNING.equals(value) || JavaCore.INFO.equals(value)) {
357369
options.put(key, JavaCore.IGNORE);
358370
}
359371
}
360372
options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
373+
374+
toSupportedVersion(options, COMPILER_COMPLIANCE);
375+
toSupportedVersion(options, COMPILER_CODEGEN_TARGET_PLATFORM);
376+
toSupportedVersion(options, COMPILER_SOURCE);
377+
toSupportedVersion(options, COMPILER_RELEASE);
361378
return options;
362379
}
363380

381+
/**
382+
* Updates given options for a given key
383+
*
384+
* @param value
385+
* one of the four "JLS versions" constants
386+
* <ul>
387+
* <li>{@link JavaCore#COMPILER_COMPLIANCE}
388+
* <li>{@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}
389+
* <li>{@link JavaCore#COMPILER_SOURCE}
390+
* <li>{@link JavaCore#COMPILER_RELEASE}
391+
* </ul>
392+
* @return given value if supported by compiler or first compiler supported value
393+
* @see JavaCore#getAllJavaSourceVersionsSupportedByCompiler()
394+
*/
395+
private static String toSupportedVersion(Map<String, String> options, String key) {
396+
SortedSet<String> supported = JavaCore.getAllJavaSourceVersionsSupportedByCompiler();
397+
String value = options.get(key);
398+
if (value != null && !supported.contains(value) && !DISABLED.equals(value)) {
399+
value = supported.first();
400+
options.put(key, value);
401+
}
402+
return value;
403+
}
404+
405+
/**
406+
* @param value
407+
* one of the four "JLS versions" constants
408+
* <ul>
409+
* <li>{@link JavaCore#COMPILER_COMPLIANCE}
410+
* <li>{@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}
411+
* <li>{@link JavaCore#COMPILER_SOURCE}
412+
* <li>{@link JavaCore#COMPILER_RELEASE}
413+
* </ul>
414+
* @return given value if supported by compiler or first compiler supported value
415+
* @see JavaCore#getAllJavaSourceVersionsSupportedByCompiler()
416+
*/
417+
private static String toSupportedVersion(String value) {
418+
SortedSet<String> supported = JavaCore.getAllJavaSourceVersionsSupportedByCompiler();
419+
if (value != null && !supported.contains(value) && !DISABLED.equals(value)) {
420+
value = supported.first();
421+
}
422+
return value;
423+
}
424+
364425
private void createEvaluationSourceFromJDIObject(
365426
BinaryBasedSourceGenerator objectToEvaluationSourceMapper) {
366427

@@ -379,7 +440,7 @@ private void createEvaluationSourceFromJDIObject(
379440
private BinaryBasedSourceGenerator getInstanceSourceMapper(
380441
JDIReferenceType referenceType, boolean isInStaticMethod,
381442
IJavaProject project) {
382-
String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
443+
String sourceLevel = toSupportedVersion(project.getOption(COMPILER_SOURCE, true));
383444
BinaryBasedSourceGenerator objectToEvaluationSourceMapper = new BinaryBasedSourceGenerator(
384445
fLocalVariableTypeNames, fLocalVariableNames, isInStaticMethod,
385446
sourceLevel);

0 commit comments

Comments
 (0)