| 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.type; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | import com.github.javaparser.ast.NodeList; |
| 26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 27 | import com.github.javaparser.ast.expr.SimpleName; |
| 28 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
| 29 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
| 30 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
| 31 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 33 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 34 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 35 | import com.github.javaparser.metamodel.ClassOrInterfaceTypeMetaModel; |
| 36 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 37 | import java.util.Optional; |
| 38 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 39 | import static java.util.stream.Collectors.joining; |
| 40 | import com.github.javaparser.TokenRange; |
| 41 | import com.github.javaparser.metamodel.OptionalProperty; |
| 42 | import com.github.javaparser.resolution.types.ResolvedReferenceType; |
| 43 | import java.util.function.Consumer; |
| 44 | import com.github.javaparser.ast.Generated; |
| 45 | |
| 46 | /** |
| 47 | * A class or an interface type. |
| 48 | * <br>{@code Object} |
| 49 | * <br>{@code HashMap<String, String>} |
| 50 | * <br>{@code java.util.Punchcard} |
| 51 | * <p> |
| 52 | * <p>Note that the syntax is ambiguous here, and JavaParser does not know what is to the left of the class. It assumes |
| 53 | * cases like {@code Map.Entry} where Map is the scope of Entry. In {@code java.util.Punchcard}, it will not |
| 54 | * recognize that java and util are parts of the package name. Instead, it will set util as the scope of Punchcard, as a |
| 55 | * ClassOrInterfaceType (which it is not). In turn, util will have java as its scope, also as a |
| 56 | * ClassOrInterfaceType</p> |
| 57 | * |
| 58 | * @author Julio Vilmar Gesser |
| 59 | */ |
| 60 | public class ClassOrInterfaceType extends ReferenceType implements NodeWithSimpleName<ClassOrInterfaceType>, NodeWithAnnotations<ClassOrInterfaceType>, NodeWithTypeArguments<ClassOrInterfaceType> { |
| 61 | |
| 62 | @OptionalProperty |
| 63 | private ClassOrInterfaceType scope; |
| 64 | |
| 65 | private SimpleName name; |
| 66 | |
| 67 | @OptionalProperty |
| 68 | private NodeList<Type> typeArguments; |
| 69 | |
| 70 | public ClassOrInterfaceType() { |
| 71 | this(null, null, new SimpleName(), null, new NodeList<>()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @deprecated use JavaParser.parseClassOrInterfaceType instead. This constructor does not understand generics. |
| 76 | */ |
| 77 | public ClassOrInterfaceType(final String name) { |
| 78 | this(null, null, new SimpleName(name), null, new NodeList<>()); |
| 79 | } |
| 80 | |
| 81 | public ClassOrInterfaceType(final ClassOrInterfaceType scope, final String name) { |
| 82 | this(null, scope, new SimpleName(name), null, new NodeList<>()); |
| 83 | } |
| 84 | |
| 85 | public ClassOrInterfaceType(final ClassOrInterfaceType scope, final SimpleName name, final NodeList<Type> typeArguments) { |
| 86 | this(null, scope, name, typeArguments, new NodeList<>()); |
| 87 | } |
| 88 | |
| 89 | @AllFieldsConstructor |
| 90 | public ClassOrInterfaceType(final ClassOrInterfaceType scope, final SimpleName name, final NodeList<Type> typeArguments, final NodeList<AnnotationExpr> annotations) { |
| 91 | this(null, scope, name, typeArguments, annotations); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * This constructor is used by the parser and is considered private. |
| 96 | */ |
| 97 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 98 | public ClassOrInterfaceType(TokenRange tokenRange, ClassOrInterfaceType scope, SimpleName name, NodeList<Type> typeArguments, NodeList<AnnotationExpr> annotations) { |
| 99 | super(tokenRange, annotations); |
| 100 | setScope(scope); |
| 101 | setName(name); |
| 102 | setTypeArguments(typeArguments); |
| 103 | customInitialization(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 108 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 109 | return v.visit(this, arg); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 114 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 115 | v.visit(this, arg); |
| 116 | } |
| 117 | |
| 118 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 119 | public SimpleName getName() { |
| 120 | return name; |
| 121 | } |
| 122 | |
| 123 | public String getNameWithScope() { |
| 124 | StringBuilder str = new StringBuilder(); |
| 125 | getScope().ifPresent(s -> str.append(s.getNameWithScope()).append(".")); |
| 126 | str.append(name.asString()); |
| 127 | return str.toString(); |
| 128 | } |
| 129 | |
| 130 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 131 | public Optional<ClassOrInterfaceType> getScope() { |
| 132 | return Optional.ofNullable(scope); |
| 133 | } |
| 134 | |
| 135 | public boolean isBoxedType() { |
| 136 | return PrimitiveType.unboxMap.containsKey(name.getIdentifier()); |
| 137 | } |
| 138 | |
| 139 | public PrimitiveType toUnboxedType() throws UnsupportedOperationException { |
| 140 | if (!isBoxedType()) { |
| 141 | throw new UnsupportedOperationException(name + " isn't a boxed type."); |
| 142 | } |
| 143 | return new PrimitiveType(PrimitiveType.unboxMap.get(name.getIdentifier())); |
| 144 | } |
| 145 | |
| 146 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 147 | public ClassOrInterfaceType setName(final SimpleName name) { |
| 148 | assertNotNull(name); |
| 149 | if (name == this.name) { |
| 150 | return (ClassOrInterfaceType) this; |
| 151 | } |
| 152 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
| 153 | if (this.name != null) |
| 154 | this.name.setParentNode(null); |
| 155 | this.name = name; |
| 156 | setAsParentNodeOf(name); |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets the scope |
| 162 | * |
| 163 | * @param scope the scope, can be null |
| 164 | * @return this, the ClassOrInterfaceType |
| 165 | */ |
| 166 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 167 | public ClassOrInterfaceType setScope(final ClassOrInterfaceType scope) { |
| 168 | if (scope == this.scope) { |
| 169 | return (ClassOrInterfaceType) this; |
| 170 | } |
| 171 | notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope); |
| 172 | if (this.scope != null) |
| 173 | this.scope.setParentNode(null); |
| 174 | this.scope = scope; |
| 175 | setAsParentNodeOf(scope); |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 180 | public Optional<NodeList<Type>> getTypeArguments() { |
| 181 | return Optional.ofNullable(typeArguments); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Sets the typeArguments |
| 186 | * |
| 187 | * @param typeArguments the typeArguments, can be null |
| 188 | * @return this, the ClassOrInterfaceType |
| 189 | */ |
| 190 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 191 | public ClassOrInterfaceType setTypeArguments(final NodeList<Type> typeArguments) { |
| 192 | if (typeArguments == this.typeArguments) { |
| 193 | return (ClassOrInterfaceType) this; |
| 194 | } |
| 195 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
| 196 | if (this.typeArguments != null) |
| 197 | this.typeArguments.setParentNode(null); |
| 198 | this.typeArguments = typeArguments; |
| 199 | setAsParentNodeOf(typeArguments); |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public ClassOrInterfaceType setAnnotations(NodeList<AnnotationExpr> annotations) { |
| 205 | return (ClassOrInterfaceType) super.setAnnotations(annotations); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 210 | public boolean remove(Node node) { |
| 211 | if (node == null) |
| 212 | return false; |
| 213 | if (scope != null) { |
| 214 | if (node == scope) { |
| 215 | removeScope(); |
| 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | if (typeArguments != null) { |
| 220 | for (int i = 0; i < typeArguments.size(); i++) { |
| 221 | if (typeArguments.get(i) == node) { |
| 222 | typeArguments.remove(i); |
| 223 | return true; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return super.remove(node); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public String asString() { |
| 232 | StringBuilder str = new StringBuilder(); |
| 233 | getScope().ifPresent(s -> str.append(s.asString()).append(".")); |
| 234 | str.append(name.asString()); |
| 235 | getTypeArguments().ifPresent(ta -> str.append(ta.stream().map(Type::asString).collect(joining(",", "<", ">")))); |
| 236 | return str.toString(); |
| 237 | } |
| 238 | |
| 239 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 240 | public ClassOrInterfaceType removeScope() { |
| 241 | return setScope((ClassOrInterfaceType) null); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 246 | public ClassOrInterfaceType clone() { |
| 247 | return (ClassOrInterfaceType) accept(new CloneVisitor(), null); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 252 | public ClassOrInterfaceTypeMetaModel getMetaModel() { |
| 253 | return JavaParserMetaModel.classOrInterfaceTypeMetaModel; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 258 | public boolean replace(Node node, Node replacementNode) { |
| 259 | if (node == null) |
| 260 | return false; |
| 261 | if (node == name) { |
| 262 | setName((SimpleName) replacementNode); |
| 263 | return true; |
| 264 | } |
| 265 | if (scope != null) { |
| 266 | if (node == scope) { |
| 267 | setScope((ClassOrInterfaceType) replacementNode); |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | if (typeArguments != null) { |
| 272 | for (int i = 0; i < typeArguments.size(); i++) { |
| 273 | if (typeArguments.get(i) == node) { |
| 274 | typeArguments.set(i, (Type) replacementNode); |
| 275 | return true; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return super.replace(node, replacementNode); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 284 | public boolean isClassOrInterfaceType() { |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 290 | public ClassOrInterfaceType asClassOrInterfaceType() { |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 295 | public void ifClassOrInterfaceType(Consumer<ClassOrInterfaceType> action) { |
| 296 | action.accept(this); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public ResolvedReferenceType resolve() { |
| 301 | return getSymbolResolver().toResolvedType(this, ResolvedReferenceType.class); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 306 | public Optional<ClassOrInterfaceType> toClassOrInterfaceType() { |
| 307 | return Optional.of(this); |
| 308 | } |
| 309 | } |
| 310 |
Members