View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200.bytecode;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Objects;
26  
27  /**
28   * Superclass for member constant pool entries, such as fields or methods.
29   */
30  public class CPMember extends ClassFileEntry {
31  
32      List<Attribute> attributes;
33      short flags;
34      CPUTF8 name;
35      transient int nameIndex;
36      protected final CPUTF8 descriptor;
37      transient int descriptorIndex;
38  
39      /**
40       * Constructs a new CPMember.
41       *
42       * @param name       TODO
43       * @param descriptor TODO
44       * @param flags      TODO
45       * @param attributes TODO
46       * @throws NullPointerException if name or descriptor is null
47       */
48      public CPMember(final CPUTF8 name, final CPUTF8 descriptor, final long flags, final List<Attribute> attributes) {
49          this.name = Objects.requireNonNull(name, "name");
50          this.descriptor = Objects.requireNonNull(descriptor, "descriptor");
51          this.flags = (short) flags;
52          this.attributes = attributes == null ? Collections.EMPTY_LIST : attributes;
53      }
54  
55      @Override
56      protected void doWrite(final DataOutputStream dos) throws IOException {
57          dos.writeShort(flags);
58          dos.writeShort(nameIndex);
59          dos.writeShort(descriptorIndex);
60          final int attributeCount = attributes.size();
61          dos.writeShort(attributeCount);
62          for (int i = 0; i < attributeCount; i++) {
63              final Attribute attribute = attributes.get(i);
64              attribute.doWrite(dos);
65          }
66      }
67  
68      @Override
69      public boolean equals(final Object obj) {
70          if (this == obj) {
71              return true;
72          }
73          if (obj == null || getClass() != obj.getClass()) {
74              return false;
75          }
76          final CPMember other = (CPMember) obj;
77          return Objects.equals(attributes, other.attributes)
78                  && Objects.equals(descriptor, other.descriptor)
79                  && flags == other.flags
80                  && Objects.equals(name, other.name);
81      }
82  
83      @Override
84      protected ClassFileEntry[] getNestedClassFileEntries() {
85          final int attributeCount = attributes.size();
86          final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
87          entries[0] = name;
88          entries[1] = descriptor;
89          for (int i = 0; i < attributeCount; i++) {
90              entries[i + 2] = attributes.get(i);
91          }
92          return entries;
93      }
94  
95      @Override
96      public int hashCode() {
97          final int PRIME = 31;
98          int result = 1;
99          result = PRIME * result + attributes.hashCode();
100         result = PRIME * result + descriptor.hashCode();
101         result = PRIME * result + flags;
102         result = PRIME * result + name.hashCode();
103         return result;
104     }
105 
106     @Override
107     protected void resolve(final ClassConstantPool pool) {
108         super.resolve(pool);
109         nameIndex = pool.indexOf(name);
110         descriptorIndex = pool.indexOf(descriptor);
111         attributes.forEach(attribute -> attribute.resolve(pool));
112     }
113 
114     @Override
115     public String toString() {
116         return "CPMember: " + name + "(" + descriptor + ")";
117     }
118 
119 }