JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/nodeTypes/NodeWithTypeArguments.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.ast.nodeTypes;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.NodeList;
26import com.github.javaparser.ast.type.Type;
27import com.github.javaparser.metamodel.DerivedProperty;
28
29import java.util.Optional;
30
31import static com.github.javaparser.ast.NodeList.nodeList;
32
33/**
34 * A node that can have type arguments.
35 * <p>
36 * <pre>
37 *     new X();        --&gt; typeArguments == Optional is empty
38 *     new X&lt;&gt;();      --&gt; typeArguments = [], diamondOperator = true
39 *     new X&lt;C,D&gt;();   --&gt; typeArguments = [C,D], diamondOperator = false
40 * </pre>
41 * Only ObjectCreationExpr uses the diamond operator.
42 * On other nodes it is treated the same as the first case.
43 */
44public interface NodeWithTypeArguments<N extends Node> {
45    /**
46     * @return the types that can be found in the type arguments: {@code  <String, Integer>}.
47     */
48    Optional<NodeList<Type>> getTypeArguments();
49
50    /**
51     * Allows you to set the generic arguments
52     *
53     * @param typeArguments The list of types of the generics, can be null
54     */
55    N setTypeArguments(NodeList<TypetypeArguments);
56
57    /**
58     * @return whether the type arguments look like {@code <>}.
59     */
60    @DerivedProperty
61    default boolean isUsingDiamondOperator() {
62        return getTypeArguments().isPresent() && getTypeArguments().get().isEmpty();
63    }
64
65    /**
66     * Sets the type arguments to {@code <>}.
67     */
68    @SuppressWarnings("unchecked")
69    default N setDiamondOperator() {
70        return setTypeArguments(new NodeList<>());
71    }
72
73    /**
74     * Removes all type arguments, including the surrounding {@code <>}.
75     */
76    @SuppressWarnings("unchecked")
77    default N removeTypeArguments() {
78        return setTypeArguments((NodeList<Type>) null);
79    }
80
81    @SuppressWarnings("unchecked")
82    default N setTypeArguments(Type... typeArguments) {
83        return setTypeArguments(nodeList(typeArguments));
84    }
85}
86
MembersX
NodeWithTypeArguments:getTypeArguments
NodeWithTypeArguments:setDiamondOperator
NodeWithTypeArguments:setTypeArguments
NodeWithTypeArguments:isUsingDiamondOperator
NodeWithTypeArguments:removeTypeArguments
Members
X