Search in sources :

Example 1 with JCTypeApply

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

the class HandleBuilder method unpack.

private static void unpack(StringBuilder sb, JCExpression expr) {
    if (expr instanceof JCIdent) {
        sb.append(((JCIdent) expr).name.toString());
        return;
    }
    if (expr instanceof JCFieldAccess) {
        JCFieldAccess jcfa = (JCFieldAccess) expr;
        unpack(sb, jcfa.selected);
        sb.append(".").append(jcfa.name.toString());
        return;
    }
    if (expr instanceof JCTypeApply) {
        sb.setLength(0);
        sb.append("ERR:");
        sb.append("@Builder(toBuilder=true) is not supported if returning a type with generics applied to an intermediate.");
        sb.append("__ERR__");
        return;
    }
    sb.setLength(0);
    sb.append("ERR:");
    sb.append("Expected a type of some sort, not a " + expr.getClass().getName());
    sb.append("__ERR__");
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply)

Example 2 with JCTypeApply

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

the class HandleBuilder method handle.

@Override
public void handle(AnnotationValues<Builder> annotation, JCAnnotation ast, JavacNode annotationNode) {
    Builder builderInstance = annotation.getInstance();
    // These exist just to support the 'old' lombok.experimental.Builder, which had these properties. lombok.Builder no longer has them.
    boolean fluent = toBoolean(annotation.getActualExpression("fluent"), true);
    boolean chain = toBoolean(annotation.getActualExpression("chain"), true);
    String builderMethodName = builderInstance.builderMethodName();
    String buildMethodName = builderInstance.buildMethodName();
    String builderClassName = builderInstance.builderClassName();
    String toBuilderMethodName = "toBuilder";
    boolean toBuilder = builderInstance.toBuilder();
    java.util.List<Name> typeArgsForToBuilder = null;
    if (builderMethodName == null)
        builderMethodName = "builder";
    if (buildMethodName == null)
        buildMethodName = "build";
    if (builderClassName == null)
        builderClassName = "";
    if (!checkName("builderMethodName", builderMethodName, annotationNode))
        return;
    if (!checkName("buildMethodName", buildMethodName, annotationNode))
        return;
    if (!builderClassName.isEmpty()) {
        if (!checkName("builderClassName", builderClassName, annotationNode))
            return;
    }
    deleteAnnotationIfNeccessary(annotationNode, Builder.class, "lombok.experimental.Builder");
    JavacNode parent = annotationNode.up();
    java.util.List<BuilderFieldData> builderFields = new ArrayList<BuilderFieldData>();
    JCExpression returnType;
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrownExceptions = List.nil();
    Name nameOfBuilderMethod;
    JavacNode tdParent;
    JavacNode fillParametersFrom = parent.get() instanceof JCMethodDecl ? parent : null;
    boolean addCleaning = false;
    boolean isStatic = true;
    if (parent.get() instanceof JCClassDecl) {
        tdParent = parent;
        JCClassDecl td = (JCClassDecl) tdParent.get();
        ListBuffer<JavacNode> allFields = new ListBuffer<JavacNode>();
        boolean valuePresent = (hasAnnotation(lombok.Value.class, parent) || hasAnnotation("lombok.experimental.Value", parent));
        for (JavacNode fieldNode : HandleConstructor.findAllFields(tdParent, true)) {
            JCVariableDecl fd = (JCVariableDecl) fieldNode.get();
            JavacNode isDefault = findAnnotation(Builder.Default.class, fieldNode, true);
            boolean isFinal = (fd.mods.flags & Flags.FINAL) != 0 || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode));
            BuilderFieldData bfd = new BuilderFieldData();
            bfd.rawName = fd.name;
            bfd.name = removePrefixFromField(fieldNode);
            bfd.type = fd.vartype;
            bfd.singularData = getSingularData(fieldNode);
            bfd.originalFieldNode = fieldNode;
            if (bfd.singularData != null && isDefault != null) {
                isDefault.addError("@Builder.Default and @Singular cannot be mixed.");
                isDefault = null;
            }
            if (fd.init == null && isDefault != null) {
                isDefault.addWarning("@Builder.Default requires an initializing expression (' = something;').");
                isDefault = null;
            }
            if (fd.init != null && isDefault == null) {
                if (isFinal)
                    continue;
                fieldNode.addWarning("@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final.");
            }
            if (isDefault != null) {
                bfd.nameOfDefaultProvider = parent.toName("$default$" + bfd.name);
                bfd.nameOfSetFlag = parent.toName(bfd.name + "$set");
                JCMethodDecl md = generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode, td.typarams);
                recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
                if (md != null)
                    injectMethod(tdParent, md);
            }
            addObtainVia(bfd, fieldNode);
            builderFields.add(bfd);
            allFields.append(fieldNode);
        }
        handleConstructor.generateConstructor(tdParent, AccessLevel.PACKAGE, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, annotationNode);
        returnType = namePlusTypeParamsToTypeReference(tdParent.getTreeMaker(), td.name, td.typarams);
        typeParams = td.typarams;
        thrownExceptions = List.nil();
        nameOfBuilderMethod = null;
        if (builderClassName.isEmpty())
            builderClassName = td.name.toString() + "Builder";
    } else if (fillParametersFrom != null && fillParametersFrom.getName().toString().equals("<init>")) {
        JCMethodDecl jmd = (JCMethodDecl) fillParametersFrom.get();
        if (!jmd.typarams.isEmpty()) {
            annotationNode.addError("@Builder is not supported on constructors with constructor type parameters.");
            return;
        }
        tdParent = parent.up();
        JCClassDecl td = (JCClassDecl) tdParent.get();
        returnType = namePlusTypeParamsToTypeReference(tdParent.getTreeMaker(), td.name, td.typarams);
        typeParams = td.typarams;
        thrownExceptions = jmd.thrown;
        nameOfBuilderMethod = null;
        if (builderClassName.isEmpty())
            builderClassName = td.name.toString() + "Builder";
    } else if (fillParametersFrom != null) {
        tdParent = parent.up();
        JCClassDecl td = (JCClassDecl) tdParent.get();
        JCMethodDecl jmd = (JCMethodDecl) fillParametersFrom.get();
        isStatic = (jmd.mods.flags & Flags.STATIC) != 0;
        JCExpression fullReturnType = jmd.restype;
        returnType = fullReturnType;
        typeParams = jmd.typarams;
        thrownExceptions = jmd.thrown;
        nameOfBuilderMethod = jmd.name;
        if (returnType instanceof JCTypeApply) {
            returnType = cloneType(tdParent.getTreeMaker(), returnType, ast, annotationNode.getContext());
        }
        if (builderClassName.isEmpty()) {
            if (returnType instanceof JCFieldAccess) {
                builderClassName = ((JCFieldAccess) returnType).name.toString() + "Builder";
            } else if (returnType instanceof JCIdent) {
                Name n = ((JCIdent) returnType).name;
                for (JCTypeParameter tp : typeParams) {
                    if (tp.name.equals(n)) {
                        annotationNode.addError("@Builder requires specifying 'builderClassName' if used on methods with a type parameter as return type.");
                        return;
                    }
                }
                builderClassName = n.toString() + "Builder";
            } else if (returnType instanceof JCPrimitiveTypeTree) {
                builderClassName = returnType.toString() + "Builder";
                if (Character.isLowerCase(builderClassName.charAt(0))) {
                    builderClassName = Character.toTitleCase(builderClassName.charAt(0)) + builderClassName.substring(1);
                }
            } else if (returnType instanceof JCTypeApply) {
                JCExpression clazz = ((JCTypeApply) returnType).clazz;
                if (clazz instanceof JCFieldAccess) {
                    builderClassName = ((JCFieldAccess) clazz).name + "Builder";
                } else if (clazz instanceof JCIdent) {
                    builderClassName = ((JCIdent) clazz).name + "Builder";
                }
            }
            if (builderClassName.isEmpty()) {
                // This shouldn't happen.
                System.err.println("Lombok bug ID#20140614-1651: javac HandleBuilder: return type to name conversion failed: " + returnType.getClass());
                builderClassName = td.name.toString() + "Builder";
            }
        }
        if (toBuilder) {
            final String TO_BUILDER_NOT_SUPPORTED = "@Builder(toBuilder=true) is only supported if you return your own type.";
            if (returnType instanceof JCArrayTypeTree) {
                annotationNode.addError(TO_BUILDER_NOT_SUPPORTED);
                return;
            }
            Name simpleName;
            String pkg;
            List<JCExpression> tpOnRet = List.nil();
            if (fullReturnType instanceof JCTypeApply) {
                tpOnRet = ((JCTypeApply) fullReturnType).arguments;
            }
            JCExpression namingType = returnType;
            if (returnType instanceof JCTypeApply)
                namingType = ((JCTypeApply) returnType).clazz;
            if (namingType instanceof JCIdent) {
                simpleName = ((JCIdent) namingType).name;
                pkg = null;
            } else if (namingType instanceof JCFieldAccess) {
                JCFieldAccess jcfa = (JCFieldAccess) namingType;
                simpleName = jcfa.name;
                pkg = unpack(jcfa.selected);
                if (pkg.startsWith("ERR:")) {
                    String err = pkg.substring(4, pkg.indexOf("__ERR__"));
                    annotationNode.addError(err);
                    return;
                }
            } else {
                annotationNode.addError("Expected a (parameterized) type here instead of a " + namingType.getClass().getName());
                return;
            }
            if (pkg != null && !parent.getPackageDeclaration().equals(pkg)) {
                annotationNode.addError(TO_BUILDER_NOT_SUPPORTED);
                return;
            }
            if (!tdParent.getName().contentEquals(simpleName)) {
                annotationNode.addError(TO_BUILDER_NOT_SUPPORTED);
                return;
            }
            List<JCTypeParameter> tpOnMethod = jmd.typarams;
            List<JCTypeParameter> tpOnType = ((JCClassDecl) tdParent.get()).typarams;
            typeArgsForToBuilder = new ArrayList<Name>();
            for (JCTypeParameter tp : tpOnMethod) {
                int pos = -1;
                int idx = -1;
                for (JCExpression tOnRet : tpOnRet) {
                    idx++;
                    if (!(tOnRet instanceof JCIdent))
                        continue;
                    if (((JCIdent) tOnRet).name != tp.name)
                        continue;
                    pos = idx;
                }
                if (pos == -1 || tpOnType.size() <= pos) {
                    annotationNode.addError("@Builder(toBuilder=true) requires that each type parameter on the static method is part of the typeargs of the return value. Type parameter " + tp.name + " is not part of the return type.");
                    return;
                }
                typeArgsForToBuilder.add(tpOnType.get(pos).name);
            }
        }
    } else {
        annotationNode.addError("@Builder is only supported on types, constructors, and methods.");
        return;
    }
    if (fillParametersFrom != null) {
        for (JavacNode param : fillParametersFrom.down()) {
            if (param.getKind() != Kind.ARGUMENT)
                continue;
            BuilderFieldData bfd = new BuilderFieldData();
            JCVariableDecl raw = (JCVariableDecl) param.get();
            bfd.name = raw.name;
            bfd.rawName = raw.name;
            bfd.type = raw.vartype;
            bfd.singularData = getSingularData(param);
            bfd.originalFieldNode = param;
            addObtainVia(bfd, param);
            builderFields.add(bfd);
        }
    }
    JavacNode builderType = findInnerClass(tdParent, builderClassName);
    if (builderType == null) {
        builderType = makeBuilderClass(isStatic, annotationNode, tdParent, builderClassName, typeParams, ast);
    } else {
        JCClassDecl builderTypeDeclaration = (JCClassDecl) builderType.get();
        if (isStatic && !builderTypeDeclaration.getModifiers().getFlags().contains(Modifier.STATIC)) {
            annotationNode.addError("Existing Builder must be a static inner class.");
            return;
        } else if (!isStatic && builderTypeDeclaration.getModifiers().getFlags().contains(Modifier.STATIC)) {
            annotationNode.addError("Existing Builder must be a non-static inner class.");
            return;
        }
        sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(builderType, annotationNode);
        /* generate errors for @Singular BFDs that have one already defined node. */
        {
            for (BuilderFieldData bfd : builderFields) {
                SingularData sd = bfd.singularData;
                if (sd == null)
                    continue;
                JavacSingularizer singularizer = sd.getSingularizer();
                if (singularizer == null)
                    continue;
                if (singularizer.checkForAlreadyExistingNodesAndGenerateError(builderType, sd)) {
                    bfd.singularData = null;
                }
            }
        }
    }
    for (BuilderFieldData bfd : builderFields) {
        if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
            if (bfd.singularData.getSingularizer().requiresCleaning()) {
                addCleaning = true;
                break;
            }
        }
        if (bfd.obtainVia != null) {
            if (bfd.obtainVia.field().isEmpty() == bfd.obtainVia.method().isEmpty()) {
                bfd.obtainViaNode.addError("The syntax is either @ObtainVia(field = \"fieldName\") or @ObtainVia(method = \"methodName\").");
                return;
            }
            if (bfd.obtainVia.method().isEmpty() && bfd.obtainVia.isStatic()) {
                bfd.obtainViaNode.addError("@ObtainVia(isStatic = true) is not valid unless 'method' has been set.");
                return;
            }
        }
    }
    generateBuilderFields(builderType, builderFields, ast);
    if (addCleaning) {
        JavacTreeMaker maker = builderType.getTreeMaker();
        JCVariableDecl uncleanField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName("$lombokUnclean"), maker.TypeIdent(CTC_BOOLEAN), null);
        injectFieldAndMarkGenerated(builderType, uncleanField);
    }
    if (constructorExists(builderType) == MemberExistsResult.NOT_EXISTS) {
        JCMethodDecl cd = HandleConstructor.createConstructor(AccessLevel.PACKAGE, List.<JCAnnotation>nil(), builderType, List.<JavacNode>nil(), false, annotationNode);
        if (cd != null)
            injectMethod(builderType, cd);
    }
    for (BuilderFieldData bfd : builderFields) {
        makeSetterMethodsForBuilder(builderType, bfd, annotationNode, fluent, chain);
    }
    if (methodExists(buildMethodName, builderType, -1) == MemberExistsResult.NOT_EXISTS) {
        JCMethodDecl md = generateBuildMethod(tdParent, isStatic, buildMethodName, nameOfBuilderMethod, returnType, builderFields, builderType, thrownExceptions, ast, addCleaning);
        if (md != null)
            injectMethod(builderType, md);
    }
    if (methodExists("toString", builderType, 0) == MemberExistsResult.NOT_EXISTS) {
        java.util.List<JavacNode> fieldNodes = new ArrayList<JavacNode>();
        for (BuilderFieldData bfd : builderFields) {
            fieldNodes.addAll(bfd.createdFields);
        }
        JCMethodDecl md = HandleToString.createToString(builderType, fieldNodes, true, false, FieldAccess.ALWAYS_FIELD, ast);
        if (md != null)
            injectMethod(builderType, md);
    }
    if (addCleaning)
        injectMethod(builderType, generateCleanMethod(builderFields, builderType, ast));
    if (methodExists(builderMethodName, tdParent, -1) == MemberExistsResult.NOT_EXISTS) {
        JCMethodDecl md = generateBuilderMethod(isStatic, builderMethodName, builderClassName, annotationNode, tdParent, typeParams);
        recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
        if (md != null)
            injectMethod(tdParent, md);
    }
    if (toBuilder) {
        switch(methodExists(toBuilderMethodName, tdParent, 0)) {
            case EXISTS_BY_USER:
                annotationNode.addWarning("Not generating toBuilder() as it already exists.");
                return;
            case NOT_EXISTS:
                List<JCTypeParameter> tps = typeParams;
                if (typeArgsForToBuilder != null) {
                    ListBuffer<JCTypeParameter> lb = new ListBuffer<JCTypeParameter>();
                    JavacTreeMaker maker = tdParent.getTreeMaker();
                    for (Name n : typeArgsForToBuilder) {
                        lb.append(maker.TypeParameter(n, List.<JCExpression>nil()));
                    }
                    tps = lb.toList();
                }
                JCMethodDecl md = generateToBuilderMethod(toBuilderMethodName, builderClassName, tdParent, tps, builderFields, fluent, ast);
                if (md != null)
                    injectMethod(tdParent, md);
        }
    }
    recursiveSetGeneratedBy(builderType.get(), ast, annotationNode.getContext());
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) Builder(lombok.Builder) ListBuffer(com.sun.tools.javac.util.ListBuffer) ArrayList(java.util.ArrayList) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JavacSingularizer(lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer) Name(com.sun.tools.javac.util.Name) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JavacNode(lombok.javac.JavacNode) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SingularData(lombok.javac.handlers.JavacSingularsRecipes.SingularData)

