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