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 | package com.github.javaparser.ast.expr; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.nodeTypes.NodeWithType; |
25 | import com.github.javaparser.ast.observer.ObservableProperty; |
26 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
27 | import com.github.javaparser.ast.type.Type; |
28 | import com.github.javaparser.ast.visitor.GenericVisitor; |
29 | import com.github.javaparser.ast.visitor.VoidVisitor; |
30 | import static com.github.javaparser.utils.Utils.assertNotNull; |
31 | import com.github.javaparser.ast.Node; |
32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
33 | import com.github.javaparser.metamodel.ClassExprMetaModel; |
34 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
35 | import com.github.javaparser.TokenRange; |
36 | import java.util.function.Consumer; |
37 | import java.util.Optional; |
38 | import com.github.javaparser.ast.Generated; |
39 | |
40 | /** |
41 | * Defines an expression that accesses the class of a type. |
42 | * <br>{@code Object.class} |
43 | * |
44 | * @author Julio Vilmar Gesser |
45 | */ |
46 | public class ClassExpr extends Expression implements NodeWithType<ClassExpr, Type> { |
47 | |
48 | private Type type; |
49 | |
50 | public ClassExpr() { |
51 | this(null, new ClassOrInterfaceType()); |
52 | } |
53 | |
54 | @AllFieldsConstructor |
55 | public ClassExpr(Type type) { |
56 | this(null, type); |
57 | } |
58 | |
59 | /** |
60 | * This constructor is used by the parser and is considered private. |
61 | */ |
62 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
63 | public ClassExpr(TokenRange tokenRange, Type type) { |
64 | super(tokenRange); |
65 | setType(type); |
66 | customInitialization(); |
67 | } |
68 | |
69 | @Override |
70 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
71 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
72 | return v.visit(this, arg); |
73 | } |
74 | |
75 | @Override |
76 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
77 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
78 | v.visit(this, arg); |
79 | } |
80 | |
81 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
82 | public Type getType() { |
83 | return type; |
84 | } |
85 | |
86 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
87 | public ClassExpr setType(final Type type) { |
88 | assertNotNull(type); |
89 | if (type == this.type) { |
90 | return (ClassExpr) this; |
91 | } |
92 | notifyPropertyChange(ObservableProperty.TYPE, this.type, type); |
93 | if (this.type != null) |
94 | this.type.setParentNode(null); |
95 | this.type = type; |
96 | setAsParentNodeOf(type); |
97 | return this; |
98 | } |
99 | |
100 | @Override |
101 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
102 | public boolean remove(Node node) { |
103 | if (node == null) |
104 | return false; |
105 | return super.remove(node); |
106 | } |
107 | |
108 | @Override |
109 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
110 | public ClassExpr clone() { |
111 | return (ClassExpr) accept(new CloneVisitor(), null); |
112 | } |
113 | |
114 | @Override |
115 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
116 | public ClassExprMetaModel getMetaModel() { |
117 | return JavaParserMetaModel.classExprMetaModel; |
118 | } |
119 | |
120 | @Override |
121 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
122 | public boolean replace(Node node, Node replacementNode) { |
123 | if (node == null) |
124 | return false; |
125 | if (node == type) { |
126 | setType((Type) replacementNode); |
127 | return true; |
128 | } |
129 | return super.replace(node, replacementNode); |
130 | } |
131 | |
132 | @Override |
133 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
134 | public boolean isClassExpr() { |
135 | return true; |
136 | } |
137 | |
138 | @Override |
139 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
140 | public ClassExpr asClassExpr() { |
141 | return this; |
142 | } |
143 | |
144 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
145 | public void ifClassExpr(Consumer<ClassExpr> action) { |
146 | action.accept(this); |
147 | } |
148 | |
149 | @Override |
150 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
151 | public Optional<ClassExpr> toClassExpr() { |
152 | return Optional.of(this); |
153 | } |
154 | } |
155 |
Members