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 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 a string representation of the parameter's modifiers, 99 * its attributes, its type, its name, and a trailing ... if it is 100 * a variadic parameter. 101 * 102 * @return A string representation of the parameter and associated 103 * information. 104 */ 105 public String toString() { 106 final StringBuilder sb = new StringBuilder(); 107 final Type type = getParameterizedType(); 108 final String typename = (type instanceof Class)? 109 Field.getTypeName((Class)type): 110 (type.toString()); 111 112 sb.append(Modifier.toString(getModifiers())); 113 sb.append(" "); 114 115 if(isVarArgs()) 116 sb.append(typename.replaceFirst("\\[\\]$", "...")); 117 else 118 sb.append(typename); 119 120 sb.append(" "); 121 sb.append(name); 122 123 return sb.toString(); 124 } 125 126 /** 127 * Return the {@code Executable} which declares this parameter. 128 * 129 * @return The {@code Executable} declaring this parameter. 130 */ 131 public Executable getDeclaringExecutable() { 132 return executable; 133 } 134 135 /** 136 * Get the modifier flags for this the parameter represented by 137 * this {@code Parameter} object. 138 * 139 * @return The modifier flags for this parameter. 140 */ 141 public int getModifiers() { 142 return modifiers; 143 } 144 145 /** 146 * Returns the name of the parameter represented by this 147 * {@code Parameter} object. 148 */ 149 public String getName() { 150 return name; 151 } 152 153 /** 154 * Returns a {@code Type} object that identifies the parameterized 155 * type for the parameter represented by this {@code Parameter} 156 * object. 157 * 158 * @return a {@code Type} object identifying the parameterized 159 * type of the parameter represented by this object 160 */ 161 public Type getParameterizedType() { 162 Type tmp = parameterTypeCache; 163 if (null == tmp) { 164 tmp = executable.getGenericParameterTypes()[index]; 165 parameterTypeCache = tmp; 166 } 167 168 return tmp; 169 } 170 171 private transient volatile Type parameterTypeCache = null; 172 173 /** 174 * Returns a {@code Class} object that identifies the 175 * declared type for the parameter represented by this 176 * {@code Parameter} object. 177 * 178 * @return a {@code Class} object identifying the declared 179 * type of the parameter represented by this object 180 */ 181 public Class<?> getType() { 182 Class<?> tmp = parameterClassCache; 183 if (null == tmp) { 184 tmp = executable.getParameterTypes()[index]; 185 parameterClassCache = tmp; 186 } 187 return tmp; 188 } 189 190 private transient volatile Class<?> parameterClassCache = null; 191 192 /** 193 * Returns {@code true} if this parameter is a synthesized 194 * construct; returns {@code false} otherwise. 195 * 196 * @return true if and only if this parameter is a synthesized 197 * construct as defined by 198 * <cite>The Java™ Language Specification</cite>. 199 */ 200 public boolean isSynthesized() { 201 return Modifier.isSynthesized(getModifiers()); 202 } 203 204 /** 205 * Returns {@code true} if this parameter is a synthetic 206 * construct; returns {@code false} otherwise. 207 * 208 * @jls 13.1 The Form of a Binary 209 * @return true if and only if this parameter is a synthetic 210 * construct as defined by 211 * <cite>The Java™ Language Specification</cite>. 212 */ 213 public boolean isSynthetic() { 214 return Modifier.isSynthetic(getModifiers()); 215 } 216 217 /** 218 * Returns {@code true} if this parameter represents a variable 219 * argument list; returns {@code false} otherwise. 220 * 221 * @return {@code true} if an only if this parameter represents a 222 * variable argument list. 223 */ 224 public boolean isVarArgs() { 225 return executable.isVarArgs() && 226 index == executable.getParameterCount() - 1; 227 } 228 229 230 /** 231 * {@inheritDoc} 232 * @throws NullPointerException {@inheritDoc} 233 */ 234 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 235 Objects.requireNonNull(annotationClass); 236 return annotationClass.cast(declaredAnnotations().get(annotationClass)); 237 } 238 239 /** 240 * {@inheritDoc} 241 * @throws NullPointerException {@inheritDoc} 242 */ 243 public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) { 244 Objects.requireNonNull(annotationClass); 245 246 return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass); 247 } 248 249 /** 250 * {@inheritDoc} 251 */ 252 public Annotation[] getDeclaredAnnotations() { 253 return executable.getParameterAnnotations()[index]; 254 } 255 256 /** 257 * @throws NullPointerException {@inheritDoc} 258 */ 259 public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { 260 // Only annotations on classes are inherited, for all other 261 // objects getDeclaredAnnotation is the same as 262 // getAnnotation. 263 return getAnnotation(annotationClass); 264 } 265 266 /** 267 * @throws NullPointerException {@inheritDoc} 268 */ 269 public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) { 270 // Only annotations on classes are inherited, for all other 271 // objects getDeclaredAnnotations is the same as 272 // getAnnotations. 273 return getAnnotations(annotationClass); 274 } 275 276 /** 277 * {@inheritDoc} 278 */ 279 public Annotation[] getAnnotations() { 280 return getDeclaredAnnotations(); 281 } 282 283 private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 284 285 private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { 286 if(null == declaredAnnotations) { 287 declaredAnnotations = 288 new HashMap<Class<? extends Annotation>, Annotation>(); 289 Annotation[] ann = getDeclaredAnnotations(); 290 for(int i = 0; i < ann.length; i++) 291 declaredAnnotations.put(ann[i].annotationType(), ann[i]); 292 } 293 return declaredAnnotations; 294 } 295 296 }