本文整理匯總了Java中com.sun.tools.javac.tree.TreeMaker.Block方法的典型用法代碼示例。如果您正苦於以下問題:Java TreeMaker.Block方法的具體用法?Java TreeMaker.Block怎麽用?Java TreeMaker.Block使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.tools.javac.tree.TreeMaker
的用法示例。
在下文中一共展示了TreeMaker.Block方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createCanEqual
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) {
/* public boolean canEqual(final java.lang.Object other) {
* return other instanceof Outer.Inner.MyType;
* }
*/
TreeMaker maker = typeNode.getTreeMaker();
JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.<JCAnnotation>nil());
JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN);
Name canEqualName = typeNode.toName("canEqual");
JCExpression objectType = chainDots(typeNode, "java", "lang", "Object");
Name otherName = typeNode.toName("other");
List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, objectType, null));
JCBlock body = maker.Block(0, List.<JCStatement>of(
maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode)))));
return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source);
}
示例2: buildBootstrapStaticBlock
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCTree.JCStatement buildBootstrapStaticBlock()
{
TreeMaker make = _javacJacker.getTreeMaker();
JavacElements javacElems = _javacJacker.getJavacElements();
JCTree.JCMethodInvocation bootstrapInitCall = make.Apply( List.nil(), memberAccess( make, javacElems, Bootstrap.class.getName() + ".init" ), List.nil() );
return make.Block( Modifier.STATIC, List.of( make.Exec( bootstrapInitCall ) ) );
}
示例3: buildTryCatchBlock
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) {
TreeMaker maker = node.getTreeMaker();
JCBlock tryBlock = setGeneratedBy(maker.Block(0, contents), source);
JCExpression varType = chainDots(node, exception.split("\\."));
JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL), node.toName("$ex"), varType, null);
JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow");
JCBlock catchBody = maker.Block(0, List.<JCStatement>of(maker.Throw(maker.Apply(
List.<JCExpression>nil(), lombokLombokSneakyThrowNameRef,
List.<JCExpression>of(maker.Ident(node.toName("$ex")))))));
return setGeneratedBy(maker.Try(tryBlock, List.of(recursiveSetGeneratedBy(maker.Catch(catchParam, catchBody), source)), null), source);
}
示例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: createGetter
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source, boolean lazy, List<JCAnnotation> onMethod) {
JCVariableDecl fieldNode = (JCVariableDecl) field.get();
// Remember the type; lazy will change it
JCExpression methodType = copyType(treeMaker, fieldNode);
// Generate the methodName; lazy will change the field type
Name methodName = field.toName(toGetterName(field));
List<JCStatement> statements;
JCTree toClearOfMarkers = null;
if (lazy) {
toClearOfMarkers = fieldNode.init;
statements = createLazyGetterBody(treeMaker, field, source);
} else {
statements = createSimpleGetterBody(treeMaker, field);
}
JCBlock methodBody = treeMaker.Block(0, statements);
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.nil();
List<JCExpression> throwsClauses = List.nil();
JCExpression annotationMethodDefaultValue = null;
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field);
List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables);
if (isFieldDeprecated(field)) {
annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil()));
}
JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source);
if (toClearOfMarkers != null) recursiveSetGeneratedBy(toClearOfMarkers, null);
decl.mods.annotations = decl.mods.annotations.appendList(delegates);
return decl;
}
示例6: createToString
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCMethodDecl createToString(JavacNode typeNode, List<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) {
TreeMaker maker = typeNode.getTreeMaker();
JCAnnotation overrideAnnotation = maker.Annotation(chainDots(typeNode, "java", "lang", "Override"), List.<JCExpression>nil());
JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
JCExpression returnType = chainDots(typeNode, "java", "lang", "String");
boolean first = true;
String typeName = getTypeName(typeNode);
String infix = ", ";
String suffix = ")";
String prefix;
if (callSuper) {
prefix = typeName + "(super=";
} else if (fields.isEmpty()) {
prefix = typeName + "()";
} else if (includeFieldNames) {
prefix = typeName + "(" + ((JCVariableDecl)fields.iterator().next().get()).name.toString() + "=";
} else {
prefix = typeName + "(";
}
JCExpression current = maker.Literal(prefix);
if (callSuper) {
JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(),
maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")),
List.<JCExpression>nil());
current = maker.Binary(CTC_PLUS, current, callToSuper);
first = false;
}
for (JavacNode fieldNode : fields) {
JCExpression expr;
JCExpression fieldAccessor = createFieldAccessor(maker, fieldNode, fieldAccess);
JCExpression fieldType = getFieldType(fieldNode, fieldAccess);
// The distinction between primitive and object will be useful if we ever add a 'hideNulls' option.
boolean fieldIsPrimitive = fieldType instanceof JCPrimitiveTypeTree;
boolean fieldIsPrimitiveArray = fieldType instanceof JCArrayTypeTree && ((JCArrayTypeTree) fieldType).elemtype instanceof JCPrimitiveTypeTree;
boolean fieldIsObjectArray = !fieldIsPrimitiveArray && fieldType instanceof JCArrayTypeTree;
@SuppressWarnings("unused")
boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray;
if (fieldIsPrimitiveArray || fieldIsObjectArray) {
JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString");
expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(fieldAccessor));
} else expr = fieldAccessor;
if (first) {
current = maker.Binary(CTC_PLUS, current, expr);
first = false;
continue;
}
if (includeFieldNames) {
current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + fieldNode.getName() + "="));
} else {
current = maker.Binary(CTC_PLUS, current, maker.Literal(infix));
}
current = maker.Binary(CTC_PLUS, current, expr);
}
if (!first) current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix));
JCStatement returnStatement = maker.Return(current);
JCBlock body = maker.Block(0, List.of(returnStatement));
return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("toString"), returnType,
List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source);
}
示例7: createWither
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private JCMethodDecl createWither(long access, JavacNode field, TreeMaker treeMaker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
String witherName = toWitherName(field);
if (witherName == null) return null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
ListBuffer<JCStatement> statements = ListBuffer.lb();
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
Name methodName = field.toName(witherName);
List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables);
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
JCExpression selfType = cloneSelfType(field);
if (selfType == null) return null;
TreeMaker maker = field.getTreeMaker();
ListBuffer<JCExpression> args = ListBuffer.lb();
for (JavacNode child : field.up().down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl childDecl = (JCVariableDecl) child.get();
// Skip fields that start with $
if (childDecl.name.toString().startsWith("$")) continue;
long fieldFlags = childDecl.mods.flags;
// Skip static fields.
if ((fieldFlags & Flags.STATIC) != 0) continue;
// Skip initialized final fields.
if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null) continue;
if (child.get() == field.get()) {
args.append(maker.Ident(fieldDecl.name));
} else {
args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD));
}
}
JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null);
JCExpression identityCheck = maker.Binary(CTC_EQUAL, createFieldAccessor(maker, field, FieldAccess.ALWAYS_FIELD), maker.Ident(fieldDecl.name));
JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass);
JCReturn returnStatement = maker.Return(conditional);
if (nonNulls.isEmpty()) {
statements.append(returnStatement);
} else {
JCStatement nullCheck = generateNullCheck(treeMaker, field);
if (nullCheck != null) statements.append(nullCheck);
statements.append(returnStatement);
}
JCExpression returnType = cloneSelfType(field);
JCBlock methodBody = treeMaker.Block(0, statements.toList());
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.of(param);
List<JCExpression> throwsClauses = List.nil();
JCExpression annotationMethodDefaultValue = null;
List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
if (isFieldDeprecated(field)) {
annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil()));
}
return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, returnType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source);
}