1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22 package com.github.javaparser.ast.visitor; 23 24 import com.github.javaparser.JavaParser; 25 import com.github.javaparser.ast.NodeList; 26 import com.github.javaparser.ast.body.BodyDeclaration; 27 import com.github.javaparser.ast.body.VariableDeclarator; 28 import com.github.javaparser.ast.expr.Expression; 29 import com.github.javaparser.ast.expr.IntegerLiteralExpr; 30 import com.github.javaparser.ast.expr.StringLiteralExpr; 31 import org.junit.Test; 32 33 import static com.github.javaparser.JavaParser.parseExpression; 34 import static com.github.javaparser.utils.Utils.EOL; 35 import static org.junit.Assert.assertEquals; 36 37 public class ModifierVisitorTest { 38 @Test 39 public void makeSureParentListsCanBeModified() { 40 NodeList<StringLiteralExpr> list = new NodeList<>(); 41 list.add(new StringLiteralExpr("t")); 42 list.add(new StringLiteralExpr("a")); 43 list.add(new StringLiteralExpr("b")); 44 list.add(new StringLiteralExpr("c")); 45 46 list.accept(new ModifierVisitor<Void>() { 47 @Override 48 public Visitable visit(final StringLiteralExpr n, final Void arg) { 49 String v = n.getValue(); 50 51 list.addFirst(new StringLiteralExpr("extra " + v)); 52 list.remove(new StringLiteralExpr("t")); 53 54 if (v.equals("a")) { 55 return new StringLiteralExpr("x"); 56 } 57 if (v.equals("b")) { 58 return null; 59 } 60 61 return n; 62 } 63 }, null); 64 65 assertEquals("extra c", list.get(0).getValue()); 66 assertEquals("extra b", list.get(1).getValue()); 67 assertEquals("extra a", list.get(2).getValue()); 68 assertEquals("extra t", list.get(3).getValue()); 69 assertEquals("x", list.get(4).getValue()); 70 assertEquals("c", list.get(5).getValue()); 71 assertEquals(6, list.size()); 72 } 73 74 @Test 75 public void binaryExprReturnsLeftExpressionWhenRightSideIsRemoved() { 76 Expression expression = parseExpression("1+2"); 77 Visitable result = expression.accept(new ModifierVisitor<Void>() { 78 public Visitable visit(IntegerLiteralExpr integerLiteralExpr, Void arg) { 79 if (integerLiteralExpr.getValue().equals("1")) { 80 return null; 81 } 82 return integerLiteralExpr; 83 } 84 }, null); 85 assertEquals("2", result.toString()); 86 } 87 88 @Test 89 public void binaryExprReturnsRightExpressionWhenLeftSideIsRemoved() { 90 final Expression expression = parseExpression("1+2"); 91 final Visitable result = expression.accept(new ModifierVisitor<Void>() { 92 public Visitable visit(IntegerLiteralExpr integerLiteralExpr, Void arg) { 93 if (integerLiteralExpr.getValue().equals("2")) { 94 return null; 95 } 96 return integerLiteralExpr; 97 } 98 }, null); 99 assertEquals("1", result.toString()); 100 } 101 102 @Test 103 public void fieldDeclarationCantSurviveWithoutVariables() { 104 final BodyDeclaration<?> bodyDeclaration = JavaParser.parseBodyDeclaration("int x=1;"); 105 106 final Visitable result = bodyDeclaration.accept(new ModifierVisitor<Void>() { 107 public Visitable visit(VariableDeclarator x, Void arg) { 108 return null; 109 } 110 }, null); 111 112 assertEquals(null, result); 113 } 114 115 @Test 116 public void variableDeclarationCantSurviveWithoutVariables() { 117 final BodyDeclaration<?> bodyDeclaration = JavaParser.parseBodyDeclaration("void x() {int x=1;}"); 118 119 final Visitable result = bodyDeclaration.accept(new ModifierVisitor<Void>() { 120 public Visitable visit(VariableDeclarator x, Void arg) { 121 return null; 122 } 123 }, null); 124 125 assertEquals("void x() {" + EOL + "}", result.toString()); 126 } 127 } 128