equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
24 */ |
24 */ |
25 |
25 |
26 package java.lang.reflect; |
26 package java.lang.reflect; |
27 |
27 |
28 import java.lang.annotation.*; |
28 import java.lang.annotation.*; |
|
29 import java.util.Collections; |
29 import java.util.Map; |
30 import java.util.Map; |
|
31 import java.util.Objects; |
30 import sun.reflect.annotation.AnnotationParser; |
32 import sun.reflect.annotation.AnnotationParser; |
|
33 import sun.reflect.annotation.AnnotationSupport; |
31 import sun.reflect.generics.repository.ConstructorRepository; |
34 import sun.reflect.generics.repository.ConstructorRepository; |
32 |
35 |
33 /** |
36 /** |
34 * A shared superclass for the common functionality of {@link Method} |
37 * A shared superclass for the common functionality of {@link Method} |
35 * and {@link Constructor}. |
38 * and {@link Constructor}. |
361 |
364 |
362 /** |
365 /** |
363 * {@inheritDoc} |
366 * {@inheritDoc} |
364 * @throws NullPointerException {@inheritDoc} |
367 * @throws NullPointerException {@inheritDoc} |
365 */ |
368 */ |
366 @SuppressWarnings("unchecked") |
|
367 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
369 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
368 if (annotationClass == null) |
370 Objects.requireNonNull(annotationClass); |
369 throw new NullPointerException(); |
371 |
370 |
372 return AnnotationSupport.getOneAnnotation(declaredAnnotations(), annotationClass); |
371 return (T) declaredAnnotations().get(annotationClass); |
|
372 } |
373 } |
373 |
374 |
374 /** |
375 /** |
375 * {@inheritDoc} |
376 * {@inheritDoc} |
|
377 * @throws NullPointerException {@inheritDoc} |
|
378 * @since 1.8 |
|
379 */ |
|
380 public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) { |
|
381 Objects.requireNonNull(annotationClass); |
|
382 |
|
383 return AnnotationSupport.getMultipleAnnotations(declaredAnnotations(), annotationClass); |
|
384 } |
|
385 |
|
386 /** |
|
387 * {@inheritDoc} |
376 */ |
388 */ |
377 public Annotation[] getDeclaredAnnotations() { |
389 public Annotation[] getDeclaredAnnotations() { |
378 return AnnotationParser.toArray(declaredAnnotations()); |
390 return AnnotationSupport.unpackToArray(declaredAnnotations()); |
379 } |
391 } |
380 |
392 |
381 private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations; |
393 private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations; |
382 |
394 |
383 private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { |
395 private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { |