喜欢这段代码
public boolean equals(Object o) {
return null;
}
我想使用 treeMaker 编写return null语句,但我不知道该怎么做。
喜欢这段代码
public boolean equals(Object o) {
return null;
}
我想使用 treeMaker 编写return null语句,但我不知道该怎么做。
我在这个问题上犯了一个错误,原始不能返回null。
return null语句可以通过这样的代码生成
JCTree.JCStatement aNull = treeMaker.Return(treeMaker.Literal(TypeTag.BOT, null));
例如
public String equal(Object o) {
return null;
}
public JCTree.JCMethodDecl generateEqualMethod(JCTree.JCClassDecl classDecl) {
JCTree.JCModifiers publicModifier = treeMaker.Modifiers(Flags.PUBLIC);
JCTree.JCExpression returnType = treeMaker.Ident(names.fromString("java"));
returnType = treeMaker.Select(returnType, names.fromString("lang"));
returnType = treeMaker.Select(returnType, names.fromString("String"));
Name method = names.fromString("equal");
JCTree.JCExpression ObjectExpr = treeMaker.Ident(names.fromString("java"));
ObjectExpr = treeMaker.Select(ObjectExpr, names.fromString("lang"));
ObjectExpr = treeMaker.Select(ObjectExpr, names.fromString("Object"));
JCTree.JCVariableDecl param =
treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER),
names.fromString("o"),
ObjectExpr,
null);
param.pos = classDecl.pos;
List<JCTree.JCVariableDecl> params = List.of(param);
List<JCTree.JCStatement> statement = List.nil();
JCTree.JCStatement aNull = treeMaker.Return(treeMaker.Literal(TypeTag.BOT, null));
statement = statement.append(aNull);
JCTree.JCBlock block = treeMaker.Block(0, statement);
JCTree.JCMethodDecl methodDecl = treeMaker.MethodDef(publicModifier, method, returnType, List.nil(), params, List.nil(), block, null);
System.out.println(methodDecl);
return methodDecl;
}