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.generic; |
21 |
||
46174 | 22 |
import java.util.ArrayList; |
23 |
import java.util.List; |
|
55496 | 24 |
import java.util.Objects; |
6 | 25 |
|
46174 | 26 |
import com.sun.org.apache.bcel.internal.Const; |
27 |
import com.sun.org.apache.bcel.internal.classfile.AccessFlags; |
|
28 |
import com.sun.org.apache.bcel.internal.classfile.AnnotationEntry; |
|
29 |
import com.sun.org.apache.bcel.internal.classfile.Annotations; |
|
30 |
import com.sun.org.apache.bcel.internal.classfile.Attribute; |
|
31 |
import com.sun.org.apache.bcel.internal.classfile.ConstantPool; |
|
32 |
import com.sun.org.apache.bcel.internal.classfile.Field; |
|
33 |
import com.sun.org.apache.bcel.internal.classfile.JavaClass; |
|
34 |
import com.sun.org.apache.bcel.internal.classfile.Method; |
|
35 |
import com.sun.org.apache.bcel.internal.classfile.RuntimeInvisibleAnnotations; |
|
36 |
import com.sun.org.apache.bcel.internal.classfile.RuntimeVisibleAnnotations; |
|
37 |
import com.sun.org.apache.bcel.internal.classfile.SourceFile; |
|
38 |
import com.sun.org.apache.bcel.internal.util.BCELComparator; |
|
6 | 39 |
|
40 |
/** |
|
41 |
* Template class for building up a java class. May be initialized with an |
|
42 |
* existing java class (file). |
|
43 |
* |
|
44 |
* @see JavaClass |
|
55496 | 45 |
* @version $Id$ |
46 |
* @LastModified: Jun 2019 |
|
6 | 47 |
*/ |
48 |
public class ClassGen extends AccessFlags implements Cloneable { |
|
49 |
||
46174 | 50 |
/* Corresponds to the fields found in a JavaClass object. |
51 |
*/ |
|
52 |
private String class_name; |
|
53 |
private String super_class_name; |
|
54 |
private final String file_name; |
|
55 |
private int class_name_index = -1; |
|
56 |
private int superclass_name_index = -1; |
|
57 |
private int major = Const.MAJOR; |
|
58 |
private int minor = Const.MINOR; |
|
59 |
private ConstantPoolGen cp; // Template for building up constant pool |
|
60 |
// ArrayLists instead of arrays to gather fields, methods, etc. |
|
61 |
private final List<Field> field_vec = new ArrayList<>(); |
|
62 |
private final List<Method> method_vec = new ArrayList<>(); |
|
63 |
private final List<Attribute> attribute_vec = new ArrayList<>(); |
|
64 |
private final List<String> interface_vec = new ArrayList<>(); |
|
65 |
private final List<AnnotationEntryGen> annotation_vec = new ArrayList<>(); |
|
6 | 66 |
|
46174 | 67 |
private static BCELComparator _cmp = new BCELComparator() { |
6 | 68 |
|
46174 | 69 |
@Override |
55496 | 70 |
public boolean equals( final Object o1, final Object o2 ) { |
46174 | 71 |
final ClassGen THIS = (ClassGen) o1; |
72 |
final ClassGen THAT = (ClassGen) o2; |
|
55496 | 73 |
return Objects.equals(THIS.getClassName(), THAT.getClassName()); |
46174 | 74 |
} |
6 | 75 |
|
55496 | 76 |
|
46174 | 77 |
@Override |
55496 | 78 |
public int hashCode( final Object o ) { |
46174 | 79 |
final ClassGen THIS = (ClassGen) o; |
80 |
return THIS.getClassName().hashCode(); |
|
81 |
} |
|
82 |
}; |
|
6 | 83 |
|
55496 | 84 |
|
85 |
/** Convenience constructor to set up some important values initially. |
|
46174 | 86 |
* |
87 |
* @param class_name fully qualified class name |
|
88 |
* @param super_class_name fully qualified superclass name |
|
89 |
* @param file_name source file name |
|
90 |
* @param access_flags access qualifiers |
|
91 |
* @param interfaces implemented interfaces |
|
92 |
* @param cp constant pool to use |
|
93 |
*/ |
|
94 |
public ClassGen(final String class_name, final String super_class_name, final String file_name, final int access_flags, |
|
95 |
final String[] interfaces, final ConstantPoolGen cp) { |
|
96 |
super(access_flags); |
|
97 |
this.class_name = class_name; |
|
98 |
this.super_class_name = super_class_name; |
|
99 |
this.file_name = file_name; |
|
100 |
this.cp = cp; |
|
101 |
// Put everything needed by default into the constant pool and the vectors |
|
102 |
if (file_name != null) { |
|
103 |
addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2, cp.addUtf8(file_name), cp |
|
104 |
.getConstantPool())); |
|
105 |
} |
|
106 |
class_name_index = cp.addClass(class_name); |
|
107 |
superclass_name_index = cp.addClass(super_class_name); |
|
108 |
if (interfaces != null) { |
|
109 |
for (final String interface1 : interfaces) { |
|
110 |
addInterface(interface1); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
6 | 114 |
|
55496 | 115 |
|
116 |
/** Convenience constructor to set up some important values initially. |
|
46174 | 117 |
* |
118 |
* @param class_name fully qualified class name |
|
119 |
* @param super_class_name fully qualified superclass name |
|
120 |
* @param file_name source file name |
|
121 |
* @param access_flags access qualifiers |
|
122 |
* @param interfaces implemented interfaces |
|
123 |
*/ |
|
124 |
public ClassGen(final String class_name, final String super_class_name, final String file_name, final int access_flags, |
|
125 |
final String[] interfaces) { |
|
126 |
this(class_name, super_class_name, file_name, access_flags, interfaces, |
|
127 |
new ConstantPoolGen()); |
|
128 |
} |
|
6 | 129 |
|
55496 | 130 |
|
46174 | 131 |
/** |
132 |
* Initialize with existing class. |
|
133 |
* @param clazz JavaClass object (e.g. read from file) |
|
134 |
*/ |
|
135 |
public ClassGen(final JavaClass clazz) { |
|
136 |
super(clazz.getAccessFlags()); |
|
137 |
class_name_index = clazz.getClassNameIndex(); |
|
138 |
superclass_name_index = clazz.getSuperclassNameIndex(); |
|
139 |
class_name = clazz.getClassName(); |
|
140 |
super_class_name = clazz.getSuperclassName(); |
|
141 |
file_name = clazz.getSourceFileName(); |
|
142 |
cp = new ConstantPoolGen(clazz.getConstantPool()); |
|
143 |
major = clazz.getMajor(); |
|
144 |
minor = clazz.getMinor(); |
|
145 |
final Attribute[] attributes = clazz.getAttributes(); |
|
146 |
// J5TODO: Could make unpacking lazy, done on first reference |
|
147 |
final AnnotationEntryGen[] annotations = unpackAnnotations(attributes); |
|
148 |
final Method[] methods = clazz.getMethods(); |
|
149 |
final Field[] fields = clazz.getFields(); |
|
150 |
final String[] interfaces = clazz.getInterfaceNames(); |
|
151 |
for (final String interface1 : interfaces) { |
|
152 |
addInterface(interface1); |
|
153 |
} |
|
154 |
for (final Attribute attribute : attributes) { |
|
155 |
if (!(attribute instanceof Annotations)) { |
|
156 |
addAttribute(attribute); |
|
157 |
} |
|
158 |
} |
|
159 |
for (final AnnotationEntryGen annotation : annotations) { |
|
160 |
addAnnotationEntry(annotation); |
|
161 |
} |
|
162 |
for (final Method method : methods) { |
|
163 |
addMethod(method); |
|
164 |
} |
|
165 |
for (final Field field : fields) { |
|
166 |
addField(field); |
|
167 |
} |
|
168 |
} |
|
6 | 169 |
|
46174 | 170 |
/** |
171 |
* Look for attributes representing annotations and unpack them. |
|
172 |
*/ |
|
55496 | 173 |
private AnnotationEntryGen[] unpackAnnotations(final Attribute[] attrs) |
174 |
{ |
|
46174 | 175 |
final List<AnnotationEntryGen> annotationGenObjs = new ArrayList<>(); |
176 |
for (final Attribute attr : attrs) { |
|
55496 | 177 |
if (attr instanceof RuntimeVisibleAnnotations) |
178 |
{ |
|
46174 | 179 |
final RuntimeVisibleAnnotations rva = (RuntimeVisibleAnnotations) attr; |
180 |
final AnnotationEntry[] annos = rva.getAnnotationEntries(); |
|
181 |
for (final AnnotationEntry a : annos) { |
|
182 |
annotationGenObjs.add(new AnnotationEntryGen(a, |
|
183 |
getConstantPool(), false)); |
|
184 |
} |
|
55496 | 185 |
} |
186 |
else |
|
187 |
if (attr instanceof RuntimeInvisibleAnnotations) |
|
188 |
{ |
|
189 |
final RuntimeInvisibleAnnotations ria = (RuntimeInvisibleAnnotations) attr; |
|
190 |
final AnnotationEntry[] annos = ria.getAnnotationEntries(); |
|
191 |
for (final AnnotationEntry a : annos) { |
|
192 |
annotationGenObjs.add(new AnnotationEntryGen(a, |
|
193 |
getConstantPool(), false)); |
|
194 |
} |
|
46174 | 195 |
} |
196 |
} |
|
197 |
return annotationGenObjs.toArray(new AnnotationEntryGen[annotationGenObjs.size()]); |
|
198 |
} |
|
6 | 199 |
|
55496 | 200 |
|
46174 | 201 |
/** |
202 |
* @return the (finally) built up Java class object. |
|
203 |
*/ |
|
204 |
public JavaClass getJavaClass() { |
|
205 |
final int[] interfaces = getInterfaces(); |
|
206 |
final Field[] fields = getFields(); |
|
207 |
final Method[] methods = getMethods(); |
|
55496 | 208 |
Attribute[] attributes = null; |
46174 | 209 |
if (annotation_vec.isEmpty()) { |
210 |
attributes = getAttributes(); |
|
211 |
} else { |
|
212 |
// TODO: Sometime later, trash any attributes called 'RuntimeVisibleAnnotations' or 'RuntimeInvisibleAnnotations' |
|
55496 | 213 |
final Attribute[] annAttributes = AnnotationEntryGen.getAnnotationAttributes(cp, getAnnotationEntries()); |
214 |
attributes = new Attribute[attribute_vec.size()+annAttributes.length]; |
|
46174 | 215 |
attribute_vec.toArray(attributes); |
55496 | 216 |
System.arraycopy(annAttributes,0,attributes,attribute_vec.size(),annAttributes.length); |
46174 | 217 |
} |
218 |
// Must be last since the above calls may still add something to it |
|
219 |
final ConstantPool _cp = this.cp.getFinalConstantPool(); |
|
220 |
return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor, |
|
221 |
super.getAccessFlags(), _cp, interfaces, fields, methods, attributes); |
|
222 |
} |
|
6 | 223 |
|
55496 | 224 |
|
46174 | 225 |
/** |
226 |
* Add an interface to this class, i.e., this class has to implement it. |
|
227 |
* @param name interface to implement (fully qualified class name) |
|
228 |
*/ |
|
55496 | 229 |
public void addInterface( final String name ) { |
46174 | 230 |
interface_vec.add(name); |
231 |
} |
|
6 | 232 |
|
55496 | 233 |
|
46174 | 234 |
/** |
235 |
* Remove an interface from this class. |
|
236 |
* @param name interface to remove (fully qualified name) |
|
237 |
*/ |
|
55496 | 238 |
public void removeInterface( final String name ) { |
46174 | 239 |
interface_vec.remove(name); |
240 |
} |
|
6 | 241 |
|
55496 | 242 |
|
46174 | 243 |
/** |
244 |
* @return major version number of class file |
|
245 |
*/ |
|
246 |
public int getMajor() { |
|
247 |
return major; |
|
248 |
} |
|
249 |
||
55496 | 250 |
|
251 |
/** Set major version number of class file, default value is 45 (JDK 1.1) |
|
46174 | 252 |
* @param major major version number |
253 |
*/ |
|
55496 | 254 |
public void setMajor( final int major ) { // TODO could be package-protected - only called by test code |
46174 | 255 |
this.major = major; |
256 |
} |
|
6 | 257 |
|
55496 | 258 |
|
259 |
/** Set minor version number of class file, default value is 3 (JDK 1.1) |
|
46174 | 260 |
* @param minor minor version number |
261 |
*/ |
|
55496 | 262 |
public void setMinor( final int minor ) { // TODO could be package-protected - only called by test code |
46174 | 263 |
this.minor = minor; |
264 |
} |
|
6 | 265 |
|
46174 | 266 |
/** |
267 |
* @return minor version number of class file |
|
268 |
*/ |
|
269 |
public int getMinor() { |
|
270 |
return minor; |
|
271 |
} |
|
6 | 272 |
|
55496 | 273 |
|
46174 | 274 |
/** |
275 |
* Add an attribute to this class. |
|
276 |
* @param a attribute to add |
|
277 |
*/ |
|
55496 | 278 |
public void addAttribute( final Attribute a ) { |
46174 | 279 |
attribute_vec.add(a); |
280 |
} |
|
281 |
||
55496 | 282 |
public void addAnnotationEntry(final AnnotationEntryGen a) { |
46174 | 283 |
annotation_vec.add(a); |
284 |
} |
|
6 | 285 |
|
55496 | 286 |
|
46174 | 287 |
/** |
288 |
* Add a method to this class. |
|
289 |
* @param m method to add |
|
290 |
*/ |
|
55496 | 291 |
public void addMethod( final Method m ) { |
46174 | 292 |
method_vec.add(m); |
293 |
} |
|
6 | 294 |
|
55496 | 295 |
|
46174 | 296 |
/** |
297 |
* Convenience method. |
|
298 |
* |
|
55496 | 299 |
* Add an empty constructor to this class that does nothing but calling super(). |
46174 | 300 |
* @param access_flags rights for constructor |
301 |
*/ |
|
55496 | 302 |
public void addEmptyConstructor( final int access_flags ) { |
46174 | 303 |
final InstructionList il = new InstructionList(); |
304 |
il.append(InstructionConst.THIS); // Push `this' |
|
305 |
il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V"))); |
|
306 |
il.append(InstructionConst.RETURN); |
|
307 |
final MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, "<init>", |
|
308 |
class_name, il, cp); |
|
309 |
mg.setMaxStack(1); |
|
310 |
addMethod(mg.getMethod()); |
|
6 | 311 |
} |
312 |
||
55496 | 313 |
|
46174 | 314 |
/** |
315 |
* Add a field to this class. |
|
316 |
* @param f field to add |
|
317 |
*/ |
|
55496 | 318 |
public void addField( final Field f ) { |
46174 | 319 |
field_vec.add(f); |
320 |
} |
|
321 |
||
55496 | 322 |
|
323 |
public boolean containsField( final Field f ) { |
|
46174 | 324 |
return field_vec.contains(f); |
325 |
} |
|
326 |
||
55496 | 327 |
|
328 |
/** @return field object with given name, or null |
|
46174 | 329 |
*/ |
55496 | 330 |
public Field containsField( final String name ) { |
46174 | 331 |
for (final Field f : field_vec) { |
332 |
if (f.getName().equals(name)) { |
|
333 |
return f; |
|
334 |
} |
|
335 |
} |
|
336 |
return null; |
|
337 |
} |
|
338 |
||
55496 | 339 |
|
340 |
/** @return method object with given name and signature, or null |
|
46174 | 341 |
*/ |
55496 | 342 |
public Method containsMethod( final String name, final String signature ) { |
46174 | 343 |
for (final Method m : method_vec) { |
344 |
if (m.getName().equals(name) && m.getSignature().equals(signature)) { |
|
345 |
return m; |
|
346 |
} |
|
347 |
} |
|
348 |
return null; |
|
349 |
} |
|
350 |
||
55496 | 351 |
|
46174 | 352 |
/** |
353 |
* Remove an attribute from this class. |
|
354 |
* @param a attribute to remove |
|
355 |
*/ |
|
55496 | 356 |
public void removeAttribute( final Attribute a ) { |
46174 | 357 |
attribute_vec.remove(a); |
358 |
} |
|
359 |
||
55496 | 360 |
|
46174 | 361 |
/** |
362 |
* Remove a method from this class. |
|
363 |
* @param m method to remove |
|
364 |
*/ |
|
55496 | 365 |
public void removeMethod( final Method m ) { |
46174 | 366 |
method_vec.remove(m); |
367 |
} |
|
6 | 368 |
|
55496 | 369 |
|
370 |
/** Replace given method with new one. If the old one does not exist |
|
371 |
* add the new_ method to the class anyway. |
|
46174 | 372 |
*/ |
55496 | 373 |
public void replaceMethod( final Method old, final Method new_ ) { |
46174 | 374 |
if (new_ == null) { |
375 |
throw new ClassGenException("Replacement method must not be null"); |
|
376 |
} |
|
377 |
final int i = method_vec.indexOf(old); |
|
378 |
if (i < 0) { |
|
379 |
method_vec.add(new_); |
|
380 |
} else { |
|
381 |
method_vec.set(i, new_); |
|
382 |
} |
|
383 |
} |
|
384 |
||
55496 | 385 |
|
386 |
/** Replace given field with new one. If the old one does not exist |
|
387 |
* add the new_ field to the class anyway. |
|
46174 | 388 |
*/ |
55496 | 389 |
public void replaceField( final Field old, final Field new_ ) { |
46174 | 390 |
if (new_ == null) { |
391 |
throw new ClassGenException("Replacement method must not be null"); |
|
392 |
} |
|
393 |
final int i = field_vec.indexOf(old); |
|
394 |
if (i < 0) { |
|
395 |
field_vec.add(new_); |
|
396 |
} else { |
|
397 |
field_vec.set(i, new_); |
|
398 |
} |
|
399 |
} |
|
400 |
||
55496 | 401 |
|
46174 | 402 |
/** |
403 |
* Remove a field to this class. |
|
404 |
* @param f field to remove |
|
405 |
*/ |
|
55496 | 406 |
public void removeField( final Field f ) { |
46174 | 407 |
field_vec.remove(f); |
408 |
} |
|
409 |
||
55496 | 410 |
|
46174 | 411 |
public String getClassName() { |
412 |
return class_name; |
|
413 |
} |
|
414 |
||
55496 | 415 |
|
46174 | 416 |
public String getSuperclassName() { |
417 |
return super_class_name; |
|
418 |
} |
|
419 |
||
55496 | 420 |
|
46174 | 421 |
public String getFileName() { |
422 |
return file_name; |
|
423 |
} |
|
424 |
||
55496 | 425 |
|
426 |
public void setClassName( final String name ) { |
|
46174 | 427 |
class_name = name.replace('/', '.'); |
428 |
class_name_index = cp.addClass(name); |
|
429 |
} |
|
430 |
||
55496 | 431 |
|
432 |
public void setSuperclassName( final String name ) { |
|
46174 | 433 |
super_class_name = name.replace('/', '.'); |
434 |
superclass_name_index = cp.addClass(name); |
|
435 |
} |
|
436 |
||
55496 | 437 |
|
46174 | 438 |
public Method[] getMethods() { |
439 |
return method_vec.toArray(new Method[method_vec.size()]); |
|
440 |
} |
|
441 |
||
55496 | 442 |
|
443 |
public void setMethods( final Method[] methods ) { |
|
46174 | 444 |
method_vec.clear(); |
445 |
for (final Method method : methods) { |
|
446 |
addMethod(method); |
|
447 |
} |
|
448 |
} |
|
449 |
||
55496 | 450 |
|
451 |
public void setMethodAt( final Method method, final int pos ) { |
|
46174 | 452 |
method_vec.set(pos, method); |
453 |
} |
|
454 |
||
55496 | 455 |
|
456 |
public Method getMethodAt( final int pos ) { |
|
46174 | 457 |
return method_vec.get(pos); |
6 | 458 |
} |
459 |
||
55496 | 460 |
|
46174 | 461 |
public String[] getInterfaceNames() { |
462 |
final int size = interface_vec.size(); |
|
463 |
final String[] interfaces = new String[size]; |
|
464 |
interface_vec.toArray(interfaces); |
|
465 |
return interfaces; |
|
466 |
} |
|
6 | 467 |
|
55496 | 468 |
|
46174 | 469 |
public int[] getInterfaces() { |
470 |
final int size = interface_vec.size(); |
|
471 |
final int[] interfaces = new int[size]; |
|
472 |
for (int i = 0; i < size; i++) { |
|
473 |
interfaces[i] = cp.addClass(interface_vec.get(i)); |
|
474 |
} |
|
475 |
return interfaces; |
|
476 |
} |
|
6 | 477 |
|
55496 | 478 |
|
46174 | 479 |
public Field[] getFields() { |
480 |
return field_vec.toArray(new Field[field_vec.size()]); |
|
481 |
} |
|
6 | 482 |
|
55496 | 483 |
|
46174 | 484 |
public Attribute[] getAttributes() { |
485 |
return attribute_vec.toArray(new Attribute[attribute_vec.size()]); |
|
486 |
} |
|
6 | 487 |
|
46174 | 488 |
// J5TODO: Should we make calling unpackAnnotations() lazy and put it in here? |
489 |
public AnnotationEntryGen[] getAnnotationEntries() { |
|
490 |
return annotation_vec.toArray(new AnnotationEntryGen[annotation_vec.size()]); |
|
491 |
} |
|
6 | 492 |
|
55496 | 493 |
|
46174 | 494 |
public ConstantPoolGen getConstantPool() { |
495 |
return cp; |
|
496 |
} |
|
6 | 497 |
|
55496 | 498 |
|
499 |
public void setConstantPool( final ConstantPoolGen constant_pool ) { |
|
46174 | 500 |
cp = constant_pool; |
501 |
} |
|
6 | 502 |
|
55496 | 503 |
|
504 |
public void setClassNameIndex( final int class_name_index ) { |
|
46174 | 505 |
this.class_name_index = class_name_index; |
506 |
class_name = cp.getConstantPool().getConstantString(class_name_index, |
|
507 |
Const.CONSTANT_Class).replace('/', '.'); |
|
508 |
} |
|
6 | 509 |
|
55496 | 510 |
|
511 |
public void setSuperclassNameIndex( final int superclass_name_index ) { |
|
46174 | 512 |
this.superclass_name_index = superclass_name_index; |
513 |
super_class_name = cp.getConstantPool().getConstantString(superclass_name_index, |
|
514 |
Const.CONSTANT_Class).replace('/', '.'); |
|
515 |
} |
|
6 | 516 |
|
55496 | 517 |
|
46174 | 518 |
public int getSuperclassNameIndex() { |
519 |
return superclass_name_index; |
|
520 |
} |
|
6 | 521 |
|
55496 | 522 |
|
46174 | 523 |
public int getClassNameIndex() { |
524 |
return class_name_index; |
|
525 |
} |
|
6 | 526 |
|
46174 | 527 |
private List<ClassObserver> observers; |
6 | 528 |
|
55496 | 529 |
|
530 |
/** Add observer for this object. |
|
46174 | 531 |
*/ |
55496 | 532 |
public void addObserver( final ClassObserver o ) { |
46174 | 533 |
if (observers == null) { |
534 |
observers = new ArrayList<>(); |
|
535 |
} |
|
536 |
observers.add(o); |
|
537 |
} |
|
6 | 538 |
|
55496 | 539 |
|
540 |
/** Remove observer for this object. |
|
46174 | 541 |
*/ |
55496 | 542 |
public void removeObserver( final ClassObserver o ) { |
46174 | 543 |
if (observers != null) { |
544 |
observers.remove(o); |
|
545 |
} |
|
546 |
} |
|
6 | 547 |
|
55496 | 548 |
|
549 |
/** Call notify() method on all observers. This method is not called |
|
550 |
* automatically whenever the state has changed, but has to be |
|
551 |
* called by the user after he has finished editing the object. |
|
46174 | 552 |
*/ |
553 |
public void update() { |
|
554 |
if (observers != null) { |
|
555 |
for (final ClassObserver observer : observers) { |
|
556 |
observer.notify(this); |
|
557 |
} |
|
558 |
} |
|
559 |
} |
|
6 | 560 |
|
55496 | 561 |
|
46174 | 562 |
@Override |
563 |
public Object clone() { |
|
564 |
try { |
|
565 |
return super.clone(); |
|
566 |
} catch (final CloneNotSupportedException e) { |
|
567 |
throw new Error("Clone Not Supported"); // never happens |
|
568 |
} |
|
569 |
} |
|
6 | 570 |
|
55496 | 571 |
|
46174 | 572 |
/** |
573 |
* @return Comparison strategy object |
|
574 |
*/ |
|
575 |
public static BCELComparator getComparator() { |
|
576 |
return _cmp; |
|
577 |
} |
|
6 | 578 |
|
55496 | 579 |
|
46174 | 580 |
/** |
581 |
* @param comparator Comparison strategy object |
|
582 |
*/ |
|
55496 | 583 |
public static void setComparator( final BCELComparator comparator ) { |
46174 | 584 |
_cmp = comparator; |
585 |
} |
|
6 | 586 |
|
55496 | 587 |
|
46174 | 588 |
/** |
55496 | 589 |
* Return value as defined by given BCELComparator strategy. |
590 |
* By default two ClassGen objects are said to be equal when |
|
591 |
* their class names are equal. |
|
46174 | 592 |
* |
593 |
* @see java.lang.Object#equals(java.lang.Object) |
|
594 |
*/ |
|
595 |
@Override |
|
55496 | 596 |
public boolean equals( final Object obj ) { |
46174 | 597 |
return _cmp.equals(this, obj); |
598 |
} |
|
6 | 599 |
|
55496 | 600 |
|
46174 | 601 |
/** |
55496 | 602 |
* Return value as defined by given BCELComparator strategy. |
603 |
* By default return the hashcode of the class name. |
|
46174 | 604 |
* |
605 |
* @see java.lang.Object#hashCode() |
|
606 |
*/ |
|
607 |
@Override |
|
608 |
public int hashCode() { |
|
609 |
return _cmp.hashCode(this); |
|
6 | 610 |
} |
611 |
} |