aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--javaparser-core/pom.xml4
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/JavaParser.java5
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/expr/CharLiteralExpr.java11
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/expr/StringLiteralExpr.java9
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/internal/Utils.java34
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/visitor/TreeVisitor.java (renamed from javaparser-core/src/main/java/com/github/javaparser/ast/TreeVisitor.java)29
-rw-r--r--javaparser-core/src/main/javacc/java_1_8.jj46
-rw-r--r--javaparser-testing/pom.xml2
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/bdd/steps/DumpingSteps.java52
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithAVisitorTest.java12
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithoutAVisitorTest.java12
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/ClassCreator.java61
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/CreatingACompilationUnitFromScratch.java10
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/CuPrinter.java25
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_1.java50
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_2.java58
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/MethodPrinter.java42
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/PrintingTheCompilationUnitToSystemOutputTest.java13
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/TestFileToken.java40
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/VisitingClassMethodsTest.java12
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/D.java9
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest.java49
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest_2.java71
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/RemoveDeleteNodeFromAst.java20
-rw-r--r--javaparser-testing/src/test/resources/com/github/javaparser/bdd/dumping_scenarios.story44
-rw-r--r--javaparser-testing/src/test/resources/com/github/javaparser/bdd/parsing_scenarios.story12
-rw-r--r--javaparser-testing/src/test/resources/com/github/javaparser/junit/TestFile.java16
27 files changed, 689 insertions, 59 deletions
diff --git a/javaparser-core/pom.xml b/javaparser-core/pom.xml
index 8f17434ea..9eb3bb2fe 100644
--- a/javaparser-core/pom.xml
+++ b/javaparser-core/pom.xml
@@ -24,7 +24,7 @@
</licenses>
<properties>
- <java.version>1.6</java.version>
+ <java.version>1.8</java.version>
</properties>
<build>
@@ -60,7 +60,7 @@
<signature>
<!-- Make sure only the API of this JDK is used -->
<groupId>org.codehaus.mojo.signature</groupId>
- <artifactId>java16</artifactId>
+ <artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>
diff --git a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java
index 383b2f3f8..dff98ef27 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java
@@ -160,6 +160,11 @@ public final class JavaParser {
return parse(file, Charset.defaultCharset().name(),true);
}
+ public static CompilationUnit parse(final Reader reader)
+ throws ParseException {
+ return parse(reader, true);
+ }
+
public static CompilationUnit parse(final Reader reader, boolean considerComments)
throws ParseException {
try {
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CharLiteralExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CharLiteralExpr.java
index 82ca7691d..da13d89f3 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CharLiteralExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CharLiteralExpr.java
@@ -22,10 +22,12 @@
package com.github.javaparser.ast.expr;
import com.github.javaparser.Range;
+import com.github.javaparser.ast.internal.Utils;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import static com.github.javaparser.Position.pos;
+import static com.github.javaparser.Range.*;
/**
* @author Julio Vilmar Gesser
@@ -44,13 +46,20 @@ public final class CharLiteralExpr extends StringLiteralExpr {
*/
@Deprecated
public CharLiteralExpr(int beginLine, int beginColumn, int endLine, int endColumn, String value) {
- this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), value);
+ this(range(pos(beginLine, beginColumn), pos(endLine, endColumn)), value);
}
public CharLiteralExpr(Range range, String value) {
super(range, value);
}
+ /**
+ * Utility method that creates a new StringLiteralExpr. Escapes EOL characters.
+ */
+ public static CharLiteralExpr escape(String string) {
+ return new CharLiteralExpr(Utils.escapeEndOfLines(string));
+ }
+
@Override
public <R, A> R accept(GenericVisitor<R, A> v, A arg) {
return v.visit(this, arg);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/StringLiteralExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/StringLiteralExpr.java
index af4625566..56b5486b4 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/StringLiteralExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/StringLiteralExpr.java
@@ -22,12 +22,14 @@
package com.github.javaparser.ast.expr;
import com.github.javaparser.Range;
+import com.github.javaparser.ast.internal.Utils;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import static com.github.javaparser.Position.pos;
/**
+ * Java® Language Specification 3.10.5 String Literals
* @author Julio Vilmar Gesser
*/
public class StringLiteralExpr extends LiteralExpr {
@@ -46,6 +48,13 @@ public class StringLiteralExpr extends LiteralExpr {
}
/**
+ * Utility method that creates a new StringLiteralExpr. Escapes EOL characters.
+ */
+ public static StringLiteralExpr escape(String string) {
+ return new StringLiteralExpr(Utils.escapeEndOfLines(string));
+ }
+
+ /**
* @deprecated prefer using Range objects.
*/
@Deprecated
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/internal/Utils.java b/javaparser-core/src/main/java/com/github/javaparser/ast/internal/Utils.java
index 983afefc0..514962de9 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/internal/Utils.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/internal/Utils.java
@@ -26,14 +26,36 @@ import java.util.Collection;
import java.util.List;
/**
+ * Any kind of utility.
+ *
* @author Federico Tomassetti
*/
public class Utils {
- public static <T> List<T> ensureNotNull(List<T> list) {
- return list == null ? new ArrayList<T>() : list;
- }
+ public static <T> List<T> ensureNotNull(List<T> list) {
+ return list == null ? new ArrayList<T>() : list;
+ }
+
+ public static <E> boolean isNullOrEmpty(Collection<E> collection) {
+ return collection == null || collection.isEmpty();
+ }
- public static <E> boolean isNullOrEmpty(Collection<E> collection) {
- return collection == null || collection.isEmpty();
- }
+ /**
+ * @return string with ASCII characters 10 and 13 replaced by the text "\n" and "\r".
+ */
+ public static String escapeEndOfLines(String string) {
+ StringBuilder escapedString = new StringBuilder();
+ for (char c : string.toCharArray()) {
+ switch (c) {
+ case '\n':
+ escapedString.append("\\n");
+ break;
+ case '\r':
+ escapedString.append("\\r");
+ break;
+ default:
+ escapedString.append(c);
+ }
+ }
+ return escapedString.toString();
+ }
}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/TreeVisitor.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/TreeVisitor.java
index f3b2672d0..94eac0b4b 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/TreeVisitor.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/TreeVisitor.java
@@ -18,18 +18,27 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
-
-package com.github.javaparser.ast;
-public abstract class TreeVisitor {
+package com.github.javaparser.ast.visitor;
+
+import com.github.javaparser.ast.Node;
- public void visitDepthFirst(Node node){
- process(node);
- for (Node child : node.getChildrenNodes()){
- visitDepthFirst(child);
- }
- }
+/**
+ * Iterate over all the nodes in (a part of) the AST.
+ */
+public abstract class TreeVisitor {
- public abstract void process(Node node);
+ /**
+ * https://en.wikipedia.org/wiki/Depth-first_search
+ *
+ * @param node the start node, and the first one that is passed to process(node).
+ */
+ public void visitDepthFirst(Node node) {
+ process(node);
+ for (Node child : node.getChildrenNodes()) {
+ visitDepthFirst(child);
+ }
+ }
+ public abstract void process(Node node);
}
diff --git a/javaparser-core/src/main/javacc/java_1_8.jj b/javaparser-core/src/main/javacc/java_1_8.jj
index 31e68c1ea..3bf9af846 100644
--- a/javaparser-core/src/main/javacc/java_1_8.jj
+++ b/javaparser-core/src/main/javacc/java_1_8.jj
@@ -25,7 +25,7 @@ options {
JAVA_UNICODE_ESCAPE=true;
COMMON_TOKEN_ACTION=true; // Using the CommonTokenAction callback to collect tokens for later usage
//SUPPORT_CLASS_VISIBILITY_PUBLIC=false;
- JDK_VERSION = "1.6";
+ JDK_VERSION = "1.8";
TOKEN_FACTORY = "ASTParser.GTToken";
JAVA_TEMPLATE_TYPE = "modern";
}
@@ -2426,34 +2426,32 @@ Expression PrimaryPrefix():
(
ret = Literal()
|
- "this" { ret = new ThisExpr(range(token.beginLine, token.beginColumn, token.endLine, token.endColumn), null); }
+ <THIS> { ret = new ThisExpr(range(token.beginLine, token.beginColumn, token.endLine, token.endColumn), null); }
|
- "super" { ret = new SuperExpr(range(token.beginLine, token.beginColumn, token.endLine, token.endColumn), null); }
+ <SUPER> { ret = new SuperExpr(range(token.beginLine, token.beginColumn, token.endLine, token.endColumn), null); }
(
- "."
- [ typeArgs = TypeArguments() {typeArgs.remove(0);} ]
- name = SimpleName()
- [ args = Arguments() {hasArgs=true;} ]
- {
- if (hasArgs) {
- MethodCallExpr m = new MethodCallExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, typeArgs, null, args);
- m.setNameExpr(name);
- ret = m;
- } else {
- FieldAccessExpr f = new FieldAccessExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, null, null);
- f.setFieldExpr(name);
- ret = f;
+ "."
+ [ typeArgs = TypeArguments() {typeArgs.remove(0);} ]
+ name = SimpleName()
+ [ args = Arguments() {hasArgs=true;} ]
+ {
+ if (hasArgs) {
+ MethodCallExpr m = new MethodCallExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, typeArgs, null, args);
+ m.setNameExpr(name);
+ ret = m;
+ } else {
+ FieldAccessExpr f = new FieldAccessExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, null, null);
+ f.setFieldExpr(name);
+ ret = f;
+ }
+ }
+ |
+ "::" [typeArgs = TypeParameters() { typeArgs.remove(0); }] (<IDENTIFIER> | "new")
+ {
+ ret = new MethodReferenceExpr(range(ret.getBegin(), pos(token.endLine, token.endColumn)), ret, typeArgs, token.image);
}
- }
- |
- "::" [typeArgs = TypeParameters() { typeArgs.remove(0); }] (<IDENTIFIER> | "new")
- {
- ret = new MethodReferenceExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, typeArgs, token.image);
- }
- | args = Arguments() {new MethodCallExpr(range(ret.getBegin().line, ret.getBegin().column, token.endLine, token.endColumn), ret, typeArgs, null, args);}
)
|
-
"(" {line=token.beginLine; column=token.beginColumn;}
[
( LOOKAHEAD(FormalParameter()) p = FormalParameter() { isLambda = true;} [args = FormalLambdaParameters()]
diff --git a/javaparser-testing/pom.xml b/javaparser-testing/pom.xml
index eec08968a..3a4579377 100644
--- a/javaparser-testing/pom.xml
+++ b/javaparser-testing/pom.xml
@@ -78,6 +78,7 @@
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>${project.version}</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -95,6 +96,7 @@
<groupId>de.codecentric</groupId>
<artifactId>jbehave-junit-runner</artifactId>
<version>1.2.0</version>
+ <scope>test</scope>
</dependency>
</dependencies>
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/bdd/steps/DumpingSteps.java b/javaparser-testing/src/test/java/com/github/javaparser/bdd/steps/DumpingSteps.java
index 172dea3ec..9aa70ed93 100644
--- a/javaparser-testing/src/test/java/com/github/javaparser/bdd/steps/DumpingSteps.java
+++ b/javaparser-testing/src/test/java/com/github/javaparser/bdd/steps/DumpingSteps.java
@@ -24,9 +24,8 @@ package com.github.javaparser.bdd.steps;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.SourcesHelper;
-import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.DumpVisitor;
-
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
@@ -38,41 +37,64 @@ import java.io.StringReader;
import java.net.URISyntaxException;
import java.net.URL;
-import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class DumpingSteps {
- private CompilationUnit compilationUnit;
+ private Node resultNode;
private String sourceUnderTest;
- @Given("the class:$classSrc")
+ @Given("the {class|compilation unit|expression|block|expressions|statement|import|annotation|body declaration}:$classSrc")
public void givenTheClass(String classSrc) {
this.sourceUnderTest = classSrc.trim();
}
- @Given("the class in the file \"$classFile\"")
+ @Given("the {class|compilation unit|expression|block|expressions|statement|import|annotation|body declaration} in the file \"$classFile\"")
public void givenTheClassInTheFile(String classFile) throws URISyntaxException, IOException, ParseException {
URL url = getClass().getResource("../samples/" + classFile);
sourceUnderTest = SourcesHelper.readerToString(new FileReader(new File(url.toURI()))).trim();
}
- @Given("the compilation unit:$classSrc")
- public void givenTheCompilationUnit(String classSrc) {
- this.sourceUnderTest = classSrc.trim();
+ @When("the {class|compilation unit} is parsed by the Java parser")
+ public void whenTheClassIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parse(new StringReader(sourceUnderTest), true);
}
- @When("the class is parsed by the Java parser")
- public void whenTheClassIsParsedByTheJavaParser() throws ParseException {
- compilationUnit = JavaParser.parse(new StringReader(sourceUnderTest), true);
+ @When("the expression is parsed by the Java parser")
+ public void whenTheExpressionIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseExpression(sourceUnderTest);
+ }
+
+ @When("the block is parsed by the Java parser")
+ public void whenTheBlockIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseBlock(sourceUnderTest);
+ }
+
+ @When("the statement is parsed by the Java parser")
+ public void whenTheStatementIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseStatement(sourceUnderTest);
+ }
+
+ @When("the import is parsed by the Java parser")
+ public void whenTheImportIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseImport(sourceUnderTest);
+ }
+
+ @When("the annotation is parsed by the Java parser")
+ public void whenTheAnnotationIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseAnnotation(sourceUnderTest);
+ }
+
+ @When("the body declaration is parsed by the Java parser")
+ public void whenTheBodyDeclarationIsParsedByTheJavaParser() throws ParseException {
+ resultNode = JavaParser.parseBodyDeclaration(sourceUnderTest);
}
@Then("it is dumped to:$dumpSrc")
public void isDumpedTo(String dumpSrc) {
- DumpVisitor dumpVisitor = new DumpVisitor();
- dumpVisitor.visit(compilationUnit, null);
- assertEquals(dumpSrc.trim(), dumpVisitor.getSource().trim());
+ assertEquals(dumpSrc.trim(), resultNode.toString().trim());
}
}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithAVisitorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithAVisitorTest.java
new file mode 100644
index 000000000..dd2d08a14
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithAVisitorTest.java
@@ -0,0 +1,12 @@
+package com.github.javaparser.junit;
+
+import org.junit.Test;
+
+public class ChangingMethodsFromAClassWithAVisitorTest {
+ @Test
+ public void printingTheCompilationUnitToSystemOutput() throws Exception {
+ try (TestFileToken f = new TestFileToken("test.java")) {
+ MethodChanger_1.main(new String[]{});
+ }
+ }
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithoutAVisitorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithoutAVisitorTest.java
new file mode 100644
index 000000000..0ef2e6fa3
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/ChangingMethodsFromAClassWithoutAVisitorTest.java
@@ -0,0 +1,12 @@
+package com.github.javaparser.junit;
+
+import org.junit.Test;
+
+public class ChangingMethodsFromAClassWithoutAVisitorTest {
+ @Test
+ public void printingTheCompilationUnitToSystemOutput() throws Exception {
+ try (TestFileToken f = new TestFileToken("test.java")) {
+ MethodChanger_2.main(new String[]{});
+ }
+ }
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/ClassCreator.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/ClassCreator.java
new file mode 100644
index 000000000..01fb0f4b3
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/ClassCreator.java
@@ -0,0 +1,61 @@
+package com.github.javaparser.junit;
+
+import com.github.javaparser.ASTHelper;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.PackageDeclaration;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.body.ModifierSet;
+import com.github.javaparser.ast.body.Parameter;
+import com.github.javaparser.ast.expr.FieldAccessExpr;
+import com.github.javaparser.ast.expr.MethodCallExpr;
+import com.github.javaparser.ast.expr.NameExpr;
+import com.github.javaparser.ast.expr.StringLiteralExpr;
+import com.github.javaparser.ast.stmt.BlockStmt;
+
+public class ClassCreator {
+
+ public static void main(String[] args) throws Exception {
+ // creates the compilation unit
+ CompilationUnit cu = createCU();
+
+ // prints the created compilation unit
+ System.out.println(cu.toString());
+ }
+
+ /**
+ * creates the compilation unit
+ */
+ private static CompilationUnit createCU() {
+ CompilationUnit cu = new CompilationUnit();
+ // set the package
+ cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));
+
+ // create the type declaration
+ ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
+ ASTHelper.addTypeDeclaration(cu, type);
+
+ // create a method
+ MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main");
+ method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC));
+ ASTHelper.addMember(type, method);
+
+ // add a parameter to the method
+ Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args");
+ param.setVarArgs(true);
+ ASTHelper.addParameter(method, param);
+
+ // add a body to the method
+ BlockStmt block = new BlockStmt();
+ method.setBody(block);
+
+ // add a statement do the method body
+ NameExpr clazz = new NameExpr("System");
+ FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
+ MethodCallExpr call = new MethodCallExpr(field, "println");
+ ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!"));
+ ASTHelper.addStmt(block, call);
+
+ return cu;
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/CreatingACompilationUnitFromScratch.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/CreatingACompilationUnitFromScratch.java
new file mode 100644
index 000000000..5fc6723d1
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/CreatingACompilationUnitFromScratch.java
@@ -0,0 +1,10 @@
+package com.github.javaparser.junit;
+
+import org.junit.Test;
+
+public class CreatingACompilationUnitFromScratch {
+ @Test
+ public void printingTheCompilationUnitToSystemOutput() throws Exception {
+ ClassCreator.main(new String[]{});
+ }
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/CuPrinter.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/CuPrinter.java
new file mode 100644
index 000000000..20cc4f66e
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/CuPrinter.java
@@ -0,0 +1,25 @@
+package com.github.javaparser.junit;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+
+import java.io.FileInputStream;
+
+public class CuPrinter {
+
+ public static void main(String[] args) throws Exception {
+ // creates an input stream for the file to be parsed
+ FileInputStream in = new FileInputStream("test.java");
+
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ } finally {
+ in.close();
+ }
+
+ // prints the resulting compilation unit to default system output
+ System.out.println(cu.toString());
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_1.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_1.java
new file mode 100644
index 000000000..98be6aa7e
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_1.java
@@ -0,0 +1,50 @@
+package com.github.javaparser.junit;
+
+import com.github.javaparser.ASTHelper;
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.body.Parameter;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.io.FileInputStream;
+
+public class MethodChanger_1 {
+
+ public static void main(String[] args) throws Exception {
+ // creates an input stream for the file to be parsed
+ FileInputStream in = new FileInputStream("test.java");
+
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ } finally {
+ in.close();
+ }
+
+ // visit and change the methods names and parameters
+ new MethodChangerVisitor().visit(cu, null);
+
+ // prints the changed compilation unit
+ System.out.println(cu.toString());
+ }
+
+ /**
+ * Simple visitor implementation for visiting MethodDeclaration nodes.
+ */
+ private static class MethodChangerVisitor extends VoidVisitorAdapter {
+
+ @Override
+ public void visit(MethodDeclaration n, Object arg) {
+ // change the name of the method to upper case
+ n.setName(n.getName().toUpperCase());
+
+ // create the new parameter
+ Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");
+
+ // add the parameter to the method
+ ASTHelper.addParameter(n, newArg);
+ }
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_2.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_2.java
new file mode 100644
index 000000000..07c9737b8
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodChanger_2.java
@@ -0,0 +1,58 @@
+package com.github.javaparser.junit;
+
+import com.github.javaparser.ASTHelper;
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.body.BodyDeclaration;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.body.Parameter;
+import com.github.javaparser.ast.body.TypeDeclaration;
+
+import java.io.FileInputStream;
+import java.util.List;
+
+public class MethodChanger_2 {
+
+ public static void main(String[] args) throws Exception {
+ // creates an input stream for the file to be parsed
+ FileInputStream in = new FileInputStream("test.java");
+
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ } finally {
+ in.close();
+ }
+
+ // change the methods names and parameters
+ changeMethods(cu);
+
+ // prints the changed compilation unit
+ System.out.println(cu.toString());
+ }
+
+ private static void changeMethods(CompilationUnit cu) {
+ List<TypeDeclaration> types = cu.getTypes();
+ for (TypeDeclaration type : types) {
+ List<BodyDeclaration> members = type.getMembers();
+ for (BodyDeclaration member : members) {
+ if (member instanceof MethodDeclaration) {
+ MethodDeclaration method = (MethodDeclaration) member;
+ changeMethod(method);
+ }
+ }
+ }
+ }
+
+ private static void changeMethod(MethodDeclaration n) {
+ // change the name of the method to upper case
+ n.setName(n.getName().toUpperCase());
+
+ // create the new parameter
+ Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");
+
+ // add the parameter to the method
+ ASTHelper.addParameter(n, newArg);
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodPrinter.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodPrinter.java
new file mode 100644
index 000000000..348a52482
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/MethodPrinter.java
@@ -0,0 +1,42 @@
+package com.github.javaparser.junit;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.io.FileInputStream;
+
+public class MethodPrinter {
+
+ public static void main(String[] args) throws Exception {
+ // creates an input stream for the file to be parsed
+ FileInputStream in = new FileInputStream("test.java");
+
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ } finally {
+ in.close();
+ }
+
+ // visit and print the methods names
+ new MethodVisitor().visit(cu, null);
+ }
+
+ /**
+ * Simple visitor implementation for visiting MethodDeclaration nodes.
+ */
+ private static class MethodVisitor extends VoidVisitorAdapter {
+
+ @Override
+ public void visit(MethodDeclaration n, Object arg) {
+ // here you can access the attributes of the method.
+ // this method will be called for all methods in this
+ // CompilationUnit, including inner class methods
+ System.out.println(n.getName());
+ super.visit(n, arg);
+ }
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/PrintingTheCompilationUnitToSystemOutputTest.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/PrintingTheCompilationUnitToSystemOutputTest.java
new file mode 100644
index 000000000..414975c32
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/PrintingTheCompilationUnitToSystemOutputTest.java
@@ -0,0 +1,13 @@
+package com.github.javaparser.junit;
+
+import org.junit.Test;
+
+public class PrintingTheCompilationUnitToSystemOutputTest {
+ @Test
+ public void printingTheCompilationUnitToSystemOutput() throws Exception {
+ try (TestFileToken f = new TestFileToken("test.java")) {
+ CuPrinter.main(new String[]{});
+ }
+ }
+
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/TestFileToken.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/TestFileToken.java
new file mode 100644
index 000000000..e3726ab2b
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/TestFileToken.java
@@ -0,0 +1,40 @@
+package com.github.javaparser.junit;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Creates a temporary test file that a sample can use. This way we don't have to rewrite the samples to fit them into
+ * these tests.
+ */
+public class TestFileToken implements AutoCloseable {
+ private final String filename;
+
+ public TestFileToken(String filename) {
+ this.filename = filename;
+ try {
+ try (InputStream i = getClass().getResourceAsStream("TestFile.java"); OutputStream o = new FileOutputStream(filename)) {
+ assertNotNull(i);
+ IOUtils.copy(i, o);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Override
+ public void close() {
+ boolean deleted = new File(filename).delete();
+ assertTrue(deleted);
+ }
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/VisitingClassMethodsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/VisitingClassMethodsTest.java
new file mode 100644
index 000000000..0a0751a2c
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/VisitingClassMethodsTest.java
@@ -0,0 +1,12 @@
+package com.github.javaparser.junit;
+
+import org.junit.Test;
+
+public class VisitingClassMethodsTest {
+ @Test
+ public void testCode() throws Exception {
+ try (TestFileToken f = new TestFileToken("test.java")) {
+ MethodPrinter.main(new String[]{});
+ }
+ }
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/D.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/D.java
new file mode 100644
index 000000000..0852da829
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/D.java
@@ -0,0 +1,9 @@
+package com.github.javaparser.junit.removenode;
+
+public class D {
+
+ public int foo(int e) {
+ int a = 20;
+ return a;
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest.java
new file mode 100644
index 000000000..501ee09c5
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest.java
@@ -0,0 +1,49 @@
+package com.github.javaparser.junit.removenode;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.VariableDeclarator;
+import com.github.javaparser.ast.visitor.ModifierVisitorAdapter;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+public class GitHubTest {
+
+ public static void main(String... args) throws Exception {
+
+ FileInputStream file1 = new FileInputStream("forGitHubTest.java");
+ CompilationUnit cu = getCompilationUnit(file1);
+ String result = cu.toString();
+ new MyVisitor().visit(cu, null);
+ System.out.println(cu.toString());
+
+ }
+
+ public static CompilationUnit getCompilationUnit(InputStream in) {
+ try {
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ return cu;
+ } finally {
+ in.close();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
+
+class MyVisitor extends ModifierVisitorAdapter {
+ @Override
+ public Node visit(VariableDeclarator declarator, Object args) {
+ if (declarator.getId().getName().equals("a") && declarator.getInit().toString().equals("20")) {
+ return null;
+ }
+ return declarator;
+ }
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest_2.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest_2.java
new file mode 100644
index 000000000..f194ee60c
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/GitHubTest_2.java
@@ -0,0 +1,71 @@
+package com.github.javaparser.junit.removenode;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.VariableDeclarator;
+import com.github.javaparser.ast.expr.VariableDeclarationExpr;
+import com.github.javaparser.ast.stmt.ExpressionStmt;
+import com.github.javaparser.ast.visitor.ModifierVisitorAdapter;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+public class GitHubTest_2 {
+
+ public static void main(String... args) throws Exception {
+ FileInputStream file1 = new FileInputStream("forGitHubTest.java");
+ CompilationUnit cu = getCompilationUnit(file1);
+ String result = cu.toString();
+ new MyVisitor_2().visit(cu, null);
+ System.out.println(cu.toString());
+ }
+
+ public static CompilationUnit getCompilationUnit(InputStream in) {
+ try {
+ CompilationUnit cu;
+ try {
+ // parse the file
+ cu = JavaParser.parse(in);
+ return cu;
+ } finally {
+ in.close();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
+
+class MyVisitor_2 extends ModifierVisitorAdapter {
+
+ @Override
+ public Node visit(ExpressionStmt stmt, Object args) {
+ super.visit(stmt, args);
+ if (stmt.getExpression() == null) {
+ return null;
+ }
+ return stmt;
+ }
+
+ @Override
+ public Node visit(VariableDeclarationExpr declarationExpr, Object args) {
+ super.visit(declarationExpr, args);
+
+ if (declarationExpr.getVars().isEmpty()) {
+ return null;
+ }
+
+ return declarationExpr;
+ }
+
+ @Override
+ public Node visit(VariableDeclarator declarator, Object args) {
+ if (declarator.getId().getName().equals("a") && declarator.getInit().toString().equals("20")) {
+ return null;
+ }
+ return declarator;
+ }
+
+} \ No newline at end of file
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/RemoveDeleteNodeFromAst.java b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/RemoveDeleteNodeFromAst.java
new file mode 100644
index 000000000..1399e74fa
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/junit/removenode/RemoveDeleteNodeFromAst.java
@@ -0,0 +1,20 @@
+package com.github.javaparser.junit.removenode;
+
+import com.github.javaparser.junit.TestFileToken;
+import org.junit.Test;
+
+public class RemoveDeleteNodeFromAst {
+ @Test
+ public void testCode1() throws Exception {
+ try (TestFileToken f = new TestFileToken("forGitHubTest.java")) {
+ GitHubTest.main();
+ }
+ }
+
+ @Test
+ public void testCode2() throws Exception {
+ try (TestFileToken f = new TestFileToken("forGitHubTest.java")) {
+ GitHubTest_2.main();
+ }
+ }
+}
diff --git a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/dumping_scenarios.story b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/dumping_scenarios.story
index 3af36de13..7e3c557ef 100644
--- a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/dumping_scenarios.story
+++ b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/dumping_scenarios.story
@@ -181,3 +181,47 @@ class A {
}
}
}
+
+Scenario: we can parse blocks
+Given the block:
+{
+ a=2;
+ b=3;
+}
+When the block is parsed by the Java parser
+Then it is dumped to:
+{
+ a = 2;
+ b = 3;
+}
+
+Scenario: we can parse statements
+Given the statement:
+while (true) {
+}
+When the statement is parsed by the Java parser
+Then it is dumped to:
+while (true) {
+}
+
+Scenario: we can parse imports
+Given the import:
+import static a.b.c.Abc.*;
+When the import is parsed by the Java parser
+Then it is dumped to:
+import static a.b.c.Abc.*;
+
+Scenario: we can parse annotations
+Given the annotation:
+@Abc
+When the annotation is parsed by the Java parser
+Then it is dumped to:
+@Abc
+
+Scenario: we can parse body declarations
+Given the body declaration:
+private static final int x = 20;
+When the body declaration is parsed by the Java parser
+Then it is dumped to:
+private static final int x = 20;
+
diff --git a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/parsing_scenarios.story b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/parsing_scenarios.story
index 660931b99..d06e98c05 100644
--- a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/parsing_scenarios.story
+++ b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/parsing_scenarios.story
@@ -369,7 +369,7 @@ When I take the PackageDeclaration
Then the package name is com.github.javaparser.bdd
-Scenario: Strings with unescaped newlines are NOT parsed correctly
+Scenario: Strings with unescaped newlines are illegal (issue 211)
Given the class:
class A {
public void helloWorld(String greeting, String name) {
@@ -379,6 +379,16 @@ class A {
}
Then the Java parser cannot parse it because of lexical errors
+Scenario: Chars with unescaped newlines are illegal (issue 211)
+Given the class:
+class A {
+ public void helloWorld(String greeting, String name) {
+ return '
+';
+ }
+}
+Then the Java parser cannot parse it because of lexical errors
+
Scenario: Diamond Operator information is exposed
Given a CompilationUnit
diff --git a/javaparser-testing/src/test/resources/com/github/javaparser/junit/TestFile.java b/javaparser-testing/src/test/resources/com/github/javaparser/junit/TestFile.java
new file mode 100644
index 000000000..d89e7e627
--- /dev/null
+++ b/javaparser-testing/src/test/resources/com/github/javaparser/junit/TestFile.java
@@ -0,0 +1,16 @@
+package com.github.javaparser.junit;
+
+public class TestFile {
+ public int foo(int e) {
+ int a = 20;
+ return a;
+ }
+
+ public void abc() {
+
+ }
+
+ public int def() {
+ return 10;
+ }
+}