當前位置: 首頁>>代碼示例>>Java>>正文


Java TreeMaker.If方法代碼示例

本文整理匯總了Java中com.sun.tools.javac.tree.TreeMaker.If方法的典型用法代碼示例。如果您正苦於以下問題:Java TreeMaker.If方法的具體用法?Java TreeMaker.If怎麽用?Java TreeMaker.If使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.tools.javac.tree.TreeMaker的用法示例。


在下文中一共展示了TreeMaker.If方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: buildVarCheck

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的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
    );
}
 
開發者ID:denis-zhdanov,項目名稱:traute,代碼行數:46,代碼來源:InstrumentationUtil.java

示例2: generateNullCheck

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
/**
 * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the
 * variable name as message.
 */
public static JCStatement generateNullCheck(TreeMaker treeMaker, JavacNode variable) {
	JCVariableDecl varDecl = (JCVariableDecl) variable.get();
	if (isPrimitive(varDecl.vartype)) return null;
	Name fieldName = varDecl.name;
	JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException");
	JCTree exception = treeMaker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(treeMaker.Literal(fieldName.toString())), null);
	JCStatement throwStatement = treeMaker.Throw(exception);
	return treeMaker.If(treeMaker.Binary(CTC_EQUAL, treeMaker.Ident(fieldName), treeMaker.Literal(CTC_BOT, null)), throwStatement, null);
}
 
開發者ID:redundent,項目名稱:lombok,代碼行數:14,代碼來源:JavacHandlerUtil.java

示例3: generateCompareFloatOrDouble

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCStatement generateCompareFloatOrDouble(JCExpression thisDotField, JCExpression otherDotField,
		TreeMaker maker, JavacNode node, boolean isDouble) {
	/* if (Float.compare(fieldName, other.fieldName) != 0) return false; */
	JCExpression clazz = chainDots(node, "java", "lang", isDouble ? "Double" : "Float");
	List<JCExpression> args = List.of(thisDotField, otherDotField);
	JCBinary compareCallEquals0 = maker.Binary(CTC_NOT_EQUAL, maker.Apply(
			List.<JCExpression>nil(), maker.Select(clazz, node.toName("compare")), args), maker.Literal(0));
	return maker.If(compareCallEquals0, returnBool(maker, false), null);
}
 
開發者ID:redundent,項目名稱:lombok,代碼行數:10,代碼來源:HandleEqualsAndHashCode.java

示例4: createWriteToParcel

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
@Override
public List<JCTree.JCStatement> createWriteToParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, JCTree.JCExpression flags, String varName, boolean isArray) {

    final TreeMaker treeMaker = astHelper.getTreeMaker();

    final Name itemName = astHelper.getName(varName);
    final JCTree.JCExpression item = treeMaker.Ident(itemName);

    final JCTree.JCExpression textUtils = astHelper.getType("android.text", "TextUtils");

    if (!isArray) {
        final JCTree.JCStatement writeToParcel = treeMaker.Exec(
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        treeMaker.Select(textUtils, astHelper.getName("writeToParcel")),
                        List.of(
                                item, parcel, flags
                        )
                )
        );
        return List.of(writeToParcel);
    }

    // if csa == null -> write -1
    // else iterate and write

    final JCTree.JCStatement ifNull = treeMaker.Exec(
            treeMaker.Apply(
                    List.<JCTree.JCExpression>nil(),
                    treeMaker.Select(parcel, astHelper.getName("writeInt")),
                    List.of((JCTree.JCExpression) treeMaker.Literal(-1))
            )
    );

    final Name loopVariableName = astHelper.getName("_cs");

    final JCTree.JCVariableDecl loopVar = treeMaker.VarDef(
            astHelper.getModifiers(),
            loopVariableName,
            astHelper.getType("java.lang", "CharSequence"),
            null
    );

    final JCTree.JCStatement writeLength = treeMaker.Exec(
            treeMaker.Apply(
                    List.<JCTree.JCExpression>nil(),
                    treeMaker.Select(parcel, astHelper.getName("writeInt")),
                    List.of(
                            (JCTree.JCExpression) treeMaker.Select(item, astHelper.getName("length"))
                    )
            )
    );

    final JCTree.JCStatement forLoop = treeMaker.ForeachLoop(
            loopVar,
            item,
            treeMaker.Exec(
                    treeMaker.Apply(
                            List.<JCTree.JCExpression>nil(),
                            treeMaker.Select(textUtils, astHelper.getName("writeToParcel")),
                            List.of(
                                    treeMaker.Ident(loopVariableName), parcel, flags
                            )
                    )
            )
    );

    final JCTree.JCStatement ifNotNull = treeMaker.Block(
            0,
            List.of(writeLength, forLoop)
    );

    final JCTree.JCStatement ifBlock = treeMaker.If(
            astHelper.getEquals(item, astHelper.getNull()),
            ifNull,
            ifNotNull
    );

    return List.of(ifBlock);
}
 
開發者ID:noties,項目名稱:ParcelGen,代碼行數:81,代碼來源:StatementCreatorCharSequence.java

示例5: handle

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
@Override public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
	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 = ListBuffer.lb();
	ListBuffer<JCStatement> tryBlock = ListBuffer.lb();
	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);
	
	TreeMaker 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())));
	
	JCMethodInvocation 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);
	
	JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast);
	
	newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast), List.<JCCatch>nil(), finalizer), ast));
	
	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();
}
 
開發者ID:redundent,項目名稱:lombok,代碼行數:80,代碼來源:HandleCleanup.java


注:本文中的com.sun.tools.javac.tree.TreeMaker.If方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。