    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 */
    017package org.apache.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021
    022/**
    023 * An {@link Attribute} representing a constant.
    024 */
    025public class ConstantValueAttribute extends Attribute {
    026
    027    private int constantIndex;
    028
    029    private final ClassFileEntry entry;
    030
    031    private static CPUTF8 attributeName;
    032
    033    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    034        attributeName = cpUTF8Value;
    035    }
    036
    037    public ConstantValueAttribute(final ClassFileEntry entry) {
    038        super(attributeName);
    039        if (entry == null) {
    040            throw new NullPointerException();
    041        }
    042        this.entry = entry;
    043    }
    044
    045    @Override
    046    public boolean equals(final Object obj) {
    047        if (this == obj) {
    048            return true;
    049        }
    050        if (!super.equals(obj)) {
    051            return false;
    052        }
    053        if (this.getClass() != obj.getClass()) {
    054            return false;
    055        }
    056        final ConstantValueAttribute other = (ConstantValueAttribute) obj;
    057        if (entry == null) {
    058            if (other.entry != null) {
    059                return false;
    060            }
    061        } else if (!entry.equals(other.entry)) {
    062            return false;
    063        }
    064        return true;
    065    }
    066
    067    @Override
    068    protected int getLength() {
    069        return 2;
    070    }
    071
    072    @Override
    073    protected ClassFileEntry[] getNestedClassFileEntries() {
    074        return new ClassFileEntry[] {getAttributeName(), entry};
    075    }
    076
    077    @Override
    078    public int hashCode() {
    079        final int PRIME = 31;
    080        int result = super.hashCode();
    081        result = PRIME * result + ((entry == null) ? 0 : entry.hashCode());
    082        return result;
    083    }
    084
    085    @Override
    086    protected void resolve(final ClassConstantPool pool) {
    087        super.resolve(pool);
    088        entry.resolve(pool);
    089        this.constantIndex = pool.indexOf(entry);
    090    }
    091
    092    @Override
    093    public String toString() {
    094        return "Constant:" + entry;
    095    }
    096
    097    @Override
    098    protected void writeBody(final DataOutputStream dos) throws IOException {
    099        dos.writeShort(constantIndex);
    100    }
    101
    102}