JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/expr/Name.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 */
21package com.github.javaparser.ast.expr;
22
23import com.github.javaparser.TokenRange;
24import com.github.javaparser.ast.AllFieldsConstructor;
25import com.github.javaparser.ast.Node;
26import com.github.javaparser.ast.nodeTypes.NodeWithIdentifier;
27import com.github.javaparser.ast.observer.ObservableProperty;
28import com.github.javaparser.ast.visitor.GenericVisitor;
29import com.github.javaparser.ast.visitor.VoidVisitor;
30import com.github.javaparser.metamodel.JavaParserMetaModel;
31import com.github.javaparser.metamodel.NameMetaModel;
32import com.github.javaparser.metamodel.NonEmptyProperty;
33import com.github.javaparser.metamodel.OptionalProperty;
34import java.util.Optional;
35import static com.github.javaparser.utils.Utils.assertNonEmpty;
36import com.github.javaparser.ast.visitor.CloneVisitor;
37import com.github.javaparser.ast.Generated;
38
39/**
40 * A name that may consist of multiple identifiers.
41 * In other words: it.may.contain.dots.
42 * <p>
43 * The rightmost identifier is "identifier",
44 * The one to the left of it is "qualifier.identifier", etc.
45 * <p>
46 * You can construct one from a String with the name(...) method.
47 *
48 * @author Julio Vilmar Gesser
49 * @see SimpleName
50 */
51public class Name extends Node implements NodeWithIdentifier<Name> {
52
53    @NonEmptyProperty
54    private String identifier;
55
56    @OptionalProperty
57    private Name qualifier;
58
59    public Name() {
60        this(nullnull"empty");
61    }
62
63    public Name(final String identifier) {
64        this(nullnullidentifier);
65    }
66
67    @AllFieldsConstructor
68    public Name(Name qualifierfinal String identifier) {
69        this(nullqualifieridentifier);
70    }
71
72    /**
73     * This constructor is used by the parser and is considered private.
74     */
75    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
76    public Name(TokenRange tokenRangeName qualifierString identifier) {
77        super(tokenRange);
78        setQualifier(qualifier);
79        setIdentifier(identifier);
80        customInitialization();
81    }
82
83    @Override
84    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
85    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
86        return v.visit(this, arg);
87    }
88
89    @Override
90    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
91    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
92        v.visit(this, arg);
93    }
94
95    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
96    public String getIdentifier() {
97        return identifier;
98    }
99
100    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
101    public Name setIdentifier(final String identifier) {
102        assertNonEmpty(identifier);
103        if (identifier == this.identifier) {
104            return (Name) this;
105        }
106        notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifieridentifier);
107        this.identifier = identifier;
108        return this;
109    }
110
111    /**
112     * @return the complete qualified name. Only the identifiers and the dots, so no comments or whitespace.
113     */
114    public String asString() {
115        if (qualifier != null) {
116            return qualifier.asString() + "." + identifier;
117        }
118        return identifier;
119    }
120
121    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
122    public Optional<NamegetQualifier() {
123        return Optional.ofNullable(qualifier);
124    }
125
126    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
127    public Name setQualifier(final Name qualifier) {
128        if (qualifier == this.qualifier) {
129            return (Name) this;
130        }
131        notifyPropertyChange(ObservableProperty.QUALIFIER, this.qualifierqualifier);
132        if (this.qualifier != null)
133            this.qualifier.setParentNode(null);
134        this.qualifier = qualifier;
135        setAsParentNodeOf(qualifier);
136        return this;
137    }
138
139    @Override
140    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
141    public boolean remove(Node node) {
142        if (node == null)
143            return false;
144        if (qualifier != null) {
145            if (node == qualifier) {
146                removeQualifier();
147                return true;
148            }
149        }
150        return super.remove(node);
151    }
152
153    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
154    public Name removeQualifier() {
155        return setQualifier((Namenull);
156    }
157
158    @Override
159    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
160    public Name clone() {
161        return (Nameaccept(new CloneVisitor(), null);
162    }
163
164    @Override
165    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
166    public NameMetaModel getMetaModel() {
167        return JavaParserMetaModel.nameMetaModel;
168    }
169
170    @Override
171    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
172    public boolean replace(Node nodeNode replacementNode) {
173        if (node == null)
174            return false;
175        if (qualifier != null) {
176            if (node == qualifier) {
177                setQualifier((NamereplacementNode);
178                return true;
179            }
180        }
181        return super.replace(nodereplacementNode);
182    }
183
184    /**
185     * A top level name is a name that is not contained in a larger Name instance.
186     */
187    public boolean isTopLevel() {
188        return !isInternal();
189    }
190
191    /**
192     * An internal name is a name that constitutes a part of a larger Name instance.
193     */
194    public boolean isInternal() {
195        return getParentNode().filter(parent -> parent instanceof Name).isPresent();
196    }
197}
198
MembersX
Name:getMetaModel
Name:remove
Name:qualifier
Name:getIdentifier
Name:replace
Name:setIdentifier
Name:accept
Name:Name
Name:setQualifier
Name:asString
Name:isTopLevel
Name:getQualifier
Name:isInternal
Name:removeQualifier
Name:identifier
Name:clone
Members
X