![]() ![]() After the transformation is applied, we can reactivate javac's error handling using reflection and force it to reapply the attribution process to the previously erroneous trees. Because the AST contains only errors that we expect it to, our job is relatively easy. Once this is done, we can add another listener to catch the analyzed AST and apply the correct modifications. As Javac is built with recoverability in mind, if it can it will continue to analyze the AST to make it as complete as possible This allows disabling Javac's error handling using Reflection just before it starts attributing a class. Essentially, unlike an annotation processor, it provides a reference to the task associated with the current compiler process which allows to easily set up a listener when each of the steps that were previously described start and finish. Theoretically, they don't have access to the AST but with some add-exports and add-opens you can access it(the java compiler is written in Java, you can find it inside the jdk.compiler module at )įinally, the AST is translated into bytecode and written to the correct class fileĪnother useful tool when dealing with the compiler is the Javac Compiler API. If there is a syntax exception or an instruction that is not conformant to the JLS(Java Language Specification), it's reported to the logger using the method previously shown.Īt this point, annotations processors are called to do their job. This step essentially generates an object-oriented representation of the input java files called AST(Abstract Syntax Tree)Īt this point, the compiler adds some important metadata to the AST to prepare for the next step(it's more complex, but it's not really important here)įinally, the compiler resolves types, classes, references and much more to make the AST ready for class generation. If you don't know what attribution is, you may not know that the compiler to do its job passes through various steps: In short, this method is used to report errors found during attribution. This field is used by the logger inside a couple of methods, but there is one that is particularly interesting: While looking at Javac's source code, I stumbled across an interesting field inside its Logger(.util.Log): This is just to show what's really possible with Java and some compiler magic. The same warnings apply: this is not something you'd probably want to use in production.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |