Skip to content

Commit 2c90feb

Browse files
committed
Use Plugin ClassLoader for fallback
1 parent cdd08ce commit 2c90feb

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

src/main/java/io/github/jeddict/ai/hints/AssistantChatManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2025 the original author or authors from the Jeddict project (https://jeddict.github.io/).
2+
* Copyright 2025-26 the original author or authors from the Jeddict project (https://jeddict.github.io/).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -61,6 +61,7 @@
6161
import static io.github.jeddict.ai.review.ReviewUtil.parseReviewsFromYaml;
6262
import io.github.jeddict.ai.scanner.ProjectMetadataInfo;
6363
import io.github.jeddict.ai.settings.PreferencesManager;
64+
import io.github.jeddict.ai.util.ClassLoaderUtil;
6465
import io.github.jeddict.ai.util.ColorUtil;
6566
import static io.github.jeddict.ai.util.ContextHelper.getFilesContextList;
6667
import static io.github.jeddict.ai.util.ContextHelper.getImageFilesContext;
@@ -477,13 +478,9 @@ public void displayHtmlContent(String filename, String title) {
477478
}));
478479
}
479480

480-
private void usePluginClassLoader() {
481-
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
482-
}
483-
484481
public void openChat(String type, final String query, String fileName, String title, Consumer<String> action) {
485482
SwingUtilities.invokeLater(() -> {
486-
usePluginClassLoader();
483+
ClassLoaderUtil.usePluginClassLoaderIfNeeded(getClass());
487484
new JeddictUpdateManager().checkForJeddictUpdate();
488485
ac = createChatInstance(title, type, getProject());
489486
ac.setLayout(new BorderLayout());

src/main/java/io/github/jeddict/ai/lang/JeddictBrain.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2025 the original author or authors from the Jeddict project (https://jeddict.github.io/).
2+
* Copyright 2025-26 the original author or authors from the Jeddict project (https://jeddict.github.io/).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -49,6 +49,7 @@
4949
import io.github.jeddict.ai.agent.ToolsProbingTool;
5050
import io.github.jeddict.ai.agent.pair.PairProgrammer;
5151
import static io.github.jeddict.ai.lang.InteractionMode.INTERACTIVE;
52+
import io.github.jeddict.ai.util.ClassLoaderUtil;
5253
import io.github.jeddict.ai.util.PropertyChangeEmitter;
5354
import java.lang.reflect.InvocationTargetException;
5455
import org.openide.DialogDisplayer;
@@ -309,18 +310,23 @@ protected boolean probeToolSupport() {
309310
);
310311

311312
return toolsSupport;
312-
} catch (final NoClassDefFoundError e) {
313-
LOG.severe(() ->
314-
"error probing tool support (conflicting classpath), returning false %s\n%s".formatted(
315-
e.toString(),
316-
Arrays.toString(e.getStackTrace())
317-
)
313+
} catch (final NoClassDefFoundError | java.util.ServiceConfigurationError e) {
314+
315+
LOG.severe(()
316+
-> "Error probing tool support (likely classloader conflict), returning false %s\n%s".formatted(
317+
e.toString(),
318+
Arrays.toString(e.getStackTrace())
319+
)
318320
);
321+
322+
ClassLoaderUtil.enablePluginClassLoaderFallback();
323+
319324
final NotifyDescriptor nd = new NotifyDescriptor.Message(
320-
"AI tool support probe failed due to a conflicting classpath issue: " + e.toString(),
321-
NotifyDescriptor.ERROR_MESSAGE
325+
"AI tool support probe failed due to a classpath conflict: " + e.toString(),
326+
NotifyDescriptor.ERROR_MESSAGE
322327
);
323328
DialogDisplayer.getDefault().notify(nd);
329+
324330
} catch (final Throwable t) {
325331
LOG.severe(() ->
326332
"error probing tool support, returning false %s\n%s".formatted(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2026 the original author or authors from the Jeddict project (https://jeddict.github.io/).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package io.github.jeddict.ai.util;
17+
18+
/**
19+
*
20+
* @author Gaurav Gupta
21+
*/
22+
public final class ClassLoaderUtil {
23+
24+
private static volatile boolean USE_PLUGIN_CLASSLOADER_FALLBACK = false;
25+
26+
private ClassLoaderUtil() {
27+
}
28+
29+
public static void usePluginClassLoaderIfNeeded(Class<?> clazz) {
30+
if (USE_PLUGIN_CLASSLOADER_FALLBACK) {
31+
Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
32+
}
33+
}
34+
35+
public static void enablePluginClassLoaderFallback() {
36+
USE_PLUGIN_CLASSLOADER_FALLBACK = true;
37+
}
38+
}

0 commit comments

Comments
 (0)