本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCIf方法的典型用法代码示例。如果您正苦于以下问题:Java JCTree.JCIf方法的具体用法?Java JCTree.JCIf怎么用?Java JCTree.JCIf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.tree.JCTree
的用法示例。
在下文中一共展示了JCTree.JCIf方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitIf
import com.sun.tools.javac.tree.JCTree; //导入方法依赖的package包/类
public void visitIf(JCTree.JCIf tree) {
JCDiagnostic.DiagnosticPosition nil = null;
// generate dummy messages to exercise the log API
log.error("not.stmt");
log.error(tree.pos, "not.stmt");
log.error(tree.pos(), "not.stmt");
log.error(nil, "not.stmt");
log.warning("div.zero");
log.warning(tree.pos, "div.zero");
log.warning(tree.pos(), "div.zero");
log.warning(nil, "div.zero");
}
示例2: mayBeInstrument
import com.sun.tools.javac.tree.JCTree; //导入方法依赖的package包/类
@Override
protected boolean mayBeInstrument(@NotNull ParameterToInstrumentInfo info) {
String parameterName = info.getMethodParameter().getName().toString();
CompilationUnitProcessingContext context = info.getContext();
ExceptionTextGenerator<ParameterToInstrumentInfo> generator =
context.getExceptionTextGeneratorManager().getGenerator(METHOD_PARAMETER, context.getPluginSettings());
String errorMessage = generator.generate(info);
TreeMaker factory = context.getAstFactory();
Names symbolsTable = context.getSymbolsTable();
JCTree.JCBlock body = info.getBody();
String exceptionToThrow = info.getContext().getPluginSettings().getExceptionToThrow(METHOD_PARAMETER);
JCTree.JCIf varCheck = buildVarCheck(factory, symbolsTable, parameterName, errorMessage, exceptionToThrow);
if (info.isConstructor() && isFirstStatementThisOrSuperCall(body)) {
List<JCTree.JCStatement> newStatements = List.of(varCheck);
List<JCTree.JCStatement> statements = body.getStatements();
for (int i = 1; i < statements.size(); i++) {
newStatements = newStatements.append(statements.get(i));
}
newStatements = newStatements.prepend(statements.get(0));
body.stats = newStatements;
} else {
body.stats = body.stats.prepend(varCheck);
}
if (context.getPluginSettings().isVerboseMode()) {
String methodName = info.getQualifiedMethodName();
String methodNotice = methodName == null ? "" : " in the method " + methodName + "()";
context.getLogger().info(String.format(
"added a null-check for argument '%s'%s",
parameterName, methodNotice
));
}
return true;
}
示例3: visitIf
import com.sun.tools.javac.tree.JCTree; //导入方法依赖的package包/类
@Override
public ReturnInstrumentationAstParent visitIf(IfTree node, Void aVoid) {
if (!(node instanceof JCTree.JCIf)) {
report(JCTree.JCIf.class, node);
return null;
}
JCTree.JCIf jcIf = (JCTree.JCIf) node;
JCTree.JCBlock block = null;
if (jcIf.thenpart == info.getReturnExpression()) {
jcIf.thenpart = (block = buildBlock());
} else if (jcIf.elsepart == info.getReturnExpression()) {
jcIf.elsepart = (block = buildBlock());
}
return block == null ? null : new BlockReturnInstrumentationAstParent(block);
}
示例4: buildVarCheck
import com.sun.tools.javac.tree.JCTree; //导入方法依赖的package包/类
/**
* Builds an {@code AST 'if'} element which looks as below:
* <pre>
* if ([given-variable-name] == null) {
* throw new [given-exception]([given-error-message]);
* }
* </pre>
*
* @param factory an {@code AST} factory to use
* @param symbolsTable a symbols table to use
* @param variableName a variable name to use
* @param errorMessage an error message to use
* @param exceptionToThrow an exception to throw in case of failed check
* @return an {@code AST 'if'} for the parameters above
*/
@NotNull
public static JCTree.JCIf buildVarCheck(@NotNull TreeMaker factory,
@NotNull Names symbolsTable,
@NotNull String variableName,
@NotNull String errorMessage,
@NotNull String exceptionToThrow)
{
return factory.If(
factory.Parens(
factory.Binary(
JCTree.Tag.EQ,
factory.Ident(
symbolsTable.fromString(variableName)
),
factory.Literal(TypeTag.BOT, null))
),
factory.Block(0, List.of(
factory.Throw(
factory.NewClass(
null,
nil(),
buildExceptionClassExpression(exceptionToThrow, factory, symbolsTable),
List.of(factory.Literal(TypeTag.CLASS, errorMessage)),
null
)
)
)),
null
);
}