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.printer.lexicalpreservation.transformations.ast.body; 23 24 import com.github.javaparser.ast.Modifier; 25 import com.github.javaparser.ast.body.MethodDeclaration; 26 import com.github.javaparser.ast.body.Parameter; 27 import com.github.javaparser.ast.expr.SimpleName; 28 import com.github.javaparser.ast.type.ArrayType; 29 import com.github.javaparser.ast.type.PrimitiveType; 30 import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; 31 import org.junit.Test; 32 33 import java.io.IOException; 34 import java.util.EnumSet; 35 36 /** 37 * Transforming MethodDeclaration and verifying the LexicalPreservation works as expected. 38 */ 39 public class MethodDeclarationTransformationsTest extends AbstractLexicalPreservingTest { 40 41 protected MethodDeclaration consider(String code) { 42 considerCode("class A { " + code + " }"); 43 return cu.getType(0).getMembers().get(0).asMethodDeclaration(); 44 } 45 46 // Name 47 48 @Test 49 public void settingName() throws IOException { 50 MethodDeclaration it = consider("void A(){}"); 51 it.setName("B"); 52 assertTransformedToString("void B(){}", it); 53 } 54 55 // JavaDoc 56 57 // Modifiers 58 59 @Test 60 public void addingModifiers() throws IOException { 61 MethodDeclaration it = consider("void A(){}"); 62 it.setModifiers(EnumSet.of(Modifier.PUBLIC)); 63 assertTransformedToString("public void A(){}", it); 64 } 65 66 @Test 67 public void removingModifiers() throws IOException { 68 MethodDeclaration it = consider("public void A(){}"); 69 it.setModifiers(EnumSet.noneOf(Modifier.class)); 70 assertTransformedToString("void A(){}", it); 71 } 72 73 @Test 74 public void replacingModifiers() throws IOException { 75 MethodDeclaration it = consider("public void A(){}"); 76 it.setModifiers(EnumSet.of(Modifier.PROTECTED)); 77 assertTransformedToString("protected void A(){}", it); 78 } 79 80 // Parameters 81 82 @Test 83 public void addingParameters() throws IOException { 84 MethodDeclaration it = consider("void foo(){}"); 85 it.addParameter(PrimitiveType.doubleType(), "d"); 86 assertTransformedToString("void foo(double d){}", it); 87 } 88 89 @Test 90 public void removingOnlyParameter() throws IOException { 91 MethodDeclaration it = consider("public void foo(double d){}"); 92 it.getParameters().remove(0); 93 assertTransformedToString("public void foo(){}", it); 94 } 95 96 @Test 97 public void removingFirstParameterOfMany() throws IOException { 98 MethodDeclaration it = consider("public void foo(double d, float f){}"); 99 it.getParameters().remove(0); 100 assertTransformedToString("public void foo(float f){}", it); 101 } 102 103 @Test 104 public void removingLastParameterOfMany() throws IOException { 105 MethodDeclaration it = consider("public void foo(double d, float f){}"); 106 it.getParameters().remove(1); 107 assertTransformedToString("public void foo(double d){}", it); 108 } 109 110 @Test 111 public void replacingOnlyParameter() throws IOException { 112 MethodDeclaration it = consider("public void foo(float f){}"); 113 it.getParameters().set(0, new Parameter(new ArrayType(PrimitiveType.intType()), new SimpleName("foo"))); 114 assertTransformedToString("public void foo(int[] foo){}", it); 115 } 116 117 // ThrownExceptions 118 119 // Body 120 121 // Annotations 122 } 123