author | joehw |
Wed, 26 Jun 2019 05:49:59 +0000 | |
changeset 55496 | 8e0ae3830fca |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* 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
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* 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
|
10 |
* (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
|
11 |
* 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
|
12 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* 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
|
16 |
* 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
|
17 |
* 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
|
18 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.bcel.internal.classfile; |
23 |
||
46174 | 24 |
import java.io.DataInput; |
25 |
import java.io.IOException; |
|
55496 | 26 |
import java.util.Objects; |
46174 | 27 |
|
28 |
import com.sun.org.apache.bcel.internal.Const; |
|
6 | 29 |
import com.sun.org.apache.bcel.internal.generic.Type; |
46174 | 30 |
import com.sun.org.apache.bcel.internal.util.BCELComparator; |
6 | 31 |
|
32 |
/** |
|
33 |
* This class represents the field info structure, i.e., the representation |
|
34 |
* for a variable in the class. See JVM specification for details. |
|
35 |
* |
|
55496 | 36 |
* @version $Id$ |
6 | 37 |
*/ |
38 |
public final class Field extends FieldOrMethod { |
|
46174 | 39 |
|
40 |
private static BCELComparator bcelComparator = new BCELComparator() { |
|
41 |
||
42 |
@Override |
|
43 |
public boolean equals( final Object o1, final Object o2 ) { |
|
44 |
final Field THIS = (Field) o1; |
|
45 |
final Field THAT = (Field) o2; |
|
55496 | 46 |
return Objects.equals(THIS.getName(), THAT.getName()) |
47 |
&& Objects.equals(THIS.getSignature(), THAT.getSignature()); |
|
46174 | 48 |
} |
49 |
||
50 |
||
51 |
@Override |
|
52 |
public int hashCode( final Object o ) { |
|
53 |
final Field THIS = (Field) o; |
|
54 |
return THIS.getSignature().hashCode() ^ THIS.getName().hashCode(); |
|
55 |
} |
|
56 |
}; |
|
6 | 57 |
|
46174 | 58 |
|
59 |
/** |
|
60 |
* Initialize from another object. Note that both objects use the same |
|
61 |
* references (shallow copy). Use clone() for a physical copy. |
|
62 |
*/ |
|
63 |
public Field(final Field c) { |
|
64 |
super(c); |
|
65 |
} |
|
6 | 66 |
|
46174 | 67 |
|
68 |
/** |
|
69 |
* Construct object from file stream. |
|
70 |
* @param file Input stream |
|
71 |
*/ |
|
72 |
Field(final DataInput file, final ConstantPool constant_pool) throws IOException, |
|
73 |
ClassFormatException { |
|
74 |
super(file, constant_pool); |
|
75 |
} |
|
6 | 76 |
|
77 |
||
46174 | 78 |
/** |
79 |
* @param access_flags Access rights of field |
|
80 |
* @param name_index Points to field name in constant pool |
|
81 |
* @param signature_index Points to encoded signature |
|
82 |
* @param attributes Collection of attributes |
|
83 |
* @param constant_pool Array of constants |
|
84 |
*/ |
|
85 |
public Field(final int access_flags, final int name_index, final int signature_index, final Attribute[] attributes, |
|
86 |
final ConstantPool constant_pool) { |
|
87 |
super(access_flags, name_index, signature_index, attributes, constant_pool); |
|
88 |
} |
|
6 | 89 |
|
90 |
||
46174 | 91 |
/** |
92 |
* Called by objects that are traversing the nodes of the tree implicitely |
|
93 |
* defined by the contents of a Java class. I.e., the hierarchy of methods, |
|
94 |
* fields, attributes, etc. spawns a tree of objects. |
|
95 |
* |
|
96 |
* @param v Visitor object |
|
97 |
*/ |
|
98 |
@Override |
|
99 |
public void accept( final Visitor v ) { |
|
100 |
v.visitField(this); |
|
101 |
} |
|
6 | 102 |
|
103 |
||
46174 | 104 |
/** |
105 |
* @return constant value associated with this field (may be null) |
|
106 |
*/ |
|
107 |
public final ConstantValue getConstantValue() { |
|
108 |
for (final Attribute attribute : super.getAttributes()) { |
|
109 |
if (attribute.getTag() == Const.ATTR_CONSTANT_VALUE) { |
|
110 |
return (ConstantValue) attribute; |
|
111 |
} |
|
112 |
} |
|
113 |
return null; |
|
6 | 114 |
} |
115 |
||
46174 | 116 |
|
117 |
/** |
|
118 |
* Return string representation close to declaration format, |
|
119 |
* `public static final short MAX = 100', e.g.. |
|
120 |
* |
|
121 |
* @return String representation of field, including the signature. |
|
122 |
*/ |
|
123 |
@Override |
|
124 |
public final String toString() { |
|
125 |
String name; |
|
126 |
String signature; |
|
127 |
String access; // Short cuts to constant pool |
|
128 |
||
129 |
// Get names from constant pool |
|
130 |
access = Utility.accessToString(super.getAccessFlags()); |
|
131 |
access = access.isEmpty() ? "" : (access + " "); |
|
132 |
signature = Utility.signatureToString(getSignature()); |
|
133 |
name = getName(); |
|
134 |
final StringBuilder buf = new StringBuilder(64); // CHECKSTYLE IGNORE MagicNumber |
|
135 |
buf.append(access).append(signature).append(" ").append(name); |
|
136 |
final ConstantValue cv = getConstantValue(); |
|
137 |
if (cv != null) { |
|
138 |
buf.append(" = ").append(cv); |
|
139 |
} |
|
140 |
for (final Attribute attribute : super.getAttributes()) { |
|
141 |
if (!(attribute instanceof ConstantValue)) { |
|
142 |
buf.append(" [").append(attribute).append("]"); |
|
143 |
} |
|
144 |
} |
|
145 |
return buf.toString(); |
|
146 |
} |
|
147 |
||
148 |
||
149 |
/** |
|
150 |
* @return deep copy of this field |
|
151 |
*/ |
|
152 |
public final Field copy( final ConstantPool _constant_pool ) { |
|
153 |
return (Field) copy_(_constant_pool); |
|
154 |
} |
|
155 |
||
6 | 156 |
|
46174 | 157 |
/** |
158 |
* @return type of field |
|
159 |
*/ |
|
160 |
public Type getType() { |
|
161 |
return Type.getReturnType(getSignature()); |
|
162 |
} |
|
163 |
||
164 |
||
165 |
/** |
|
166 |
* @return Comparison strategy object |
|
167 |
*/ |
|
168 |
public static BCELComparator getComparator() { |
|
169 |
return bcelComparator; |
|
170 |
} |
|
171 |
||
172 |
||
173 |
/** |
|
174 |
* @param comparator Comparison strategy object |
|
175 |
*/ |
|
176 |
public static void setComparator( final BCELComparator comparator ) { |
|
177 |
bcelComparator = comparator; |
|
178 |
} |
|
179 |
||
6 | 180 |
|
46174 | 181 |
/** |
182 |
* Return value as defined by given BCELComparator strategy. |
|
183 |
* By default two Field objects are said to be equal when |
|
184 |
* their names and signatures are equal. |
|
185 |
* |
|
186 |
* @see java.lang.Object#equals(java.lang.Object) |
|
187 |
*/ |
|
188 |
@Override |
|
189 |
public boolean equals( final Object obj ) { |
|
190 |
return bcelComparator.equals(this, obj); |
|
191 |
} |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* Return value as defined by given BCELComparator strategy. |
|
196 |
* By default return the hashcode of the field's name XOR signature. |
|
197 |
* |
|
198 |
* @see java.lang.Object#hashCode() |
|
199 |
*/ |
|
200 |
@Override |
|
201 |
public int hashCode() { |
|
202 |
return bcelComparator.hashCode(this); |
|
203 |
} |
|
6 | 204 |
} |