-
-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
review: feat: Add support to Java 10 'var' keyword #2054
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
7d8b51c
Upgrade JDT library version
surli eba7039
Update pom.xml
surli 45858c7
Fix MavenLauncherTest
surli c610586
Fix another assertion
surli ec4ec77
Add a resource to provide an example of usage of var keyword in Java 10
surli d9aeb34
Add a new concept for an inferred variable
surli 39259f3
Instead of creating a new node type, add a property in CtLocalVariable
surli 2b3bab5
Start building a test
surli 35d8a61
Merge branch 'upgrade-jdt-version' into add-var-keyword
surli 632daab
Fix compliance issue with Java 10 and more.
surli 5c33cba
First test is passing
surli df2e14d
Minor change in DJPP to print var when needed
surli c2edf14
Add contracts
surli beb4404
Add a resource to provide an example of usage of var keyword in Java 10
surli cbbe3bb
Add a new concept for an inferred variable
surli c48c4de
Instead of creating a new node type, add a property in CtLocalVariable
surli 7136fdc
Start building a test
surli bcba3c7
Fix compliance issue with Java 10 and more.
surli 8293439
First test is passing
surli b38a358
Minor change in DJPP to print var when needed
surli 8df170a
Add contracts
surli 994cfec
Merge branch 'add-var-keyword' of github.com:surli/spoon into add-var…
surli 1f101f9
Fix CloneBuilder
surli 3613829
Add right metamodel property on getter and setter
surli 7855393
Fix AstCheckerTest to only fail if the parameters are not primitive t…
surli 862626b
Fix last errors
surli cb69592
Fix checkstyle
surli a0ceeac
Add the modified ModelRoleHandlers
surli 98ff3ab
Fix documentation
surli afeac73
Merge remote-tracking branch 'inria/master' into add-var-keyword
surli cbfaa6a
Fix the test
surli f160f0d
Fix licenses headers
surli 96d3fec
Merge remote-tracking branch 'inria/master' into add-var-keyword
surli 4d08254
Refactor: rename Role
surli 60dd731
Fix missing license header
surli 4335c6c
Fix test and contract
surli b32a427
Fix checkstyle and test
surli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 44 additions & 27 deletions
71
src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/spoon/test/variable/InferredVariableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package spoon.test.variable; | ||
|
||
import com.google.common.io.Files; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Test; | ||
import spoon.Launcher; | ||
import spoon.reflect.CtModel; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.factory.TypeFactory; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class InferredVariableTest { | ||
|
||
@Test | ||
public void testInferredVariableAreMarked() { | ||
// contract: if a variable is declared with 'var' keyword, it must be marked as inferred in the model | ||
Launcher launcher = new Launcher(); | ||
launcher.getEnvironment().setComplianceLevel(10); | ||
launcher.addInputResource("./src/test/resources/spoon/test/var/Main.java"); | ||
|
||
CtModel model = launcher.buildModel(); | ||
List<CtLocalVariable> localVariables = model.getElements(new TypeFilter<>(CtLocalVariable.class)); | ||
assertEquals(8, localVariables.size()); | ||
|
||
TypeFactory typeFactory = launcher.getFactory().Type(); | ||
|
||
assertTrue(localVariables.get(0).isInferred()); | ||
assertEquals(typeFactory.STRING, localVariables.get(0).getType()); | ||
|
||
assertFalse(localVariables.get(1).isInferred()); | ||
assertEquals(typeFactory.STRING, localVariables.get(1).getType()); | ||
|
||
assertTrue(localVariables.get(2).isInferred()); | ||
assertEquals("java.io.FileReader", localVariables.get(2).getType().getQualifiedName()); | ||
|
||
assertFalse(localVariables.get(3).isInferred()); | ||
assertEquals("java.io.FileReader", localVariables.get(3).getType().getQualifiedName()); | ||
|
||
assertTrue(localVariables.get(4).isInferred()); | ||
assertEquals(typeFactory.BOOLEAN_PRIMITIVE, localVariables.get(4).getType()); | ||
|
||
assertFalse(localVariables.get(5).isInferred()); | ||
assertEquals(typeFactory.BOOLEAN_PRIMITIVE, localVariables.get(5).getType()); | ||
|
||
assertTrue(localVariables.get(6).isInferred()); | ||
assertEquals(typeFactory.INTEGER_PRIMITIVE, localVariables.get(6).getType()); | ||
|
||
assertFalse(localVariables.get(7).isInferred()); | ||
assertEquals(typeFactory.INTEGER_PRIMITIVE, localVariables.get(7).getType()); | ||
} | ||
|
||
@Test | ||
public void testInferredVariableArePrintedWithVar() throws IOException { | ||
// contract: if a variable is marked as inferred in the model, it must be pretty-printed with a 'var' keyword | ||
Launcher launcher = new Launcher(); | ||
launcher.getEnvironment().setComplianceLevel(10); | ||
launcher.addInputResource("./src/test/resources/spoon/test/var/Main.java"); | ||
|
||
File outputDir = Files.createTempDir(); | ||
launcher.setSourceOutputDirectory(outputDir); | ||
|
||
launcher.run(); | ||
|
||
File outputFile = new File(outputDir, "fr/inria/sandbox/Main.java"); | ||
assertTrue(outputFile.exists()); | ||
|
||
String fileContent = StringUtils.join(Files.readLines(outputFile, Charset.defaultCharset())); | ||
|
||
assertTrue(fileContent.contains("var mySubstring = \"bla\";")); | ||
assertTrue(fileContent.contains("var myFile = new java.io.FileReader(new java.io.File(\"/tmp/myfile\")")); | ||
assertTrue(fileContent.contains("var myboolean = true;")); | ||
assertTrue(fileContent.contains("for (var i = 0;")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fr.inria.sandbox; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Main { | ||
|
||
public String toString() { | ||
// 0 | ||
var mySubstring = "bla"; | ||
|
||
// 1 -> not inferred | ||
String anotherSub = "bidule"; | ||
|
||
// 2 | ||
try (var myFile = new FileReader(new File("/tmp/myfile")); | ||
|
||
// 3 -> not inferred | ||
FileReader anotherOne = new FileReader(new File("/other/path"))) { | ||
mySubstring += myFile.toString(); | ||
} catch (IOException e) { | ||
mySubstring += "error"; | ||
} | ||
|
||
switch (mySubstring) { | ||
case "bla": | ||
|
||
// 4 | ||
var myboolean = true; | ||
|
||
// 5 | ||
boolean another = false; | ||
|
||
return ""; | ||
} | ||
|
||
// 6 | ||
for (var i = 0; i < mySubstring.length(); i++) { | ||
|
||
// 7 -> not inferred | ||
for (int j = i; j < 10; j++) { | ||
return mySubstring; | ||
} | ||
} | ||
|
||
return mySubstring; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this required here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new documentation snippet in CtLocalVariable with the following code:
This won't compile if I do not use a compliance level to 10.