Coverage Summary for Class: Parameter (com.google.common.reflect)

Class Class, % Method, % Line, %
Parameter 100% (1/1) 50% (7/14) 44.8% (13/29)


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 java.lang.reflect.AnnotatedType; 25 import javax.annotation.CheckForNull; 26  27 /** 28  * Represents a method or constructor parameter. 29  * 30  * @author Ben Yu 31  * @since 14.0 32  */ 33 @Beta 34 @ElementTypesAreNonnullByDefault 35 public final class Parameter implements AnnotatedElement { 36  37  private final Invokable<?, ?> declaration; 38  private final int position; 39  private final TypeToken<?> type; 40  private final ImmutableList<Annotation> annotations; 41  private final AnnotatedType annotatedType; 42  43  Parameter( 44  Invokable<?, ?> declaration, 45  int position, 46  TypeToken<?> type, 47  Annotation[] annotations, 48  AnnotatedType annotatedType) { 49  this.declaration = declaration; 50  this.position = position; 51  this.type = type; 52  this.annotations = ImmutableList.copyOf(annotations); 53  this.annotatedType = annotatedType; 54  } 55  56  /** Returns the type of the parameter. */ 57  public TypeToken<?> getType() { 58  return type; 59  } 60  61  /** Returns the {@link Invokable} that declares this parameter. */ 62  public Invokable<?, ?> getDeclaringInvokable() { 63  return declaration; 64  } 65  66  @Override 67  public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 68  return getAnnotation(annotationType) != null; 69  } 70  71  @Override 72  @CheckForNull 73  public <A extends Annotation> A getAnnotation(Class<A> annotationType) { 74  checkNotNull(annotationType); 75  for (Annotation annotation : annotations) { 76  if (annotationType.isInstance(annotation)) { 77  return annotationType.cast(annotation); 78  } 79  } 80  return null; 81  } 82  83  @Override 84  public Annotation[] getAnnotations() { 85  return getDeclaredAnnotations(); 86  } 87  88  /** @since 18.0 */ 89  // @Override on JDK8 90  @Override 91  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { 92  return getDeclaredAnnotationsByType(annotationType); 93  } 94  95  /** @since 18.0 */ 96  // @Override on JDK8 97  @Override 98  public Annotation[] getDeclaredAnnotations() { 99  return annotations.toArray(new Annotation[0]); 100  } 101  102  /** @since 18.0 */ 103  // @Override on JDK8 104  @Override 105  @CheckForNull 106  public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) { 107  checkNotNull(annotationType); 108  return FluentIterable.from(annotations).filter(annotationType).first().orNull(); 109  } 110  111  /** @since 18.0 */ 112  // @Override on JDK8 113  @Override 114  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) { 115  return FluentIterable.from(annotations).filter(annotationType).toArray(annotationType); 116  } 117  118  /** @since 25.1 */ 119  // @Override on JDK8 120  public AnnotatedType getAnnotatedType() { 121  return annotatedType; 122  } 123  124  @Override 125  public boolean equals(@CheckForNull Object obj) { 126  if (obj instanceof Parameter) { 127  Parameter that = (Parameter) obj; 128  return position == that.position && declaration.equals(that.declaration); 129  } 130  return false; 131  } 132  133  @Override 134  public int hashCode() { 135  return position; 136  } 137  138  @Override 139  public String toString() { 140  return type + " arg" + position; 141  } 142 }