From b2ac1e4f9835ff055b0af84a49ec88d92aba1c44 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Wed, 10 Apr 2024 12:18:53 +0200 Subject: [PATCH] ASTParser should use CompilationUnit options. Instead of parent project one, as the CompilationUnit may override some options. --- .../dom/org/eclipse/jdt/core/dom/ASTParser.java | 7 ++++++- .../eclipse/jdt/internal/core/CompilationUnit.java | 12 +++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java index 346bcf29457..0a247a25dd1 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java @@ -602,7 +602,9 @@ public void setSource(char[] source) { * *

This method automatically sets the project (and compiler * options) based on the given compilation unit, in a manner - * equivalent to {@link #setProject(IJavaProject) setProject(source.getJavaProject())}.

+ * equivalent to {@link #setProject(IJavaProject) setProject(source.getJavaProject())} + * and the custom compiler options supported by the compilation unit through + * {@link ICompilationUnit#getCustomOptions() getCustomOptions()}.

* *

This source is not used when the AST is built using * {@link #createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor)}.

@@ -612,6 +614,9 @@ public void setSource(char[] source) { */ public void setSource(ICompilationUnit source) { setSource((ITypeRoot)source); + if (source != null) { + setCompilerOptions(source.getOptions(true)); + } } /** diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java index 3598751a242..794bf5c08d5 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java @@ -1453,11 +1453,13 @@ public void setOptions(Map newOptions) { @Override public Map getCustomOptions() { - try { - Map customOptions = this.getCompilationUnitElementInfo().getCustomOptions(); - return customOptions == null ? Collections.emptyMap() : customOptions; - } catch (JavaModelException e) { - // do nothing + if (this.owner != null) { + try { + Map customOptions = this.getCompilationUnitElementInfo().getCustomOptions(); + return customOptions == null ? Collections.emptyMap() : customOptions; + } catch (JavaModelException e) { + // do nothing + } } return Collections.emptyMap();