    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.harmony.unpack200.bytecode;
    020
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023import java.util.Objects;
    024
    025/**
    026 * An {@link Attribute} representing a constant.
    027 */
    028public class ConstantValueAttribute extends Attribute {
    029
    030    private static CPUTF8 attributeName;
    031
    032    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    033        attributeName = cpUTF8Value;
    034    }
    035
    036    private int constantIndex;
    037
    038    private final ClassFileEntry entry;
    039
    040    /**
    041     * Constructs a new instance.
    042     *
    043     * @param entry an entry.
    044     */
    045    public ConstantValueAttribute(final ClassFileEntry entry) {
    046        super(attributeName);
    047        this.entry = Objects.requireNonNull(entry, "entry");
    048    }
    049
    050    @Override
    051    public boolean equals(final Object obj) {
    052        if (this == obj) {
    053            return true;
    054        }
    055        if (!super.equals(obj) || this.getClass() != obj.getClass()) {
    056            return false;
    057        }
    058        final ConstantValueAttribute other = (ConstantValueAttribute) obj;
    059        return Objects.equals(entry, other.entry);
    060    }
    061
    062    @Override
    063    protected int getLength() {
    064        return 2;
    065    }
    066
    067    @Override
    068    protected ClassFileEntry[] getNestedClassFileEntries() {
    069        return new ClassFileEntry[] { getAttributeName(), entry };
    070    }
    071
    072    @Override
    073    public int hashCode() {
    074        final int PRIME = 31;
    075        int result = super.hashCode();
    076        result = PRIME * result + (entry == null ? 0 : entry.hashCode());
    077        return result;
    078    }
    079
    080    @Override
    081    protected void resolve(final ClassConstantPool pool) {
    082        super.resolve(pool);
    083        entry.resolve(pool);
    084        this.constantIndex = pool.indexOf(entry);
    085    }
    086
    087    @Override
    088    public String toString() {
    089        return "Constant:" + entry;
    090    }
    091
    092    @Override
    093    protected void writeBody(final DataOutputStream dos) throws IOException {
    094        dos.writeShort(constantIndex);
    095    }
    096
    097}