Skip to content

Commit

Permalink
Better setter + some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepSnowNeeL committed Jul 21, 2016
1 parent 58441c9 commit 982be37
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 28 deletions.
Expand Up @@ -33,6 +33,9 @@
import com.github.javaparser.ast.Modifier; import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.AssignExpr.Operator;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithJavaDoc; import com.github.javaparser.ast.nodeTypes.NodeWithJavaDoc;
import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; import com.github.javaparser.ast.nodeTypes.NodeWithModifiers;
import com.github.javaparser.ast.nodeTypes.NodeWithType; import com.github.javaparser.ast.nodeTypes.NodeWithType;
Expand Down Expand Up @@ -134,7 +137,6 @@ public FieldDeclaration setModifiers(EnumSet<Modifier> modifiers) {
return this; return this;
} }



@Override @Override
public FieldDeclaration setType(Type type) { public FieldDeclaration setType(Type type) {
this.type = type; this.type = type;
Expand All @@ -155,24 +157,31 @@ public JavadocComment getJavaDoc() {
return null; return null;
} }



/** /**
* Create a getter for this field, <b>will only work if this field declares only 1 identifier and if this field is * Create a getter for this field, <b>will only work if this field declares only 1 identifier and if this field is
* already added to a ClassOrInterfaceDeclaration</b> * already added to a ClassOrInterfaceDeclaration</b>
* *
* @return the {@link MethodDeclaration} created * @return the {@link MethodDeclaration} created
* @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
* class or enum
*/ */
public MethodDeclaration createGetter() { public MethodDeclaration createGetter() {
if (getVariables().size() != 1) if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
ClassOrInterfaceDeclaration parent = getParentNodeOfType(ClassOrInterfaceDeclaration.class); ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
if (parent == null) EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
throw new IllegalStateException( throw new IllegalStateException(
"You can use this only when the field is attached to a ClassOrInterfaceDeclaration"); "You can use this only when the field is attached to a class or an enum");


String fieldName = getVariables().get(0).getId().getName(); String fieldName = getVariables().get(0).getId().getName();
fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
MethodDeclaration getter = parent.addMethod("get" + fieldName, EnumSet.of(Modifier.PUBLIC)).setType(getType()); MethodDeclaration getter = null;
if (parentClass != null)
getter = parentClass.addMethod("get" + fieldName, EnumSet.of(Modifier.PUBLIC));
else
getter = parentEnum.addMethod("get" + fieldName, EnumSet.of(Modifier.PUBLIC));
getter.setType(getType());
BlockStmt blockStmt = new BlockStmt(); BlockStmt blockStmt = new BlockStmt();
getter.setBody(blockStmt); getter.setBody(blockStmt);
ReturnStmt r = new ReturnStmt(ASTHelper.createNameExpr(fieldName)); ReturnStmt r = new ReturnStmt(ASTHelper.createNameExpr(fieldName));
Expand All @@ -185,23 +194,32 @@ public MethodDeclaration createGetter() {
* already added to a ClassOrInterfaceDeclaration</b> * already added to a ClassOrInterfaceDeclaration</b>
* *
* @return the {@link MethodDeclaration} created * @return the {@link MethodDeclaration} created
* @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
* class or enum
*/ */
public MethodDeclaration createSetter() { public MethodDeclaration createSetter() {
if (getVariables().size() != 1) if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
ClassOrInterfaceDeclaration parent = getParentNodeOfType(ClassOrInterfaceDeclaration.class); ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
if (parent == null) EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
throw new IllegalStateException( throw new IllegalStateException(
"You can use this only when the field is attached to a ClassOrInterfaceDeclaration"); "You can use this only when the field is attached to a class or an enum");

String fieldName = getVariables().get(0).getId().getName(); String fieldName = getVariables().get(0).getId().getName();
fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());


MethodDeclaration setter = parent.addMethod("set" + fieldName, EnumSet.of(Modifier.PUBLIC)) MethodDeclaration setter = null;
.setType(ASTHelper.VOID_TYPE); if (parentClass != null)
setter = parentClass.addMethod("set" + fieldName, EnumSet.of(Modifier.PUBLIC));
else
setter = parentEnum.addMethod("set" + fieldName, EnumSet.of(Modifier.PUBLIC));
setter.setType(ASTHelper.VOID_TYPE);
setter.getParameters().add(new Parameter(getType(), new VariableDeclaratorId(fieldName))); setter.getParameters().add(new Parameter(getType(), new VariableDeclaratorId(fieldName)));
BlockStmt blockStmt2 = new BlockStmt(); BlockStmt blockStmt2 = new BlockStmt();
setter.setBody(blockStmt2); setter.setBody(blockStmt2);
ASTHelper.addStmt(blockStmt2, ASTHelper.createNameExpr("this." + fieldName + " = " + fieldName + ";")); ASTHelper.addStmt(blockStmt2,
new AssignExpr(new NameExpr("this." + fieldName), new NameExpr(fieldName), Operator.assign));
return setter; return setter;
} }


Expand Down
@@ -1,25 +1,91 @@
package com.github.javaparser.junit.builders; package com.github.javaparser.junit.builders;


import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test;


import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.body.VariableDeclaratorId;
import com.github.javaparser.ast.stmt.ReturnStmt;


public class FieldDeclarationBuildersTest { public class FieldDeclarationBuildersTest {
CompilationUnit cu; CompilationUnit cu;

private ClassOrInterfaceDeclaration testClass;
@Before private EnumDeclaration testEnum;
public void setup() {
cu = new CompilationUnit(); @Before
} public void setup() {

cu = new CompilationUnit();
@After testClass = cu.addClass("testClass");
public void teardown() { testEnum = cu.addEnum("testEnum");
cu = null; }
}
/* @After
FieldDeclaration public void teardown() {
public MethodDeclaration createGetter() { cu = null;
public MethodDeclaration createSetter() { testClass = null;
*/ testEnum = null;
}

@Test(expected = IllegalStateException.class)
public void testOrphanFieldGetter() {
new FieldDeclaration().createGetter();
}

@Test(expected = IllegalStateException.class)
public void testOrphanFieldSetter() {
new FieldDeclaration().createSetter();
}

@Test
public void testCreateGetterInAClass() {
testClass.addPrivateField(int.class, "myField").createGetter();
assertEquals(2, testClass.getMembers().size());
assertEquals(MethodDeclaration.class, testClass.getMembers().get(1).getClass());
List<MethodDeclaration> methodsWithName = testClass.getMethodsWithName("getMyField");
assertEquals(1, methodsWithName.size());
MethodDeclaration getter = methodsWithName.get(0);
assertEquals("getMyField", getter.getName());
assertEquals("int", getter.getType().toString());
assertEquals(ReturnStmt.class, getter.getBody().getStmts().get(0).getClass());
}

@Test
public void testCreateSetterInAClass() {
testClass.addPrivateField(int.class, "myField").createSetter();
// TODO asserts
}

@Test
public void testCreateGetterInEnum() {
testEnum.addPrivateField(int.class, "myField").createGetter();
// TODO asserts
}

@Test
public void testCreateSetterInEnum() {
testEnum.addPrivateField(int.class, "myField").createSetter();
// TODO asserts
}

@Test(expected = IllegalStateException.class)
public void testCreateGetterWithANonValidField() {
FieldDeclaration myPrivateField = testClass.addPrivateField(int.class, "myField");
myPrivateField.getVariables().add(new VariableDeclarator(new VariableDeclaratorId("secondField")));
myPrivateField.createGetter();
}
/*
* FieldDeclaration
* public MethodDeclaration createGetter() {
* public MethodDeclaration createSetter() {
*/
} }

0 comments on commit 982be37

Please sign in to comment.