代码生成器是干啥用的,谁用过

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码生成器是干啥用的,谁用过相关的知识,希望对你有一定的参考价值。

这个算是一个插件吗,比如说我用的是VS2010,他直接就可以用吗?还是单独的?

代码生成器是按照特定编码规范输出代码的软件,可以直接生成项目,也可以单页生成

比如动软,生成项目可以选择vs版本的,其实嘛,动软挺垃圾的,不如你去学学orm框架,很简单的,比动软的三层好得多追问

那还是自己敲代码比较好?

追答

也不是吧,自己敲的可能注意不到一些安全,规范这些的,生成的代码可能这方面好些吧,只是生成器的话,你数据库加了字段,就改的麻烦了,基本都会自己改一些

参考技术A package utils;

import pojo.Page;
import pojo.UIElement;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static utils.XmlUtils.loadXml;

public class GenerateJave
public static ArrayList<Page> pages;
static
pages = loadXml("C:\\workspace\\CommonSubject\\src\\test\\resources\\UILibrary.xml");


public static String getPageWantPageVar(String wantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
return captureName(p.getVar());


return null;


public static String getPageWantPageKeyWord(String wantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
return p.getKeyword();


return null;


public static String getEleWantDes(String wantPageKeyWord,String eleWantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
List<UIElement> uiElementList = p.getUiElementList();
for (UIElement u:uiElementList)
if (u.getKeyword().contains(eleWantPageKeyWord))
return u.getDes();





return null;


public static String getEleWantVar(String wantPageKeyWord,String eleWantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
List<UIElement> uiElementList = p.getUiElementList();
for (UIElement u:uiElementList)
if (u.getKeyword().contains(eleWantPageKeyWord))
return captureName(u.getVar());





return null;


public static String getEleKeyword(String wantPageKeyWord,String eleWantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
List<UIElement> uiElementList = p.getUiElementList();
for (UIElement u:uiElementList)
if (u.getKeyword().contains(eleWantPageKeyWord))
return u.getKeyword();





return null;


public static String getEleWantUIElement(String wantPageKeyWord,String eleWantPageKeyWord)
for (Page p: pages)
if (p.getKeyword().contains(wantPageKeyWord))
List<UIElement> uiElementList = p.getUiElementList();
for (UIElement u:uiElementList)
if (u.getKeyword().contains(eleWantPageKeyWord))
return u.toString();





return null;


/**
* 将字符串的首字母转大写
* @param str 需要转换的字符串
* @return
*/
private static String captureName(String str)
// 进行字母的ascii编码前移,效率要高于截取字符串进行转换的操作
char[] cs=str.toCharArray();
cs[0]-=32;
return String.valueOf(cs);


public static void write1(String fileName,String content,String wantPageKeyWord,String eleWantPageKeyWord)

String projectDir= (System.getProperty("user.dir")+"\\src\\test\\java\\po\\"+fileName+".java");
File file = new File(projectDir);

String content1 = "package po;\n" +
"\n" +
"import org.openqa.selenium.By;\n" +
"\n" +
"import common.BasePage;\n"
+"import utils.GenerateJave;\n"+
"import java.io.File;\n"+
"import org.testng.Assert;";
String contentPageDes = "\n/**\n" +
" * " +getPageWantPageKeyWord(wantPageKeyWord) +"\n" +
" */\n";
String content2 = "class "+getPageWantPageVar(wantPageKeyWord)+" extends BasePage \n";

String contenteEd = "\n";

String method = "";
if(getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("输入"))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void modifyValueByJs"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"(String value) \n" +
"\t\tinput(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\", \""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\",value);\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("点击按钮 "))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void click"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"() \n" +
"\t\tclick(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\", \""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("断言"))
method ="\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void assertEleFor"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"()\n" +
"\t\tassertEleContainsText(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\",\"\");\n" +
"\t\tassertInfoCountBigDengYuOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\tassertInfoCountBigOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\tassertInfoCountOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("点击按钮|验证下载"))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void assertDownloadFor"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"()\n" +
"\t\tlong beforeFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tclick(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\twaitTime(3000);\n" +
"\t\tlong afterFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tswitchWindowByTitle(\"企业信息管理\");\n" +
"\t\tString result = (afterFilesNum - beforeFilesNum) + \"\";\n" +
"\t\tAssert.assertEquals(\"1\",result);\n" +
"\t";

try (FileOutputStream fop = new FileOutputStream(file))

// if file doesn't exists, then create it
if (!file.exists()&& !file.isDirectory())
file.createNewFile();


// get the content in bytes
byte[] contentInBytePageDes = contentPageDes.getBytes();
byte[] contentInByteMethod = method.getBytes();

byte[] contentInByte1 = content1.getBytes();
byte[] contentInByte2 = content2.getBytes();
byte[] contentInByteEnd = contenteEd.getBytes();

// byte[] contentInBytes = content.getBytes();
fop.write(contentInByte1);
fop.write(contentInBytePageDes);
fop.write(contentInByte2);
// fop.write(contentInByte1);
fop.write(contentInByteMethod);
fop.write(contentInByteEnd);

// fop.write(contentInBytes);
fop.flush();
fop.close();

System.out.println("Done");

catch (Exception e)
e.printStackTrace();



public static void write(String fileName,String content,String wantPageKeyWord,String eleWantPageKeyWord)

String projectDir= (System.getProperty("user.dir")+"\\src\\test\\java\\po\\"+fileName+".java");
File file = new File(projectDir);

String content1 = "package po;\n" +
"\n" +
"import org.openqa.selenium.By;\n" +
"\n" +
"import common.BasePage;\n"
+"import utils.GenerateJave;\n"+
"import java.io.File;\n"+
"import org.testng.Assert;";
String contentPageDes = "\n/**\n" +
" * " +getPageWantPageKeyWord(wantPageKeyWord) +"\n" +
" */\n";
String content2 = "class "+getPageWantPageVar(wantPageKeyWord)+" extends BasePage \n";

String contenteEd = "\n";

String method = "";
if(getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("输入"))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void modifyValueByJs"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"(String value) \n" +
"\t\tinput(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\", \""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\",value);\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("点击按钮 "))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void click"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"() \n" +
"\t\tclick(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\", \""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("断言"))
method ="\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void assertEleFor"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"()\n" +
"\t\tassertEleContainsText(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\",\"\");\n" +
"\t\tassertInfoCountBigDengYuOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\tassertInfoCountBigOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\tassertInfoCountOne(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword(wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\n";
else if (getEleWantDes( wantPageKeyWord,eleWantPageKeyWord).contains("点击按钮|验证下载"))
method = "\n\t/**\n" +
"\t * \t\t"+getEleWantDes( wantPageKeyWord,eleWantPageKeyWord)+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t * \t\t"+getEleWantUIElement(wantPageKeyWord,eleWantPageKeyWord)+"\n" +
"\t */\n" +
"\tpublic void assertDownloadFor"+getEleWantVar(wantPageKeyWord,eleWantPageKeyWord)+"()\n" +
"\t\tlong beforeFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tclick(\""+getPageWantPageKeyWord(wantPageKeyWord)+"\",\""+getEleKeyword( wantPageKeyWord,eleWantPageKeyWord)+"\");\n" +
"\t\twaitTime(3000);\n" +
"\t\tlong afterFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tswitchWindowByTitle(\"企业信息管理\");\n" +
"\t\tString result = (afterFilesNum - beforeFilesNum) + \"\";\n" +
"\t\tAssert.assertEquals(\"1\",result);\n" +
"\t";

try (FileOutputStream fop = new FileOutputStream(file))

// if file doesn't exists, then create it
if (!file.exists()&& !file.isDirectory())
file.createNewFile();


// get the content in bytes
byte[] contentInBytePageDes = contentPageDes.getBytes();
byte[] contentInByteMethod = method.getBytes();

byte[] contentInByte1 = content1.getBytes();
byte[] contentInByte2 = content2.getBytes();
byte[] contentInByteEnd = contenteEd.getBytes();

// byte[] contentInBytes = content.getBytes();
fop.write(contentInByte1);
fop.write(contentInBytePageDes);
fop.write(contentInByte2);
// fop.write(contentInByte1);
fop.write(contentInByteMethod);
fop.write(contentInByteEnd);

// fop.write(contentInBytes);
fop.flush();
fop.close();

System.out.println("Done");

catch (Exception e)
e.printStackTrace();



// public static void main(String[] args)
//// write("11","","保险页","历史账查单查询页-保单号输入框");
//// write("11","","保险页","历史账查单查询页-查询按钮");
// write("11","","保险页","历史账查单查询页-结算详情页-汇总税费明细链接");
//
// String projectDir= (System.getProperty("user.dir")+"\\po\\"+"11"+".java");
// System.out.println(projectDir);
//

public static void createFile(String fileName)
String projectDir= (System.getProperty("user.dir")+"\\src\\test\\java\\po\\"+fileName+".java");
File file = new File(projectDir);
//
//
// String content1 = "package po;\n" +
// "\n" +
// "import org.openqa.selenium.By;\n" +
// "\n" +
// "import common.BasePage;\n"
// +"import utils.GenerateJave;\n"+
// "import java.io.File;\n"+
// "import org.testng.Assert;";

try (FileOutputStream fop = new FileOutputStream(file))

if (!file.exists()&& !file.isDirectory())
file.createNewFile();

// byte[] contentInByte1 = content1.getBytes();
// fop.write(contentInByte1);
// fop.flush();
// fop.close();
// System.out.println("Done");
catch (Exception e)
e.printStackTrace();



public static void writeMethod(String wantPageKey)
String pageVar = null;
for (Page p :pages)
if (p.getKeyword().contains(wantPageKey))
/**
* 页面关键字页面描述
*/
String pageKey = p.getKeyword();
/**
* 页面变量
*/
pageVar = p.getVar();
createFile(pageKey+"Page");
String head = "package po;\n" +
"\n" +
"import org.openqa.selenium.By;\n" +
"\n" +
"import common.BasePage;\n"
+"import utils.GenerateJave;\n"+
"import java.io.File;\n"+
"import org.testng.Assert;";
appendMethod(pageVar+"Page",head);

String method = "";
String contentClass = "\n/**\n" +
" * " +pageKey +"\n" +
" */\n"+"class "+getPageWantPageVar(pageKey)+" extends BasePage \n";
appendMethod(pageVar+"Page",contentClass);
List<UIElement> uiElementList = p.getUiElementList();
for (UIElement u:uiElementList)
/**
* 元素描述
*/
String eleWantDes = getEleWantDes(pageKey, u.getKeyword());
/**
* 元素信息
*/
String eleWantUIElement = getEleWantUIElement(pageKey, u.getKeyword());
/**
* 元素变量
*/
String eleWantVar = getEleWantVar(pageKey, u.getKeyword());
/**
* 元素关键字
*/
String eleKeyword = getEleKeyword(pageKey, u.getKeyword());
if(eleWantDes.contains("输入"))
method = "\n\t/**\n" +
"\t * \t\t"+eleWantDes+eleKeyword+"\n" +
"\t * \t\t"+eleWantUIElement+"\n" +
"\t */\n" +
"\tpublic void modifyValueByJs"+eleWantVar+"(String value) \n" +
"\t\tinput(\""+pageKey+"\", \""+eleKeyword+"\",value);\n" +
"\t\n";
else if (eleWantDes.contains("点击按钮 "))
method = "\n\t/**\n" +
"\t * \t\t"+eleWantDes+eleKeyword+"\n" +
"\t * \t\t"+eleWantUIElement+"\n" +
"\t */\n" +
"\tpublic void click"+eleWantVar+"() \n" +
"\t\tclick(\""+pageKey+"\", \""+eleKeyword+"\");\n" +
"\t\n";
else if (eleWantDes.contains("断言"))
method ="\n\t/**\n" +
"\t * \t\t"+eleWantDes+eleKeyword+"\n" +
"\t * \t\t"+eleWantUIElement+"\n" +
"\t */\n" +
"\tpublic void assertEleFor"+eleWantVar+"()\n" +
"\t\tassertEleContainsText(\""+pageKey+"\",\""+eleKeyword+"\",\"\");\n" +
"\t\tassertInfoCountBigDengYuOne(\""+pageKey+"\",\""+eleKeyword+"\");\n" +
"\t\tassertInfoCountBigOne(\""+pageKey+"\",\""+eleKeyword+"\");\n" +
"\t\tassertInfoCountOne(\""+pageKey+"\",\""+eleKeyword+"\");\n" +
"\t\n";
else if (eleWantDes.contains("点击按钮|验证下载"))
method = "\n\t/**\n" +
"\t * \t\t"+eleWantDes+eleKeyword+"\n" +
"\t * \t\t"+eleWantUIElement+"\n" +
"\t */\n" +
"\tpublic void assertDownloadFor"+eleWantVar+"()\n" +
"\t\tlong beforeFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tclick(\""+pageKey+"\",\""+eleKeyword+"\");\n" +
"\t\twaitTime(3000);\n" +
"\t\tlong afterFilesNum = GenerateJave.getList(new File(\"C:\\\\workspace\\\\CommonSubject\\\\src\\\\test\\\\resources\"));\n" +
"\t\tswitchWindowByTitle(\"企业信息管理\");\n" +
"\t\tString result = (afterFilesNum - beforeFilesNum) + \"\";\n" +
"\t\tAssert.assertEquals(\"1\",result);\n" +
"\t";

appendMethod(pageVar+"Page",method);



appendMethod( pageVar+"Page","\n");


public static void main(String[] args)
writeMethod("保险页");



/**
* 获取文件个数
* @param f
* @return
*/
public static long getList(File f)
long size = 0;
File[] files = f.listFiles();
size = files.length;
for (int i = 0; i < files.length; i++)
if (files[i].isDirectory())
size = size + getList(files[i]);


return size;

/**
????* B方法追加文件:使用FileWriter
????*/
public static void appendMethod(String fileName, String content)
String projectDir= (System.getProperty("user.dir")+"\\src\\test\\java\\po\\"+fileName+".java");

BufferedWriter out = null;
try
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(projectDir, true)));
out.write(content);
catch (Exception e)
e.printStackTrace();
finally
try
out.close();
catch (IOException e)
e.printStackTrace();



参考技术B 就是帮你生成一些可以快捷的代码,比如生成增删改查的方法:自己再到里面去改些参数。 参考技术C 代码生成器就是帮你生成代码,省去你的好多时间,比如(Models,数据库的一般操作) 参考技术D 生成代码的?

这个“1342177280”标志是干啥用的?

【中文标题】这个“1342177280”标志是干啥用的?【英文标题】:For whats this "1342177280" flag for?这个“1342177280”标志是干什么用的? 【发布时间】:2020-11-04 03:02:32 【问题描述】:

我有一些代码 sn-p 用于打开带有以下标志的 whatsapp

  Intent intent = new Intent();
            intent.setClassName("com.whatsapp", "com.whatsapp.HomeActivity");
            intent.addFlags(1342177280);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivityForResult(intent, 9);

上面的 sn-p 显示错误 1342177280 如下所示

Error image here

我不明白这个标志 1342177280 是关于什么的,知道吗?

【问题讨论】:

这些是标志 OR'ed 在一起 (developer.android.com/reference/android/content/…); 10100000000000000000000000000000 二进制。 (也许:FLAG_ACTIVITY_NEW_TASK 和 FLAG_RECEIVER_REGISTERED_ONLY) 警告是因为您使用的是显式整数,而不是在“已批准”标志列表中绘制。 【参考方案1】:

我正在扩展 codebod 的评论

警告是因为您使用的是显式整数,而不是在“已批准”标志列表中绘制。

Intent 的 addFlags() 方法接受一个在 android 框架中预定义为常量的 int 参数。预定义的常量具有相应的整数值。您看到该错误是因为您输入的整数值与任何预定义值都不匹配。

查看this 文档以查看 Intent 的 addFlags() 方法可以接受的有效参数列表。

【讨论】:

以上是关于代码生成器是干啥用的,谁用过的主要内容,如果未能解决你的问题,请参考以下文章

这个“1342177280”标志是干啥用的?

pycharm是干啥用的?

手机里的android文件夹是干啥用的

vscode是干啥用的

PHP 的 serialize() 函数是干啥用的?

eclipse菜单里面的clean和build是干啥用的