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 java.lang.annotation.Annotation;
20  import java.lang.reflect.AccessibleObject;
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.Field;
23  import java.lang.reflect.Member;
24  import java.lang.reflect.Method;
25  import java.lang.reflect.Modifier;
26  import javax.annotation.Nullable;
27  
28  /**
29   * Represents either a {@link Field}, a {@link Method} or a {@link Constructor}. Provides
30   * convenience methods such as {@link #isPublic} and {@link #isPackagePrivate}.
31   *
32   * @author Ben Yu
33   */
34  class Element extends AccessibleObject implements Member {
35  
36    private final AccessibleObject accessibleObject;
37    private final Member member;
38  
39    <M extends AccessibleObject & Member> Element(M member) {
40      checkNotNull(member);
41      this.accessibleObject = member;
42      this.member = member;
43    }
44  
45    public TypeToken<?> getOwnerType() {
46      return TypeToken.of(getDeclaringClass());
47    }
48  
49    @Override
50    public final boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
51      return accessibleObject.isAnnotationPresent(annotationClass);
52    }
53  
54    @Override
55    public final <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
56      return accessibleObject.getAnnotation(annotationClass);
57    }
58  
59    @Override
60    public final Annotation[] getAnnotations() {
61      return accessibleObject.getAnnotations();
62    }
63  
64    @Override
65    public final Annotation[] getDeclaredAnnotations() {
66      return accessibleObject.getDeclaredAnnotations();
67    }
68  
69    @Override
70    public final void setAccessible(boolean flag) throws SecurityException {
71      accessibleObject.setAccessible(flag);
72    }
73  
74    @Override
75    public final boolean isAccessible() {
76      return accessibleObject.isAccessible();
77    }
78  
79    @Override
80    public Class<?> getDeclaringClass() {
81      return member.getDeclaringClass();
82    }
83  
84    @Override
85    public final String getName() {
86      return member.getName();
87    }
88  
89    @Override
90    public final int getModifiers() {
91      return member.getModifiers();
92    }
93  
94    @Override
95    public final boolean isSynthetic() {
96      return member.isSynthetic();
97    }
98  
99    /** Returns true if the element is public. */
100   public final boolean isPublic() {
101     return Modifier.isPublic(getModifiers());
102   }
103 
104   /** Returns true if the element is protected. */
105   public final boolean isProtected() {
106     return Modifier.isProtected(getModifiers());
107   }
108 
109   /** Returns true if the element is package-private. */
110   public final boolean isPackagePrivate() {
111     return !isPrivate() && !isPublic() && !isProtected();
112   }
113 
114   /** Returns true if the element is private. */
115   public final boolean isPrivate() {
116     return Modifier.isPrivate(getModifiers());
117   }
118 
119   /** Returns true if the element is static. */
120   public final boolean isStatic() {
121     return Modifier.isStatic(getModifiers());
122   }
123 
124   /**
125    * Returns {@code true} if this method is final, per {@code Modifier.isFinal(getModifiers())}.
126    *
127    * <p>Note that a method may still be effectively "final", or non-overridable when it has no
128    * {@code final} keyword. For example, it could be private, or it could be declared by a final
129    * class. To tell whether a method is overridable, use {@link Invokable#isOverridable}.
130    */
131   public final boolean isFinal() {
132     return Modifier.isFinal(getModifiers());
133   }
134 
135   /** Returns true if the method is abstract. */
136   public final boolean isAbstract() {
137     return Modifier.isAbstract(getModifiers());
138   }
139 
140   /** Returns true if the element is native. */
141   public final boolean isNative() {
142     return Modifier.isNative(getModifiers());
143   }
144 
145   /** Returns true if the method is synchronized. */
146   public final boolean isSynchronized() {
147     return Modifier.isSynchronized(getModifiers());
148   }
149 
150   /** Returns true if the field is volatile. */
151   final boolean isVolatile() {
152     return Modifier.isVolatile(getModifiers());
153   }
154 
155   /** Returns true if the field is transient. */
156   final boolean isTransient() {
157     return Modifier.isTransient(getModifiers());
158   }
159 
160   @Override
161   public boolean equals(@Nullable Object obj) {
162     if (obj instanceof Element) {
163       Element that = (Element) obj;
164       return getOwnerType().equals(that.getOwnerType()) && member.equals(that.member);
165     }
166     return false;
167   }
168 
169   @Override
170   public int hashCode() {
171     return member.hashCode();
172   }
173 
174   @Override
175   public String toString() {
176     return member.toString();
177   }
178 }