From 1da1ea63d024a63752f510efd321ec6f5b43f586 Mon Sep 17 00:00:00 2001
From: Jinbo Wang <jinbwan@microsoft.com>
Date: Wed, 28 Aug 2024 16:42:08 +0800
Subject: [PATCH] Enable lombok annotation processing when parsing DOM for
 workingcopy

---
 .../dom/JavacCompilationUnitResolver.java     | 46 ++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java
index e00b7b7e4f7..cf57b047bc9 100644
--- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java
+++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java
@@ -33,6 +33,7 @@
 import javax.tools.DiagnosticListener;
 import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
+import javax.tools.StandardLocation;
 import javax.tools.ToolProvider;
 
 import org.eclipse.core.resources.IResource;
@@ -595,7 +596,7 @@ public Void visitClass(ClassTree node, Void p) {
 
 
 		JCCompilationUnit javacCompilationUnit = null;
-		Iterable<String> options = Arrays.asList("-proc:none"); // disable annotation processing in the parser.
+		Iterable<String> options = configureAPIfNecessary(fileManager) ? null : Arrays.asList("-proc:none");
 		JavacTask task = ((JavacTool)compiler).getTask(null, fileManager, null /* already added to context */, options, List.of() /* already set */, fileObjects, context);
 		{
 			// don't know yet a better way to ensure those necessary flags get configured
@@ -923,4 +924,47 @@ private static Function<String, IBinding> javacAdditionalBindingCreator(Map<Stri
 		};
 	}
 
+	private boolean configureAPIfNecessary(JavacFileManager fileManager) {
+		Iterable<? extends File> apPaths = fileManager.getLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH);
+		if (apPaths != null) {
+			return true;
+		}
+
+		Iterable<? extends File> apModulePaths = fileManager.getLocation(StandardLocation.ANNOTATION_PROCESSOR_MODULE_PATH);
+		if (apModulePaths != null) {
+			return true;
+		}
+
+		Iterable<? extends File> classPaths = fileManager.getLocation(StandardLocation.CLASS_PATH);
+		if (classPaths != null) {
+			for(File cp : classPaths) {
+				String fileName = cp.getName();
+				if (fileName != null && fileName.startsWith("lombok") && fileName.endsWith(".jar")) {
+					try {
+						fileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, List.of(cp));
+						return true;
+					} catch (IOException ex) {
+						ILog.get().error(ex.getMessage(), ex);
+					}
+				}
+			}
+		}
+
+		Iterable<? extends File> modulePaths = fileManager.getLocation(StandardLocation.MODULE_PATH);
+		if (modulePaths != null) {
+			for(File mp : modulePaths) {
+				String fileName = mp.getName();
+				if (fileName != null && fileName.startsWith("lombok") && fileName.endsWith(".jar")) {
+					try {
+						fileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_MODULE_PATH, List.of(mp));
+						return true;
+					} catch (IOException ex) {
+						ILog.get().error(ex.getMessage(), ex);
+					}
+				}
+			}
+		}
+
+		return false;
+	}
 }