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          return Objects.equals(attributes, other.attributes)
79                  && Objects.equals(descriptor, other.descriptor)
80                  && flags == other.flags
81                  && Objects.equals(name, other.name);
82      }
83  
84      @Override
85      protected ClassFileEntry[] getNestedClassFileEntries() {
86          final int attributeCount = attributes.size();
87          final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
88          entries[0] = name;
89          entries[1] = descriptor;
90          for (int i = 0; i < attributeCount; i++) {
91              entries[i + 2] = attributes.get(i);
92          }
93          return entries;
94      }
95  
96      @Override
97      public int hashCode() {
98          final int PRIME = 31;
99          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 }