    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;
    022
    023import org.apache.bcel.Const;
    024import org.apache.bcel.generic.Type;
    025import org.apache.bcel.util.BCELComparator;
    026
    027/**
    028 * This class represents the field info structure, i.e., the representation
    029 * for a variable in the class. See JVM specification for details.
    030 *
    031 * @version $Id: Field.java 1806200 2017-08-25 16:33:06Z ggregory $
    032 */
    033public final class Field extends FieldOrMethod {
    034
    035    private static BCELComparator bcelComparator = new BCELComparator() {
    036
    037        @Override
    038        public boolean equals( final Object o1, final Object o2 ) {
    039            final Field THIS = (Field) o1;
    040            final Field THAT = (Field) o2;
    041            return THIS.getName().equals(THAT.getName())
    042                    && THIS.getSignature().equals(THAT.getSignature());
    043        }
    044
    045
    046        @Override
    047        public int hashCode( final Object o ) {
    048            final Field THIS = (Field) o;
    049            return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
    050        }
    051    };
    052
    053
    054    /**
    055     * Initialize from another object. Note that both objects use the same
    056     * references (shallow copy). Use clone() for a physical copy.
    057     */
    058    public Field(final Field c) {
    059        super(c);
    060    }
    061
    062
    063    /**
    064     * Construct object from file stream.
    065     * @param file Input stream
    066     */
    067    Field(final DataInput file, final ConstantPool constant_pool) throws IOException,
    068            ClassFormatException {
    069        super(file, constant_pool);
    070    }
    071
    072
    073    /**
    074     * @param access_flags Access rights of field
    075     * @param name_index Points to field name in constant pool
    076     * @param signature_index Points to encoded signature
    077     * @param attributes Collection of attributes
    078     * @param constant_pool Array of constants
    079     */
    080    public Field(final int access_flags, final int name_index, final int signature_index, final Attribute[] attributes,
    081            final ConstantPool constant_pool) {
    082        super(access_flags, name_index, signature_index, attributes, constant_pool);
    083    }
    084
    085
    086    /**
    087     * Called by objects that are traversing the nodes of the tree implicitely
    088     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    089     * fields, attributes, etc. spawns a tree of objects.
    090     *
    091     * @param v Visitor object
    092     */
    093    @Override
    094    public void accept( final Visitor v ) {
    095        v.visitField(this);
    096    }
    097
    098
    099    /**
    100     * @return constant value associated with this field (may be null)
    101     */
    102    public final ConstantValue getConstantValue() {
    103        for (final Attribute attribute : super.getAttributes()) {
    104            if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) {
    105                return (ConstantValue) attribute;
    106            }
    107        }
    108        return null;
    109    }
    110
    111
    112    /**
    113     * Return string representation close to declaration format,
    114     * `public static final short MAX = 100', e.g..
    115     *
    116     * @return String representation of field, including the signature.
    117     */
    118    @Override
    119    public final String toString() {
    120        String name;
    121        String signature;
    122        String access; // Short cuts to constant pool
    123
    124        // Get names from constant pool
    125        access = Utility.accessToString(super.getAccessFlags());
    126        access = access.isEmpty() ? "" : (access + " ");
    127        signature = Utility.signatureToString(getSignature());
    128        name = getName();
    129        final StringBuilder buf = new StringBuilder(64); // CHECKSTYLE IGNORE MagicNumber
    130        buf.append(access).append(signature).append(" ").append(name);
    131        final ConstantValue cv = getConstantValue();
    132        if (cv != null) {
    133            buf.append(" = ").append(cv);
    134        }
    135        for (final Attribute attribute : super.getAttributes()) {
    136            if (!(attribute instanceof ConstantValue)) {
    137                buf.append(" [").append(attribute).append("]");
    138            }
    139        }
    140        return buf.toString();
    141    }
    142
    143
    144    /**
    145     * @return deep copy of this field
    146     */
    147    public final Field copy( final ConstantPool _constant_pool ) {
    148        return (Field) copy_(_constant_pool);
    149    }
    150
    151
    152    /**
    153     * @return type of field
    154     */
    155    public Type getType() {
    156        return Type.getReturnType(getSignature());
    157    }
    158
    159
    160    /**
    161     * @return Comparison strategy object
    162     */
    163    public static BCELComparator getComparator() {
    164        return bcelComparator;
    165    }
    166
    167
    168    /**
    169     * @param comparator Comparison strategy object
    170     */
    171    public static void setComparator( final BCELComparator comparator ) {
    172        bcelComparator = comparator;
    173    }
    174
    175
    176    /**
    177     * Return value as defined by given BCELComparator strategy.
    178     * By default two Field objects are said to be equal when
    179     * their names and signatures are equal.
    180     *
    181     * @see java.lang.Object#equals(java.lang.Object)
    182     */
    183    @Override
    184    public boolean equals( final Object obj ) {
    185        return bcelComparator.equals(this, obj);
    186    }
    187
    188
    189    /**
    190     * Return value as defined by given BCELComparator strategy.
    191     * By default return the hashcode of the field's name XOR signature.
    192     *
    193     * @see java.lang.Object#hashCode()
    194     */
    195    @Override
    196    public int hashCode() {
    197        return bcelComparator.hashCode(this);
    198    }
    199}