Skip to content

Commit

Permalink
[fixes #3406] Find jdt.core classloader if initialized in batch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 authored and rspilker committed Apr 16, 2023
1 parent 3f51b25 commit 250bc06
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,49 @@ public static final class Transform {
private static synchronized void init(ClassLoader prepend) {
if (TRANSFORM != null) return;

Main.prependClassLoader(prepend);
prependClassLoader(prepend);
if (!prepend.toString().contains("org.eclipse.jdt.core:")) {
ClassLoader jdtCoreClassLoader = findJdtCoreClassLoader(prepend);
prependClassLoader(jdtCoreClassLoader);
}
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST");
TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform");
TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped");
}

private static void prependClassLoader(ClassLoader classLoader) {
Main.prependClassLoader(classLoader);
try {
ClassLoader currentClassLoader = Transform.class.getClassLoader();

Method prependParentMethod = Permit.getMethod(currentClassLoader.getClass(), "prependParent", ClassLoader.class);
Permit.invoke(prependParentMethod, currentClassLoader, prepend);
Permit.invoke(prependParentMethod, currentClassLoader, classLoader);
} catch (Throwable t) {
// Ignore
}
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.TransformEclipseAST");
TRANSFORM = Util.findMethodAnyArgs(shadowed, "transform");
TRANSFORM_SWAPPED = Util.findMethodAnyArgs(shadowed, "transform_swapped");
}

private static ClassLoader findJdtCoreClassLoader(ClassLoader classLoader) {
try {
Method getBundleMethod = Permit.getMethod(classLoader.getClass(), "getBundle");
Object bundle = Permit.invoke(getBundleMethod, classLoader);

Method getBundleContextMethod = Permit.getMethod(bundle.getClass(), "getBundleContext");
Object bundleContext = Permit.invoke(getBundleContextMethod, bundle);

Method getBundlesMethod = Permit.getMethod(bundleContext.getClass(), "getBundles");
Object[] bundles = (Object[]) Permit.invoke(getBundlesMethod, bundleContext);

for (Object searchBundle : bundles) {
if (searchBundle.toString().startsWith("org.eclipse.jdt.core_")) {
Method getModuleClassLoaderMethod = Permit.getMethod(searchBundle.getClass(), "getModuleClassLoader", boolean.class);
return (ClassLoader) Permit.invoke(getModuleClassLoaderMethod, searchBundle, false);
}
}
} catch (Throwable t) {
// Ignore
}
return null;
}

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

0 comments on commit 250bc06

Please sign in to comment.