一、字面量
字面量并不是定义,而是一个字面表达式,比如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

系列二:变量定义&spm=1001.2101.3001.5002&articleId=124357555&d=1&t=3&u=52bbf8a4efe741278c957141b42d2993)
1350

被折叠的 条评论
为什么被折叠?



