View Javadoc

1   /**
2    * EasyBeans
3    * Copyright (C) 2006 Bull S.A.S.
4    * Contact: easybeans@objectweb.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   * USA
20   *
21   * --------------------------------------------------------------------------
22   * $Id: JField.java 47 2006-02-28 10:42:29Z benoitf $
23   * --------------------------------------------------------------------------
24   */
25  
26  package com.bm.ejb3metadata.annotations;
27  
28  /**
29   * This class defines a Field object. It is not based on reflection.
30   * @author Florent Benoit
31   */
32  public class JField {
33  
34      /**
35       * Name of the field.
36       */
37      private String name = null;
38  
39      /**
40       * Access mode (see {@link org.ejb3unit.asm.Opcodes}).
41       */
42      private int access;
43  
44      /**
45       * Field's descriptor.
46       */
47      private String descriptor = null;
48  
49      /**
50       * Field's signature.
51       */
52      private String signature;
53  
54      /**
55       * Value of the field.
56       */
57      private Object value;
58  
59      /**
60       * Constructor. *
61       * @param access the field's access flags (see
62       *        {@link org.ejb3unit.asm.Opcodes}). This parameter also indicates
63       *        if the field is synthetic and/or deprecated.
64       * @param name the field's name.
65       * @param descriptor the field's descriptor (see
66       *        {@link org.ejb3unit.asm.Type}).
67       * @param signature the field's signature. May be <tt>null</tt> if the
68       *        field's type does not use generic types.
69       * @param value the field's initial value. This parameter, which may be
70       *        <tt>null</tt> if the field does not have an initial value, must
71       *        be an {@link Integer}, a {@link Float}, a {@link Long}, a
72       *        {@link Double} or a {@link String} (for <tt>int</tt>,
73       *        <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
74       *        respectively). <i>This parameter is only used for static fields</i>.
75       *        Its value is ignored for non static fields, which must be
76       *        initialized through bytecode instructions in constructors or
77       *        methods.
78       */
79      public JField(final int access, final String name, final String descriptor, final String signature,
80              final Object value) {
81          this.access = access;
82          this.name = name;
83          this.descriptor = descriptor;
84          this.signature = signature;
85          this.value = value;
86      }
87  
88      /**
89       * Indicates whether some other object is "equal to" this one.
90       * @param obj object to compare
91       * @return true if given object is equals
92       */
93      @Override
94      public boolean equals(final Object obj) {
95          if (obj != null && obj instanceof JField) {
96              JField other = (JField) obj;
97  
98              // same name
99              if (!this.name.equals(other.name)) {
100                 return false;
101             }
102 
103             // same descriptor
104             if ((this.descriptor != null) && (!this.descriptor.equals(other.descriptor))) {
105                 return false;
106             }
107 
108             // same signature
109             if ((this.signature != null) && (!this.signature.equals(other.signature))) {
110                 return false;
111             }
112 
113             // if all tests succeed, return true
114             return true;
115         }
116         return false;
117     }
118 
119     /**
120      * a hash code value for the object.
121      * @return a hash code value for the object.
122      */
123     @Override
124     public int hashCode() {
125         return name.hashCode();
126     }
127 
128     /**
129      * field's descriptor.
130      * @return field's descriptor.
131      */
132     public String getDescriptor() {
133         return descriptor;
134     }
135 
136     /**
137      * field's value.
138      * @return field's value.
139      */
140     public Object getValue() {
141         return value;
142     }
143 
144     /**
145      * method name.
146      * @return method name
147      */
148     public String getName() {
149         return name;
150     }
151 
152     /**
153      * method signature.
154      * @return method signature
155      */
156     public String getSignature() {
157         return signature;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public String toString() {
165         StringBuilder sb = new StringBuilder();
166         // classname
167         sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1));
168 
169         // name
170         sb.append("[name=");
171         sb.append(name);
172 
173         // access
174         sb.append(", access=");
175         sb.append(access);
176 
177         // descriptor
178         if (descriptor != null) {
179             sb.append(", descriptor=");
180             sb.append(descriptor);
181         }
182 
183         // signature
184         if (signature != null) {
185             sb.append(", signature=");
186             sb.append(signature);
187         }
188 
189         // exceptions
190         if (value != null) {
191             sb.append(", value=");
192             sb.append(value);
193         }
194         sb.append("]");
195         return sb.toString();
196     }
197 
198     /**
199      * the field's access flags.
200      * @return the field's access flags
201      */
202     public int getAccess() {
203         return access;
204     }
205 }