01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.pack200.bytecode;
18:
19: import java.io.DataOutputStream;
20: import java.io.IOException;
21:
22: public class ConstantValueAttribute extends Attribute {
23: private int constantIndex;
24:
25: private ClassFileEntry entry;
26:
27: public ConstantValueAttribute(Object value) {
28: super ("ConstantValue"); //$NON-NLS-1$
29: if (value instanceof java.lang.Integer) {
30: this .entry = new CPInteger((Integer) value);
31: } else if (value instanceof java.lang.Long) {
32: this .entry = new CPLong((Long) value);
33: } else if (value instanceof java.lang.Float) {
34: this .entry = new CPFloat((Float) value);
35: } else if (value instanceof java.lang.Double) {
36: this .entry = new CPDouble((Double) value);
37: } else if (value instanceof java.lang.String) {
38: this .entry = new CPString((String) value);
39: } else {
40: throw new Error("Oops, I've not done it again");
41: }
42: }
43:
44: public boolean equals(Object obj) {
45: if (this == obj)
46: return true;
47: if (!super .equals(obj))
48: return false;
49: if (this .getClass() != obj.getClass())
50: return false;
51: final ConstantValueAttribute other = (ConstantValueAttribute) obj;
52: if (entry == null) {
53: if (other.entry != null)
54: return false;
55: } else if (!entry.equals(other.entry))
56: return false;
57: return true;
58: }
59:
60: protected int getLength() {
61: return 2;
62: }
63:
64: protected ClassFileEntry[] getNestedClassFileEntries() {
65: return new ClassFileEntry[] { getAttributeName(), entry };
66: }
67:
68: public int hashCode() {
69: final int PRIME = 31;
70: int result = super .hashCode();
71: result = PRIME * result
72: + ((entry == null) ? 0 : entry.hashCode());
73: return result;
74: }
75:
76: protected void resolve(ClassConstantPool pool) {
77: super .resolve(pool);
78: entry.resolve(pool);
79: this .constantIndex = pool.indexOf(entry);
80: }
81:
82: public String toString() {
83: return "Constant:" + entry;
84: }
85:
86: protected void writeBody(DataOutputStream dos) throws IOException {
87: dos.writeShort(constantIndex);
88: }
89:
90: }
|