At once I will say that I know, there is, the Javaparser project, but it is from third-party developers. I just can not understand if java compiled into bytecode from source, then there must be a parser , isn't it? By parser, I mean building the structure of a compiled application so that, for example, you can get a node of a certain variable by name. What are the pitfalls here, if such a feature is not provided ( javap know about javap , but it works with bytecode, not source)

  • 2
    and why did you decide that it is not? - michael_best 7:02
  • @michael_best, it’s about the absence of a parser in the form of a standard library class. - ߊߚߤߘ 7:06 pm
  • one
    @Arhad so he is. - Sergey Gornostaev 7:08 pm
  • that is, do you think the code itself is converted to bytecode? - michael_best
  • @michael_best, And what you do not like classes: ScriptContext , ScriptEngine , ScriptEngineFactory , ScriptEngineManager , ScriptException , SimpleScriptContext . I'm really interested. - And

2 answers 2

The Java compiler has an API . On its use Lombok code generation is built, for example. Examples of its use are on Stackoverflow, for example, this answer and this one .

  • Thank you, your answer helped me review my question. I think the answer is correct, but in the end I came to the use of spoon api - Belenot

Java is written in C ++ (now partially rewritten). Therefore, providing access to the syntax tree is not a trivial task.

And since it was very few people needed, then they did not add this feature.

  • Now I’ll tell you something terrible: The vast majority of lexers, parsers, translators, interpreters, compilers, and virtual machines are written in C or C ++. This does not make the task of “providing access to the syntactic tree” any less. - Sergey Gornostaev
  • Apart from the need to write classes for all the syntax structures, the integration code that will perform the conversion, the need to change the parser so that it becomes reentrant, and even heaps of everything, then I agree with you: it doesn't weigh down at all. - talex am
  • one
    Now I will tell you a terrible (c), Javac is written in Java ( hg.openjdk.java.net/jdk/jdk12/file/5fa71cce89eb/src/… ), because everything is in order with the syntax tree, here is the JVM that interprets bytecode written in C ++. - Alexander Berezovsky
  • Looks like I was wrong. Partially. OpenJDK does contain a java version of javac, but it’s not the only JDK, and it’s not the original compiler. The first versions were written in C. This invalidates the first part of my answer. The second remains valid. - talex