Search in sources :

Example 31 with JCModifiers

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

the class EmptySetMultibindingContributions method fixByModifyingMethod.

private Description fixByModifyingMethod(VisitorState state, JCClassDecl enclosingClass, MethodTree method) {
    JCModifiers methodModifiers = ((JCMethodDecl) method).getModifiers();
    String replacementModifiersString = createReplacementMethodModifiers(state, methodModifiers);
    JCModifiers enclosingClassModifiers = enclosingClass.getModifiers();
    String enclosingClassReplacementModifiersString = createReplacementClassModifiers(state, enclosingClassModifiers);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder().addImport("dagger.multibindings.Multibinds").replace(methodModifiers, replacementModifiersString).replace(method.getBody(), ";");
    fixBuilder = (enclosingClassModifiers.pos == -1) ? fixBuilder.prefixWith(enclosingClass, enclosingClassReplacementModifiersString) : fixBuilder.replace(enclosingClassModifiers, enclosingClassReplacementModifiersString);
    return describeMatch(method, fixBuilder.build());
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 32 with JCModifiers

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

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

Example 34 with JCModifiers

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

the class JavacGuavaSingularizer 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();
    JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
    JCStatement clearField = maker.Exec(maker.Assign(thisDotField, maker.Literal(CTC_BOT, null)));
    List<JCStatement> statements = returnStatement != null ? List.of(clearField, returnStatement) : List.of(clearField);
    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 35 with JCModifiers

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

the class JavacJavaUtilListSetSingularizer method generatePluralMethod.

void generatePluralMethod(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(), "addAll");
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName())));
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getPluralName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString()));
    JCExpression paramType = chainDots(builderType, "java", "util", "Collection");
    paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), 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)

Aggregations

JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)58 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)43 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)43 Name (com.sun.tools.javac.util.Name)43 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)37 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)35 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)31 JavacTreeMaker (lombok.javac.JavacTreeMaker)30 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)29 ListBuffer (com.sun.tools.javac.util.ListBuffer)26 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)22 JavacNode (lombok.javac.JavacNode)15 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)12 ArrayList (java.util.ArrayList)7 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)6 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)3 JCReturn (com.sun.tools.javac.tree.JCTree.JCReturn)3