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