001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.IOException;
022import java.util.Objects;
023
024import org.apache.bcel.Const;
025import org.apache.bcel.generic.Type;
026import org.apache.bcel.util.BCELComparator;
027
028/**
029 * This class represents the field info structure, i.e., the representation
030 * for a variable in the class. See JVM specification for details.
031 *
032 * @version $Id$
033 */
034public final class Field extends FieldOrMethod {
035
036    private static BCELComparator bcelComparator = new BCELComparator() {
037
038        @Override
039        public boolean equals( final Object o1, final Object o2 ) {
040            final Field THIS = (Field) o1;
041            final Field THAT = (Field) o2;
042            return Objects.equals(THIS.getName(), THAT.getName())
043                    && Objects.equals(THIS.getSignature(), THAT.getSignature());
044        }
045
046
047        @Override
048        public int hashCode( final Object o ) {
049            final Field THIS = (Field) o;
050            return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
051        }
052    };
053
054
055    /**
056     * Initialize from another object. Note that both objects use the same
057     * references (shallow copy). Use clone() for a physical copy.
058     */
059    public Field(final Field c) {
060        super(c);
061    }
062
063
064    /**
065     * Construct object from file stream.
066     * @param file Input stream
067     */
068    Field(final DataInput file, final ConstantPool constant_pool) throws IOException,
069            ClassFormatException {
070        super(file, constant_pool);
071    }
072
073
074    /**
075     * @param access_flags Access rights of field
076     * @param name_index Points to field name in constant pool
077     * @param signature_index Points to encoded signature
078     * @param attributes Collection of attributes
079     * @param constant_pool Array of constants
080     */
081    public Field(final int access_flags, final int name_index, final int signature_index, final Attribute[] attributes,
082            final ConstantPool constant_pool) {
083        super(access_flags, name_index, signature_index, attributes, constant_pool);
084    }
085
086
087    /**
088     * Called by objects that are traversing the nodes of the tree implicitely
089     * defined by the contents of a Java class. I.e., the hierarchy of methods,
090     * fields, attributes, etc. spawns a tree of objects.
091     *
092     * @param v Visitor object
093     */
094    @Override
095    public void accept( final Visitor v ) {
096        v.visitField(this);
097    }
098
099
100    /**
101     * @return constant value associated with this field (may be null)
102     */
103    public final ConstantValue getConstantValue() {
104        for (final Attribute attribute : super.getAttributes()) {
105            if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
106                return (ConstantValue) attribute;
107            }
108        }
109        return null;
110    }
111
112
113    /**
114     * Return string representation close to declaration format,
115     * `public static final short MAX = 100', e.g..
116     *
117     * @return String representation of field, including the signature.
118     */
119    @Override
120    public final String toString() {
121        String name;
122        String signature;
123        String access; // Short cuts to constant pool
124
125        // Get names from constant pool
126        access = Utility.accessToString(super.getAccessFlags());
127        access = access.isEmpty() ? "" : (access + " ");
128        signature = Utility.signatureToString(getSignature());
129        name = getName();
130        final StringBuilder buf = new StringBuilder(64); // CHECKSTYLE IGNORE MagicNumber
131        buf.append(access).append(signature).append(" ").append(name);
132        final ConstantValue cv = getConstantValue();
133        if (cv != null) {
134            buf.append(" = ").append(cv);
135        }
136        for (final Attribute attribute : super.getAttributes()) {
137            if (!(attribute instanceof ConstantValue)) {
138                buf.append(" [").append(attribute).append("]");
139            }
140        }
141        return buf.toString();
142    }
143
144
145    /**
146     * @return deep copy of this field
147     */
148    public final Field copy( final ConstantPool _constant_pool ) {
149        return (Field) copy_(_constant_pool);
150    }
151
152
153    /**
154     * @return type of field
155     */
156    public Type getType() {
157        return Type.getReturnType(getSignature());
158    }
159
160
161    /**
162     * @return Comparison strategy object
163     */
164    public static BCELComparator getComparator() {
165        return bcelComparator;
166    }
167
168
169    /**
170     * @param comparator Comparison strategy object
171     */
172    public static void setComparator( final BCELComparator comparator ) {
173        bcelComparator = comparator;
174    }
175
176
177    /**
178     * Return value as defined by given BCELComparator strategy.
179     * By default two Field objects are said to be equal when
180     * their names and signatures are equal.
181     *
182     * @see java.lang.Object#equals(java.lang.Object)
183     */
184    @Override
185    public boolean equals( final Object obj ) {
186        return bcelComparator.equals(this, obj);
187    }
188
189
190    /**
191     * Return value as defined by given BCELComparator strategy.
192     * By default return the hashcode of the field's name XOR signature.
193     *
194     * @see java.lang.Object#hashCode()
195     */
196    @Override
197    public int hashCode() {
198        return bcelComparator.hashCode(this);
199    }
200}