| 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.nodeTypes.NodeWithAnnotations; |
| 28 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 29 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 30 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 31 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 32 | import com.github.javaparser.metamodel.IntersectionTypeMetaModel; |
| 33 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 34 | import com.github.javaparser.metamodel.NonEmptyProperty; |
| 35 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 36 | import static java.util.stream.Collectors.joining; |
| 37 | import com.github.javaparser.TokenRange; |
| 38 | import com.github.javaparser.resolution.types.ResolvedIntersectionType; |
| 39 | import java.util.function.Consumer; |
| 40 | import java.util.Optional; |
| 41 | import com.github.javaparser.ast.Generated; |
| 42 | |
| 43 | /** |
| 44 | * Represents a set of types. A given value of this type has to be assignable to at all of the element types. |
| 45 | * As of Java 8 it is used in casts or while expressing bounds for generic types. |
| 46 | * <p> |
| 47 | * For example: |
| 48 | * {@code public class A<T extends Serializable & Cloneable> { }} |
| 49 | * <p> |
| 50 | * Or: |
| 51 | * {@code void foo((Serializable & Cloneable)myObject);} |
| 52 | * |
| 53 | * @since 3.0.0 |
| 54 | */ |
| 55 | public class IntersectionType extends Type implements NodeWithAnnotations<IntersectionType> { |
| 56 | |
| 57 | @NonEmptyProperty |
| 58 | private NodeList<ReferenceType> elements; |
| 59 | |
| 60 | @AllFieldsConstructor |
| 61 | public IntersectionType(NodeList<ReferenceType> elements) { |
| 62 | this(null, elements); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * This constructor is used by the parser and is considered private. |
| 67 | */ |
| 68 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 69 | public IntersectionType(TokenRange tokenRange, NodeList<ReferenceType> elements) { |
| 70 | super(tokenRange); |
| 71 | setElements(elements); |
| 72 | customInitialization(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 77 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 78 | return v.visit(this, arg); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 83 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 84 | v.visit(this, arg); |
| 85 | } |
| 86 | |
| 87 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 88 | public NodeList<ReferenceType> getElements() { |
| 89 | return elements; |
| 90 | } |
| 91 | |
| 92 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 93 | public IntersectionType setElements(final NodeList<ReferenceType> elements) { |
| 94 | assertNotNull(elements); |
| 95 | if (elements == this.elements) { |
| 96 | return (IntersectionType) this; |
| 97 | } |
| 98 | notifyPropertyChange(ObservableProperty.ELEMENTS, this.elements, elements); |
| 99 | if (this.elements != null) |
| 100 | this.elements.setParentNode(null); |
| 101 | this.elements = elements; |
| 102 | setAsParentNodeOf(elements); |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public IntersectionType setAnnotations(NodeList<AnnotationExpr> annotations) { |
| 108 | return (IntersectionType) super.setAnnotations(annotations); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 113 | public boolean remove(Node node) { |
| 114 | if (node == null) |
| 115 | return false; |
| 116 | for (int i = 0; i < elements.size(); i++) { |
| 117 | if (elements.get(i) == node) { |
| 118 | elements.remove(i); |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | return super.remove(node); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String asString() { |
| 127 | return elements.stream().map(Type::asString).collect(joining("&")); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 132 | public IntersectionType clone() { |
| 133 | return (IntersectionType) accept(new CloneVisitor(), null); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 138 | public IntersectionTypeMetaModel getMetaModel() { |
| 139 | return JavaParserMetaModel.intersectionTypeMetaModel; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 144 | public boolean replace(Node node, Node replacementNode) { |
| 145 | if (node == null) |
| 146 | return false; |
| 147 | for (int i = 0; i < elements.size(); i++) { |
| 148 | if (elements.get(i) == node) { |
| 149 | elements.set(i, (ReferenceType) replacementNode); |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return super.replace(node, replacementNode); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 158 | public boolean isIntersectionType() { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 164 | public IntersectionType asIntersectionType() { |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 169 | public void ifIntersectionType(Consumer<IntersectionType> action) { |
| 170 | action.accept(this); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public ResolvedIntersectionType resolve() { |
| 175 | return getSymbolResolver().toResolvedType(this, ResolvedIntersectionType.class); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 180 | public Optional<IntersectionType> toIntersectionType() { |
| 181 | return Optional.of(this); |
| 182 | } |
| 183 | } |
| 184 |
Members