java如何找到AST表达式的底部
我是AST新手(我第一次写插件)。表达式在现实生活中可能相当复杂。例如,我想知道如何解决asignment的左侧和右侧
class Visitor extends ASTVisitor
{
@Override
public boolean visit(Assignment node)
{
//here, how do I get the final name to each each side of the assignment resolves?
}
}
我还有另一个疑问,如何获取用于调用方法的实例
public boolean visit(MethodInvocation node)
{
//how do I get to know the object used to invoke this method?
//like, for example, MyClass is a class, and it has a field called myField
//the type of myField has a method called myMethod.
//how do I find myField? or for that matter some myLocalVariable used in the same way.
}
假设以下任务
SomeType.someStaticMethod(params).someInstanceMethod(moreParams).someField =
[another expression with arbitrary complexity]
如何从Assigment
节点到达someField
另外,MethodInvocation
的哪个属性提供了用于调用该方法的实例
编辑1:根据我收到的答案,我的问题显然不清楚。我不想解决这个特殊的表达式。我希望能够在给定任何赋值的情况下,找出它被赋值的名称,以及第一个赋值的名称(如果不是右值)
例如,方法调用的参数可以是字段访问或以前声明的局部变量
SomeType.someStaticMethod(instance.field).someInstanceMethod(type.staticField, localVariable, localField).Field.destinationField
因此,这里有一个很有希望的客观问题:如果任何赋值语句的左侧和右侧都具有任意的复杂性,那么如何获得被赋值的最终字段/变量,以及赋值给它的最终(如果有的话)字段/变量
编辑2:更具体地说,我想通过注释@Const:
/**
* When Applied to a method, ensures the method doesn't change in any
* way the state of the object used to invoke it, i.e., all the fields
* of the object must remain the same, and no field may be returned,
* unless the field itself is marked as {@code @Const} or the field is
* a primitive non-array type. A method annotated with {@code @Const}
* can only invoke other {@code @Const} methods of its class, can only
* use the class's fields to invoke {@code @Const} methods of the fields
* classes and can only pass fields as parameters to methods that
* annotate that formal parameter as {@code @Const}.
*
* When applied to a formal parameter, ensures the method will not
* modify the value referenced by the formal parameter. A formal
* parameter annotated as {@code @Const} will not be aliased inside the
* body of the method. The method is not allowed to invoke another
* method and pass the annotated parameter, save if the other method
* also annotates the formal parameter as {@code @Const}. The method is
* not allowed to use the parameter to invoke any of its type's methods,
* unless the method being invoked is also annotated as {@code @Const}
*
* When applied to a field, ensures the field cannot be aliased and that
* no code can alter the state of that field, either from inside the
* class that owns the field or from outside it. Any constructor in any
* derived class is allowed to set the value of the field and invoke any
* methods using it. As for methods, only those annotated as
* {@code @Const} may be invoked using the field. The field may only be
* passed as a parameter to a method if the method annotates the
* corresponding formal parameter as {@code @Const}
*
* When applied to a local variable, ensures neither the block where the
* variable is declared or any nested block will alter the value of that
* local variable. The local variable may be defined only once, at any
* point where it is in scope and cannot be aliased. Only methods
* annotated as {@code @Const} may be invoked using this variable, and
* the variable may only be passed as a parameter to another method if
* said method annotates its corresponding formal parameter as
* {@code @Const}
*
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,
ElementType.LOCAL_VARIABLE})
@Inherited
public @interface Const
{
}
为了实现这一点,我必须做的第一件事是当赋值的左侧被标记为@Const
(很容易)。我还必须检测and表达式的右侧何时是标记为@Const的字段,在这种情况下,只能在同一类型的@Const变量的定义处分配它
问题是我很难在表达式的右侧找到最终字段,以避免字段的别名和@Const注释的无用性
# 1 楼答案
首先引用我发布的一个答案:
以下访客应帮助您做您想做的事情:
# 2 楼答案
访问者是一个非常好的工具,但解决特定问题的正确方法并不总是让一个访问者耐心地等待其访问方法被调用。。。你所问的问题就是这种情况的一个例子
让我们重新表述一下您试图做的事情:
您希望标识以标识每个分配(即
leftSide = rightSide
)对于每个赋值,您要确定左侧的性质(即,它是局部变量或字段访问),如果它确实是字段访问,您希望构建一个与该字段对应的“路径”(即源对象,后跟一系列方法调用或字段访问,并以字段访问结束)
对于每个赋值,您希望确定与右侧对应的类似“路径”
我认为您已经解决了第1点:您只需创建一个扩展
org.eclipse.jdt.core.dom.ASTVisitor
的类;在这里,您重写了#visit(Assignment)
方法。最后,在适当的情况下,实例化visitor类,并让它访问AST树,从符合您需求的节点开始(最有可能是CompilationUnit
、TypeDeclaration
或MethodDeclaration
的实例)然后呢?
#visit(Assignment)
方法确实接收Assignment
节点。直接在该对象上,您可以获得左侧和右侧表达式(assignment.getLeftHandSide()
和assignment.getRightHandSide()
)。正如您所提到的,这两个都是Expression
,结果可能非常复杂,那么我们如何从这些子树中提取一条干净的线性“路径”?访问者当然是最好的方式,但这里有一个陷阱,它应该使用不同的访问者来完成,而不是让你的第一个访问者(捕捉Assignment
的访问者)继续其两侧的表达式。从技术上讲,可以使用单个访问者来完成这一切,但这将涉及该访问者内部的重要状态管理。无论如何,我非常确信,这种管理的复杂性将非常高,以至于这种实现的效率实际上低于独立访问者的方法所以我们可以想象这样的情况:
请注意,在这段代码中
MyAssignmentListVisitor.visit(Assignment)
返回true
,以指示应该递归地检查赋值的子项。这听起来可能不必要,Java语言确实支持几个结构,其中一个赋值可能包含其他赋值;例如,考虑以下极端情况:出于同样的原因,在表达式线性化期间,仅访问赋值的右侧,因为赋值的“结果值”是其右侧。在这种情况下,左侧只是一个可以安全忽略的副作用
考虑到我不知道您的具体情况所需的信息的性质,我将不再进一步介绍如何对路径进行实际建模。对于您来说,分别为左侧表达式和右侧表达式创建不同的访问者类可能更合适,例如,为了更好地处理右侧可能实际涉及通过二进制运算符组合的多个变量/字段/方法调用这一事实。那必须是你的决定
关于AST树的访问者遍历,仍有一些主要问题有待讨论,即,通过依赖默认节点遍历顺序,您失去了获取每个节点之间关系信息的机会。例如,给定表达式
this.someMethod(this.fieldA).fieldB
,您将看到类似于以下序列的内容:根本没有办法从这一系列事件中推导出线性化表达式。相反,您将希望显式地拦截每个节点,并且仅在适当的情况下在节点的子节点上显式地递归,并且在Approvr中迟交的订单。例如,我们本可以做到以下几点:
在前面的示例中,这些方法将在
path
中收集以下序列:this
,someMethod()
,fieldB
。我相信这和你要找的很接近。如果希望收集所有字段访问/方法调用序列(例如,希望访问者同时返回this,someMethod(),fieldB
和this,fieldA
),则可以重写visit(MethodInvocation)
方法,大致如下所示:最后,如果您想知道路径中每个步骤的值的类型,那么您必须查看与每个节点关联的绑定对象,例如:
methodInvocation.resolveMethodBinding().getDeclaringClass()
。但是请注意,绑定解析必须在构建AST树时明确请求还有更多的语言结构无法被上述代码正确处理;不过,我相信你应该能够自己解决这些遗留问题。如果您需要一个参考实现,请查看类
org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFlattener
,它基本上是从现有的AST树重构Java源代码;虽然这个特定的访问者比大多数其他的访问者要大得多,但它更容易理解根据OP的编辑进行更新#2
以下是最新编辑后的更新起点。仍有许多案件需要处理,但这更符合您的具体问题。还要注意的是,尽管我使用了大量的
instanceof
检查(因为目前这对我来说比较容易,因为我在一个简单的文本编辑器中编写代码,并且没有ASTNode常量的代码完成),但您可以选择在node.getNodeType()
上使用switch语句,这通常会更有效希望有帮助
# 3 楼答案
您可以进一步访问
node.getLeftHandSide()
。 我认为在锐化(Java2C#翻译)代码中可以找到一个很好的例子:https://github.com/mono/sharpen/blob/master/src/main/sharpen/core/CSharpBuilder.java#L2848
这里有一个简单的示例项目: https://github.com/revaultch/jdt-sample
Junit测试在这里: https://github.com/revaultch/jdt-sample/blob/master/src/test/java/ch/revault/jdt/test/VisitorTest.java