View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.reflect;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.collect.FluentIterable;
21  import com.google.common.collect.ImmutableList;
22  import java.lang.annotation.Annotation;
23  import java.lang.reflect.AnnotatedElement;
24  import javax.annotation.Nullable;
25  
26  /**
27   * Represents a method or constructor parameter.
28   *
29   * @author Ben Yu
30   * @since 14.0
31   */
32  @Beta
33  public final class Parameter implements AnnotatedElement {
34  
35    private final Invokable<?, ?> declaration;
36    private final int position;
37    private final TypeToken<?> type;
38    private final ImmutableList<Annotation> annotations;
39  
40    Parameter(
41        Invokable<?, ?> declaration, int position, TypeToken<?> type, Annotation[] annotations) {
42      this.declaration = declaration;
43      this.position = position;
44      this.type = type;
45      this.annotations = ImmutableList.copyOf(annotations);
46    }
47  
48    /** Returns the type of the parameter. */
49    public TypeToken<?> getType() {
50      return type;
51    }
52  
53    /** Returns the {@link Invokable} that declares this parameter. */
54    public Invokable<?, ?> getDeclaringInvokable() {
55      return declaration;
56    }
57  
58    @Override
59    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
60      return getAnnotation(annotationType) != null;
61    }
62  
63    @Override
64    @Nullable
65    public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
66      checkNotNull(annotationType);
67      for (Annotation annotation : annotations) {
68        if (annotationType.isInstance(annotation)) {
69          return annotationType.cast(annotation);
70        }
71      }
72      return null;
73    }
74  
75    @Override
76    public Annotation[] getAnnotations() {
77      return getDeclaredAnnotations();
78    }
79  
80    /**
81     * @since 18.0
82     */
83    // @Override on JDK8
84    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
85      return getDeclaredAnnotationsByType(annotationType);
86    }
87  
88    /**
89     * @since 18.0
90     */
91    // @Override on JDK8
92    @Override
93    public Annotation[] getDeclaredAnnotations() {
94      return annotations.toArray(new Annotation[annotations.size()]);
95    }
96  
97    /**
98     * @since 18.0
99     */
100   // @Override on JDK8
101   @Nullable
102   public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
103     checkNotNull(annotationType);
104     return FluentIterable.from(annotations).filter(annotationType).first().orNull();
105   }
106 
107   /**
108    * @since 18.0
109    */
110   // @Override on JDK8
111   public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
112     return FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
113   }
114 
115   @Override
116   public boolean equals(@Nullable Object obj) {
117     if (obj instanceof Parameter) {
118       Parameter that = (Parameter) obj;
119       return position == that.position && declaration.equals(that.declaration);
120     }
121     return false;
122   }
123 
124   @Override
125   public int hashCode() {
126     return position;
127   }
128 
129   @Override
130   public String toString() {
131     return type + " arg" + position;
132   }
133 }