1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.bcel.internal.classfile;
23
24import java.io.DataInput;
25import java.io.IOException;
26
27import com.sun.org.apache.bcel.internal.Const;
28import com.sun.org.apache.bcel.internal.generic.Type;
29import com.sun.org.apache.bcel.internal.util.BCELComparator;
30
31/**
32 * This class represents the field info structure, i.e., the representation
33 * for a variable in the class. See JVM specification for details.
34 *
35 * @version $Id: Field.java 1749603 2016-06-21 20:50:19Z ggregory $
36 */
37public final class Field extends FieldOrMethod {
38
39    private static BCELComparator bcelComparator = new BCELComparator() {
40
41        @Override
42        public boolean equals( final Object o1, final Object o2 ) {
43            final Field THIS = (Field) o1;
44            final Field THAT = (Field) o2;
45            return THIS.getName().equals(THAT.getName())
46                    && THIS.getSignature().equals(THAT.getSignature());
47        }
48
49
50        @Override
51        public int hashCode( final Object o ) {
52            final Field THIS = (Field) o;
53            return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
54        }
55    };
56
57
58    /**
59     * Initialize from another object. Note that both objects use the same
60     * references (shallow copy). Use clone() for a physical copy.
61     */
62    public Field(final Field c) {
63        super(c);
64    }
65
66
67    /**
68     * Construct object from file stream.
69     * @param file Input stream
70     */
71    Field(final DataInput file, final ConstantPool constant_pool) throws IOException,
72            ClassFormatException {
73        super(file, constant_pool);
74    }
75
76
77    /**
78     * @param access_flags Access rights of field
79     * @param name_index Points to field name in constant pool
80     * @param signature_index Points to encoded signature
81     * @param attributes Collection of attributes
82     * @param constant_pool Array of constants
83     */
84    public Field(final int access_flags, final int name_index, final int signature_index, final Attribute[] attributes,
85            final ConstantPool constant_pool) {
86        super(access_flags, name_index, signature_index, attributes, constant_pool);
87    }
88
89
90    /**
91     * Called by objects that are traversing the nodes of the tree implicitely
92     * defined by the contents of a Java class. I.e., the hierarchy of methods,
93     * fields, attributes, etc. spawns a tree of objects.
94     *
95     * @param v Visitor object
96     */
97    @Override
98    public void accept( final Visitor v ) {
99        v.visitField(this);
100    }
101
102
103    /**
104     * @return constant value associated with this field (may be null)
105     */
106    public final ConstantValue getConstantValue() {
107        for (final Attribute attribute : super.getAttributes()) {
108            if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
109                return (ConstantValue) attribute;
110            }
111        }
112        return null;
113    }
114
115
116    /**
117     * Return string representation close to declaration format,
118     * `public static final short MAX = 100', e.g..
119     *
120     * @return String representation of field, including the signature.
121     */
122    @Override
123    public final String toString() {
124        String name;
125        String signature;
126        String access; // Short cuts to constant pool
127
128        // Get names from constant pool
129        access = Utility.accessToString(super.getAccessFlags());
130        access = access.isEmpty() ? "" : (access + " ");
131        signature = Utility.signatureToString(getSignature());
132        name = getName();
133        final StringBuilder buf = new StringBuilder(64); // CHECKSTYLE IGNORE MagicNumber
134        buf.append(access).append(signature).append(" ").append(name);
135        final ConstantValue cv = getConstantValue();
136        if (cv != null) {
137            buf.append(" = ").append(cv);
138        }
139        for (final Attribute attribute : super.getAttributes()) {
140            if (!(attribute instanceof ConstantValue)) {
141                buf.append(" [").append(attribute).append("]");
142            }
143        }
144        return buf.toString();
145    }
146
147
148    /**
149     * @return deep copy of this field
150     */
151    public final Field copy( final ConstantPool _constant_pool ) {
152        return (Field) copy_(_constant_pool);
153    }
154
155
156    /**
157     * @return type of field
158     */
159    public Type getType() {
160        return Type.getReturnType(getSignature());
161    }
162
163
164    /**
165     * @return Comparison strategy object
166     */
167    public static BCELComparator getComparator() {
168        return bcelComparator;
169    }
170
171
172    /**
173     * @param comparator Comparison strategy object
174     */
175    public static void setComparator( final BCELComparator comparator ) {
176        bcelComparator = comparator;
177    }
178
179
180    /**
181     * Return value as defined by given BCELComparator strategy.
182     * By default two Field objects are said to be equal when
183     * their names and signatures are equal.
184     *
185     * @see java.lang.Object#equals(java.lang.Object)
186     */
187    @Override
188    public boolean equals( final Object obj ) {
189        return bcelComparator.equals(this, obj);
190    }
191
192
193    /**
194     * Return value as defined by given BCELComparator strategy.
195     * By default return the hashcode of the field's name XOR signature.
196     *
197     * @see java.lang.Object#hashCode()
198     */
199    @Override
200    public int hashCode() {
201        return bcelComparator.hashCode(this);
202    }
203}
204