How to encapsulate a third party complex object structure?
Posted
by tangens
on Stack Overflow
See other posts from Stack Overflow
or by tangens
Published on 2010-05-22T23:09:12Z
Indexed on
2010/05/22
23:20 UTC
Read the original article
Hit count: 254
Motivation
Currently I'm using the java parser japa to create an abstract syntax tree (AST) of a java file. With this AST I'm doing some code generation (e.g.: if there's an annotation on a method, create some other source files, ...)
Problem
When my code generation becomes more complex, I've to dive deeper into the structure of the AST (e.g. I have to use visitors to extract some type information of method parameters).
But I'm not sure if I want to stay with japa or if I will change the parser library later.
Because my code generator uses freemarker (which isn't good at automatic refactoring) I want the interface that it uses to access the AST information to be stable, even if I decide to change the java parser.
Question
What's the best way to encapsulate complex datastructures of third party libraries?
I could create my own datatypes and copy the parts of the AST that I need into these.
I could create lots of specialized access methods that work with the AST and create exactly the infos I need (e.g. the fully qualified return type of a method as one string, or the first template parameter of a class).
I could create wrapper classes for the japa datastructures I currently need and embed the japa types inside, so that I can delegate requests to the japa types and transform the resulting japa types to my wrapper classes again.
Which solution should I take? Are there other (better) solutions to this problem?
© Stack Overflow or respective owner