下面列出了org.eclipse.jdt.core.dom.ASTNode#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public VariableDeclaration getVariableDeclaration(SimpleName name) {
ASTNode node = name;
do {
node = node.getParent();
} while ( node != null && !(node instanceof MethodDeclaration) );
String key = null;
if(node != null) {
key = node.toString() + "/" + name.getFullyQualifiedName();
}else {
key = "GLOBAL" + name.getFullyQualifiedName();
}
logError("Trying " + key);
VariableDeclaration var = getVariableDeclaration(key);
if(var == null && node != null) {
key = "GLOBAL" + name.getFullyQualifiedName();
log("Trying " + key);
var = getVariableDeclaration(key);
}
return var;
}
public static List<Expression> getArguments(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).arguments();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).arguments();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).arguments();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).arguments();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).arguments();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).arguments();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return MethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_METHOD_INVOCATION:
return SuperMethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CONSTRUCTOR_INVOCATION:
return ConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CLASS_INSTANCE_CREATION:
return ClassInstanceCreation.ARGUMENTS_PROPERTY;
case ASTNode.ENUM_CONSTANT_DECLARATION:
return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static Expression getExpression(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).getExpression();
case ASTNode.SUPER_METHOD_INVOCATION:
return null;
case ASTNode.CONSTRUCTOR_INVOCATION:
return null;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).getExpression();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).getExpression();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return null;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static IMethodBinding resolveBinding(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).resolveMethodBinding();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).resolveMethodBinding();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static String buildASTLabel(ASTNode node) {
String label = node.getClass().getSimpleName();
if (node instanceof Expression) {
if (node.getClass().getSimpleName().endsWith("Literal")) {
return label + "(" + node.toString() + ")";
}
int type = node.getNodeType();
switch (type) {
case ASTNode.INFIX_EXPRESSION:
return label + "(" + ((InfixExpression) node).getOperator().toString() + ")";
case ASTNode.SIMPLE_NAME:
return label + "(" + node.toString() + ")";
case ASTNode.POSTFIX_EXPRESSION:
return label + "(" + ((PostfixExpression) node).getOperator().toString() + ")";
case ASTNode.PREFIX_EXPRESSION:
return label + "(" + ((PrefixExpression) node).getOperator().toString() + ")";
default:
break;
}
} else if (node instanceof Modifier) {
return label + "(" + node.toString() + ")";
} else if (node instanceof Type) {
if (node instanceof PrimitiveType)
return label + "(" + node.toString() + ")";
} else if (node instanceof TextElement) {
return label + "(" + node.toString() + ")";
} else if (node instanceof TagElement) {
String tag = ((TagElement) node).getTagName();
if (tag == null)
return label;
return label + "(" + tag + ")";
}
return label;
}