Example 3 with JCTypeApply

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

the class HandleBuilder method getSingularData.

/**
 * Returns the explicitly requested singular annotation on this node (field
 * or parameter), or null if there's no {@code @Singular} annotation on it.
 *
 * @param node The node (field or method param) to inspect for its name and potential {@code @Singular} annotation.
 */
private SingularData getSingularData(JavacNode node) {
    for (JavacNode child : node.down()) {
        if (!annotationTypeMatches(Singular.class, child))
            continue;
        Name pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((JCVariableDecl) node.get()).name;
        AnnotationValues<Singular> ann = createAnnotation(Singular.class, child);
        deleteAnnotationIfNeccessary(child, Singular.class);
        String explicitSingular = ann.getInstance().value();
        if (explicitSingular.isEmpty()) {
            if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) {
                node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled.");
                explicitSingular = pluralName.toString();
            } else {
                explicitSingular = autoSingularize(pluralName.toString());
                if (explicitSingular == null) {
                    node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))");
                    explicitSingular = pluralName.toString();
                }
            }
        }
        Name singularName = node.toName(explicitSingular);
        JCExpression type = null;
        if (node.get() instanceof JCVariableDecl) {
            type = ((JCVariableDecl) node.get()).vartype;
        }
        String name = null;
        List<JCExpression> typeArgs = List.nil();
        if (type instanceof JCTypeApply) {
            typeArgs = ((JCTypeApply) type).arguments;
            type = ((JCTypeApply) type).clazz;
        }
        name = type.toString();
        String targetFqn = JavacSingularsRecipes.get().toQualified(name);
        JavacSingularizer singularizer = JavacSingularsRecipes.get().getSingularizer(targetFqn);
        if (singularizer == null) {
            node.addError("Lombok does not know how to create the singular-form builder methods for type '" + name + "'; they won't be generated.");
            return null;
        }
        return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer);
    }
    return null;
}
Also used : Singular(lombok.Singular) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) SingularData(lombok.javac.handlers.JavacSingularsRecipes.SingularData) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JavacSingularizer(lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 4 with JCTypeApply

use of com.sun.tools.javac.tree.JCTree.JCTypeApply in project checker-framework by typetools.

the class CFTreeBuilder method createAnnotatedType.

/**
 * Builds an AST Tree representing a type, including AnnotationTrees for its annotations. This
 * internal method differs from the public {@link #buildAnnotatedType(TypeMirror)} only in that it
 * does not reset the list of visited wildcards.
 *
 * @param type the type for which to create a tree
 * @return a Tree representing the type
 */
private Tree createAnnotatedType(TypeMirror type) {
    // Implementation based on com.sun.tools.javac.tree.TreeMaker.Type
    // Convert the annotations from a set of AnnotationMirrors
    // to a list of AnnotationTrees.
    java.util.List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors();
    List<JCAnnotation> annotationTrees = convertAnnotationMirrorsToAnnotationTrees(annotations);
    // Convert the underlying type from a TypeMirror to an ExpressionTree and combine with the
    // AnnotationTrees to form a ClassTree of kind ANNOTATION_TYPE.
    JCExpression typeTree;
    switch(type.getKind()) {
        case BYTE:
            typeTree = maker.TypeIdent(TypeTag.BYTE);
            break;
        case CHAR:
            typeTree = maker.TypeIdent(TypeTag.CHAR);
            break;
        case SHORT:
            typeTree = maker.TypeIdent(TypeTag.SHORT);
            break;
        case INT:
            typeTree = maker.TypeIdent(TypeTag.INT);
            break;
        case LONG:
            typeTree = maker.TypeIdent(TypeTag.LONG);
            break;
        case FLOAT:
            typeTree = maker.TypeIdent(TypeTag.FLOAT);
            break;
        case DOUBLE:
            typeTree = maker.TypeIdent(TypeTag.DOUBLE);
            break;
        case BOOLEAN:
            typeTree = maker.TypeIdent(TypeTag.BOOLEAN);
            break;
        case VOID:
            typeTree = maker.TypeIdent(TypeTag.VOID);
            break;
        case TYPEVAR:
            // No recursive annotations.
            TypeVariable underlyingTypeVar = (TypeVariable) type;
            typeTree = maker.Ident((TypeSymbol) underlyingTypeVar.asElement());
            break;
        case WILDCARD:
            WildcardType wildcardType = (WildcardType) type;
            boolean visitedBefore = !visitedWildcards.add(wildcardType);
            if (!visitedBefore && wildcardType.getExtendsBound() != null) {
                Tree annotatedExtendsBound = createAnnotatedType(wildcardType.getExtendsBound());
                typeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), (JCTree) annotatedExtendsBound);
            } else if (!visitedBefore && wildcardType.getSuperBound() != null) {
                Tree annotatedSuperBound = createAnnotatedType(wildcardType.getSuperBound());
                typeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), (JCTree) annotatedSuperBound);
            } else {
                typeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
            }
            break;
        case INTERSECTION:
            IntersectionType intersectionType = (IntersectionType) type;
            List<JCExpression> components = List.nil();
            for (TypeMirror bound : intersectionType.getBounds()) {
                components = components.append((JCExpression) createAnnotatedType(bound));
            }
            typeTree = maker.TypeIntersection(components);
            break;
        // TODO: case UNION similar to INTERSECTION, but write test first.
        case DECLARED:
            typeTree = maker.Type((Type) type);
            if (typeTree instanceof JCTypeApply) {
                // Replace the type parameters with annotated versions.
                DeclaredType annotatedDeclaredType = (DeclaredType) type;
                List<JCExpression> typeArgTrees = List.nil();
                for (TypeMirror arg : annotatedDeclaredType.getTypeArguments()) {
                    typeArgTrees = typeArgTrees.append((JCExpression) createAnnotatedType(arg));
                }
                JCExpression clazz = (JCExpression) ((JCTypeApply) typeTree).getType();
                typeTree = maker.TypeApply(clazz, typeArgTrees);
            }
            break;
        case ARRAY:
            ArrayType arrayType = (ArrayType) type;
            Tree componentTree = createAnnotatedType(arrayType.getComponentType());
            typeTree = maker.TypeArray((JCExpression) componentTree);
            break;
        case ERROR:
            typeTree = maker.TypeIdent(TypeTag.ERROR);
            break;
        default:
            assert false : "unexpected type: " + type;
            typeTree = null;
            break;
    }
    typeTree.setType((Type) type);
    if (annotationTrees.isEmpty()) {
        return typeTree;
    }
    JCAnnotatedType annotatedTypeTree = maker.AnnotatedType(annotationTrees, typeTree);
    annotatedTypeTree.setType((Type) type);
    return annotatedTypeTree;
}
Also used : JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) JCTree(com.sun.tools.javac.tree.JCTree) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) ArrayType(javax.lang.model.type.ArrayType) WildcardType(javax.lang.model.type.WildcardType) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) IntersectionType(javax.lang.model.type.IntersectionType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) IntersectionType(javax.lang.model.type.IntersectionType) TypeVariable(javax.lang.model.type.TypeVariable) TypeMirror(javax.lang.model.type.TypeMirror) Tree(com.sun.source.tree.Tree) JCTree(com.sun.tools.javac.tree.JCTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) DeclaredType(javax.lang.model.type.DeclaredType)

Example 5 with JCTypeApply

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

JCTypeApply (com.sun.tools.javac.tree.JCTree.JCTypeApply)6 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)4 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)3 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 JCTree (com.sun.tools.javac.tree.JCTree)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)2 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)2 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)2 JCWildcard (com.sun.tools.javac.tree.JCTree.JCWildcard)2 TypeBoundKind (com.sun.tools.javac.tree.JCTree.TypeBoundKind)2 Name (com.sun.tools.javac.util.Name)2 JavacNode (lombok.javac.JavacNode)2 JavacSingularizer (lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer)2 SingularData (lombok.javac.handlers.JavacSingularsRecipes.SingularData)2 Tree (com.sun.source.tree.Tree)1 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 Type (com.sun.tools.javac.code.Type)1