INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.
http://spoon.gforge.inria.fr/
Other
1.76k stars 351 forks source link

having trouble with deep copying a CtElement in Scala #3082

Open krlu opened 5 years ago

krlu commented 5 years ago

I would like to deep copy the entire AST of a CtElement, especially when I want to use parts of the sub-tree to create brand new CtElements

The Clone method available to all CtElement types are protected I had this issue last year, see link below

1978

Any help much appreciated!

monperrus commented 5 years ago

Which type of element are you cloning? What happens when you call el.clone()?

krlu commented 5 years ago

I'm using CtMethod When I do method.Clone() i get the following issue:

Error:(199, 12) method clone in class Object cannot be accessed in spoon.reflect.declaration.CtMethod[Any]
 Access to protected method clone not permitted because
 prefix type spoon.reflect.declaration.CtMethod[Any] does not conform to
 object ProgramTransformer in package analysis where the access takes place
    method.clone()

I should mention, I am doing these operations using scala, not java. Could that be a problem?

krlu commented 5 years ago

@monperrus any suggestions?

monperrus commented 5 years ago

The problem comes from Scala and is related to #2710. I'm not knowledgeable enough in Scala to fix it. But I'd be happy to merge a PR that makes the life of Scala users easier.

krlu commented 5 years ago

Ah i see, ok I'll see what I can do. I'm happy to close this issue.

krlu commented 5 years ago

@monperrus

Hey I wanted to give an update. While not ideal, I find that if I encapsulate the el.clone() method call inside a java class and invoke that java class in my scala code, everything works just fine.

While I'm too busy to package that up nicely and submit a formal PR, I suspect the best thing to do would be to create a java utility method (i.e. CtElementUtil) that contains a trivial clone method, shown below. Perhaps one of you guys can do that?

public static CtElement clone(CtElement originalCtElement){
     return originalCtElement.clone()
}
monperrus commented 5 years ago

that's a good idea!