author | joehw |
Wed, 26 Jun 2019 05:49:59 +0000 | |
changeset 55496 | 8e0ae3830fca |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
55496 | 2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
6 | 3 |
*/ |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
4 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
*/ |
6 | 20 |
package com.sun.org.apache.bcel.internal.classfile; |
21 |
||
46174 | 22 |
import java.io.DataInput; |
23 |
import java.io.DataOutputStream; |
|
24 |
import java.io.IOException; |
|
55496 | 25 |
import java.util.Objects; |
6 | 26 |
|
46174 | 27 |
import com.sun.org.apache.bcel.internal.Const; |
28 |
import com.sun.org.apache.bcel.internal.util.BCELComparator; |
|
6 | 29 |
|
30 |
/** |
|
55496 | 31 |
* Abstract superclass for classes to represent the different constant types |
32 |
* in the constant pool of a class file. The classes keep closely to |
|
33 |
* the JVM specification. |
|
6 | 34 |
* |
55496 | 35 |
* @version $Id$ |
36 |
* @LastModified: Jun 2019 |
|
6 | 37 |
*/ |
46174 | 38 |
public abstract class Constant implements Cloneable, Node { |
39 |
||
40 |
private static BCELComparator bcelComparator = new BCELComparator() { |
|
41 |
||
42 |
@Override |
|
55496 | 43 |
public boolean equals( final Object o1, final Object o2 ) { |
46174 | 44 |
final Constant THIS = (Constant) o1; |
45 |
final Constant THAT = (Constant) o2; |
|
55496 | 46 |
return Objects.equals(THIS.toString(), THAT.toString()); |
46174 | 47 |
} |
6 | 48 |
|
55496 | 49 |
|
46174 | 50 |
@Override |
55496 | 51 |
public int hashCode( final Object o ) { |
46174 | 52 |
final Constant THIS = (Constant) o; |
53 |
return THIS.toString().hashCode(); |
|
54 |
} |
|
55 |
}; |
|
56 |
||
57 |
/* In fact this tag is redundant since we can distinguish different |
|
58 |
* `Constant' objects by their type, i.e., via `instanceof'. In some |
|
59 |
* places we will use the tag for switch()es anyway. |
|
60 |
* |
|
61 |
* First, we want match the specification as closely as possible. Second we |
|
62 |
* need the tag as an index to select the corresponding class name from the |
|
63 |
* `CONSTANT_NAMES' array. |
|
64 |
*/ |
|
65 |
private byte tag; |
|
66 |
||
55496 | 67 |
|
46174 | 68 |
Constant(final byte tag) { |
69 |
this.tag = tag; |
|
70 |
} |
|
6 | 71 |
|
55496 | 72 |
|
46174 | 73 |
/** |
74 |
* Called by objects that are traversing the nodes of the tree implicitely |
|
75 |
* defined by the contents of a Java class. I.e., the hierarchy of methods, |
|
76 |
* fields, attributes, etc. spawns a tree of objects. |
|
77 |
* |
|
78 |
* @param v Visitor object |
|
79 |
*/ |
|
80 |
@Override |
|
55496 | 81 |
public abstract void accept( Visitor v ); |
82 |
||
6 | 83 |
|
55496 | 84 |
public abstract void dump( DataOutputStream file ) throws IOException; |
85 |
||
46174 | 86 |
|
87 |
/** |
|
88 |
* @return Tag of constant, i.e., its type. No setTag() method to avoid |
|
89 |
* confusion. |
|
90 |
*/ |
|
91 |
public final byte getTag() { |
|
92 |
return tag; |
|
93 |
} |
|
6 | 94 |
|
55496 | 95 |
|
46174 | 96 |
/** |
97 |
* @return String representation. |
|
98 |
*/ |
|
99 |
@Override |
|
100 |
public String toString() { |
|
101 |
return Const.getConstantName(tag) + "[" + tag + "]"; |
|
102 |
} |
|
6 | 103 |
|
55496 | 104 |
|
46174 | 105 |
/** |
106 |
* @return deep copy of this constant |
|
107 |
*/ |
|
108 |
public Constant copy() { |
|
109 |
try { |
|
110 |
return (Constant) super.clone(); |
|
111 |
} catch (final CloneNotSupportedException e) { |
|
112 |
// TODO should this throw? |
|
113 |
} |
|
114 |
return null; |
|
115 |
} |
|
6 | 116 |
|
55496 | 117 |
|
46174 | 118 |
@Override |
119 |
public Object clone() { |
|
120 |
try { |
|
121 |
return super.clone(); |
|
122 |
} catch (final CloneNotSupportedException e) { |
|
123 |
throw new Error("Clone Not Supported"); // never happens |
|
124 |
} |
|
125 |
} |
|
6 | 126 |
|
55496 | 127 |
|
46174 | 128 |
/** |
129 |
* Read one constant from the given input, the type depends on a tag byte. |
|
130 |
* |
|
55496 | 131 |
* @param dataInput Input stream |
46174 | 132 |
* @return Constant object |
55496 | 133 |
* @throws IOException if an I/O error occurs reading from the given {@code dataInput}. |
134 |
* @throws ClassFormatException if the next byte is not recognized |
|
46174 | 135 |
* @since 6.0 made public |
136 |
*/ |
|
55496 | 137 |
public static Constant readConstant(final DataInput dataInput) throws IOException, ClassFormatException { |
138 |
final byte b = dataInput.readByte(); // Read tag byte |
|
46174 | 139 |
switch (b) { |
55496 | 140 |
case Const.CONSTANT_Class: |
141 |
return new ConstantClass(dataInput); |
|
142 |
case Const.CONSTANT_Fieldref: |
|
143 |
return new ConstantFieldref(dataInput); |
|
144 |
case Const.CONSTANT_Methodref: |
|
145 |
return new ConstantMethodref(dataInput); |
|
146 |
case Const.CONSTANT_InterfaceMethodref: |
|
147 |
return new ConstantInterfaceMethodref(dataInput); |
|
148 |
case Const.CONSTANT_String: |
|
149 |
return new ConstantString(dataInput); |
|
150 |
case Const.CONSTANT_Integer: |
|
151 |
return new ConstantInteger(dataInput); |
|
152 |
case Const.CONSTANT_Float: |
|
153 |
return new ConstantFloat(dataInput); |
|
154 |
case Const.CONSTANT_Long: |
|
155 |
return new ConstantLong(dataInput); |
|
156 |
case Const.CONSTANT_Double: |
|
157 |
return new ConstantDouble(dataInput); |
|
158 |
case Const.CONSTANT_NameAndType: |
|
159 |
return new ConstantNameAndType(dataInput); |
|
160 |
case Const.CONSTANT_Utf8: |
|
161 |
return ConstantUtf8.getInstance(dataInput); |
|
162 |
case Const.CONSTANT_MethodHandle: |
|
163 |
return new ConstantMethodHandle(dataInput); |
|
164 |
case Const.CONSTANT_MethodType: |
|
165 |
return new ConstantMethodType(dataInput); |
|
166 |
case Const.CONSTANT_Dynamic: |
|
167 |
return new ConstantDynamic(dataInput); |
|
168 |
case Const.CONSTANT_InvokeDynamic: |
|
169 |
return new ConstantInvokeDynamic(dataInput); |
|
170 |
case Const.CONSTANT_Module: |
|
171 |
return new ConstantModule(dataInput); |
|
172 |
case Const.CONSTANT_Package: |
|
173 |
return new ConstantPackage(dataInput); |
|
174 |
default: |
|
175 |
throw new ClassFormatException("Invalid byte tag in constant pool: " + b); |
|
46174 | 176 |
} |
177 |
} |
|
6 | 178 |
|
46174 | 179 |
/** |
180 |
* @return Comparison strategy object |
|
181 |
*/ |
|
182 |
public static BCELComparator getComparator() { |
|
183 |
return bcelComparator; |
|
184 |
} |
|
185 |
||
55496 | 186 |
|
46174 | 187 |
/** |
188 |
* @param comparator Comparison strategy object |
|
189 |
*/ |
|
55496 | 190 |
public static void setComparator( final BCELComparator comparator ) { |
46174 | 191 |
bcelComparator = comparator; |
6 | 192 |
} |
46174 | 193 |
|
55496 | 194 |
|
46174 | 195 |
/** |
55496 | 196 |
* Return value as defined by given BCELComparator strategy. |
197 |
* By default two Constant objects are said to be equal when |
|
198 |
* the result of toString() is equal. |
|
46174 | 199 |
* |
200 |
* @see java.lang.Object#equals(java.lang.Object) |
|
201 |
*/ |
|
202 |
@Override |
|
55496 | 203 |
public boolean equals( final Object obj ) { |
46174 | 204 |
return bcelComparator.equals(this, obj); |
205 |
} |
|
206 |
||
55496 | 207 |
|
46174 | 208 |
/** |
55496 | 209 |
* Return value as defined by given BCELComparator strategy. |
210 |
* By default return the hashcode of the result of toString(). |
|
46174 | 211 |
* |
212 |
* @see java.lang.Object#hashCode() |
|
213 |
*/ |
|
214 |
@Override |
|
215 |
public int hashCode() { |
|
216 |
return bcelComparator.hashCode(this); |
|
217 |
} |
|
6 | 218 |
} |