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.75k stars 351 forks source link

[Question]: Is there a way to set the character encoding of processed source code? #5343

Closed tenax66 closed 1 year ago

tenax66 commented 1 year ago

When processing source code as follows, the character encoding of output files depends on the environment.

Launcher l = new Launcher();
l.addInputResource("src/main/java");
l.run();

For example, when working in Windows (Japanese setting), the encoding of processed source code is CP932, I wanted UTF-8 code though. Is there any simple way to set the encoding of processed source code?

In the case above, I created my own FileGenerator (which outputs files in UTF-8) from JavaOutputProcessor and set it to Environment as a work-around. like the following:

l.getEnvironment().setDefaultFileGenerator(new SomeUTF8OutputProcessor());

But I don't think this is a good approach.

MartinWitt commented 1 year ago

Hey,

have you tried using https://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/compiler/Environment.html#setEncoding(java.nio.charset.Charset) to set the charset?

tenax66 commented 1 year ago

Yes, I tried Environment.setEncoding(StandardCharsets.UTF_8). But it only set the encoding of source code that spoon parses, not the output.

MartinWitt commented 1 year ago

Oh, you are correct. It looks like we have no API, or I am not aware of any method for this use case. Your solution might be the correct one.