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.Collections; 022import java.util.List; 023 024/** 025 * Superclass for member constant pool entries, such as fields or methods. 026 */ 027public class CPMember extends ClassFileEntry { 028 029 List attributes; 030 short flags; 031 CPUTF8 name; 032 transient int nameIndex; 033 protected final CPUTF8 descriptor; 034 transient int descriptorIndex; 035 036 /** 037 * Create a new CPMember 038 * 039 * @param name TODO 040 * @param descriptor TODO 041 * @param flags TODO 042 * @param attributes TODO 043 * @throws NullPointerException if name or descriptor is null 044 */ 045 public CPMember(final CPUTF8 name, final CPUTF8 descriptor, final long flags, final List attributes) { 046 this.name = name; 047 this.descriptor = descriptor; 048 this.flags = (short) flags; 049 this.attributes = (attributes == null ? Collections.EMPTY_LIST : attributes); 050 if (name == null || descriptor == null) { 051 throw new NullPointerException("Null arguments are not allowed"); 052 } 053 } 054 055 @Override 056 protected ClassFileEntry[] getNestedClassFileEntries() { 057 final int attributeCount = attributes.size(); 058 final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2]; 059 entries[0] = name; 060 entries[1] = descriptor; 061 for (int i = 0; i < attributeCount; i++) { 062 entries[i + 2] = (Attribute) attributes.get(i); 063 } 064 return entries; 065 } 066 067 @Override 068 protected void resolve(final ClassConstantPool pool) { 069 super.resolve(pool); 070 nameIndex = pool.indexOf(name); 071 descriptorIndex = pool.indexOf(descriptor); 072 for (int it = 0; it < attributes.size(); it++) { 073 final Attribute attribute = (Attribute) attributes.get(it); 074 attribute.resolve(pool); 075 } 076 } 077 078 @Override 079 public int hashCode() { 080 final int PRIME = 31; 081 int result = 1; 082 result = PRIME * result + attributes.hashCode(); 083 result = PRIME * result + descriptor.hashCode(); 084 result = PRIME * result + flags; 085 result = PRIME * result + name.hashCode(); 086 return result; 087 } 088 089 @Override 090 public String toString() { 091 return "CPMember: " + name + "(" + descriptor + ")"; 092 } 093 094 @Override 095 public boolean equals(final Object obj) { 096 if (this == obj) { 097 return true; 098 } 099 if (obj == null) { 100 return false; 101 } 102 if (getClass() != obj.getClass()) { 103 return false; 104 } 105 final CPMember other = (CPMember) obj; 106 if (!attributes.equals(other.attributes)) { 107 return false; 108 } 109 if (!descriptor.equals(other.descriptor)) { 110 return false; 111 } 112 if (flags != other.flags) { 113 return false; 114 } 115 if (!name.equals(other.name)) { 116 return false; 117 } 118 return true; 119 } 120 121 @Override 122 protected void doWrite(final DataOutputStream dos) throws IOException { 123 dos.writeShort(flags); 124 dos.writeShort(nameIndex); 125 dos.writeShort(descriptorIndex); 126 final int attributeCount = attributes.size(); 127 dos.writeShort(attributeCount); 128 for (int i = 0; i < attributeCount; i++) { 129 final Attribute attribute = (Attribute) attributes.get(i); 130 attribute.doWrite(dos); 131 } 132 } 133 134}