我已将 BindingsRecovery 的值设置为true parser.setBindingsRecovery(true); ,但问题仍然存在。这段代码可能有什么问题?
public void correctParser(IProject project) throws IOException, CoreException
{
File[] FileFolderInSrc = new File(project.getFile("src").getLocation().toString() + "//").listFiles();
ArrayList<File> JavaFiles = new ArrayList<File>();
findJavaFiles(FileFolderInSrc, JavaFiles);
//IPackageFragment[] packages = JavaCore.create(srcFolder).getPackageFragments();
File file = new File("C:\\Users\\XYZ\\com.myplugin.rmp\\filename.txt");
StringBuilder str = new StringBuilder("");
for (final File curFile : JavaFiles)
{
IWorkspace workspace= ResourcesPlugin.getWorkspace();
IPath location= Path.fromOSString(curFile.getAbsolutePath());
IFile ifile= workspace.getRoot().getFileForLocation(location);
InputStream CheckCurJavaFile = (ifile).getContents();
BufferedReader CheckCurJavaFileReader = new BufferedReader(new InputStreamReader(CheckCurJavaFile));
String curline = null;
while ((curline = CheckCurJavaFileReader.readLine()) != null)
{
str.append(curline + "\n");
}
CheckCurJavaFileReader.close();
}
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(str.toString().toCharArray());
parser.setResolveBindings(true);
IJavaProject targetProject=null;
if (project.hasNature(JavaCore.NATURE_ID))
{
targetProject = JavaCore.create(project);
}
parser.setProject(targetProject);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setBindingsRecovery(true);
final FileWriter fw = new FileWriter(file.getAbsoluteFile());
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
final BufferedWriter bw = new BufferedWriter(fw);
try
{
cu.accept(new ASTVisitor()
{
Set<String> names = new HashSet<String>();
public boolean visit(MethodDeclaration node)
{
//if (node.getName().getIdentifier().equals("testSearch")) { //if starts
Block block = node.getBody();
block.accept(new ASTVisitor()
{
public boolean visit(MethodInvocation node)
{
SimpleName name = node.getName();
try
{
bw.write("Name '"+ node.getName()+ "" + "' at line"+cu.getLineNumber(name.getStartPosition()) + "\n");
Expression expression = node.getExpression();
if (expression != null)
{
bw.write("Expr: " + expression.toString() + "\n");
ITypeBinding typeBinding = expression.resolveTypeBinding();
//IType type = (IType)typeBinding.getJavaElement();
//bw.write("Type " + type.getFullyQualifiedName());
// System.out.printf("Type %s (method %s) calls %s\n", typeName, methodName, type.getFullyQualifiedName());
if (typeBinding != null)
{
bw.write("Type: " + typeBinding.getName() + "\n");
}
}
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null)
{
ITypeBinding type = binding.getDeclaringClass();
if (type != null)
{
bw.write("Decl: " + type.getName() + "\n");
}
}
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
});
return true;
}
});
}//try brace
finally
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}