如何使用EclipseJDTAPI更改Java类名?

如何使用EclipseJDTAPI更改Java类名?,java,eclipse,Java,Eclipse,我已经有了下面的代码,但是类型声明中的setName不能将字符串作为参数,它需要一个SimpleName对象,而我不知道如何获得SimpleName对象,也许这个想法是错误的?毕竟,我需要的是更改Java类名,有什么解决方案吗 ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(str.toCharArray()); //str is the code of .java file parser.setKind(AS

我已经有了下面的代码,但是类型声明中的setName不能将字符串作为参数,它需要一个SimpleName对象,而我不知道如何获得SimpleName对象,也许这个想法是错误的?毕竟,我需要的是更改Java类名,有什么解决方案吗

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray()); //str is the code of .java file
parser.setKind(ASTParser.K_COMPILATION_UNIT);

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

List types = cu.types();    
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class  
System.out.println("className:" + typeDec.getName());

//SimpleName sn = new SimpleName();
//sn.setIdentifier("aqaa"); //change the class name to "aqaa", but this code fails
//typeDec.setName(sn); 
如果我们将最后3句话改为:

SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa"); 
typeDec.setName(sn);
最后一个setName将引发异常:

importName: java.awt.BorderLayout start: 61 length: 29Exception in thread "main" java.lang.IllegalArgumentException
className: NI

    at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1873)
    at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:1935)
    at org.eclipse.jdt.core.dom.AbstractTypeDeclaration.setName(AbstractTypeDeclaration.java:155)
    at org.catr.JavaRefiner.parse(JavaRefiner.java:52)
    at org.catr.JavaRefiner.ParseFilesInDir(JavaRefiner.java:130)
    at org.catr.JavaRefiner.main(JavaRefiner.java:142)
也许我应该发布整个课程,这样你们可以自己尝试代码:)

package org.catr;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
导入org.eclipse.jdt.core.dom.AST;
导入org.eclipse.jdt.core.dom.ASTParser;
导入org.eclipse.jdt.core.dom.ASTVisitor;
导入org.eclipse.jdt.core.dom.CompilationUnit;
导入org.eclipse.jdt.core.dom.ImportDeclaration;
导入org.eclipse.jdt.core.dom.SimpleName;
导入org.eclipse.jdt.core.dom.Type;
导入org.eclipse.jdt.core.dom.TypeDeclaration;
导入org.eclipse.jdt.core.dom.VariableDeclarationFragment;
导入org.eclipse.jdt.core.dom.VariableDeclarationStatement;
公共类JavaRefiner
{
静态int a;
//使用ASTParse解析字符串
公共静态void解析(字符串str)
{
ASTParser=ASTParser.newParser(AST.JLS3);
setSource(str.toCharArray());
setKind(ASTParser.K_编译单元);
final CompilationUnit cu=(CompilationUnit)parser.createAST(null);
列表导入=cu.imports();
ImportDeclaration importDec=(ImportDeclaration)imports.get(0);
System.out.println(“importName:+importDec.getName()+”start:+importDec.getStartPosition()+”length:+importDec.getLength());
列表类型=cu.types();
TypeDeclaration typeDec=(TypeDeclaration)types.get(0);
System.out.println(“className:+typeDec.getName());
SimpleName sn=typeDec.getName();
序列号设置标识符(“NII”);
typeDec.setName(序列号);
cu.接受(新的ASTVisitor()
{
Set name=新的HashSet();
公共布尔访问(VariableDeclarationStatement状态)
{
List frags=state.fragments();
Type=state.getType();
System.out.println(“类型:\t\t\t'+Type+“\t\t\t行”+cu.getLineNumber(类型.getStartPosition());
for(VariableDeclarationFragment frag:frags)
{
探视2(frag);
}
返回false;
}
公共布尔访问2(VariableDeclarationFragment节点)
{
SimpleName=node.getName();
this.names.add(name.getIdentifier());
System.out.println(“声明:\t\t'+name+'\t\t\t行”
+cu.getLineNumber(name.getStartPosition());
返回false;//不继续
}
公共布尔访问(SimpleName节点)
{
if(this.names.contains(node.getIdentifier()))
{
System.out.println(“用法:\t\t\t'+node+'\t\t\t行”
+cu.getLineNumber(node.getStartPosition());
}
返回true;
}
});
}
//将文件内容读入字符串
公共静态字符串readFileToString(字符串文件路径)引发IOException
{
StringBuilder文件数据=新的StringBuilder(1000);
BufferedReader reader=新BufferedReader(新文件读取器(文件路径));
char[]buf=新字符[1024];
int numRead=0;
while((numRead=reader.read(buf))!=-1)
{
//系统输出打印项次(numRead);
String readData=String.valueOf(buf,0,numRead);
追加(readData);
buf=新字符[1024];
}
reader.close();
返回fileData.toString();
}
//循环目录以获取文件列表
public static void ParseFilesInDir()引发IOException
{
文件目录=新文件(“.”);
字符串dirPath=dirs.getCanonicalPath()+File.separator+“data”+File.separator;
文件根=新文件(dirPath);
//System.out.println(rootDir.listFiles());
File[]files=root.listFiles();
字符串filePath=null;
用于(文件f:文件)
{
filePath=f.getAbsolutePath();
if(f.isFile())
{
解析(readFileToString(filePath));
}
其他的
{
a=0;
a=a+1;
}
}
}
公共静态void main(字符串[]args)引发IOException
{
ParseFilesInDir();
}
}使用以下代码:

List types = cu.types();    
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class  
System.out.println("className:" + typeDec.getName());

SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa"); 
typeDec.setName(sn);

运行代码..时出现什么错误?构造函数SimpleName()未定义“typeDec.setName(sn);”引发异常,请转到文章获取异常内容。
List types = cu.types();    
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class  
System.out.println("className:" + typeDec.getName());

SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa"); 
typeDec.setName(sn);