Skip to content

Commit

Permalink
Merge branch 'master' into hfreeb/varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
MysterAitch authored May 17, 2020
2 parents 1777631 + 7c60ee1 commit c66c16c
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 45 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/maven_tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
name: Build and test (using maven)

on: [push, pull_request]
#on: [push, pull_request]
#on: [pull_request]
on:
# Trigger the workflow on push to master (ignoring .md only changes)
push:
branches:
- master
paths-ignore:
- '**.md'

# Trigger the workflow on any pull_request (ignoring .md only changes)
pull_request:
paths-ignore:
- '**.md'

jobs:
maven_test:
Expand Down
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Next Release (Version 3.15.23)
------------------
[issues resolved](https://github.com/javaparser/javaparser/milestone/174?closed=1)
* FIXED: Edits to the value of a string value are now correctly handled for use with Lexical Preservation
(PR [#2646](https://github.com/javaparser/javaparser/pull/2646), by [@lemoncurry](https://github.com/lemoncurry))
* FIXED: Edits to the value of other literal values also now handled
(PR [#2679](https://github.com/javaparser/javaparser/pull/2679), by [@MysterAitch](https://github.com/MysterAitch))
* BREAKING CHANGE: Tokens relating to literal values now have the category of `JavaToken.Category.LITERAL` (previously `JavaToken.Category.KEYWORD`)
(PR [#2679](https://github.com/javaparser/javaparser/pull/2679), by [@MysterAitch](https://github.com/MysterAitch))



Version 3.15.22
------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,19 @@

package com.github.javaparser.printer.lexicalpreservation;

import static com.github.javaparser.StaticJavaParser.parse;
import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC;
import static com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA;
import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol;
import static com.github.javaparser.utils.Utils.EOL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.github.javaparser.*;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.AnnotationMemberDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.InitializerDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.GeneratedJavaParserConstants;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.ArrayCreationLevel;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.ArrayCreationExpr;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.CatchClause;
import com.github.javaparser.ast.stmt.ExpressionStmt;
Expand All @@ -72,6 +47,19 @@
import com.github.javaparser.ast.visitor.Visitable;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.github.javaparser.StaticJavaParser.parse;
import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC;
import static com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.NODE_TEXT_DATA;
import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol;
import static com.github.javaparser.utils.Utils.EOL;
import static org.junit.jupiter.api.Assertions.*;

class LexicalPreservingPrinterTest extends AbstractLexicalPreservingTest {
private NodeText getTextForNode(Node node) {
return node.getData(NODE_TEXT_DATA);
Expand Down Expand Up @@ -1334,4 +1322,172 @@ void commentAddedAtTopLevel() {
cu.removeComment();
assertEqualsNoEol("package x;class X{}", LexicalPreservingPrinter.print(cu));
}

@Test
public void testReplaceStringLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

final String code = "\"asd\"";
final String expected = "\"REPLACEMENT\"";

final Expression b = javaParser.parseExpression(code).getResult().orElseThrow(AssertionError::new);

assertTrue(b.isStringLiteralExpr());
StringLiteralExpr sle = (StringLiteralExpr) b;
sle.setValue("REPLACEMENT");

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceStringLiteralWithinStatement() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "String str = \"aaa\";";
String expected = "String str = \"REPLACEMENT\";";

Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(StringLiteralExpr.class).forEach(stringLiteralExpr -> {
stringLiteralExpr.setValue("REPLACEMENT");
});

assertEquals(expected, LexicalPreservingPrinter.print(b));
assertEquals(expected, b.toString());
}

@Test
public void testReplaceClassName() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

final String code = "class A {}";
final CompilationUnit b = javaParser.parse(code).getResult().orElseThrow(AssertionError::new);
LexicalPreservingPrinter.setup(b);

assertEquals(1, b.findAll(ClassOrInterfaceDeclaration.class).size());
b.findAll(ClassOrInterfaceDeclaration.class).forEach(coid -> coid.setName("B"));

final String expected = "class B {}";

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceIntLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

final String code = "5";
final String expected = "10";

final Expression b = javaParser.parseExpression(code).getResult().orElseThrow(AssertionError::new);

assertTrue(b.isIntegerLiteralExpr());
((IntegerLiteralExpr) b).setValue("10");

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceLongLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "long x = 5L;";
String expected = "long x = 10L;";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(LongLiteralExpr.class).forEach(longLiteralExpr -> {
longLiteralExpr.setValue("10L");
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceBooleanLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "boolean x = true;";
String expected = "boolean x = false;";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(BooleanLiteralExpr.class).forEach(booleanLiteralExpr -> {
booleanLiteralExpr.setValue(false);
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceDoubleLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "double x = 5.0D;";
String expected = "double x = 10.0D;";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(DoubleLiteralExpr.class).forEach(doubleLiteralExpr -> {
doubleLiteralExpr.setValue("10.0D");
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceCharLiteral() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "char x = 'a';";
String expected = "char x = 'b';";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(CharLiteralExpr.class).forEach(charLiteralExpr -> {
charLiteralExpr.setValue("b");
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceCharLiteralUnicode() {
final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLexicalPreservationEnabled(true));

String code = "char x = 'a';";
String expected = "char x = '\\u0000';";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(CharLiteralExpr.class).forEach(charLiteralExpr -> {
charLiteralExpr.setValue("\\u0000");
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}

@Test
public void testReplaceTextBlockLiteral() {
final JavaParser javaParser = new JavaParser(
new ParserConfiguration()
.setLexicalPreservationEnabled(true)
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_14)
);

String code = "String x = \"\"\"a\"\"\";";
String expected = "String x = \"\"\"\n" +
" REPLACEMENT\n" +
" \"\"\";";

final Statement b = javaParser.parseStatement(code).getResult().orElseThrow(AssertionError::new);
b.findAll(TextBlockLiteralExpr.class).forEach(textblockLiteralExpr -> {
textblockLiteralExpr.setValue("\n REPLACEMENT\n ");
});

final String actual = LexicalPreservingPrinter.print(b);
assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public static JavaToken.Category getCategory(int kind) {
case ELSE:
case ENUM:
case EXTENDS:
case FALSE:
case FINAL:
case FINALLY:
case FLOAT:
Expand All @@ -121,7 +120,6 @@ public static JavaToken.Category getCategory(int kind) {
case LONG:
case NATIVE:
case NEW:
case NULL:
case PACKAGE:
case PRIVATE:
case PROTECTED:
Expand All @@ -137,7 +135,6 @@ public static JavaToken.Category getCategory(int kind) {
case THROW:
case THROWS:
case TRANSIENT:
case TRUE:
case TRY:
case VOID:
case VOLATILE:
Expand Down Expand Up @@ -168,6 +165,9 @@ public static JavaToken.Category getCategory(int kind) {
case CHARACTER_LITERAL:
case STRING_LITERAL:
case TEXT_BLOCK_LITERAL:
case TRUE:
case FALSE:
case NULL:
return JavaToken.Category.LITERAL;
case IDENTIFIER:
return JavaToken.Category.IDENTIFIER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public boolean isPrimitive() {
return false;
}

@Override
public boolean isLiteral() {
return false;
}

@Override
public boolean isChildOfClass(Class<? extends Node> nodeClass) {
return nodeClass.isInstance(child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ private void applyRemovedDiffElement(RemovedGroup removedGroup, Removed removed,
diffIndex++;
} else if (originalElementIsToken && originalElement.isWhiteSpaceOrComment()) {
originalIndex++;
} else if (originalElement.isLiteral()) {
nodeText.removeElement(originalIndex);
diffIndex++;
} else if (removed.isPrimitiveType()) {
if (originalElement.isPrimitive()) {
nodeText.removeElement(originalIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ static boolean matching(CsmElement a, CsmElement b) {
}
} else if (a instanceof CsmToken) {
if (b instanceof CsmToken) {
// fix #2382:
// Tokens are described by their type AND their content
// and TokenContentCalculator. By using .equals(), all
// three values are compared.
CsmToken childA = (CsmToken)a;
CsmToken childB = (CsmToken)b;
return childA.getTokenType() == childB.getTokenType();
return childA.equals(childB);
} else if (b instanceof CsmChild) {
return false;
} else if (b instanceof CsmIndent) {
Expand Down
Loading

0 comments on commit c66c16c

Please sign in to comment.