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 CPFieldRef extends ConstantPoolEntry {
23:
24: CPClass className;
25: transient int classNameIndex;
26:
27: private CPNameAndType nameAndType;
28: transient int nameAndTypeIndex;
29:
30: public CPFieldRef(String className, String descriptor) {
31: super (ConstantPoolEntry.CP_Fieldref);
32: this .domain = ClassConstantPool.DOMAIN_FIELD;
33: this .className = new CPClass(className);
34: this .nameAndType = new CPNameAndType(descriptor);
35: }
36:
37: protected ClassFileEntry[] getNestedClassFileEntries() {
38: return new ClassFileEntry[] { className, nameAndType };
39: }
40:
41: protected void resolve(ClassConstantPool pool) {
42: super .resolve(pool);
43: nameAndTypeIndex = pool.indexOf(nameAndType);
44: classNameIndex = pool.indexOf(className);
45: }
46:
47: protected void writeBody(DataOutputStream dos) throws IOException {
48: dos.writeShort(classNameIndex);
49: dos.writeShort(nameAndTypeIndex);
50: }
51:
52: public String toString() {
53: return "FieldRef: " + className + "#" + nameAndType;
54: }
55:
56: public String comparisonString() {
57: return (className.getName() + Character.MAX_VALUE)
58: + nameAndType.descriptor + Character.MAX_VALUE
59: + nameAndType.name;
60: }
61:
62: public int hashCode() {
63: final int PRIME = 31;
64: int result = 1;
65: result = PRIME * result
66: + ((className == null) ? 0 : className.hashCode());
67: result = PRIME * result
68: + ((nameAndType == null) ? 0 : nameAndType.hashCode());
69: return result;
70: }
71:
72: public boolean equals(Object obj) {
73: if (this == obj)
74: return true;
75: if (obj == null)
76: return false;
77: if (getClass() != obj.getClass())
78: return false;
79: final CPFieldRef other = (CPFieldRef) obj;
80: if (className == null) {
81: if (other.className != null)
82: return false;
83: } else if (!className.equals(other.className))
84: return false;
85: if (nameAndType == null) {
86: if (other.nameAndType != null)
87: return false;
88: } else if (!nameAndType.equals(other.nameAndType))
89: return false;
90: return true;
91: }
92:
93: }
|