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


Java TreeMaker.Literal方法代碼示例

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


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

示例1: addSuppressWarningsAll

import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類
private static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source) {
	TreeMaker maker = node.getTreeMaker();
	JCExpression suppressWarningsType = chainDots(node, "java", "lang", "SuppressWarnings");
	JCLiteral allLiteral = maker.Literal("all");
	suppressWarningsType.pos = pos;
	allLiteral.pos = pos;
	JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(suppressWarningsType, List.<JCExpression>of(allLiteral)), source);
	annotation.pos = pos;
	mods.annotations = mods.annotations.append(annotation);
}
 
開發者ID:redundent,項目名稱:lombok,代碼行數:11,代碼來源:JavacHandlerUtil.java

示例2: 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);
}
 
開發者ID:redundent,項目名稱:lombok,代碼行數:77,代碼來源:HandleToString.java


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