本文整理匯總了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
);
}
示例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);
}
示例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);
}
示例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);
}
示例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();
}