Skip to content

Add Adapters.convert() for multi-hop adapter chain resolution#124

Draft
Copilot wants to merge 6 commits into
masterfrom
copilot/add-adapters-convert-method
Draft

Add Adapters.convert() for multi-hop adapter chain resolution#124
Copilot wants to merge 6 commits into
masterfrom
copilot/add-adapters-convert-method

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 2, 2025

Adapters currently only supports direct type adaptation (X → Y). When an indirect path exists (X → Z → Y), users must manually chain adaptations. This PR adds Adapters.convert() to automatically discover and execute adapter chains.

Implementation

Core method:

public static <T> T convert(Object sourceObject, Class<T> adapter)
  • BFS from source type to target, finding shortest conversion paths
  • Tries direct adaptation first as shortcut
  • Executes found paths sequentially until one succeeds
  • Depth-limited to 10 hops to prevent excessive computation

Path finding:

  • Uses IAdapterManager.computeAdapterTypes() to explore adapter graph
  • Returns intermediate type names only (source and target excluded)
  • Handles multiple paths at same depth, tries each until success

Example usage:

// Given factories: A→B, B→C, C→D
TypeA source = new TypeA();
TypeD result = Adapters.convert(source, TypeD.class);  // Finds and executes A→B→C→D

Changes

  • Adapters.java: Added convert(), findConversionPaths(), tryConversionPath() with BFS implementation
  • AdaptersTest.java: Tests for direct adaptation, single-hop, multi-hop, null handling, and no-path cases
  • Logging for ClassNotFoundException during path discovery and execution

Design decisions

  • Forward BFS over backward search: more intuitive, simpler implementation
  • MAX_CONVERSION_DEPTH = 10: prevents pathological cases while allowing reasonable chains
  • Returns first successful path rather than trying all: favors shortest paths, fails fast

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • repo.eclipse.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/equinox/equinox org.codehaus.plexus.classworlds.launcher.Launcher clean install -DskipTests -pl bundles/org.eclipse.equinox.common -am (dns block)
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/equinox/equinox org.codehaus.plexus.classworlds.launcher.Launcher clean test -Pbuild-individual-bundles (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Create a new Adapters.convert method</issue_title>
<issue_description>We already have Adapters.of adapting an object X > Y when there is a adapterfactory that can Map X to Y or any super types.

In some cases if one wants to get an Y and there is no direct mapping but for example one can convert X > Z and the Z > Y what can be a daunting task.

We want to help the user by creating a new Adapters.convert method that performs the task:

  • it first try if there is an adaption of X > Y possible (shortcut)
  • if not we need to find a path through the relation of other objects starting at possible factories for Y then look what can convert from I_1 > Y then from I_2 > I1 and so on until we get X > I_n > ... I_1 > Y and if we found such chain try the full adoption and if it is not null return it, otherwise try possible other chains.
  • We can assume that shorter is better here so as less as possible conversions needed.
  • We should not try to build the whole graph but simply go on iterative / recursive or using a backtracking approach</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits November 2, 2025 17:13
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
… add logging

Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new Adapters.convert method for flexible conversion Add Adapters.convert() for multi-hop adapter chain resolution Nov 2, 2025
Copilot AI requested a review from laeubi November 2, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a new Adapters.convert method

2 participants