Feuermagier / autograder

Automatic grading of student's Java code
MIT License
13 stars 7 forks source link

Rewrite `DiamondOperatorCheck` in Spoon #559

Open Luro02 opened 3 months ago

Luro02 commented 3 months ago

Description

What it does?

For generic calls one can explicitly add the type like this new ArrayList<String>(). In many cases explicitly writing the type is redundant, because the compiler can infer it.

Current implementation

The implementation is currently done in PMD, but this has several problems:

PMD implements the check by checking each invocation and creating a new one, but with the explicit type removed. If the new invocation works, the type is redundant.

How to implement this in Spoon?

The problem is that spoon does not expose any of the type inference done by jdt and every constructed invocation must set the type manually.

There are some solutions that come to mind to replicate a similar behavior:

  1. Implement type inference according to the JLS (note: not worth the amount of work and error prone)
  2. Try to work with the spoon developers to expose the jdt type inference and apply them to created elements
  3. Replicate the relevant source code in a new project and recompile it to check if it still works with the type removed. The proof of concept would be very easy to implement, but it would result in a compilation of the model for every diamond operator with an explicit type. The compilation of the model is the part that takes by far the longest time. To mitigate this, one could try to minimize the source code as much as possible, before recompiling or one could remove all diamond operators at once, and then try to map the compiler messages to the relevant source fragments (in that case the project has to be recompiled only once).