Question

After some google search, I feel still in the mud.

I am wondering whether what is the best approach for writing wrappers mapping two similar APIs that offer similar semantics but perhaps in different models, syntax or styles.

For example: map Cairo (a graphics library) into Java2D (another graphics library, with a fairly similar semantics model).

PS: Some brainstorming about what it would be nice to have in an integrated tool supporting such a project to map library A into library B.

  • Some formal semantics engine (e.g. K-framework) to define domain semantics (A) and image semantics (B)
  • Some rules-based, configurable parser to perform static code analysis on the domain side to recognize use patters in code calling A and use them to suggest mapped patterns in terms of B
  • A transformation engine (replaces in source code calls to A by equivalent snippets using B, based perhaps on the formal semantics mentioned above

So my question is whether there is some framework, or at least some state-of-the-art approach that would provide with most of this stuff into an API-transformation framework. I have some APIs to port and I would need two and a half lifetimes if I had to do it by hand...

EDIT: after some more research and hints received by others, I'm starting to lose hope on a proven and well-known (within the niche) approach, not even mentioning dedicated tools. On the other hand, I have stumbled upon a bunch of less specific tactiques that might (or might not) work:

  • XML for the semantics definition (there are several formats out there specifically intended to define syntax; probably others can be find to define a little logic constraitns language)
  • XSLT for the transformation (to be implemented from zero); if one could hook in a formal semantics framework for transformation validation, such as the K-Framework, it would be coolest
  • A domain analysis tool to be implemented not from zero, but on the basis of some code analyis API (e.g. the one used by FindBugs or any other such tool)

EDIT2: I wonder why this question is being downvoted with no feedback whatsoever. I wonder how many of those downvoters actually have understood what the problem exposed is really about.

Was it helpful?

Solution

The problem is similar to decompilation and re-compilation. I would approach it as follows:

  • Decompilation stage: using a parser, create an abstract syntax tree of the calls made to create and modify objects.
  • Recompilation stage: using transformations and techniques similar to optimizing compilers (recognition of common patterns), generate output using the new API.

I would not use anything like xlst as you are struggling with the wrong tool for the job. Use something like ANTLR.

Unless you have a truly enormous code base, you may find that creating this code generation tool takes more time than converting the code.

Licensed under: CC-BY-SA with attribution
scroll top