Search in sources :

Example 56 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project error-prone by google.

the class Inliner method inlineList.

public <R> com.sun.tools.javac.util.List<R> inlineList(Iterable<? extends Inlineable<? extends R>> elements) throws CouldNotResolveImportException {
    ListBuffer<R> result = new ListBuffer<>();
    for (Inlineable<? extends R> e : elements) {
        if (e instanceof URepeated) {
            // URepeated is bound to a list of expressions.
            URepeated repeated = (URepeated) e;
            for (JCExpression expr : getBinding(repeated.key())) {
                @SuppressWarnings("unchecked") R // then R must be ? super JCExpression.
                r = (R) expr;
                result.append(r);
            }
        } else {
            result.append(e.inline(this));
        }
    }
    return result.toList();
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 57 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class JavacJavaUtilMapSingularizer method generateSingularMethod.

private void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source));
    Name keyName = builderType.toName(data.getSingularName().toString() + "Key");
    Name valueName = builderType.toName(data.getSingularName().toString() + "Value");
    /* this.pluralname$key.add(singularnameKey); */
    {
        JCExpression thisDotKeyFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$key", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotKeyFieldDotAdd, List.<JCExpression>of(maker.Ident(keyName)));
        statements.append(maker.Exec(invokeAdd));
    }
    /* this.pluralname$value.add(singularnameValue); */
    {
        JCExpression thisDotValueFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$value", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotValueFieldDotAdd, List.<JCExpression>of(maker.Ident(valueName)));
        statements.append(maker.Exec(invokeAdd));
    }
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    Name name = data.getSingularName();
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString()));
    JCExpression paramTypeKey = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
    JCExpression paramTypeValue = cloneParamType(1, maker, data.getTypeArgs(), builderType, source);
    JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, paramTypeKey, null);
    JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, paramTypeValue, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 58 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer 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 59 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class JavacHandlerUtil method unboxAndRemoveAnnotationParameter.

static List<JCAnnotation> unboxAndRemoveAnnotationParameter(JCAnnotation ast, String parameterName, String errorName, JavacNode annotationNode) {
    ListBuffer<JCExpression> params = new ListBuffer<JCExpression>();
    ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
    outer: for (JCExpression param : ast.args) {
        boolean allowRaw;
        String nameOfParam = "value";
        JCExpression valueOfParam = null;
        if (param instanceof JCAssign) {
            JCAssign assign = (JCAssign) param;
            if (assign.lhs instanceof JCIdent) {
                JCIdent ident = (JCIdent) assign.lhs;
                nameOfParam = ident.name.toString();
            }
            valueOfParam = assign.rhs;
        }
        /* strip trailing underscores */
        {
            int lastIdx;
            for (lastIdx = nameOfParam.length(); lastIdx > 0; lastIdx--) {
                if (nameOfParam.charAt(lastIdx - 1) != '_')
                    break;
            }
            allowRaw = lastIdx < nameOfParam.length();
            nameOfParam = nameOfParam.substring(0, lastIdx);
        }
        if (!parameterName.equals(nameOfParam)) {
            params.append(param);
            continue outer;
        }
        int endPos = Javac.getEndPosition(param.pos(), (JCCompilationUnit) annotationNode.top().get());
        annotationNode.getAst().removeFromDeferredDiagnostics(param.pos, endPos);
        if (valueOfParam instanceof JCAnnotation) {
            String dummyAnnotationName = ((JCAnnotation) valueOfParam).annotationType.toString();
            dummyAnnotationName = dummyAnnotationName.replace("_", "").replace("$", "").replace("x", "").replace("X", "");
            if (dummyAnnotationName.length() > 0) {
                if (allowRaw) {
                    result.append((JCAnnotation) valueOfParam);
                } else {
                    addError(errorName, annotationNode);
                    continue outer;
                }
            } else {
                for (JCExpression expr : ((JCAnnotation) valueOfParam).args) {
                    if (expr instanceof JCAssign && ((JCAssign) expr).lhs instanceof JCIdent) {
                        JCIdent id = (JCIdent) ((JCAssign) expr).lhs;
                        if ("value".equals(id.name.toString())) {
                            expr = ((JCAssign) expr).rhs;
                        } else {
                            addError(errorName, annotationNode);
                        }
                    }
                    if (expr instanceof JCAnnotation) {
                        result.append((JCAnnotation) expr);
                    } else if (expr instanceof JCNewArray) {
                        for (JCExpression expr2 : ((JCNewArray) expr).elems) {
                            if (expr2 instanceof JCAnnotation) {
                                result.append((JCAnnotation) expr2);
                            } else {
                                addError(errorName, annotationNode);
                                continue outer;
                            }
                        }
                    } else {
                        addError(errorName, annotationNode);
                        continue outer;
                    }
                }
            }
        } else if (valueOfParam instanceof JCNewArray) {
            JCNewArray arr = (JCNewArray) valueOfParam;
            if (arr.elems.isEmpty()) {
            // Just remove it, this is always fine.
            } else if (allowRaw) {
                for (JCExpression jce : arr.elems) {
                    if (jce instanceof JCAnnotation)
                        result.append((JCAnnotation) jce);
                    else
                        addError(errorName, annotationNode);
                }
            } else {
                addError(errorName, annotationNode);
            }
        } else {
            addError(errorName, annotationNode);
        }
    }
    ast.args = params.toList();
    return result.toList();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 60 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class JavacHandlerUtil method cloneType0.

private static JCExpression cloneType0(JavacTreeMaker maker, JCTree in) {
    if (in == null)
        return null;
    if (in instanceof JCPrimitiveTypeTree)
        return (JCExpression) in;
    if (in instanceof JCIdent) {
        return maker.Ident(((JCIdent) in).name);
    }
    if (in instanceof JCFieldAccess) {
        JCFieldAccess fa = (JCFieldAccess) in;
        return maker.Select(cloneType0(maker, fa.selected), fa.name);
    }
    if (in instanceof JCArrayTypeTree) {
        JCArrayTypeTree att = (JCArrayTypeTree) in;
        return maker.TypeArray(cloneType0(maker, att.elemtype));
    }
    if (in instanceof JCTypeApply) {
        JCTypeApply ta = (JCTypeApply) in;
        ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
        for (JCExpression typeArg : ta.arguments) {
            lb.append(cloneType0(maker, typeArg));
        }
        return maker.TypeApply(cloneType0(maker, ta.clazz), lb.toList());
    }
    if (in instanceof JCWildcard) {
        JCWildcard w = (JCWildcard) in;
        JCExpression newInner = cloneType0(maker, w.inner);
        TypeBoundKind newKind;
        switch(w.getKind()) {
            case SUPER_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.SUPER);
                break;
            case EXTENDS_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.EXTENDS);
                break;
            default:
            case UNBOUNDED_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.UNBOUND);
                break;
        }
        return maker.Wildcard(newKind, newInner);
    }
    // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline.
    return (JCExpression) in;
}
Also used : JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree) TypeBoundKind(com.sun.tools.javac.tree.JCTree.TypeBoundKind)

Aggregations

ListBuffer (com.sun.tools.javac.util.ListBuffer)88 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)54 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)25 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)25 JCTree (com.sun.tools.javac.tree.JCTree)22 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)22 Name (com.sun.tools.javac.util.Name)19 JavacNode (lombok.javac.JavacNode)18 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)17 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)17 JavacTreeMaker (lombok.javac.JavacTreeMaker)17 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)14 Type (com.redhat.ceylon.model.typechecker.model.Type)12 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)12 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)11 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)8 List (com.sun.tools.javac.util.List)7 ArrayList (java.util.ArrayList)7 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)6 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)6