本文整理汇总了Java中com.github.javaparser.ast.stmt.BlockStmt.addStatement方法的典型用法代码示例。如果您正苦于以下问题:Java BlockStmt.addStatement方法的具体用法?Java BlockStmt.addStatement怎么用?Java BlockStmt.addStatement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.javaparser.ast.stmt.BlockStmt
的用法示例。
在下文中一共展示了BlockStmt.addStatement方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createGetter
import com.github.javaparser.ast.stmt.BlockStmt; //导入方法依赖的package包/类
/**
* 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>
*
* @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() {
if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
throw new IllegalStateException(
"You can use this only when the field is attached to a class or an enum");
VariableDeclarator variable = getVariables().get(0);
String fieldName = variable.getId().getName();
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
final MethodDeclaration getter;
if (parentClass != null)
getter = parentClass.addMethod("get" + fieldNameUpper, PUBLIC);
else
getter = parentEnum.addMethod("get" + fieldNameUpper, PUBLIC);
getter.setType(variable.getType());
BlockStmt blockStmt = new BlockStmt();
getter.setBody(blockStmt);
blockStmt.addStatement(new ReturnStmt(name(fieldName)));
return getter;
}
示例2: createSetter
import com.github.javaparser.ast.stmt.BlockStmt; //导入方法依赖的package包/类
/**
* Create a setter 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>
*
* @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() {
if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
throw new IllegalStateException(
"You can use this only when the field is attached to a class or an enum");
VariableDeclarator variable = getVariables().get(0);
String fieldName = variable.getId().getName();
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
final MethodDeclaration setter;
if (parentClass != null)
setter = parentClass.addMethod("set" + fieldNameUpper, PUBLIC);
else
setter = parentEnum.addMethod("set" + fieldNameUpper, PUBLIC);
setter.setType(VOID_TYPE);
setter.getParameters().add(new Parameter(variable.getType(), new VariableDeclaratorId(fieldName)));
BlockStmt blockStmt2 = new BlockStmt();
setter.setBody(blockStmt2);
blockStmt2.addStatement(new AssignExpr(new NameExpr("this." + fieldName), new NameExpr(fieldName), Operator.assign));
return setter;
}
示例3: regenerateEquals
import com.github.javaparser.ast.stmt.BlockStmt; //导入方法依赖的package包/类
private void regenerateEquals() {
importIf("java.util.Objects");
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration eq = new MethodDeclaration(modifiers, new PrimitiveType(Primitive.BOOLEAN), "equals");
eq.setModifiers(modifiers);
createOrFindMarkerAnnotation(eq, "java.lang.Override");
Parameter param = new Parameter(new ClassOrInterfaceType("Object"), "o");
eq.addParameter(param);
ClassOrInterfaceDeclaration rootType = getRootType();
rootType.addMember(eq);
BlockStmt block = new BlockStmt();
block.addStatement(JavaParser.parseStatement("if(o == this)\nreturn true;"));
block.addStatement(JavaParser.parseStatement("if(o == null || getClass() != o.getClass())\n\t\t\treturn false;"));
block.addStatement(JavaParser.parseStatement(getSimpleName() + " that = (" + getSimpleName() + ") o;"));
StringBuilder sb = new StringBuilder();
sb.append("return ");
int count = 0;
for(ColumnWrapper cw : m_allColumnWrappers) {
String propName = cw.getPropertyName();
String fieldPrefix = g().getFieldPrefix();
if(fieldPrefix != null) {
propName = fieldPrefix + propName;
}
sb.append("\t");
if(count++ > 0)
sb.append("&& ");
sb.append("Objects.equals(").append(propName).append(", that.").append(propName).append(")");
sb.append("\n");
}
sb.append(";");
System.out.println(sb.toString());
block.addStatement(JavaParser.parseStatement(sb.toString()));
eq.setBody(block);
}
示例4: regenerateHashcode
import com.github.javaparser.ast.stmt.BlockStmt; //导入方法依赖的package包/类
private void regenerateHashcode() {
importIf("java.util.Objects");
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration eq = new MethodDeclaration(modifiers, new PrimitiveType(Primitive.INT), "hashCode");
eq.setModifiers(modifiers);
createOrFindMarkerAnnotation(eq, "java.lang.Override");
ClassOrInterfaceDeclaration rootType = getRootType();
rootType.addMember(eq);
BlockStmt block = new BlockStmt();
eq.setBody(block);
StringBuilder sb = new StringBuilder();
sb.append("return Objects.hash(");
int count = 0;
for(ColumnWrapper cw : m_allColumnWrappers) {
String propName = cw.getPropertyName();
String fieldPrefix = g().getFieldPrefix();
if(fieldPrefix != null) {
propName = fieldPrefix + propName;
}
if(count++ > 0)
sb.append(", ");
sb.append(propName);
}
sb.append(");");
block.addStatement(JavaParser.parseStatement(sb.toString()));
}