Java源码示例:com.sun.tools.javac.tree.JCTree.JCIf
示例1
public void visitIf(JCIf tree) {
scanCond(tree.cond);
final Bits initsBeforeElse = new Bits(initsWhenFalse);
final Bits uninitsBeforeElse = new Bits(uninitsWhenFalse);
inits.assign(initsWhenTrue);
uninits.assign(uninitsWhenTrue);
scan(tree.thenpart);
if (tree.elsepart != null) {
final Bits initsAfterThen = new Bits(inits);
final Bits uninitsAfterThen = new Bits(uninits);
inits.assign(initsBeforeElse);
uninits.assign(uninitsBeforeElse);
scan(tree.elsepart);
inits.andSet(initsAfterThen);
uninits.andSet(uninitsAfterThen);
} else {
inits.andSet(initsBeforeElse);
uninits.andSet(uninitsBeforeElse);
}
}
示例2
/** Visitor method for if statements.
*/
public void visitIf(JCIf tree) {
JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
if (isTrue(cond)) {
result = translate(tree.thenpart);
addPrunedInfo(cond);
} else if (isFalse(cond)) {
if (tree.elsepart != null) {
result = translate(tree.elsepart);
} else {
result = make.Skip();
}
addPrunedInfo(cond);
} else {
// Condition is not a compile-time constant.
tree.thenpart = translate(tree.thenpart);
tree.elsepart = translate(tree.elsepart);
result = tree;
}
}
示例3
public void visitIf(JCIf tree) {
try {
print("if ");
if (PARENS.equals(treeTag(tree.cond))) {
printExpr(tree.cond);
} else {
print("(");
printExpr(tree.cond);
print(")");
}
print(" ");
printStat(tree.thenpart);
if (tree.elsepart != null) {
print(" else ");
printStat(tree.elsepart);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例4
public void visitIf(JCIf tree) {
SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
sr.mergeWith(csp(tree.cond));
sr.mergeWith(csp(tree.thenpart));
sr.mergeWith(csp(tree.elsepart));
result = sr;
}
示例5
@Override
public void visitIf(JCIf tree) {
scan(tree.cond);
scanStat(tree.thenpart);
if (tree.elsepart != null) {
boolean aliveAfterThen = alive;
alive = true;
scanStat(tree.elsepart);
alive = alive | aliveAfterThen;
} else {
alive = true;
}
}
示例6
@Override
public void visitIf(JCIf tree) {
scan(tree.cond);
scan(tree.thenpart);
if (tree.elsepart != null) {
scan(tree.elsepart);
}
}
示例7
private JCStatement makeTwrCloseStatement(Symbol primaryException, JCExpression resource) {
// primaryException.addSuppressed(catchException);
VarSymbol catchException =
new VarSymbol(SYNTHETIC, make.paramName(2),
syms.throwableType,
currentMethodSym);
JCStatement addSuppressionStatement =
make.Exec(makeCall(make.Ident(primaryException),
names.addSuppressed,
List.of(make.Ident(catchException))));
// try { resource.close(); } catch (e) { primaryException.addSuppressed(e); }
JCBlock tryBlock =
make.Block(0L, List.of(makeResourceCloseInvocation(resource)));
JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
JCBlock catchBlock = make.Block(0L, List.of(addSuppressionStatement));
List<JCCatch> catchClauses = List.of(make.Catch(catchExceptionDecl, catchBlock));
JCTry tryTree = make.Try(tryBlock, catchClauses, null);
tryTree.finallyCanCompleteNormally = true;
// if (primaryException != null) {try...} else resourceClose;
JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)),
tryTree,
makeResourceCloseInvocation(resource));
return closeIfStatement;
}
示例8
private IfStatement convertIf(JCIf statement) {
return IfStatement.newBuilder()
.setSourcePosition(getSourcePosition(statement))
.setConditionExpression(convertConditionRemovingOuterParentheses(statement.getCondition()))
.setThenStatement(convertStatement(statement.getThenStatement()))
.setElseStatement(convertStatementOrNull(statement.getElseStatement()))
.build();
}
示例9
/**
* Checks if the statement is of the form 'if (x == null) {throw WHATEVER;},
* where the block braces are optional. If it is of this form, returns "x".
* If it is not of this form, returns null.
*/
public String returnVarNameIfNullCheck(JCStatement stat) {
if (!(stat instanceof JCIf)) return null;
/* Check that the if's statement is a throw statement, possibly in a block. */ {
JCStatement then = ((JCIf) stat).thenpart;
if (then instanceof JCBlock) {
List<JCStatement> stats = ((JCBlock) then).stats;
if (stats.length() == 0) return null;
then = stats.get(0);
}
if (!(then instanceof JCThrow)) return null;
}
/* Check that the if's conditional is like 'x == null'. Return from this method (don't generate
a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ {
JCExpression cond = ((JCIf) stat).cond;
while (cond instanceof JCParens) cond = ((JCParens) cond).expr;
if (!(cond instanceof JCBinary)) return null;
JCBinary bin = (JCBinary) cond;
if (!CTC_EQUAL.equals(treeTag(bin))) return null;
if (!(bin.lhs instanceof JCIdent)) return null;
if (!(bin.rhs instanceof JCLiteral)) return null;
if (!CTC_BOT.equals(typeTag(bin.rhs))) return null;
return ((JCIdent) bin.lhs).name.toString();
}
}
示例10
@Override
public JCIf inline(Inliner inliner) throws CouldNotResolveImportException {
return inliner.maker().If(
getCondition().inline(inliner),
getThenStatement().inline(inliner),
(getElseStatement() == null) ? null : getElseStatement().inline(inliner));
}
示例11
public void visitIf(JCIf tree) {
tree.cond = translate(tree.cond, syms.booleanType);
tree.thenpart = translate(tree.thenpart);
tree.elsepart = translate(tree.elsepart);
result = tree;
}
示例12
/**
* Check for empty statements after if
*/
void checkEmptyIf(JCIf tree) {
if (tree.thenpart.hasTag(SKIP) && tree.elsepart == null &&
lint.isEnabled(LintCategory.EMPTY))
log.warning(LintCategory.EMPTY, tree.thenpart.pos(), Warnings.EmptyIf);
}
示例13
@Override
public void visitIf(JCIf tree) {
//skip body (to prevents same statements to be analyzed twice)
scan(tree.getCondition());
}
示例14
private Statement convertStatement(JCStatement jcStatement) {
switch (jcStatement.getKind()) {
case ASSERT:
return convertAssert((JCAssert) jcStatement);
case BLOCK:
return convertBlock((JCBlock) jcStatement);
case BREAK:
return convertBreak((JCBreak) jcStatement);
case CLASS:
convertClassDeclaration((JCClassDecl) jcStatement);
return null;
case CONTINUE:
return convertContinue((JCContinue) jcStatement);
case DO_WHILE_LOOP:
return convertDoWhileLoop((JCDoWhileLoop) jcStatement);
case EMPTY_STATEMENT:
return new EmptyStatement(getSourcePosition(jcStatement));
case ENHANCED_FOR_LOOP:
return convertEnhancedForLoop((JCEnhancedForLoop) jcStatement);
case EXPRESSION_STATEMENT:
return convertExpressionStatement((JCExpressionStatement) jcStatement);
case FOR_LOOP:
return convertForLoop((JCForLoop) jcStatement);
case IF:
return convertIf((JCIf) jcStatement);
case LABELED_STATEMENT:
return convertLabeledStatement((JCLabeledStatement) jcStatement);
case RETURN:
return convertReturn((JCReturn) jcStatement);
case SWITCH:
return convertSwitch((JCSwitch) jcStatement);
case THROW:
return convertThrow((JCThrow) jcStatement);
case TRY:
return convertTry((JCTry) jcStatement);
case VARIABLE:
return convertVariableDeclaration((JCVariableDecl) jcStatement);
case WHILE_LOOP:
return convertWhileLoop((JCWhileLoop) jcStatement);
case SYNCHRONIZED:
return convertSynchronized((JCSynchronized) jcStatement);
default:
throw new AssertionError("Unknown statement node type: " + jcStatement.getKind());
}
}
示例15
@Override
public void visitIf(JCIf tree) {
scan(tree.getCondition());
}
示例16
public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
return invoke(If, cond, thenpart, elsepart);
}
示例17
@Override public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
if (inNetbeansEditor(annotationNode)) return;
deleteAnnotationIfNeccessary(annotationNode, Cleanup.class);
String cleanupName = annotation.getInstance().value();
if (cleanupName.length() == 0) {
annotationNode.addError("cleanupName cannot be the empty string.");
return;
}
if (annotationNode.up().getKind() != Kind.LOCAL) {
annotationNode.addError("@Cleanup is legal only on local variable declarations.");
return;
}
JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get();
if (decl.init == null) {
annotationNode.addError("@Cleanup variable declarations need to be initialized.");
return;
}
JavacNode ancestor = annotationNode.up().directUp();
JCTree blockNode = ancestor.get();
final List<JCStatement> statements;
if (blockNode instanceof JCBlock) {
statements = ((JCBlock)blockNode).stats;
} else if (blockNode instanceof JCCase) {
statements = ((JCCase)blockNode).stats;
} else if (blockNode instanceof JCMethodDecl) {
statements = ((JCMethodDecl)blockNode).body.stats;
} else {
annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
return;
}
boolean seenDeclaration = false;
ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
ListBuffer<JCStatement> tryBlock = new ListBuffer<JCStatement>();
for (JCStatement statement : statements) {
if (!seenDeclaration) {
if (statement == decl) seenDeclaration = true;
newStatements.append(statement);
} else {
tryBlock.append(statement);
}
}
if (!seenDeclaration) {
annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
return;
}
doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);
JavacTreeMaker maker = annotationNode.getTreeMaker();
JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec(
maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil())));
JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name));
JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null));
JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null);
Context context = annotationNode.getContext();
JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast, context);
newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast, context), List.<JCCatch>nil(), finalizer), ast, context));
if (blockNode instanceof JCBlock) {
((JCBlock)blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCCase) {
((JCCase)blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCMethodDecl) {
((JCMethodDecl)blockNode).body.stats = newStatements.toList();
} else throw new AssertionError("Should not get here");
ancestor.rebuild();
}
示例18
public JCIf build() {
return treeMaker.If(condition, thenBlock, elseBlock);
}
示例19