Search in sources :

Example 26 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class JavacHandlerUtil method cloneSelfType.

public static JCExpression cloneSelfType(JavacNode childOfType) {
    JavacNode typeNode = childOfType;
    JavacTreeMaker maker = childOfType.getTreeMaker();
    while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
    if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
        JCClassDecl type = (JCClassDecl) typeNode.get();
        ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
        if (!type.typarams.isEmpty()) {
            for (JCTypeParameter tp : type.typarams) {
                typeArgs.append(maker.Ident(tp.name));
            }
            return maker.TypeApply(maker.Ident(type.name), typeArgs.toList());
        } else {
            return maker.Ident(type.name);
        }
    } else {
        return null;
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 27 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class JavacHandlerUtil method copyTypeParams.

public static List<JCTypeParameter> copyTypeParams(JavacNode source, List<JCTypeParameter> params) {
    if (params == null || params.isEmpty())
        return params;
    ListBuffer<JCTypeParameter> out = new ListBuffer<JCTypeParameter>();
    JavacTreeMaker maker = source.getTreeMaker();
    Context context = source.getContext();
    for (JCTypeParameter tp : params) {
        List<JCExpression> bounds = tp.bounds;
        if (bounds != null && !bounds.isEmpty()) {
            ListBuffer<JCExpression> boundsCopy = new ListBuffer<JCExpression>();
            for (JCExpression expr : tp.bounds) {
                boundsCopy.append(cloneType(maker, expr, source.get(), context));
            }
            bounds = boundsCopy.toList();
        }
        out.append(maker.TypeParameter(tp.name, bounds));
    }
    return out.toList();
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Context(com.sun.tools.javac.util.Context) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 28 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class JavacHandlerUtil method addAnnotation.

private static void addAnnotation(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context, String annotationTypeFqn, JCExpression arg) {
    boolean isJavaLangBased;
    String simpleName;
    {
        int idx = annotationTypeFqn.lastIndexOf('.');
        simpleName = idx == -1 ? annotationTypeFqn : annotationTypeFqn.substring(idx + 1);
        isJavaLangBased = idx == 9 && annotationTypeFqn.regionMatches(0, "java.lang.", 0, 10);
    }
    for (JCAnnotation ann : mods.annotations) {
        JCTree annType = ann.getAnnotationType();
        if (annType instanceof JCIdent) {
            Name lastPart = ((JCIdent) annType).name;
            if (lastPart.contentEquals(simpleName))
                return;
        }
        if (annType instanceof JCFieldAccess) {
            if (annType.toString().equals(annotationTypeFqn))
                return;
        }
    }
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression annType = isJavaLangBased ? genJavaLangTypeRef(node, simpleName) : chainDotsString(node, annotationTypeFqn);
    annType.pos = pos;
    if (arg != null) {
        arg.pos = pos;
        if (arg instanceof JCAssign) {
            ((JCAssign) arg).lhs.pos = pos;
            ((JCAssign) arg).rhs.pos = pos;
        }
    }
    List<JCExpression> argList = arg != null ? List.of(arg) : List.<JCExpression>nil();
    JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(annType, argList), source, context);
    annotation.pos = pos;
    mods.annotations = mods.annotations.append(annotation);
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) JCTree(com.sun.tools.javac.tree.JCTree) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) Name(com.sun.tools.javac.util.Name)

Example 29 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class JavacHandlerUtil method chainDots.

/**
	 * In javac, dotted access of any kind, from {@code java.lang.String} to {@code var.methodName}
	 * is represented by a fold-left of {@code Select} nodes with the leftmost string represented by
	 * a {@code Ident} node. This method generates such an expression.
	 * <p>
	 * The position of the generated node(s) will be equal to the {@code pos} parameter.
	 *
	 * For example, maker.Select(maker.Select(maker.Ident(NAME[java]), NAME[lang]), NAME[String]).
	 * 
	 * @see com.sun.tools.javac.tree.JCTree.JCIdent
	 * @see com.sun.tools.javac.tree.JCTree.JCFieldAccess
	 */
public static JCExpression chainDots(JavacNode node, int pos, String elem1, String elem2, String... elems) {
    assert elems != null;
    JavacTreeMaker maker = node.getTreeMaker();
    if (pos != -1)
        maker = maker.at(pos);
    JCExpression e = null;
    if (elem1 != null)
        e = maker.Ident(node.toName(elem1));
    if (elem2 != null)
        e = e == null ? maker.Ident(node.toName(elem2)) : maker.Select(e, node.toName(elem2));
    for (int i = 0; i < elems.length; i++) {
        e = e == null ? maker.Ident(node.toName(elems[i])) : maker.Select(e, node.toName(elems[i]));
    }
    assert e != null;
    return e;
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 30 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class HandleToString method createToString.

static JCMethodDecl createToString(JavacNode typeNode, Collection<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil());
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
    JCExpression returnType = genJavaLangTypeRef(typeNode, "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, typeNode.getContext());
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) ToString(lombok.ToString) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)

Aggregations

JavacTreeMaker (lombok.javac.JavacTreeMaker)39 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)33 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)18 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 Name (com.sun.tools.javac.util.Name)17 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)16 JavacNode (lombok.javac.JavacNode)15 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)13 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)13 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)10 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)9 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)8 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)5 ArrayList (java.util.ArrayList)5 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 Context (com.sun.tools.javac.util.Context)4 List (com.sun.tools.javac.util.List)4