• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.reflect;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import java.lang.annotation.Annotation;
22 import java.lang.reflect.AccessibleObject;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Member;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Modifier;
28 
29 import javax.annotation.Nullable;
30 
31 /**
32  * Represents either a {@link Field}, a {@link Method} or a {@link Constructor}.
33  * Provides convenience methods such as {@link #isPublic} and {@link #isPackagePrivate}.
34  *
35  * @author Ben Yu
36  */
37 class Element extends AccessibleObject implements Member {
38 
39   private final AccessibleObject accessibleObject;
40   private final Member member;
41 
Element(M member)42   <M extends AccessibleObject & Member> Element(M member) {
43     checkNotNull(member);
44     this.accessibleObject = member;
45     this.member = member;
46   }
47 
getOwnerType()48   public TypeToken<?> getOwnerType() {
49     return TypeToken.of(getDeclaringClass());
50   }
51 
isAnnotationPresent(Class<? extends Annotation> annotationClass)52   @Override public final boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
53     return accessibleObject.isAnnotationPresent(annotationClass);
54   }
55 
getAnnotation(Class<A> annotationClass)56   @Override public final <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
57     return accessibleObject.getAnnotation(annotationClass);
58   }
59 
getAnnotations()60   @Override public final Annotation[] getAnnotations() {
61     return accessibleObject.getAnnotations();
62   }
63 
getDeclaredAnnotations()64   @Override public final Annotation[] getDeclaredAnnotations() {
65     return accessibleObject.getDeclaredAnnotations();
66   }
67 
setAccessible(boolean flag)68   @Override public final void setAccessible(boolean flag) throws SecurityException {
69     accessibleObject.setAccessible(flag);
70   }
71 
isAccessible()72   @Override public final boolean isAccessible() {
73     return accessibleObject.isAccessible();
74   }
75 
getDeclaringClass()76   @Override public Class<?> getDeclaringClass() {
77     return member.getDeclaringClass();
78   }
79 
getName()80   @Override public final String getName() {
81     return member.getName();
82   }
83 
getModifiers()84   @Override public final int getModifiers() {
85     return member.getModifiers();
86   }
87 
isSynthetic()88   @Override public final boolean isSynthetic() {
89     return member.isSynthetic();
90   }
91 
92   /** Returns true if the element is public. */
isPublic()93   public final boolean isPublic() {
94     return Modifier.isPublic(getModifiers());
95   }
96 
97   /** Returns true if the element is protected. */
isProtected()98   public final boolean isProtected() {
99     return Modifier.isProtected(getModifiers());
100   }
101 
102   /** Returns true if the element is package-private. */
isPackagePrivate()103   public final boolean isPackagePrivate() {
104     return !isPrivate() && !isPublic() && !isProtected();
105   }
106 
107   /** Returns true if the element is private. */
isPrivate()108   public final boolean isPrivate() {
109     return Modifier.isPrivate(getModifiers());
110   }
111 
112   /** Returns true if the element is static. */
isStatic()113   public final boolean isStatic() {
114     return Modifier.isStatic(getModifiers());
115   }
116 
117   /**
118    * Returns {@code true} if this method is final, per {@code Modifier.isFinal(getModifiers())}.
119    *
120    * <p>Note that a method may still be effectively "final", or non-overridable when it has no
121    * {@code final} keyword. For example, it could be private, or it could be declared by a final
122    * class. To tell whether a method is overridable, use {@link Invokable#isOverridable}.
123    */
isFinal()124   public final boolean isFinal() {
125     return Modifier.isFinal(getModifiers());
126   }
127 
128   /** Returns true if the method is abstract. */
isAbstract()129   public final boolean isAbstract() {
130     return Modifier.isAbstract(getModifiers());
131   }
132 
133   /** Returns true if the element is native. */
isNative()134   public final boolean isNative() {
135     return Modifier.isNative(getModifiers());
136   }
137 
138   /** Returns true if the method is synchronized. */
isSynchronized()139   public final boolean isSynchronized() {
140     return Modifier.isSynchronized(getModifiers());
141   }
142 
143   /** Returns true if the field is volatile. */
isVolatile()144   final boolean isVolatile() {
145     return Modifier.isVolatile(getModifiers());
146   }
147 
148   /** Returns true if the field is transient. */
isTransient()149   final boolean isTransient() {
150     return Modifier.isTransient(getModifiers());
151   }
152 
equals(@ullable Object obj)153   @Override public boolean equals(@Nullable Object obj) {
154     if (obj instanceof Element) {
155       Element that = (Element) obj;
156       return getOwnerType().equals(that.getOwnerType()) && member.equals(that.member);
157     }
158     return false;
159   }
160 
hashCode()161   @Override public int hashCode() {
162     return member.hashCode();
163   }
164 
toString()165   @Override public String toString() {
166     return member.toString();
167   }
168 }
169