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