Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

having trouble with deep copying a CtElement in Scala #3082

Open
krlu opened this issue Aug 29, 2019 · 7 comments
Open

having trouble with deep copying a CtElement in Scala #3082

krlu opened this issue Aug 29, 2019 · 7 comments

Comments

@krlu
Copy link

krlu commented Aug 29, 2019

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
Copy link
Collaborator

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

@monperrus monperrus changed the title Still having trouble with deep copying a CtElement having trouble with deep copying a CtElement Aug 30, 2019
@krlu
Copy link
Author

krlu commented Aug 31, 2019

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
Copy link
Author

krlu commented Sep 1, 2019

@monperrus any suggestions?

@monperrus
Copy link
Collaborator

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
Copy link
Author

krlu commented Sep 2, 2019

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

@krlu krlu closed this as completed Sep 2, 2019
@krlu krlu reopened this Sep 17, 2019
@krlu
Copy link
Author

krlu commented Sep 17, 2019

@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 monperrus changed the title having trouble with deep copying a CtElement having trouble with deep copying a CtElement i nScala Sep 17, 2019
@monperrus monperrus changed the title having trouble with deep copying a CtElement i nScala having trouble with deep copying a CtElement in Scala Sep 17, 2019
@monperrus
Copy link
Collaborator

that's a good idea!

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

No branches or pull requests

2 participants