1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.lang.reflect; 26 27 import java.lang.annotation.*; 28 import java.util.HashMap; 29 import java.util.Map; 30 import java.util.Objects; 31 import sun.reflect.annotation.AnnotationSupport; 32 33 /** 34 * Information about method parameters. 35 * 36 * A {@code Parameter} provides information about method parameters, 37 * including its name and modifiers. It also provides an alternate 38 * means of obtaining attributes for the parameter. 39 * 40 * @since 1.8 41 */ 42 public final class Parameter implements AnnotatedElement { 43 44 private final String name; 45 private final int modifiers; 46 private final Executable executable; 47 private final int index; 48 49 /** 50 * Package-private constructor for {@code Parameter}. 51 * 52 * If method parameter data is present in the classfile, then the 53 * JVM creates {@code Parameter} objects directly. If it is 54 * absent, however, then {@code Executable} uses this constructor 55 * to synthesize them. 56 * 57 * @param name The name of the parameter. 58 * @param modifiers The modifier flags for the parameter. 59 * @param executable The executable which defines this parameter. 60 * @param index The index of the parameter. 61 */ 62 Parameter(String name, 63 int modifiers, 64 Executable executable, 65 int index) { 66 this.name = name; 67 this.modifiers = modifiers; 68 this.executable = executable; 69 this.index = index; 70 } 71 72 /** 73 * Compares based on the executable and the index. 74 * 75 * @param obj The object to compare. 76 * @return Whether or not this is equal to the argument. 77 */ 78 public boolean equals(Object obj) { 79 if(obj instanceof Parameter) { 80 Parameter other = (Parameter)obj; 81 return (other.executable.equals(executable) && 82 other.index == index); 83 } 84 return false; 85 } 86 87 /** 88 * Returns a hash code based on the executable's hash code and the 89 * index. 90 * 91 * @return A hash code based on the executable's hash code. 92 */ 93 public int hashCode() { 94 return executable.hashCode() ^ index; 95 } 96 97 /** 98 * Returns true if the parameter has a name according to the class 99 * file; returns false otherwise. Whether a parameter has a name 100 * is determined by the {@literal MethodParameters} attribute of 101 * the method which declares the parameter. 102 * 103 * @return true if and only if the parameter has a name according 104 * to the class file. 105 */ 106 public boolean isNamePresent() { 107 return executable.hasRealParameterData(); 108 } 109 110 /** 111 * Returns a string describing this parameter. The format is the 112 * modifiers for the parameter, if any, in canonical order as 113 * recommended by <cite>The Java™ Language 114 * Specification</cite>, followed by the fully- qualified type of 115 * the parameter (excluding the last [] if the parameter is 116 * variable arity), followed by "..." if the parameter is variable 117 * arity, followed by a space, followed by the name of the 118 * parameter. 119 * 120 * @return A string representation of the parameter and associated 121 * information. 122 */ 123 public String toString() { 124 final StringBuilder sb = new StringBuilder(); 125 final Type type = getParameterizedType(); 126 final String typename = type.getTypeName(); 127 128 sb.append(Modifier.toString(getModifiers())); 129 130 if(0 != modifiers) 131 sb.append(' '); 132 133 if(isVarArgs()) 134 sb.append(typename.replaceFirst("\\[\\]$", "...")); 135 else 136 sb.append(typename); 137 138 sb.append(' '); 139 sb.append(getName()); 140 141 return sb.toString(); 142 } 143 144 /** 145 * Return the {@code Executable} which declares this parameter. 146 * 147 * @return The {@code Executable} declaring this parameter. 148 */ 149 public Executable getDeclaringExecutable() { 150 return executable; 151 } 152 153 /** 154 * Get the modifier flags for this the parameter represented by 155 * this {@code Parameter} object. 156 * 157 * @return The modifier flags for this parameter. 158 */ 159 public int getModifiers() { 160 return modifiers; 161 } 162 163 /** 164 * Returns the name of the parameter. If the parameter's name is 165 * {@linkplain isNamePresent() present}, then this method returns 166 * the name provided by the class file. Otherwise, this method 167 * synthesizes a name of the form argN, where N is the index of 168 * the parameter in the descriptor of the method which declares 169 * the parameter. 170 * 171 * @return The name of the parameter, either provided by the class 172 * file or synthesized if the class file does not provide 173 * a name. 174 */ 175 public String getName() { 176 // Note: empty strings as paramete names are now outlawed. 177 // The .equals("") is for compatibility with current JVM 178 // behavior. It may be removed at some point. 179 if(name == null || name.equals("")) 180 return "arg" + index; 181 else 182 return name; 183 } 184 185 /** 186 * Returns a {@code Type} object that identifies the parameterized 187 * type for the parameter represented by this {@code Parameter} 188 * object. 189 * 190 * @return a {@code Type} object identifying the parameterized 191 * type of the parameter represented by this object 192 */ 193 public Type getParameterizedType() { 194 Type tmp = parameterTypeCache; 195 if (null == tmp) { 196 tmp = executable.getGenericParameterTypes()[index]; 197 parameterTypeCache = tmp; 198 } 199 200 return tmp; 201 } 202 203 private transient volatile Type parameterTypeCache = null; 204 205 /** 206 * Returns a {@code Class} object that identifies the 207 * declared type for the parameter represented by this 208 * {@code Parameter} object. 209 * 210 * @return a {@code Class} object identifying the declared 211 * type of the parameter represented by this object 212 */ 213 public Class<?> getType() { 214 Class<?> tmp = parameterClassCache; 215 if (null == tmp) { 216 tmp = executable.getParameterTypes()[index]; 217 parameterClassCache = tmp; 218 } 219 return tmp; 220 } 221 222 /** 223 * Returns an AnnotatedType object that represents the use of a type to 224 * specify the type of the formal parameter represented by this Parameter. 225 * 226 * @return an {@code AnnotatedType} object representing the use of a type 227 * to specify the type of the formal parameter represented by this 228 * Parameter 229 */ 230 public AnnotatedType getAnnotatedType() { 231 // no caching for now 232 return executable.getAnnotatedParameterTypes()[index]; 233 } 234 235 private transient volatile Class<?> parameterClassCache = null; 236 237 /** 238 * Returns {@code true} if this parameter is implicitly declared 239 * in source code; returns {@code false} otherwise. 240 * 241 * @return true if and only if this parameter is implicitly 242 * declared as defined by <cite>The Java™ Language 243 * Specification</cite>. 244 */ 245 public boolean isImplicit() { 246 return Modifier.isMandated(getModifiers()); 247 } 248 249 /** 250 * Returns {@code true} if this parameter is neither implicitly 251 * nor explicitly declared in source code; returns {@code false} 252 * otherwise. 253 * 254 * @jls 13.1 The Form of a Binary 255 * @return true if and only if this parameter is a synthetic 256 * construct as defined by 257 * <cite>The Java™ Language Specification</cite>. 258 */ 259 public boolean isSynthetic() { 260 return Modifier.isSynthetic(getModifiers()); 261 } 262 263 /** 264 * Returns {@code true} if this parameter represents a variable 265 * argument list; returns {@code false} otherwise. 266 * 267 * @return {@code true} if an only if this parameter represents a 268 * variable argument list. 269 */ 270 public boolean isVarArgs() { 271 return executable.isVarArgs() && 272 index == executable.getParameterCount() - 1; 273 } 274 275 276 /** 277 * {@inheritDoc} 278 * @throws NullPointerException {@inheritDoc} 279 */ 280 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 281 Objects.requireNonNull(annotationClass); 282 return annotationClass.cast(declaredAnnotations().get(annotationClass)); 283 } 284 285 /** 286 * {@inheritDoc} 287 * @throws NullPointerException {@inheritDoc} 288 */ 289 @Override 290 public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 291 Objects.requireNonNull(annotationClass); 292 293 return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass); 294 } 295 296 /** 297 * {@inheritDoc} 298 */ 299 public Annotation[] getDeclaredAnnotations() { 300 return executable.getParameterAnnotations()[index]; 301 } 302 303 /** 304 * @throws NullPointerException {@inheritDoc} 305 */ 306 public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { 307 // Only annotations on classes are inherited, for all other 308 // objects getDeclaredAnnotation is the same as 309 // getAnnotation. 310 return getAnnotation(annotationClass); 311 } 312 313 /** 314 * @throws NullPointerException {@inheritDoc} 315 */ 316 @Override 317 public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) { 318 // Only annotations on classes are inherited, for all other 319 // objects getDeclaredAnnotations is the same as 320 // getAnnotations. 321 return getAnnotationsByType(annotationClass); 322 } 323 324 /** 325 * {@inheritDoc} 326 */ 327 public Annotation[] getAnnotations() { 328 return getDeclaredAnnotations(); 329 } 330 331 private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 332 333 private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { 334 if(null == declaredAnnotations) { 335 declaredAnnotations = 336 new HashMap<Class<? extends Annotation>, Annotation>(); 337 Annotation[] ann = getDeclaredAnnotations(); 338 for(int i = 0; i < ann.length; i++) 339 declaredAnnotations.put(ann[i].annotationType(), ann[i]); 340 } 341 return declaredAnnotations; 342 } 343 344 }