概述:ASTNode就是AST的描述节点,想很娴熟的生成出java文件,必须得此套节点有所了解
ASTNode:所有AST节点的父类
static Class nodeClassForType(int nodeType):静态方法,通过类型type获取到对应的class对象
checkNewChild(ASTNode node, ASTNode newChild,boolean cycleCheck, Class nodeType):检查传入的孩子。
boolean subtreeMatch(ASTMatcher matcher, Object other):比较两个节点的子是否向匹配,ASTMatcher 可以通过节点获取
ASTNode copySubtree(AST target, ASTNode node):拷贝节点
ASTNode 的子节点:
ASTNode 有几个抽象节点,它们不具体描述,但是却对节点的类型进行了很好的分组
抽象类:
Comment:所有跟注释相关的节点的父类,它里面有三个状态属性,isBlockComment,isLineComment和isDocComment来描述是什么类型的注释。这些跟它的子类也是一一对应的。
Comment: LineComment BlockComment Javadoc
Comment的三个子类:都只是起到描述的作用,并不实质包含内容。LineComment就是表示单行注释"//",BlockComment表示的是一个注释区域"/* */",不过Javadoc是有内容的,TagElement就是具体的内容
Javadoc: /** { TagElement } */
BodyDeclaration:在body里面定义了javadoc和IExtendedModifier两个子节点,所以它的所以子类都具备描述Modifier的特性。
BodyDeclaration: ClassDeclaration InterfaceDeclaration EnumDeclaration MethodDeclaration ConstructorDeclaration FieldDeclaration Initializer EnumConstantDeclaration AnnotationTypeDeclaration AnnotationTypeMemberDeclaration
[Javadoc] { ExtendedModifier } XXXX
Expression:所有具有表达式特性的节点的父类,
Expression: Annotation, ArrayAccess, ArrayCreation, ArrayInitializer, Assignment, BooleanLiteral, CastExpression, CharacterLiteral, ClassInstanceCreation, ConditionalExpression, FieldAccess, InfixExpression, InstanceofExpression, MethodInvocation, Name, NullLiteral, NumberLiteral, ParenthesizedExpression, PostfixExpression, PrefixExpression, StringLiteral, SuperFieldAccess, SuperMethodInvocation, ThisExpression, TypeLiteral, VariableDeclarationExpression
Statement:表示一个代码片段,此父类的大部分方法都过时了
Statement: AssertStatement, Block, BreakStatement, ConstructorInvocation, ContinueStatement, DoStatement, EmptyStatement, ExpressionStatement, ForStatement, IfStatement, LabeledStatement, ReturnStatement, SuperConstructorInvocation, SwitchCase, SwitchStatement, SynchronizedStatement, ThrowStatement, TryStatement, TypeDeclarationStatement, VariableDeclarationStatement, WhileStatement
Type:所有描述类型的节点的父类,它里面就提供了一些判断节点类型的方法,isXXX()
Type: PrimitiveType ArrayType SimpleType QualifiedType ParameterizedType WildcardType PrimitiveType: byte short char int long float double boolean void ArrayType: Type [ ] SimpleType: TypeName ParameterizedType: Type < Type { , Type } > QualifiedType: Type . SimpleName WildcardType: ? [ ( extends | super) Type ]
VariableDeclaration:它的两个子类SingleVariableDeclaration是定义一个变量,并且可以初始化。VariableDeclarationFragment是为已有的类型进行赋值。
VariableDeclaration: SingleVariableDeclaration VariableDeclarationFragment
实体类:这些实体类都是直接继承ASTNode的
CompilationUnit:一个编辑单元包含PackageDeclaration,ImportDeclaration和TypeDeclaration(包,引入和类体)
CompilationUnit: [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration | EnumDeclaration | AnnotationTypeDeclaration | ; }
对于单数子节点用[]修饰,复数子节点用{}描述。单数子节点是用ChildPropertyDescriptor对象存储的,复数节点用ChildListPropertyDescriptor存储。
recordModifications():记录修改痕迹的一个方法,在你要对该编辑单元做任何更改之前,调用此方法开始进行记录。
TextEdit rewrite(IDocument document, Map options):用于把编辑单元的内容写入到文件中。
unit添加三中子元素的方式:
PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); unit.setPackage(packageDeclaration); packageDeclaration.setName(ast.newSimpleName("astdemo")); for (int i = 0; i < IMPORTS.length; ++i) { ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(getSimpleNames(IMPORTS[i]))); if (IMPORTS[i].indexOf("*") > 0) importDeclaration.setOnDemand(true); else importDeclaration.setOnDemand(false); unit.imports().add(importDeclaration); } //类名 TypeDeclaration classType = ast.newTypeDeclaration(); classType.setInterface(false); List classTypeModifier = classType.modifiers(); classTypeModifier.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); classType.setName(ast.newSimpleName("MyFirstApp")); classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName("ApplicationWindow"))); unit.types().add(classType);
从上面代码可以看出,astnode节点在添加单数类型的子节点的时候,用的是set,在添加复数类型的子节点的时候,用的是list的add方法。
PackageDeclaration:包描述节点,如下所示Package有三个子节点:Javadoc,Annotation和Name
PackageDeclaration: [ Javadoc ] { Annotation } package Name ;
PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); unit.setPackage(packageDeclaration); packageDeclaration.setName(ast.newSimpleName("astdemo")); packageDeclaration.setJavadoc(null); packageDeclaration.annotations();
ImportDeclaration:import 有两种简单属性和一个子节点 static和. *状态,Name子节点。
注意在AST中是用SimplePropertyDescriptor存放简单属性的。
ImportDeclaration: import [ static ] Name [ . * ] ;
for (int i = 0; i < IMPORTS.length; ++i) { ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(getSimpleNames(IMPORTS[i]))); if (IMPORTS[i].indexOf("*") > 0) importDeclaration.setOnDemand(true); else importDeclaration.setOnDemand(false); importDeclaration.setStatic(true); unit.imports().add(importDeclaration); }
Modifier: public protected private static abstract final native synchronized transient volatile strictfp
TagElement: [ @ Identifier ] { DocElement } DocElement: TextElement Name MethodRef MemberRef { TagElement }