Search in sources :

Example 11 with JCModifiers

use of com.sun.tools.javac.tree.JCTree.JCModifiers 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 12 with JCModifiers

use of com.sun.tools.javac.tree.JCTree.JCModifiers 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 13 with JCModifiers

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

the class JavacJavaUtilMapSingularizer 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 thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key");
    JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear");
    JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear");
    JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank));
    JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank));
    JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null));
    JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall));
    JCStatement ifSetCallClear = maker.If(cond, clearCalls, 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 14 with JCModifiers

use of com.sun.tools.javac.tree.JCTree.JCModifiers in project error-prone by google.

the class UVariableDecl method inline.

private JCVariableDecl inline(@Nullable UExpression type, Inliner inliner) throws CouldNotResolveImportException {
    Optional<LocalVarBinding> binding = inliner.getOptionalBinding(key());
    JCModifiers modifiers;
    Name name;
    TreeMaker maker = inliner.maker();
    if (binding.isPresent()) {
        modifiers = (JCModifiers) binding.get().getModifiers();
        name = binding.get().getName();
    } else {
        modifiers = maker.Modifiers(0L);
        name = getName().inline(inliner);
    }
    return maker.VarDef(modifiers, name, (type == null) ? null : type.inline(inliner), (getInitializer() == null) ? null : getInitializer().inline(inliner));
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) Name(com.sun.tools.javac.util.Name)

Example 15 with JCModifiers

use of com.sun.tools.javac.tree.JCTree.JCModifiers in project error-prone by google.

the class UseBinds method convertMethodToBinds.

private SuggestedFix.Builder convertMethodToBinds(MethodTree method, VisitorState state) {
    SuggestedFix.Builder fix = SuggestedFix.builder();
    JCModifiers modifiers = ((JCMethodDecl) method).getModifiers();
    ImmutableList.Builder<String> modifierStringsBuilder = new ImmutableList.Builder<String>().add("@Binds");
    for (JCAnnotation annotation : modifiers.annotations) {
        Name annotationQualifiedName = getSymbol(annotation).getQualifiedName();
        if (annotationQualifiedName.contentEquals(PROVIDES_CLASS_NAME) || annotationQualifiedName.contentEquals(PRODUCES_CLASS_NAME)) {
            List<JCExpression> arguments = annotation.getArguments();
            if (!arguments.isEmpty()) {
                JCExpression argument = Iterables.getOnlyElement(arguments);
                checkState(argument.getKind().equals(ASSIGNMENT));
                JCAssign assignment = (JCAssign) argument;
                checkState(getSymbol(assignment.getVariable()).getSimpleName().contentEquals("type"));
                String typeName = getSymbol(assignment.getExpression()).getSimpleName().toString();
                switch(typeName) {
                    case "SET":
                        modifierStringsBuilder.add("@IntoSet");
                        fix.addImport(INTO_SET_CLASS_NAME);
                        break;
                    case "SET_VALUES":
                        modifierStringsBuilder.add("@ElementsIntoSet");
                        fix.addImport(ELEMENTS_INTO_SET_CLASS_NAME);
                        break;
                    case "MAP":
                        modifierStringsBuilder.add("@IntoMap");
                        fix.addImport(INTO_MAP_CLASS_NAME);
                        break;
                    default:
                        throw new AssertionError("Unknown type name: " + typeName);
                }
            }
        } else {
            modifierStringsBuilder.add(state.getSourceForNode(annotation));
        }
    }
    EnumSet<Flag> methodFlags = Flags.asFlagSet(modifiers.flags);
    methodFlags.remove(Flags.Flag.STATIC);
    methodFlags.remove(Flags.Flag.FINAL);
    methodFlags.add(Flags.Flag.ABSTRACT);
    for (Flag flag : methodFlags) {
        modifierStringsBuilder.add(flag.toString());
    }
    fix.replace(modifiers, Joiner.on(' ').join(modifierStringsBuilder.build()));
    fix.replace(method.getBody(), ";");
    return fix;
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ImmutableList(com.google.common.collect.ImmutableList) Flag(com.sun.tools.javac.code.Flags.Flag) Name(com.sun.tools.javac.util.Name) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)25 Name (com.sun.tools.javac.util.Name)20 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)18 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)17 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)17 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)14 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)13 ListBuffer (com.sun.tools.javac.util.ListBuffer)11 JavacTreeMaker (lombok.javac.JavacTreeMaker)10 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)9 JavacNode (lombok.javac.JavacNode)7 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)5 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 JCTree (com.sun.tools.javac.tree.JCTree)2 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)2 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)2