Debian Codegraph @

src/org/apache/commons/compress/harmony/unpack200/bytecode/CPRef.java

raw symbols SCIP
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.Objects;
24
25 /**
26 * Abstract superclass for reference constant pool entries, such as a method or field reference.
27 */
28 public abstract class CPRef extends ConstantPoolEntry {
29
30 CPClass className;
31 transient int classNameIndex;
32
33 protected CPNameAndType nameAndType;
34 transient int nameAndTypeIndex;
35
36 protected String cachedToString;
37
38 /**
39 * Constructs a new CPRef.
40 *
41 * @param type TODO
42 * @param className TODO
43 * @param descriptor TODO
44 * @param globalIndex index in CpBands
45 * @throws NullPointerException if descriptor or className is null
46 */
47 public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
48 super(type, globalIndex);
49 this.className = Objects.requireNonNull(className, "className");
50 this.nameAndType = Objects.requireNonNull(descriptor, "descriptor");
51 }
52
53 @Override
54 public boolean equals(final Object obj) {
55 if (this == obj) {
56 return true;
57 }
58 if (obj == null || getClass() != obj.getClass() || hashCode() != obj.hashCode()) {
59 return false;
60 }
61 final CPRef other = (CPRef) obj;
62 return Objects.equals(className, other.className)
63 && Objects.equals(nameAndType, other.nameAndType);
64 }
65
66 @Override
67 protected ClassFileEntry[] getNestedClassFileEntries() {
68 final ClassFileEntry[] entries = new ClassFileEntry[2];
69 entries[0] = className;
70 entries[1] = nameAndType;
71 return entries;
72 }
73
74 @Override
75 protected void resolve(final ClassConstantPool pool) {
76 super.resolve(pool);
77 nameAndTypeIndex = pool.indexOf(nameAndType);
78 classNameIndex = pool.indexOf(className);
79 }
80
81 @Override
82 public String toString() {
83 if (cachedToString == null) {
84 final String type;
85 if (getTag() == CP_Fieldref) {
86 type = "FieldRef"; //$NON-NLS-1$
87 } else if (getTag() == CP_Methodref) {
88 type = "MethoddRef"; //$NON-NLS-1$
89 } else if (getTag() == CP_InterfaceMethodref) {
90 type = "InterfaceMethodRef"; //$NON-NLS-1$
91 } else {
92 type = "unknown"; //$NON-NLS-1$
93 }
94 cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
95 }
96 return cachedToString;
97 }
98
99 @Override
100 protected void writeBody(final DataOutputStream dos) throws IOException {
101 dos.writeShort(classNameIndex);
102 dos.writeShort(nameAndTypeIndex);
103 }
104
105 }