MopeSWTP-SS21 / MopeSWTP

MIT License
1 stars 0 forks source link

Obscure Notes during startMopeServer #84

Open manuEbg opened 3 years ago

manuEbg commented 3 years ago

I am not sure what I have done, but today I noticed the following notes during Compilation of the MopeLspServer.

> Task :compileJava
Note: /home/swtp/Documents/src/MopeSWTP/src/main/java/Server/MopeModelicaService.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :processResources UP-TO-DATE
> Task :classes

I cant remember if i saw them earlier... but I think they are connected to my Changes in the feature/Diagnostics Branche

Has anyone an Idea what this means?

After reading through some StackOverflow Questions i think this has something to do with unsafe Typehandling... But I am not sure where i did this in MopeModelicaService.java

CSchoel commented 3 years ago

Two quick questions:

  1. Is this issue not reported in IntellIJ? Usually the IDE should already highlight any warnings.
  2. Have you tried the debug parameter -Xlint:unchecked suggested by the note?

As a general hint: This sounds like an incorrect usage of Java generics. Maybe you forgot the diamond operator (<>) or some concrete name for a generic type parameter somewhere in your code?

CSchoel commented 3 years ago

The first google result for "uses unchecked or unsafe operations" explains this quite nicely.

CSchoel commented 3 years ago

After having a quick peek into the code, I can find at least one major issue with generics in the completion method:

CompletableFuture<?> completion = server.getTextDocumentService().completion(params);

You never want to use <?> in generics, because this is essentially the equivalent of throwing your hands in the air and yelling "the fuck do I care what type this is!".

At this point, the correct return type that you get is CompletableFuture<Either<List<CompletionItem>, CompletionList>>. I admit that this is a little unwieldy, but since Java 10 you can use the type inference keyword var for this. You can think of this as the safe variant of <?>, which does not throw away the generic type information, but rather tells the compiler to determine the most fitting type for you.

So the above line should probably be changed to...

var completion = server.getTextDocumentService().completion(params);

After that, you can unpack the value properly by first checking whether you got a Java list or a CompletionList and then proceeding accordingly.