首页 / JAVA / java-JDT如何知道超类的全名
java-JDT如何知道超类的全名
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-JDT如何知道超类的全名,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2255字,纯文字阅读大概需要4分钟。
内容图文
![java-JDT如何知道超类的全名](/upload/InfoBanner/zyjiaocheng/679/e108263fbc204176a058e19a5ad993cc.jpg)
我正在开发Eclipse插件.我正在使用ASTVisitor的以下实现,以替换一个类的超类(如果该类扩展了第三个).
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
public class SuperClassVisitor extends ASTVisitor{
public Type superClass;
public String newSuperClass;
private String oldSuperClass;
public SuperClassVisitor(String newType, String oldType) {
this.newSuperClass = newType;
this.oldSuperClass = oldType;
}
public boolean visit(TypeDeclaration node) {
superClass = node.getSuperclassType();
if (newSuperClass != null) {
Name oldName = node.getAST().newName(oldSuperClass);
SimpleType oldType = node.getAST().newSimpleType(oldName);
Name newName = node.getAST().newName(newSuperClass);
SimpleType newType = node.getAST().newSimpleType(newName);
if (superClass != null && superClass.equals(oldType)) {
node.setSuperclassType(newType);
}
}
return true;
}
}
我正在访问项目中的每个班级.基本上,在扩展oldType的类中,我想将其更改为newType.但是,条件superClass.equals(oldType)永远不会为真,因为我的oldType字符串是一个由点分隔的完全限定名称,而node.getSuperclassType()仅返回类的名称.
是否可以找出超类的全名?
供参考,此答案帮助我创建了此访客:
How Can a SuperClass name be retrieved from a java file using ASTParser?
解决方法:
我可能误解了这个问题,但是…
my oldType string is a dot-separated fully qualified name, while node.getSuperclassType() returns just the name of the class.
错了您的代码显示为:
public Type superClass;
<...>
SimpleType oldType = <...>
也不是Type,也不是SimpleType子类String.他们不是名字.它们是完全合格的类,具有有关类型的信息.他们不测试是否相等的原因写在Javadoc上的Type.equals上:
public final boolean equals(Object obj)
The ASTNode implementation of this Object method uses object identity (==). Use subtreeMatch to compare two subtrees for equality.
后者还指出了在哪里寻找合适的平等测试人员.至于为什么节点使用不同的名称-Type上的toString说得很清楚
Returns a string representation of this node suitable for debugging purposes only.
因此您不能将其用于任何决策.
我想您会混合使用getName和toString来获得该结果,因为没有为Type定义getName,而是为SimpleType定义的,尽管那部分代码丢失了,所以我只是在推测.
内容总结
以上是互联网集市为您收集整理的java-JDT如何知道超类的全名全部内容,希望文章能够帮你解决java-JDT如何知道超类的全名所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。