com.sun.tools.javac.tree.JCTree.JCIf Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCIf. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 6 votes vote down vote up
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);
	}
}
 
Example #2
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** 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;
    }
}
 
Example #4
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private IfStatement convertIf(JCIf statement) {
  return IfStatement.newBuilder()
      .setSourcePosition(getSourcePosition(statement))
      .setConditionExpression(convertConditionRemovingOuterParentheses(statement.getCondition()))
      .setThenStatement(convertStatement(statement.getThenStatement()))
      .setElseStatement(convertStatementOrNull(statement.getElseStatement()))
      .build();
}
 
Example #5
Source File: UIf.java    From Refaster with Apache License 2.0 5 votes vote down vote up
@Override
public JCIf inline(Inliner inliner) throws CouldNotResolveImportException {
  return inliner.maker().If(
      getCondition().inline(inliner),
      getThenStatement().inline(inliner),
      (getElseStatement() == null) ? null : getElseStatement().inline(inliner));
}
 
Example #6
Source File: HandleNonNull.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example #7
Source File: CRTable.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example #8
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example #9
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void visitIf(JCIf tree) {
    scan(tree.cond);
    scan(tree.thenpart);
    if (tree.elsepart != null) {
        scan(tree.elsepart);
    }
}
 
Example #10
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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;
    }
}
 
Example #11
Source File: Analyzer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void visitIf(JCIf tree) {
    //skip body (to prevents same statements to be analyzed twice)
    scan(tree.getCondition());
}
 
Example #12
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 4 votes vote down vote up
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());
  }
}
 
Example #13
Source File: Analyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visitIf(JCIf tree) {
    scan(tree.getCondition());
}
 
Example #14
Source File: JavacTreeMaker.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
	return invoke(If, cond, thenpart, elsepart);
}
 
Example #15
Source File: HandleCleanup.java    From EasyMPermission with MIT License 4 votes vote down vote up
@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();
}
 
Example #16
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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);
}
 
Example #17
Source File: If.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCIf build() {
    return treeMaker.If(condition, thenBlock, elseBlock);
}
 
Example #18
Source File: TransTypes.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitIf(JCIf tree) {
    tree.cond = translate(tree.cond, syms.booleanType);
    tree.thenpart = translate(tree.thenpart);
    tree.elsepart = translate(tree.elsepart);
    result = tree;
}
 
Example #19
Source File: If.java    From EasyMPermission with MIT License votes vote down vote up
JCIf build();