From e6de5e1c591e7135466327325d4050883f2263c4 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 25 Aug 2020 21:10:40 +0100 Subject: [PATCH 1/3] Fixed JSLCorrectness fix when simplenamePart == "" --- .../reflect/reference/CtReferenceImpl.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/spoon/support/reflect/reference/CtReferenceImpl.java b/src/main/java/spoon/support/reflect/reference/CtReferenceImpl.java index e2c84d8cba8..bb3ae28b07f 100644 --- a/src/main/java/spoon/support/reflect/reference/CtReferenceImpl.java +++ b/src/main/java/spoon/support/reflect/reference/CtReferenceImpl.java @@ -93,22 +93,22 @@ public boolean equals(Object o) { return false; } private void checkIdentiferForJLSCorrectness(String simplename) { - /* - * At the level of the Java Virtual Machine, every constructor written in the Java programming language (JLS §8.8) - * appears as an instance initialization method that has the special name . - * This name is supplied by a compiler. Because the name is not a valid identifier, - * it cannot be used directly in a program written in the Java programming language. - */ - //JDTTreeBuilderHelper.computeAnonymousName returns "$numbers$Name" so we have to skip them if they start with numbers - //allow empty identifier because they are sometimes used. + /* + * At the level of the Java Virtual Machine, every constructor written in the Java programming language (JLS §8.8) + * appears as an instance initialization method that has the special name . + * This name is supplied by a compiler. Because the name is not a valid identifier, + * it cannot be used directly in a program written in the Java programming language. + */ + //JDTTreeBuilderHelper.computeAnonymousName returns "$numbers$Name" so we have to skip them if they start with numbers + //allow empty identifier because they are sometimes used. if (!simplename.matches("<.*>|\\d.*|^.{0}$")) { //split at "<" and ">" because "Iterator>>" submits setSimplename ("Cache.Entry"); if (checkAllParts(splittedSimplename)) { throw new SpoonException("Not allowed javaletter or keyword in identifier found. See JLS for correct identifier. Identifier: " + simplename); } + } } -} private boolean isKeyword(String simplename) { return keywords.contains(simplename); } @@ -118,19 +118,25 @@ private boolean checkAllParts(String[] simplenameParts) { simpleName = simpleName.replaceAll("\\[\\]|@", ""); if (isKeyword(simpleName) || checkIdentifierChars(simpleName)) { return true; + } } - } - return false; + return false; } private boolean checkIdentifierChars(String simplename) { - return (!Character.isJavaIdentifierStart(simplename.charAt(0))) || simplename.chars().anyMatch(letter -> !Character.isJavaIdentifierPart(letter)); + if (simplename.length() == 0) { + return false; + } + return (!Character.isJavaIdentifierStart(simplename.charAt(0))) + || simplename.chars().anyMatch(letter -> !Character.isJavaIdentifierPart(letter) + ); } + private static Collection fillWithKeywords() { - //removed types because needed as ref: "int","short", "char", "void", "byte","float", "true","false","boolean","double","long","class", "null" - return Stream.of("abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "do", "goto", "private", - "this", "break", "implements", "protected", "throw", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", - "transient", "catch", "extends", "try", "final", "interface", "static", "finally", "strictfp", "volatile", - "const", "native", "super", "while", "_") - .collect(Collectors.toCollection(HashSet::new)); + //removed types because needed as ref: "int","short", "char", "void", "byte","float", "true","false","boolean","double","long","class", "null" + return Stream.of("abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "do", "goto", "private", + "this", "break", "implements", "protected", "throw", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", + "transient", "catch", "extends", "try", "final", "interface", "static", "finally", "strictfp", "volatile", + "const", "native", "super", "while", "_") + .collect(Collectors.toCollection(HashSet::new)); } } From fd3313d5820f5aca1b1a949aa621bb0502913c70 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Wed, 9 Sep 2020 11:56:49 +0100 Subject: [PATCH 2/3] Added test case for JLSCorrectness with simpleName List[] --- src/test/java/spoon/generating/CorrectIdentifierTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/spoon/generating/CorrectIdentifierTest.java b/src/test/java/spoon/generating/CorrectIdentifierTest.java index 6719088e7b0..23e6126700d 100644 --- a/src/test/java/spoon/generating/CorrectIdentifierTest.java +++ b/src/test/java/spoon/generating/CorrectIdentifierTest.java @@ -69,4 +69,10 @@ public void correctIdentiferUtfChinese() { CtLocalVariableReference localVariableRef = new Launcher().getFactory().createLocalVariableReference(); assertDoesNotThrow(() -> localVariableRef.setSimpleName("処理")); } + + @Test + public void correctSquareBrackets() { + CtLocalVariableReference localVariableRef = new Launcher().getFactory().createLocalVariableReference(); + assertDoesNotThrow(() -> localVariableRef.setSimpleName("List[]")); + } } From b073f05e956c9c59da749d7118493d2ffab75165 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Fri, 11 Sep 2020 07:43:59 +0200 Subject: [PATCH 3/3] up --- .../java/spoon/generating/CorrectIdentifierTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/spoon/generating/CorrectIdentifierTest.java b/src/test/java/spoon/generating/CorrectIdentifierTest.java index 19eaf8d598b..1ec33c3986e 100644 --- a/src/test/java/spoon/generating/CorrectIdentifierTest.java +++ b/src/test/java/spoon/generating/CorrectIdentifierTest.java @@ -1,13 +1,16 @@ package spoon.generating; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.Ignore; import org.junit.Test; import spoon.FluentLauncher; import spoon.Launcher; import spoon.SpoonException; import spoon.reflect.reference.CtLocalVariableReference; +import spoon.reflect.reference.CtTypeReference; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * for correct identifier see JLS chapter 3.8 and for keywords 3.9. * Ignored tests because we have to cut some corners between spec and jdt. @@ -79,7 +82,7 @@ public void intersectionTypeIdentifierTest() { @Test public void correctSquareBrackets() { - CtLocalVariableReference localVariableRef = new Launcher().getFactory().createLocalVariableReference(); + CtTypeReference localVariableRef = new Launcher().getFactory().createTypeReference(); assertDoesNotThrow(() -> localVariableRef.setSimpleName("List[]")); } }