Skip to content

Commit d5e6c8c

Browse files
committed
Unify compiler options, set level to Java 7
Does not guarantee support for all Java 7 features (only some), but warns about lambdas not being available. Lambdas are not supported, because they make ANTLR angry. Fixes processing#4034
1 parent 054d601 commit d5e6c8c

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

java/src/processing/mode/java/pdex/ASTGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ protected DefaultMutableTreeNode buildAST(String source, CompilationUnit cu) {
216216

217217
Map<String, String> options = JavaCore.getOptions();
218218

219-
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
220-
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
219+
JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
221220
parser.setCompilerOptions(options);
222221
compilationUnit = (CompilationUnit) parser.createAST(null);
223222
} else {

java/src/processing/mode/java/pdex/ErrorCheckerService.java

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,7 @@ protected CodeCheckResult checkCode() {
387387

388388
parser.setSource(sourceCodeArray);
389389
parser.setKind(ASTParser.K_COMPILATION_UNIT);
390-
391-
Map<String, String> options = JavaCore.getOptions();
392-
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
393-
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
394-
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
395-
parser.setCompilerOptions(options);
390+
parser.setCompilerOptions(COMPILER_OPTIONS);
396391
parser.setStatementsRecovery(true);
397392

398393
result.compilationUnit = (CompilationUnit) parser.createAST(null);
@@ -419,12 +414,7 @@ protected CodeCheckResult checkCode() {
419414

420415
parser.setSource(sourceCodeArray);
421416
parser.setKind(ASTParser.K_COMPILATION_UNIT);
422-
423-
Map<String, String> options = JavaCore.getOptions();
424-
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
425-
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
426-
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
427-
parser.setCompilerOptions(options);
417+
parser.setCompilerOptions(COMPILER_OPTIONS);
428418
parser.setStatementsRecovery(true);
429419

430420
result.compilationUnit = (CompilationUnit) parser.createAST(null);
@@ -453,13 +443,8 @@ protected CodeCheckResult checkCode() {
453443
loadCompClass = false;
454444
}
455445

456-
457-
if (compilerSettings == null) {
458-
prepareCompilerSetting();
459-
}
460-
461446
problems = compileAndReturnProblems(className, sourceCode,
462-
compilerSettings, classLoader);
447+
COMPILER_OPTIONS, classLoader);
463448
} catch (Exception e) {
464449
System.err.println("compileCheck() problem." + e);
465450
e.printStackTrace();
@@ -815,29 +800,32 @@ protected boolean ignorableSuggestionImport(String impName) {
815800
return true;
816801
}
817802

803+
static final Map<String, String> COMPILER_OPTIONS;
804+
static {
805+
Map<String, String> compilerOptions = new HashMap<>();
806+
807+
JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, compilerOptions);
808+
809+
final String[] generate = {
810+
JavaCore.COMPILER_LINE_NUMBER_ATTR,
811+
JavaCore.COMPILER_SOURCE_FILE_ATTR
812+
};
813+
814+
final String[] ignore = {
815+
JavaCore.COMPILER_PB_UNUSED_IMPORT,
816+
JavaCore.COMPILER_PB_MISSING_SERIAL_VERSION,
817+
JavaCore.COMPILER_PB_RAW_TYPE_REFERENCE,
818+
JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION
819+
};
820+
821+
final String[] warn = {
822+
};
823+
824+
for (String s : generate) compilerOptions.put(s, JavaCore.GENERATE);
825+
for (String s : ignore) compilerOptions.put(s, JavaCore.IGNORE);
826+
for (String s : warn) compilerOptions.put(s, JavaCore.WARNING);
818827

819-
/** Options for the JDT Compiler */
820-
protected Map<String, String> compilerSettings;
821-
822-
823-
/** Set compiler options for JDT Compiler */
824-
protected void prepareCompilerSetting() {
825-
compilerSettings = new HashMap<>();
826-
827-
compilerSettings.put(CompilerOptions.OPTION_LineNumberAttribute,
828-
CompilerOptions.GENERATE);
829-
compilerSettings.put(CompilerOptions.OPTION_SourceFileAttribute,
830-
CompilerOptions.GENERATE);
831-
compilerSettings.put(CompilerOptions.OPTION_Source,
832-
CompilerOptions.VERSION_1_8);
833-
compilerSettings.put(CompilerOptions.OPTION_ReportUnusedImport,
834-
CompilerOptions.IGNORE);
835-
compilerSettings.put(CompilerOptions.OPTION_ReportMissingSerialVersion,
836-
CompilerOptions.IGNORE);
837-
compilerSettings.put(CompilerOptions.OPTION_ReportRawTypeReference,
838-
CompilerOptions.IGNORE);
839-
compilerSettings.put(CompilerOptions.OPTION_ReportUncheckedTypeOperation,
840-
CompilerOptions.IGNORE);
828+
COMPILER_OPTIONS = Collections.unmodifiableMap(compilerOptions);
841829
}
842830

843831

java/src/processing/mode/java/pdex/XQPreprocessor.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
package processing.mode.java.pdex;
2222

2323
import java.util.List;
24-
import java.util.Map;
2524

26-
import org.eclipse.jdt.core.JavaCore;
2725
import org.eclipse.jdt.core.dom.AST;
2826
import org.eclipse.jdt.core.dom.ASTParser;
2927
import org.eclipse.jdt.core.dom.ASTVisitor;
@@ -76,12 +74,7 @@ protected String handle(String source,
7674
parser.setSource(doc.get().toCharArray());
7775
parser.setKind(ASTParser.K_COMPILATION_UNIT);
7876

79-
@SuppressWarnings("unchecked")
80-
Map<String, String> options = JavaCore.getOptions();
81-
82-
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
83-
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
84-
parser.setCompilerOptions(options);
77+
parser.setCompilerOptions(ErrorCheckerService.COMPILER_OPTIONS);
8578
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
8679
cu.recordModifications();
8780
rewrite = ASTRewrite.create(cu.getAST());
@@ -90,9 +83,7 @@ protected String handle(String source,
9083
TextEdit edits = cu.rewrite(doc, null);
9184
try {
9285
edits.apply(doc);
93-
} catch (MalformedTreeException e) {
94-
e.printStackTrace();
95-
} catch (BadLocationException e) {
86+
} catch (MalformedTreeException | BadLocationException e) {
9687
e.printStackTrace();
9788
}
9889
return doc.get();

0 commit comments

Comments
 (0)