第一讲:抽象语法树基本知识
一、字面量
字面量并不是定义,而是一个字面表达式,比如a=1,1就是一个字面
使用treeMaker.Literal进行定义
//com.sun.tools.javac.code.TypeTag 定义了各种基本的类型
treeMaker.Literal(typeTag, o);
二、int
使用JCVariableDecl进行变量的定义,注意modifier。
注意:基本类型即使也对了,编译的时候也会编译成int a = true这种形式,所以最好使用包装类型
JCTree.JCVariableDecl intI1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("intI1"),
treeMaker.Ident(names.fromString("Integer")),
treeMaker.Literal(i)
);
// 注意:实际生成了 int intI2 = true;
JCTree.JCVariableDecl intI2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("intI2"),
treeMaker.TypeIdent(TypeTag.INT),
treeMaker.Literal(TypeTag.INT, i)
);
三、short
// 注意:实际生成了 short short1 = true;
JCTree.JCVariableDecl short1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("short1"),
treeMaker.TypeIdent(TypeTag.SHORT),
treeMaker.Literal(TypeTag.SHORT, 500)
);
JCTree.JCVariableDecl short2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("short2"),
treeMaker.Ident(names.fromString("Short")),
treeMaker.Literal(500)
);
四、byte
// 注意:实际生成 byte byte1 = true;
JCTree.JCVariableDecl byte1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("byte1"),
treeMaker.TypeIdent(TypeTag.BYTE),
treeMaker.Literal(TypeTag.BYTE, 1)
);
JCTree.JCVariableDecl byte2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("byte2"),
treeMaker.Ident(names.fromString("Byte")),
treeMaker.Literal(1)
);
五、boolean
// boolean bo = true;
JCTree.JCVariableDecl boolean1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("boolean1"),
treeMaker.TypeIdent(TypeTag.BOOLEAN),
treeMaker.Literal(true)
);
JCTree.JCVariableDecl boolean2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("boolean2"),
treeMaker.Ident(names.fromString("Boolean")),
treeMaker.Literal(false)
);
六、char
JCTree.JCVariableDecl char1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("char1"),
treeMaker.TypeIdent(TypeTag.CHAR),
// 这里是不能使用'a',否则会报错
treeMaker.Literal(TypeTag.CHAR, 97)
);
JCTree.JCVariableDecl char2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("char2"),
treeMaker.Ident(names.fromString("Character")),
treeMaker.Literal(TypeTag.CHAR, 97)
);
七、long
JCTree.JCVariableDecl long1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("long1"),
treeMaker.TypeIdent(TypeTag.LONG),
treeMaker.Literal(12312L)
);
JCTree.JCVariableDecl long2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("long2"),
treeMaker.Ident(names.fromString("Long")),
treeMaker.Literal(12324L)
);
八、double
JCTree.JCVariableDecl double1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("double1"),
treeMaker.TypeIdent(TypeTag.DOUBLE),
treeMaker.Literal(4.567D)
);
JCTree.JCVariableDecl double2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("double2"),
treeMaker.Ident(names.fromString("Double")),
treeMaker.Literal(4.567D)
);
九、float
JCTree.JCVariableDecl float1 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("float1"),
treeMaker.TypeIdent(TypeTag.FLOAT),
treeMaker.Literal(4.567F)
);
JCTree.JCVariableDecl float2 = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("float2"),
treeMaker.Ident(names.fromString("Float")),
treeMaker.Literal(4.567F)
);
十、object
// 需要先进行import
JCTree.JCIdent jcIdent = treeMaker.Ident(names.fromString("com.leisure.lombok.thrift"));
Name className = names.fromString("Test");
JCTree.JCFieldAccess jcFieldAccess = treeMaker.Select(jcIdent, className);
JCTree.JCImport anImport = treeMaker.Import(jcFieldAccess, false);
jcCompilationUnit.defs = jcCompilationUnit.defs.append(anImport);
JCTree.JCNewClass newClass = treeMaker.NewClass(null, null, treeMaker.Ident(names.fromString("Test")), List.nil(), null);
JCTree.JCVariableDecl object = treeMaker.VarDef(
treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("object"),
treeMaker.Ident(names.fromString("Test")),
newClass
);
?
|