ExpressionFunctionUtils { static BlockStmt getConstantExpressionBlockStmt(final String variableName, final Constant constant, final ClassOrInterfaceType returnedType, final LinkedHashMap<String, ClassOrInterfaceType> parameterNameTypeMap) { final BlockStmt toReturn = new BlockStmt(); VariableDeclarator variableDeclarator = new VariableDeclarator(); variableDeclarator.setType(returnedType); variableDeclarator.setName(variableName); final Object constantValue = constant.getValue(); if (constantValue instanceof String) { variableDeclarator.setInitializer(new StringLiteralExpr((String) constantValue)); } else { variableDeclarator.setInitializer(new NameExpr(constantValue.toString())); } VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(); variableDeclarationExpr.setVariables(NodeList.nodeList(variableDeclarator)); toReturn.addStatement(variableDeclarationExpr); return toReturn; } private  ExpressionFunctionUtils();   }
@Test public void getConstantExpressionBlockStmt() { String variableName = "VARIABLE_NAME"; Constant constant = constantSupplier.get(); ClassOrInterfaceType returnedType = parseClassOrInterfaceType(Double.class.getName()); BlockStmt retrieved = ExpressionFunctionUtils.getConstantExpressionBlockStmt(variableName, constant, returnedType, DEFAULT_PARAMETERTYPE_MAP); String expected = String.format("{\n" + " %1$s %2$s = %3$s;\n" + "}", Double.class.getName(), variableName, constant.getValue()); assertEquals(expected, retrieved.toString()); constant.setDataType(DataType.STRING); constant.setValue("STRING_VALUE"); returnedType = parseClassOrInterfaceType(String.class.getName()); retrieved = ExpressionFunctionUtils.getConstantExpressionBlockStmt(variableName, constant, returnedType, DEFAULT_PARAMETERTYPE_MAP); expected = String.format("{\n" + " %1$s %2$s = \"%3$s\";\n" + "}", String.class.getName(), variableName, constant.getValue()); assertEquals(expected, retrieved.toString()); }