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: */
017: package org.apache.harmony.pack200.bytecode;
018:
019: import java.io.DataOutputStream;
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: public class CPMember extends ClassFileEntry {
026:
027: List attributes;
028:
029: short flags;
030:
031: CPUTF8 name;
032: transient int nameIndex;
033:
034: private CPUTF8 descriptor;
035: transient int descriptorIndex;
036:
037: public CPMember(String descriptor, long flags, List attributes) {
038: int colon = descriptor.indexOf(':');
039: this .name = new CPUTF8(descriptor.substring(0, colon),
040: ClassConstantPool.DOMAIN_NORMALASCIIZ);
041: this .descriptor = new CPUTF8(descriptor.substring(colon + 1),
042: ClassConstantPool.DOMAIN_SIGNATUREASCIIZ);
043: this .flags = (short) flags;
044: this .attributes = (attributes == null ? new ArrayList()
045: : attributes);
046: }
047:
048: protected ClassFileEntry[] getNestedClassFileEntries() {
049: int attributeCount = attributes.size();
050: ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
051: entries[0] = name;
052: entries[1] = descriptor;
053: for (int i = 0; i < attributeCount; i++) {
054: entries[i + 2] = (Attribute) attributes.get(i);
055: }
056: return entries;
057: }
058:
059: protected void resolve(ClassConstantPool pool) {
060: super .resolve(pool);
061: nameIndex = pool.indexOf(name);
062: descriptorIndex = pool.indexOf(descriptor);
063: for (Iterator it = attributes.iterator(); it.hasNext();) {
064: Attribute attribute = (Attribute) it.next();
065: attribute.resolve(pool);
066: }
067: }
068:
069: public String toString() {
070: return "Field: " + name + "(" + descriptor + ")";
071: }
072:
073: public int hashCode() {
074: final int PRIME = 31;
075: int result = 1;
076: result = PRIME * result
077: + ((attributes == null) ? 0 : attributes.hashCode());
078: result = PRIME * result
079: + ((descriptor == null) ? 0 : descriptor.hashCode());
080: result = PRIME * result + flags;
081: result = PRIME * result
082: + ((name == null) ? 0 : name.hashCode());
083: return result;
084: }
085:
086: public boolean equals(Object obj) {
087: if (this == obj)
088: return true;
089: if (obj == null)
090: return false;
091: if (getClass() != obj.getClass())
092: return false;
093: final CPMember other = (CPMember) obj;
094: if (attributes == null) {
095: if (other.attributes != null)
096: return false;
097: } else if (!attributes.equals(other.attributes))
098: return false;
099: if (descriptor == null) {
100: if (other.descriptor != null)
101: return false;
102: } else if (!descriptor.equals(other.descriptor))
103: return false;
104: if (flags != other.flags)
105: return false;
106: if (name == null) {
107: if (other.name != null)
108: return false;
109: } else if (!name.equals(other.name))
110: return false;
111: return true;
112: }
113:
114: protected void doWrite(DataOutputStream dos) throws IOException {
115: dos.writeShort(flags);
116: dos.writeShort(nameIndex);
117: dos.writeShort(descriptorIndex);
118: int attributeCount = attributes.size();
119: dos.writeShort(attributeCount);
120: for (int i = 0; i < attributeCount; i++) {
121: Attribute attribute = (Attribute) attributes.get(i);
122: attribute.doWrite(dos);
123: }
124:
125: }
126:
127: }
|