JavacDiff

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

Recompleting Symbols from Sources

Consider the following usecase: let there be two classes, A and B available both in source code and (up-to-date) class files. These classes are interdependent (each refers to the other). Let there be a refactoring, that needs to work over both these files. The refactoring needs Trees (to access method bodies, to get offsets, etc), and these trees need to be attributed.

There are the following ways for the refactoring to work:

  • parse the source codes in two instances of javac (each time completing the other one from the class files). This does not require much memory (only needs memory to parse one file), but is quite slow.

  • create one instance of javac and parse both files in that instance - fast, but requires a memory to parse both/all the files, which is not reasonable in the IDE.

  • create an instance of javac, parse A from sources, complete B from the classfile. After processing of file A is done, check the available memory and either parse the source code for B in the same instance of javac (if there is enough memory), or discard the old javac and create a new one for B. This allows to use the available memory to speed up the refactoring, but allows to perform it (slowly) in memory required to process one file. Unfortunately, the standard javac does not allow to recomplete a Symbol from sources (i.e. the Symbol was originally created from a class file and now we parse and attribute a file containing the same Symbol).

Recompleting of Symbols is also used:

  • when an API client asks for Tree for Symbol that was loaded from classfiles

  • when Symbol originates from classfile without parameter names (debugging information), and parameter names are required. Source is parsed to fill in the parameter names.

The ability to recomplete a Symbol from source file is one of the most important part of the NetBeans fork/patch.

Stable annonymous innerclass numbers

When a Scope is created, the corresponding Tree is duplicated and attributed. This may lead into incorrect Symbols created for anonymous innerclasses. It is necessary to ensure that the anonymous innerclass numbers will match the numbers that would be produced by a batch compiler. This needs to be ensured even in case when the Scope is created before the tree is attributed.

Error Recovery

While editing, the code in the editor contains compilation errors almost all the time. It is therefore unacceptable to loose e.g. code completion only because of a missing semicolon, or because of an (unrelated) unresolvable symbol.

The list of current changes includes (not an exhaustive list):

  • JavacParser:

    • a variable declaration tree is produced even for cases where variable declaration is not allowed, e.g. in then/else section of if. The tree is wrapped in an erroneous tree and an error is reported. Trees produced before this change were too confusing for some hints (NB bug #192561). Relates to parseStatement and parseBlockStatement(s).

    • parseCompilationUnit is modified to handle multiple package clauses

  • Attr:

    • in "return <expr>" <expr> is always attributed, even for initializers and methods with return type void

    • the last-resort symbol filler PostAttrAnalyzer is modified to dive into erroneous trees (NB bug #152334)

  • Annotate:

    • modified to always attribute the annotation’s attribute’s values, even if the annotation itself is unresolvable (i.e. "@Undefined(@Another)" will attribute also @Another).

Cancellability

NetBeans need to be able to stop javac processing even inside one phase (parse/member enter/attribute). This is used e.g. when the user types into the editor, and the current instance of javac is already processing an obsolette source code. Then, there is no point in wasting time and memory in continuing the processing of the file.

Inferring Binary Names

Consider file A.java, containing classes B and C, and file D.java, referencing B and C. If file D.java is being parsed (and class file for B and C do not exist yet), the javac is currently unable to locate file A.java. Yet, the IDE may have the information about the content of the A.java file. So, the problem is how to pass this information into the javac. ClassNamesForFileOraculum is currently used for this.

Support for Reparsing Method Bodies

If the user changes are contained only inside one method body, it is desirable to reparse only the body of the one method. This leads into faster reparse times and less garbage on the heap. The NetBeans' fork/patch contains support for reparsing methods bodies.

Repair

In the NetBeans' fork/patch, there is a new phase, Repair, running after Flow. For source code with compilation errors, this phase converts the "uncompilable" trees into "compilable" trees.

New attributes are added to the classfiles to keep:

  • error types (stripped from the trees during repair to obtain valid trees)

  • parameters names, without depending on debug information

  • annotations with RetentionPolicy.SOURCE

Annotation Processing

In vanilla javac, new Symbols are created for classes/methods/fields in the sources for each annotation processing round. This is rewritten in the NetBeans' fork/patch to use the symbol recompleting. Symbols, once created, are used in all rounds on annotation processing and also for the final compilation. Annotation processing is also supported when completing symbols from sources. Exceptions thrown by annotation processors are logged, but do not stop the compilation.

Ide Mode

A special option, "ideMode", has been introduced by the NetBean’s fork/patch to improve javac behavior in the following cases:

  • when there is no java.lang package (i.e. no platform), vanilla javac stops with an error. In IDE mode, the compiler handles the situation more gracefully. - !May not be needed anymore.

  • when loading 1.5+ classfile with sourcelevel ⇐1.4, vanilla javac throws away any 1.5 information stored in the classfile. In IDE mode, this information is preserved.

Miscallenous

  • small extensions to allow parsing and attributing a "standalone" statement, expression, etc. in the given context.

  • small extensions to JavacTaskImpl to allow per-file per-phase parsing of multiple sources in the same instance of javac

  • access restrictions loosened for a lot methods/classes (private→protected, etc.)

  • inlineTags cache for ParamTagImpl and ThrowsTagImpl

  • string folding is disabled (by "disableStringFolding" option) to so that concatenated strings are represented by full trees

  • ToolProvider loads classes from context classloader

  • TreeInfo.symbolFor works all trees that refer to a symbol