本文整理匯總了Java中com.sun.tools.javac.tree.TreeMaker.VarDef方法的典型用法代碼示例。如果您正苦於以下問題:Java TreeMaker.VarDef方法的具體用法?Java TreeMaker.VarDef怎麽用?Java TreeMaker.VarDef使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.tools.javac.tree.TreeMaker
的用法示例。
在下文中一共展示了TreeMaker.VarDef方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createOrGetField
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
public GField createOrGetField(long modifiers, String name, String type, JCTree.JCExpression init) {
GField field = (GField) getMember(name);
if (field == null) {
TreeMaker maker = getEnvironment().getMaker();
JavacElements utils = getEnvironment().getUtils();
JCVariableDecl variableDecl = maker.VarDef(maker.Modifiers(modifiers), utils.getName(name),
maker.Ident(utils.getName(type)), init);
field = new GField(this, variableDecl, null);
field.setName(name);
field.setMemberSignature(name);
putMember(field);
classDecl.defs = classDecl.defs.append(variableDecl);
}
return field;
}
示例2: visit
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
@Override
public void visit(DeclareVariableWrapper w) {
TreeMaker maker = builder.getTreeMaker();
JCVariableDecl var = (JCVariableDecl) w.getTarget();
if (var != null && !w.isLocalVariable()) {
// Just use the original field, with annotations and all.
result = var;
return;
}
long numericFlags = toNumericFlags(w.getFlags());
result = maker.VarDef(maker.Modifiers(numericFlags),
nameOrNull(w.getName()), generate(w.getVariableType()),
generate(w.getInit()));
}
示例3: 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);
}
示例4: 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);
}
示例5: createReadFromParcel
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
@Override
public List<JCTree.JCStatement> createReadFromParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, String varName, boolean isArray) {
if (isArray) {
return List.nil();
}
// int = read
// value = int > -1 ? Value.values()[int] : null
final JCTree.JCExpression enumTypeExpression = astHelper.getTypeFromElement(
astHelper.findFieldByName(rootElement, varName),
false,
false
);
if (enumTypeExpression == null) {
return List.nil();
}
final TreeMaker treeMaker = astHelper.getTreeMaker();
final String tmpEnumName = "_" + varName;
final JCTree.JCExpression tmpEnumIdent = treeMaker.Ident(astHelper.getName(tmpEnumName));
final JCTree.JCStatement enumVar = treeMaker.VarDef(
astHelper.getModifiers(Modifier.FINAL),
astHelper.getName(tmpEnumName),
astHelper.getPrimitiveType(TypeKind.INT),
treeMaker.Apply(
List.<JCTree.JCExpression>nil(),
treeMaker.Select(parcel, astHelper.getName("readInt")),
List.<JCTree.JCExpression>nil()
)
);
final JCTree.JCExpression enumCond = treeMaker.Conditional(
astHelper.getGreater(tmpEnumIdent, treeMaker.Literal(-1)),
treeMaker.Indexed(
treeMaker.Apply(
List.<JCTree.JCExpression>nil(),
treeMaker.Select(enumTypeExpression, astHelper.getName("values")),
List.<JCTree.JCExpression>nil()
),
tmpEnumIdent
),
astHelper.getNull()
);
final JCTree.JCStatement enumStatement = treeMaker.Exec(
treeMaker.Assign(
treeMaker.Ident(astHelper.getName(varName)), enumCond
)
);
return List.of(enumVar, enumStatement);
}
示例6: 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);
}
示例7: createField
import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private static JCVariableDecl createField(Object anno, JavacNode fieldNode) {
TreeMaker maker = fieldNode.getTreeMaker();
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
String relatedFieldName = null;
boolean isOneToOne = false;
boolean isUnique = false;
Name baseTypeName = ((JCClassDecl) fieldNode.up().get()).name;
JCIdent baseType = maker.Ident(baseTypeName);
JCExpression referenceType = getFieldType(fieldNode, FieldAccess.ALWAYS_FIELD);
if (anno instanceof OneToOne) {
isOneToOne = true;
relatedFieldName = ((OneToOne) anno).field();
} else {
relatedFieldName = ((OneToMany) anno).field();
isUnique = ((OneToMany) anno).unique();
if (referenceType instanceof JCTypeApply) {
referenceType = ((JCTypeApply) referenceType).arguments.get(0);
}
}
JCClassDecl anonClass = maker.AnonymousClassDef(
maker.Modifiers(0),
List.<JCTree>of(
createGetReferencedKeyMethod(fieldNode, maker, relatedFieldName, isOneToOne, baseType, referenceType),
createSetReferencedObjectMethod(fieldNode, maker, field, baseType, isUnique),
createSetRelatedIdMethod(fieldNode, maker, relatedFieldName, isOneToOne, baseType, referenceType)
)
);
JCVariableDecl var = maker.VarDef(
maker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL),
fieldNode.toName(toUpperCase(field.name.toString())),
maker.TypeApply(
(isOneToOne ? chainDots(fieldNode, OneToOneRelation.class) : chainDots(fieldNode, OneToManyRelation.class)),
List.<JCExpression>of(baseType, referenceType)
),
maker.NewClass(
null,
List.<JCExpression>nil(),
maker.TypeApply(
(isOneToOne ? chainDots(fieldNode, OneToOneRelation.class) : chainDots(fieldNode, OneToManyRelation.class)),
List.<JCExpression>of(baseType, referenceType)
),
List.<JCExpression>nil(),
anonClass
)
);
return var;
}