Search in sources :

Example 81 with JCStatement

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

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

the class HandleUtilityClass method createThrowStatement.

private List<JCStatement> createThrowStatement(JavacNode typeNode, JavacTreeMaker maker) {
    JCExpression exceptionType = genJavaLangTypeRef(typeNode, "UnsupportedOperationException");
    List<JCExpression> jceBlank = List.nil();
    JCExpression message = maker.Literal("This is a utility class and cannot be instantiated");
    JCExpression exceptionInstance = maker.NewClass(null, jceBlank, exceptionType, List.of(message), null);
    JCStatement throwStatement = maker.Throw(exceptionInstance);
    return List.of(throwStatement);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 83 with JCStatement

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

the class HandleBuilder method generateBuilderMethod.

public JCMethodDecl generateBuilderMethod(boolean isStatic, String builderMethodName, String builderClassName, JavacNode source, JavacNode type, List<JCTypeParameter> typeParams) {
    JavacTreeMaker maker = type.getTreeMaker();
    ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
    for (JCTypeParameter typeParam : typeParams) {
        typeArgs.append(maker.Ident(typeParam.name));
    }
    JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null);
    JCStatement statement = maker.Return(call);
    JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
    int modifiers = Flags.PUBLIC;
    if (isStatic)
        modifiers |= Flags.STATIC;
    return maker.MethodDef(maker.Modifiers(modifiers), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(source, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 84 with JCStatement

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

the class HandleConstructor method createConstructor.

public static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, boolean allToDefault, JavacNode source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    boolean isEnum = (((JCClassDecl) typeNode.get()).mods.flags & Flags.ENUM) != 0;
    if (isEnum)
        level = AccessLevel.PRIVATE;
    boolean suppressConstructorProperties;
    if (fields.isEmpty()) {
        suppressConstructorProperties = false;
    } else {
        suppressConstructorProperties = Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
    }
    ListBuffer<JCStatement> nullChecks = new ListBuffer<JCStatement>();
    ListBuffer<JCStatement> assigns = new ListBuffer<JCStatement>();
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    for (JavacNode fieldNode : fields) {
        JCVariableDecl field = (JCVariableDecl) fieldNode.get();
        Name fieldName = removePrefixFromField(fieldNode);
        Name rawName = field.name;
        List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
        if (!allToDefault) {
            List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
            long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
            JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, field.vartype, null);
            params.append(param);
            if (!nonNulls.isEmpty()) {
                JCStatement nullCheck = generateNullCheck(maker, fieldNode, source);
                if (nullCheck != null)
                    nullChecks.append(nullCheck);
            }
        }
        JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), rawName);
        JCExpression assign = maker.Assign(thisX, allToDefault ? getDefaultExpr(maker, field.vartype) : maker.Ident(fieldName));
        assigns.append(maker.Exec(assign));
    }
    JCModifiers mods = maker.Modifiers(toJavacModifier(level), List.<JCAnnotation>nil());
    if (!allToDefault && !suppressConstructorProperties && level != AccessLevel.PRIVATE && level != AccessLevel.PACKAGE && !isLocalType(typeNode) && LombokOptionsFactory.getDelombokOptions(typeNode.getContext()).getFormatPreferences().generateConstructorProperties()) {
        addConstructorProperties(mods, typeNode, fields);
    }
    if (onConstructor != null)
        mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source.get(), typeNode.getContext());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) 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) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 85 with JCStatement

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

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