Search in sources :

Example 16 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacGuavaSingularizer method generateSingularMethod.

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();
    LombokImmutableList<String> suffixes = getArgumentSuffixes();
    Name[] names = new Name[suffixes.size()];
    for (int i = 0; i < suffixes.size(); i++) {
        String s = suffixes.get(i);
        Name n = data.getSingularName();
        names[i] = s.isEmpty() ? n : builderType.toName(s);
    }
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName());
    ListBuffer<JCExpression> invokeAddExprBuilder = new ListBuffer<JCExpression>();
    for (int i = 0; i < suffixes.size(); i++) {
        invokeAddExprBuilder.append(maker.Ident(names[i]));
    }
    List<JCExpression> invokeAddExpr = invokeAddExprBuilder.toList();
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, invokeAddExpr);
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name methodName = data.getSingularName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        methodName = builderType.toName(HandlerUtil.buildAccessorName(getAddMethodName(), methodName.toString()));
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    for (int i = 0; i < suffixes.size(); i++) {
        JCExpression pt = cloneParamType(i, maker, data.getTypeArgs(), builderType, source);
        JCVariableDecl p = maker.VarDef(maker.Modifiers(paramFlags), names[i], pt, null);
        params.append(p);
    }
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params.toList(), 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 17 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generateClearMethod.

private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    List<JCVariableDecl> params = List.nil();
    List<JCExpression> jceBlank = List.nil();
    JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
    JCExpression thisDotFieldDotClear = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), builderType.toName("clear"));
    JCStatement clearCall = maker.Exec(maker.Apply(jceBlank, thisDotFieldDotClear, jceBlank));
    JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
    JCStatement ifSetCallClear = maker.If(cond, clearCall, null);
    List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
    JCBlock body = maker.Block(0, statements);
    Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 18 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generateSingularMethod.

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, false, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add");
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName())));
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getSingularName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString()));
    JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), 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 19 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generateMethods.

@Override
public void generateMethods(SingularData data, JavacNode builderType, JCTree source, boolean fluent, boolean chain) {
    if (useGuavaInstead(builderType)) {
        guavaListSetSingularizer.generateMethods(data, builderType, source, fluent, chain);
        return;
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    Symtab symbolTable = builderType.getSymbolTable();
    Name thisName = builderType.toName("this");
    JCExpression returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    JCStatement returnStatement = chain ? maker.Return(maker.Ident(thisName)) : null;
    generateSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(thisName)) : null;
    generatePluralMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(thisName)) : null;
    generateClearMethod(maker, returnType, returnStatement, data, builderType, source);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Example 20 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacJavaUtilListSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements, Name targetVariableName) {
    if (useGuavaInstead(builderType)) {
        guavaListSetSingularizer.appendBuildCode(data, builderType, source, statements, targetVariableName);
        return;
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    List<JCExpression> jceBlank = List.nil();
    ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
    /* case 0: (empty); break; */
    {
        JCStatement assignStat;
        {
            // pluralName = java.util.Collections.emptyList();
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "emptyList"), jceBlank);
            assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
        }
        JCStatement breakStat = maker.Break(null);
        JCCase emptyCase = maker.Case(maker.Literal(CTC_INT, 0), List.of(assignStat, breakStat));
        cases.append(emptyCase);
    }
    /* case 1: (singletonList); break; */
    {
        JCStatement assignStat;
        {
            // pluralName = java.util.Collections.singletonList(this.pluralName.get(0));
            JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
            JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName().toString(), "get"), List.of(zeroLiteral));
            List<JCExpression> args = List.of(arg);
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "singletonList"), args);
            assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
        }
        JCStatement breakStat = maker.Break(null);
        JCCase singletonCase = maker.Case(maker.Literal(CTC_INT, 1), List.of(assignStat, breakStat));
        cases.append(singletonCase);
    }
    /* default: Create with right size, then addAll */
    {
        List<JCStatement> defStats = createListCopy(maker, data, builderType, source);
        JCCase defaultCase = maker.Case(null, defStats);
        cases.append(defaultCase);
    }
    JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true, false), cases.toList());
    JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
    localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
    JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null);
    statements.append(varDefStat);
    statements.append(switchStat);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Aggregations

JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)112 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)82 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)37 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)33 JCTree (com.sun.tools.javac.tree.JCTree)32 Name (com.sun.tools.javac.util.Name)32 Type (com.redhat.ceylon.model.typechecker.model.Type)26 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)26 ListBuffer (com.sun.tools.javac.util.ListBuffer)25 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)23 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)18 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)16 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)14 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)14 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)12 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)12 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)12 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)11 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)10