Вот неполные (некоторые элементы TODO) попытки разобрать его с помощью регулярного выражения, мы можем сначала попробовать это с #luckFindInScope (), если не получилось, а затем попробовать сложный способ со сложным исходным кодом.
package com.mytest.core.share;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
public class TestParse {
public static void main(String[] args) throws Exception {
//testHit();
testNotHit();
}
public static void testNotHit() throws Exception {
// input position
int position = 511;
String id = "sample";
parse(position, id);
}
public static void testHit() throws Exception {
// input position
int position = 955;
String id = "sample";
parse(position, id);
}
public static void parse(int position, String id) throws Exception {
int end = position;
FileInputStream fs = new FileInputStream(new File("TestFile.java"));
String source = IOUtils.toString(fs, "UTF-8");
source = source.substring(0, end);
System.out.println("### look from" +source);
// remove all String in source code;
source = removeStringInSource(source);
int start = 0;
int stack = 0;// for nested scope
boolean hit = false;
// find the char '{' from the end of source
for (int i = source.length(); i > 0; i--) {
String current = source.substring(i - 1, i);
if (stack == 0) {
if (current.equals("{")) {
start = i;// lookup from start to end;
System.err.println("from this {, start to search from the location at " + i);
hit = findInScope(source, id, start, end);
end = start; // skip, search next scope;
if (hit) {
break;
} else {
continue;
}
}
}
// skip bracket pair {}
if (current.equals("}")) {
stack++;
end = i;// skip it;
}
if (current.equals("{")) {
stack--;
end = i;// skip it;
}
}
if (hit == false) {
// TODO: find the type in the class members and super class members
}
// TODO: find full class name in the java.lang.* or in the header of the source
// of import section.
}
private static boolean findInScope(String source, String id, int start, int end) {
String regex = "[A-Za-z0-9_$]+\\s+" + id;
String text = source.substring(start, end);
Matcher matcher = Pattern.compile(regex).matcher(text);
boolean result = matcher.find();
if (result) {
hitString = matcher.group();
System.err.println("hitString = " + hitString + " ,in the text scope: " + text);
}
return result;
}
private static boolean luckFindInScope(String source, String id) {
String regex = "[A-Za-z0-9_$]+\\s+" + id;
Matcher matcher = Pattern.compile(regex).matcher(source);
int count = 0;
while (matcher.find()) {
count++;
hitString= matcher.group();
}
return count == 1;
}
private static String hitString = "";
// fill star * in the string value
private static String removeStringInSource(String input) {
Matcher matcher = Pattern.compile("\".*?\"").matcher(input);
String match = "";
while(matcher.find()) {
match = matcher.group();
char[] symbols = new char[match.length()];
Arrays.fill(symbols, '*');
input = input.replace(match, new String(symbols));
}
System.out.println("$$ removed: " + input);
return input;
}
}
Это более сложный исходный код ниже:
package com.mytest.test.parse;
public class TestFile {
public static void main(String[] args) {
String sample = "foo bar";
System.out.println(sample.length());
}
public static String sample = null;
public static void test1() {
if (sample != null) {
{
String sample = "foo bar";
System.out.println(sample);
{
sample = "foo bar";
System.out.println(sample);
}
}
{
System.out.println(sample);
int test = 0;
{
if (test >= 0) {
sample = "foo bar";
System.out.println(sample);
String sample = "foo bar";
System.out.println(sample);
}
}
}
if(sample.equals("foo bar")) {
System.out.println(sample);
}
}
if( sample == null) {
String sample = "foo bar";
if(sample.equals("foo bar")) {
System.out.println(sample);
}
}
}
public static void test2{
if( sample == null) {
String sample = "foo bar";
if(sample.equals("foo bar")) {
System.out.println(sample);
}
}
}
}