[英]Distinguishing Literals from Variables/Symbols in Source Code
By literals, I means all the constants like 按字面意思,我指的是所有常量
Here 10 is integer literal, 10.5f is floating literal and Hello is a string literal However after trying something I am successful in some part of code. 这里10是整数文字,10.5f是浮点文字,Hello是字符串文字。但是尝试了一下之后,我在代码的某些部分中成功了。
int a = 10;
float b = 10.5f;
String all = "Hello";
String s = "my source program that i am reading from file";
String lines[] = s.split("\n"); //Break my program into lines
for(int i=0;i<lines.length;i++) {
if(lines[i].contains("="))
System.err.println(lines[i].substring(lines[i].indexOf("=")+1),lines[i].indexOf(";"));
}
but it also provides me the output with assignments like:- 但它也为我提供了如下输出:
Myapp a=new Myapp();
However I need to find only literals 但是我只需要找到文字
While there are better ways to approach this problem, a quick fix in your existing code would be to make a small tweak : 尽管有更好的方法来解决此问题,但现有代码中的快速解决方案是进行一些小的调整:
String s = "my source program that i am reading from file";
String lines[] = s.split("\n"); // Break my program into lines
for (int i = 0; i < lines.length; i++) {
if (lines[i].contains("=")) {
String literal = lines[i].substring((lines[i].indexOf("=") + 1), lines[i].indexOf(";"));
if (!literal.contains("new"))
System.err.println(literal);
}
}
If you really want to find all literals, hook up a java parser or use the "javap" tool to look at the generated class-files. 如果您真的想查找所有文字,请连接Java解析器或使用“ javap”工具查看生成的类文件。 Running it on code that includes these lines:
在包含以下行的代码上运行它:
int a = 20;
long b = 10L;
float c = 1.10E12f;
And using "grep" to choose only those lines that describe long, float, and String, returns 然后使用“ grep”选择仅描述long,float和String的那些行,则返回
javap -c Main.class | grep -E "const|push|//" | grep -vE "Field|Method|class"
0: bipush 20
2: ldc2_w #2 // long 10l
6: ldc #4 // float 1.1E12f
This finds all literals. 这会找到所有文字。 Even those inside strings, implicit (
i++
) or somehow quoted. 甚至那些内部字符串,隐式(
i++
)或以某种方式引用。 Notice that int
literals can only be located via the bipush
and iconst_*
instructions, as the javap decompiler generates no annotations for them. 请注意,
int
文字只能通过bipush
和iconst_*
指令定位,因为javap反编译器不会为它们生成任何注释。 More on bytecode and constants here 有关字节码和常量的更多信息,请点击此处
If you are only interested in simple lines of the form <atomicType> <identifier> = <literal>;
如果您只对格式为
<atomicType> <identifier> = <literal>;
简单行感兴趣<atomicType> <identifier> = <literal>;
- then search for them using a regular expression: -然后使用正则表达式搜索它们:
String pattern =
"\\s*\\p{Alpha}[\\p{Alnum}_]*\\s+" + // type with space, eg.: "int "
"\\p{Alpha}[\\p{Alnum}_]*\\s*=\\s*" + // java identifier with =, eg.: "myVar ="
"(([-+]?\\s*\\d*\\.?\\d+([eE][-+]?\\d+)?[Lf]?)?|" + // numeric non-hex
"(\"[^\"]*\"))\\s*;"; // or unquoted string constant
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
while (m.find()) {
String literal = m.group(1);
System.err.println(literal);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.