1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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.body; 22 23 import com.github.javaparser.ast.AllFieldsConstructor; 24 import com.github.javaparser.ast.Modifier; 25 import com.github.javaparser.ast.Node; 26 import com.github.javaparser.ast.NodeList; 27 import com.github.javaparser.ast.expr.AnnotationExpr; 28 import com.github.javaparser.ast.expr.SimpleName; 29 import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; 30 import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; 31 import com.github.javaparser.ast.nodeTypes.NodeWithType; 32 import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier; 33 import com.github.javaparser.ast.observer.ObservableProperty; 34 import com.github.javaparser.ast.type.ClassOrInterfaceType; 35 import com.github.javaparser.ast.type.Type; 36 import com.github.javaparser.ast.visitor.CloneVisitor; 37 import com.github.javaparser.ast.visitor.GenericVisitor; 38 import com.github.javaparser.ast.visitor.VoidVisitor; 39 import com.github.javaparser.metamodel.JavaParserMetaModel; 40 import com.github.javaparser.metamodel.ParameterMetaModel; 41 import static com.github.javaparser.utils.Utils.assertNotNull; 42 import com.github.javaparser.TokenRange; 43 import com.github.javaparser.resolution.Resolvable; 44 import com.github.javaparser.resolution.declarations.ResolvedParameterDeclaration; 45 import com.github.javaparser.ast.Generated; 46 47 /** 48 * The parameters to a method or lambda. Lambda parameters may have inferred types, in that case "type" is UnknownType. 49 * <br/>Note that <a href="https://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments">parameters 50 * are different from arguments.</a> <br/>"String x" and "float y" are the parameters in <code>int abc(String x, float 51 * y) {...}</code> 52 * 53 * <br/>All annotations preceding the type will be set on this object, not on the type. 54 * JavaParser doesn't know if it they are applicable to the parameter or the type. 55 * 56 * @author Julio Vilmar Gesser 57 */ 58 public class Parameter extends Node implements NodeWithType<Parameter, Type>, NodeWithAnnotations<Parameter>, NodeWithSimpleName<Parameter>, NodeWithFinalModifier<Parameter>, Resolvable<ResolvedParameterDeclaration> { 59 60 private Type type; 61 62 private boolean isVarArgs; 63 64 private NodeList<AnnotationExpr> varArgsAnnotations; 65 66 private NodeList<Modifier> modifiers; 67 68 private NodeList<AnnotationExpr> annotations; 69 70 private SimpleName name; 71 Parameter()72 public Parameter() { 73 this(null, new NodeList<>(), new NodeList<>(), new ClassOrInterfaceType(), false, new NodeList<>(), new SimpleName()); 74 } 75 Parameter(Type type, SimpleName name)76 public Parameter(Type type, SimpleName name) { 77 this(null, new NodeList<>(), new NodeList<>(), type, false, new NodeList<>(), name); 78 } 79 80 /** 81 * Creates a new {@link Parameter}. 82 * 83 * @param type type of the parameter 84 * @param name name of the parameter 85 */ Parameter(Type type, String name)86 public Parameter(Type type, String name) { 87 this(null, new NodeList<>(), new NodeList<>(), type, false, new NodeList<>(), new SimpleName(name)); 88 } 89 Parameter(NodeList<Modifier> modifiers, Type type, SimpleName name)90 public Parameter(NodeList<Modifier> modifiers, Type type, SimpleName name) { 91 this(null, modifiers, new NodeList<>(), type, false, new NodeList<>(), name); 92 } 93 94 @AllFieldsConstructor Parameter(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, boolean isVarArgs, NodeList<AnnotationExpr> varArgsAnnotations, SimpleName name)95 public Parameter(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, boolean isVarArgs, NodeList<AnnotationExpr> varArgsAnnotations, SimpleName name) { 96 this(null, modifiers, annotations, type, isVarArgs, varArgsAnnotations, name); 97 } 98 99 /** 100 * This constructor is used by the parser and is considered private. 101 */ 102 @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") Parameter(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, boolean isVarArgs, NodeList<AnnotationExpr> varArgsAnnotations, SimpleName name)103 public Parameter(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, boolean isVarArgs, NodeList<AnnotationExpr> varArgsAnnotations, SimpleName name) { 104 super(tokenRange); 105 setModifiers(modifiers); 106 setAnnotations(annotations); 107 setType(type); 108 setVarArgs(isVarArgs); 109 setVarArgsAnnotations(varArgsAnnotations); 110 setName(name); 111 customInitialization(); 112 } 113 114 @Override 115 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") accept(final GenericVisitor<R, A> v, final A arg)116 public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { 117 return v.visit(this, arg); 118 } 119 120 @Override 121 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") accept(final VoidVisitor<A> v, final A arg)122 public <A> void accept(final VoidVisitor<A> v, final A arg) { 123 v.visit(this, arg); 124 } 125 126 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getType()127 public Type getType() { 128 return type; 129 } 130 131 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") isVarArgs()132 public boolean isVarArgs() { 133 return isVarArgs; 134 } 135 136 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setType(final Type type)137 public Parameter setType(final Type type) { 138 assertNotNull(type); 139 if (type == this.type) { 140 return (Parameter) this; 141 } 142 notifyPropertyChange(ObservableProperty.TYPE, this.type, type); 143 if (this.type != null) 144 this.type.setParentNode(null); 145 this.type = type; 146 setAsParentNodeOf(type); 147 return this; 148 } 149 150 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setVarArgs(final boolean isVarArgs)151 public Parameter setVarArgs(final boolean isVarArgs) { 152 if (isVarArgs == this.isVarArgs) { 153 return (Parameter) this; 154 } 155 notifyPropertyChange(ObservableProperty.VAR_ARGS, this.isVarArgs, isVarArgs); 156 this.isVarArgs = isVarArgs; 157 return this; 158 } 159 160 /** 161 * @return the list returned could be immutable (in that case it will be empty) 162 */ 163 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getAnnotations()164 public NodeList<AnnotationExpr> getAnnotations() { 165 return annotations; 166 } 167 168 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getName()169 public SimpleName getName() { 170 return name; 171 } 172 173 /** 174 * Return the modifiers of this parameter declaration. 175 * 176 * @return modifiers 177 * @see Modifier 178 */ 179 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getModifiers()180 public NodeList<Modifier> getModifiers() { 181 return modifiers; 182 } 183 184 /** 185 * @param annotations a null value is currently treated as an empty list. This behavior could change in the future, 186 * so please avoid passing null 187 */ 188 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setAnnotations(final NodeList<AnnotationExpr> annotations)189 public Parameter setAnnotations(final NodeList<AnnotationExpr> annotations) { 190 assertNotNull(annotations); 191 if (annotations == this.annotations) { 192 return (Parameter) this; 193 } 194 notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); 195 if (this.annotations != null) 196 this.annotations.setParentNode(null); 197 this.annotations = annotations; 198 setAsParentNodeOf(annotations); 199 return this; 200 } 201 202 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setName(final SimpleName name)203 public Parameter setName(final SimpleName name) { 204 assertNotNull(name); 205 if (name == this.name) { 206 return (Parameter) this; 207 } 208 notifyPropertyChange(ObservableProperty.NAME, this.name, name); 209 if (this.name != null) 210 this.name.setParentNode(null); 211 this.name = name; 212 setAsParentNodeOf(name); 213 return this; 214 } 215 216 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setModifiers(final NodeList<Modifier> modifiers)217 public Parameter setModifiers(final NodeList<Modifier> modifiers) { 218 assertNotNull(modifiers); 219 if (modifiers == this.modifiers) { 220 return (Parameter) this; 221 } 222 notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers); 223 if (this.modifiers != null) 224 this.modifiers.setParentNode(null); 225 this.modifiers = modifiers; 226 setAsParentNodeOf(modifiers); 227 return this; 228 } 229 230 @Override 231 @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") remove(Node node)232 public boolean remove(Node node) { 233 if (node == null) 234 return false; 235 for (int i = 0; i < annotations.size(); i++) { 236 if (annotations.get(i) == node) { 237 annotations.remove(i); 238 return true; 239 } 240 } 241 for (int i = 0; i < modifiers.size(); i++) { 242 if (modifiers.get(i) == node) { 243 modifiers.remove(i); 244 return true; 245 } 246 } 247 for (int i = 0; i < varArgsAnnotations.size(); i++) { 248 if (varArgsAnnotations.get(i) == node) { 249 varArgsAnnotations.remove(i); 250 return true; 251 } 252 } 253 return super.remove(node); 254 } 255 256 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getVarArgsAnnotations()257 public NodeList<AnnotationExpr> getVarArgsAnnotations() { 258 return varArgsAnnotations; 259 } 260 261 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setVarArgsAnnotations(final NodeList<AnnotationExpr> varArgsAnnotations)262 public Parameter setVarArgsAnnotations(final NodeList<AnnotationExpr> varArgsAnnotations) { 263 assertNotNull(varArgsAnnotations); 264 if (varArgsAnnotations == this.varArgsAnnotations) { 265 return (Parameter) this; 266 } 267 notifyPropertyChange(ObservableProperty.VAR_ARGS_ANNOTATIONS, this.varArgsAnnotations, varArgsAnnotations); 268 if (this.varArgsAnnotations != null) 269 this.varArgsAnnotations.setParentNode(null); 270 this.varArgsAnnotations = varArgsAnnotations; 271 setAsParentNodeOf(varArgsAnnotations); 272 return this; 273 } 274 275 @Override 276 @Generated("com.github.javaparser.generator.core.node.CloneGenerator") clone()277 public Parameter clone() { 278 return (Parameter) accept(new CloneVisitor(), null); 279 } 280 281 @Override 282 @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") getMetaModel()283 public ParameterMetaModel getMetaModel() { 284 return JavaParserMetaModel.parameterMetaModel; 285 } 286 287 @Override 288 @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") replace(Node node, Node replacementNode)289 public boolean replace(Node node, Node replacementNode) { 290 if (node == null) 291 return false; 292 for (int i = 0; i < annotations.size(); i++) { 293 if (annotations.get(i) == node) { 294 annotations.set(i, (AnnotationExpr) replacementNode); 295 return true; 296 } 297 } 298 for (int i = 0; i < modifiers.size(); i++) { 299 if (modifiers.get(i) == node) { 300 modifiers.set(i, (Modifier) replacementNode); 301 return true; 302 } 303 } 304 if (node == name) { 305 setName((SimpleName) replacementNode); 306 return true; 307 } 308 if (node == type) { 309 setType((Type) replacementNode); 310 return true; 311 } 312 for (int i = 0; i < varArgsAnnotations.size(); i++) { 313 if (varArgsAnnotations.get(i) == node) { 314 varArgsAnnotations.set(i, (AnnotationExpr) replacementNode); 315 return true; 316 } 317 } 318 return super.replace(node, replacementNode); 319 } 320 321 @Override resolve()322 public ResolvedParameterDeclaration resolve() { 323 return getSymbolResolver().resolveDeclaration(this, ResolvedParameterDeclaration.class); 324 } 325 } 326