Skip to content

Commit 250bc06

Browse files
Rawi01rspilker
authored andcommitted
[fixes #3406] Find jdt.core classloader if initialized in batch mode
1 parent 3f51b25 commit 250bc06

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/eclipseAgent/lombok/launch/PatchFixesHider.java

+37-5
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,49 @@ public static final class Transform {
201201
private static synchronized void init(ClassLoader prepend) {
202202
if (TRANSFORM != null) return;
203203

204-
Main.prependClassLoader(prepend);
204+
prependClassLoader(prepend);
205+
if (!prepend.toString().contains("org.eclipse.jdt.core:")) {
206+
ClassLoader jdtCoreClassLoader = findJdtCoreClassLoader(prepend);
207+
prependClassLoader(jdtCoreClassLoader);
208+
}
209+
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST");
210+
TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform");
211+
TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped");
212+
}
213+
214+
private static void prependClassLoader(ClassLoader classLoader) {
215+
Main.prependClassLoader(classLoader);
205216
try {
206217
ClassLoader currentClassLoader = Transform.class.getClassLoader();
218+
207219
Method prependParentMethod = Permit.getMethod(currentClassLoader.getClass(), "prependParent", ClassLoader.class);
208-
Permit.invoke(prependParentMethod, currentClassLoader, prepend);
220+
Permit.invoke(prependParentMethod, currentClassLoader, classLoader);
209221
} catch (Throwable t) {
210222
// Ignore
211223
}
212-
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST");
213-
TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform");
214-
TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped");
224+
}
225+
226+
private static ClassLoader findJdtCoreClassLoader(ClassLoader classLoader) {
227+
try {
228+
Method getBundleMethod = Permit.getMethod(classLoader.getClass(), "getBundle");
229+
Object bundle = Permit.invoke(getBundleMethod, classLoader);
230+
231+
Method getBundleContextMethod = Permit.getMethod(bundle.getClass(), "getBundleContext");
232+
Object bundleContext = Permit.invoke(getBundleContextMethod, bundle);
233+
234+
Method getBundlesMethod = Permit.getMethod(bundleContext.getClass(), "getBundles");
235+
Object[] bundles = (Object[]) Permit.invoke(getBundlesMethod, bundleContext);
236+
237+
for (Object searchBundle : bundles) {
238+
if (searchBundle.toString().startsWith("org.eclipse.jdt.core_")) {
239+
Method getModuleClassLoaderMethod = Permit.getMethod(searchBundle.getClass(), "getModuleClassLoader", boolean.class);
240+
return (ClassLoader) Permit.invoke(getModuleClassLoaderMethod, searchBundle, false);
241+
}
242+
}
243+
} catch (Throwable t) {
244+
// Ignore
245+
}
246+
return null;
215247
}
216248

217249
public static void transform(Object parser, Object ast) throws IOException {

0 commit comments

Comments
 (0)