/
CtClass.java
1688 lines (1545 loc) · 55.2 KB
/
CtClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package javassist;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Collection;
import javassist.bytecode.ClassFile;
import javassist.bytecode.Descriptor;
import javassist.bytecode.Opcode;
import javassist.expr.ExprEditor;
/* Note:
*
* This class is an abstract class and several methods just return null
* or throw an exception. Those methods are overridden in subclasses
* of this class. Read the source code of CtClassType if you are
* interested in the implementation of Javassist.
*
* Subclasses of CtClass are CtClassType, CtPrimitiveType, and CtArray.
*/
/**
* An instance of <code>CtClass</code> represents a class.
* It is obtained from <code>ClassPool</code>.
*
* @see ClassPool#get(String)
*/
public abstract class CtClass {
protected String qualifiedName;
/**
* If the value of this field is not null, then all class
* files modified by Javassist are saved under the directory
* specified by this variable. For example, if the value is
* <code>"./debug"</code>, then all class files are saved
* there. The directory name must not end with a directory
* separator such as <code>/</code>.
*
* <p>The default value is null.
*
* @see #debugWriteFile(String)
* @since 3.16
*/
public static String debugDump = null;
/**
* The version number of this release.
*/
public static final String version = "3.29.0-SNAPSHOT";
/**
* Prints the version number and the copyright notice.
*
* <p>The following command invokes this method:
*
* <pre>java -jar javassist.jar</pre>
*/
public static void main(String[] args) {
System.out.println("Javassist version " + CtClass.version);
System.out.println("Copyright (C) 1999-2021 Shigeru Chiba."
+ " All Rights Reserved.");
}
static final String javaLangObject = "java.lang.Object";
/**
* The <code>CtClass</code> object representing
* the <code>boolean</code> type.
*/
public static CtClass booleanType;
/**
* The <code>CtClass</code> object representing
* the <code>char</code> type.
*/
public static CtClass charType;
/**
* The <code>CtClass</code> object representing
* the <code>byte</code> type.
*/
public static CtClass byteType;
/**
* The <code>CtClass</code> object representing
* the <code>short</code> type.
*/
public static CtClass shortType;
/**
* The <code>CtClass</code> object representing
* the <code>int</code> type.
*/
public static CtClass intType;
/**
* The <code>CtClass</code> object representing
* the <code>long</code> type.
*/
public static CtClass longType;
/**
* The <code>CtClass</code> object representing
* the <code>float</code> type.
*/
public static CtClass floatType;
/**
* The <code>CtClass</code> object representing
* the <code>double</code> type.
*/
public static CtClass doubleType;
/**
* The <code>CtClass</code> object representing
* the <code>void</code> type.
*/
public static CtClass voidType;
static CtClass[] primitiveTypes;
static {
primitiveTypes = new CtClass[9];
booleanType =
new CtPrimitiveType("boolean", 'Z', "java.lang.Boolean",
"booleanValue", "()Z", Opcode.IRETURN,
Opcode.T_BOOLEAN, 1);
primitiveTypes[0] = booleanType;
charType = new CtPrimitiveType("char", 'C', "java.lang.Character",
"charValue", "()C", Opcode.IRETURN,
Opcode.T_CHAR, 1);
primitiveTypes[1] = charType;
byteType = new CtPrimitiveType("byte", 'B', "java.lang.Byte",
"byteValue", "()B", Opcode.IRETURN,
Opcode.T_BYTE, 1);
primitiveTypes[2] = byteType;
shortType = new CtPrimitiveType("short", 'S', "java.lang.Short",
"shortValue", "()S", Opcode.IRETURN,
Opcode.T_SHORT, 1);
primitiveTypes[3] = shortType;
intType = new CtPrimitiveType("int", 'I', "java.lang.Integer",
"intValue", "()I", Opcode.IRETURN,
Opcode.T_INT, 1);
primitiveTypes[4] = intType;
longType = new CtPrimitiveType("long", 'J', "java.lang.Long",
"longValue", "()J", Opcode.LRETURN,
Opcode.T_LONG, 2);
primitiveTypes[5] = longType;
floatType = new CtPrimitiveType("float", 'F', "java.lang.Float",
"floatValue", "()F", Opcode.FRETURN,
Opcode.T_FLOAT, 1);
primitiveTypes[6] = floatType;
doubleType = new CtPrimitiveType("double", 'D', "java.lang.Double",
"doubleValue", "()D", Opcode.DRETURN,
Opcode.T_DOUBLE, 2);
primitiveTypes[7] = doubleType;
voidType = new CtPrimitiveType("void", 'V', "java.lang.Void",
null, null, Opcode.RETURN, 0, 0);
primitiveTypes[8] = voidType;
}
protected CtClass(String name) {
qualifiedName = name;
}
/**
* Converts the object to a string.
*/
@Override
public String toString() {
StringBuffer buf = new StringBuffer(getClass().getName());
buf.append("@");
buf.append(Integer.toHexString(hashCode()));
buf.append("[");
extendToString(buf);
buf.append("]");
return buf.toString();
}
/**
* Implemented in subclasses to add to the {@link #toString()} result.
* Subclasses should put a space before each token added to the buffer.
*/
protected void extendToString(StringBuffer buffer) {
buffer.append(getName());
}
/**
* Returns a <code>ClassPool</code> for this class.
*/
public ClassPool getClassPool() { return null; }
/**
* Returns a class file for this class.
*
* <p>This method is not available if <code>isFrozen()</code>
* is true.
*/
public ClassFile getClassFile() {
checkModify();
return getClassFile2();
}
/**
* Returns a class file for this class (read only).
* Normal applications do not need calling this method. Use
* <code>getClassFile()</code>.
*
* <p>The <code>ClassFile</code> object obtained by this method
* is read only. Changes to this object might not be reflected
* on a class file generated by <code>toBytecode()</code>,
* <code>toClass()</code>, etc.
*
* <p>This method is available even if <code>isFrozen()</code>
* is true. However, if the class is frozen, it might be also
* pruned.
*
* @see CtClass#getClassFile()
* @see CtClass#isFrozen()
* @see CtClass#prune()
*/
public ClassFile getClassFile2() { return null; }
/**
* Undocumented method. Do not use; internal-use only.
*/
public javassist.compiler.AccessorMaker getAccessorMaker() {
return null;
}
/**
* Returns the uniform resource locator (URL) of the class file.
*/
public URL getURL() throws NotFoundException {
throw new NotFoundException(getName());
}
/**
* Returns true if the definition of the class has been modified.
*/
public boolean isModified() { return false; }
/**
* Returns true if the class has been loaded or written out
* and thus it cannot be modified any more.
*
* @see #defrost()
* @see #detach()
*/
public boolean isFrozen() { return true; }
/**
* Makes the class frozen.
*
* @see #isFrozen()
* @see #defrost()
* @since 3.6
*/
public void freeze() {}
/* Note: this method is overridden by CtClassType
*/
void checkModify() throws RuntimeException {
if (isFrozen())
throw new RuntimeException(getName() + " class is frozen");
// isModified() must return true after this method is invoked.
}
/**
* Defrosts the class so that the class can be modified again.
*
* <p>To avoid changes that will be never reflected,
* the class is frozen to be unmodifiable if it is loaded or
* written out. This method should be called only in a case
* that the class will be reloaded or written out later again.
*
* <p>If <code>defrost()</code> will be called later, pruning
* must be disallowed in advance.
*
* @see #isFrozen()
* @see #stopPruning(boolean)
* @see #detach()
*/
public void defrost() {
throw new RuntimeException("cannot defrost " + getName());
}
/**
* Returns <code>true</code> if this object represents a primitive
* Java type: boolean, byte, char, short, int, long, float, double,
* or void.
*/
public boolean isPrimitive() { return false; }
/**
* Returns <code>true</code> if this object represents an array type.
*/
public boolean isArray() {
return false;
}
/**
* Returns <code>true</code> if this object represents a Kotlin class.
* @since 3.26
*/
public boolean isKotlin() {
return hasAnnotation("kotlin.Metadata");
}
/**
* If this object represents an array, this method returns the component
* type of the array. Otherwise, it returns <code>null</code>.
*/
public CtClass getComponentType() throws NotFoundException {
return null;
}
/**
* Returns <code>true</code> if this class extends or implements
* <code>clazz</code>. It also returns <code>true</code> if
* this class is the same as <code>clazz</code>.
*/
public boolean subtypeOf(CtClass clazz) throws NotFoundException {
return this == clazz || getName().equals(clazz.getName());
}
/**
* Obtains the fully-qualified name of the class.
*/
public String getName() { return qualifiedName; }
/**
* Obtains the not-qualified class name.
*/
public final String getSimpleName() {
String qname = qualifiedName;
int index = qname.lastIndexOf('.');
if (index < 0)
return qname;
return qname.substring(index + 1);
}
/**
* Obtains the package name. It may be <code>null</code>.
*/
public final String getPackageName() {
String qname = qualifiedName;
int index = qname.lastIndexOf('.');
if (index < 0)
return null;
return qname.substring(0, index);
}
/**
* Sets the class name
*
* @param name fully-qualified name
*/
public void setName(String name) {
checkModify();
if (name != null)
qualifiedName = name;
}
/**
* Returns the generic signature of the class.
*
* <p>The generics of Java is implemented by the erasure technique.
* After compilation, all type parameters are dropped off from the
* main part of a class file. However, for reflection, the type
* parameters are encoded into generic signatures and attached
* to a class file.
*
* @return null if the generic signature is not included.
* @see javassist.bytecode.SignatureAttribute#toClassSignature(String)
* @see CtMember#getGenericSignature()
* @since 3.17
*/
public String getGenericSignature() { return null; }
/**
* Sets the generic signature of the class.
*
* <p>The generics of Java is implemented by the erasure technique.
* After compilation, all type parameters are dropped off from the
* main part of a class file. However, for reflection, the type
* parameters must be encoded into generic signatures and attached
* to a class file.
*
* <p>For example,
*
* <pre>class List<T> {
* T value;
* T get() { return value; }
* void set(T v) { value = v; }
* }
* </pre>
*
* <p>this class is generated by the following code:
*
* <pre>
* ClassPool pool = ClassPool.getDefault();
* CtClass cc = pool.makeClass("List");
* CtClass objectClass = pool.get(CtClass.javaLangObject);
* ClassSignature cs = new ClassSignature(
* new TypeParameter[] { new TypeParameter("T") });
* cc.setGenericSignature(cs.encode()); // <T:Ljava/lang/Object;>Ljava/lang/Object;
*
* CtField f = new CtField(objClass, "value", cc);
* TypeVariable tvar = new TypeVariable("T");
* f.setGenericSignature(tvar.encode()); // TT;
* cc.addField(f);
*
* CtMethod m = CtNewMethod.make("public Object get(){return value;}", cc);
* MethodSignature ms = new MethodSignature(null, null, tvar, null);
* m.setGenericSignature(ms.encode()); // ()TT;
* cc.addMethod(m);
*
* CtMethod m2 = CtNewMethod.make("public void set(Object v){value = v;}", cc);
* MethodSignature ms2 = new MethodSignature(null, new Type[] { tvar },
* new BaseType("void"), null);
* m2.setGenericSignature(ms2.encode()); // (TT;)V;
* cc.addMethod(m2);
*
* cc.writeFile();
* </pre>
*
* <p>The generated class file is equivalent to the following:
*
* <pre>class List {
* Object value;
* Object get() { return value; }
* void set(Object v) { value = v; }
* }</pre>
*
* <p>but it includes generic signatures for the class, the field,
* and the methods so that the type variable <code>T</code> can be
* accessible through reflection.
*
* <p><code>MethodSignature</code> is a utility class. You can directly
* pass the signature string to the <code>setGenericSignature</code> method.
* For the specification of the signatures, see Section 4.7.9.1 <i>Signatures</i>
* of The Java Virtual Machine Specification (Java SE 8).
*
* @param sig a generic signature.
* @see javassist.bytecode.SignatureAttribute.ClassSignature#encode()
* @see javassist.bytecode.SignatureAttribute.MethodSignature#encode()
* @see CtMember#setGenericSignature(String)
* @since 3.17
*/
public void setGenericSignature(String sig) { checkModify(); }
/**
* Substitutes <code>newName</code> for all occurrences of a class
* name <code>oldName</code> in the class file.
*
* @param oldName replaced class name
* @param newName substituted class name
*/
public void replaceClassName(String oldName, String newName) {
checkModify();
}
/**
* Changes class names appearing in the class file according to the
* given <code>map</code>.
*
* <p>All the class names appearing in the class file are tested
* with <code>map</code> to determine whether each class name is
* replaced or not. Thus this method can be used for collecting
* all the class names in the class file. To do that, first define
* a subclass of <code>ClassMap</code> so that <code>get()</code>
* records all the given parameters. Then, make an instance of
* that subclass as an empty hash-table. Finally, pass that instance
* to this method. After this method finishes, that instance would
* contain all the class names appearing in the class file.
*
* @param map the hashtable associating replaced class names
* with substituted names.
*/
public void replaceClassName(ClassMap map) {
checkModify();
}
/**
* Returns a collection of the names of all the classes
* referenced in this class.
* That collection includes the name of this class.
*
* <p>This method may return <code>null</code>.
*
* @return a <code>Collection<String></code> object.
*/
public synchronized Collection<String> getRefClasses() {
ClassFile cf = getClassFile2();
if (cf != null) {
ClassMap cm = new ClassMap() {
/** default serialVersionUID */
private static final long serialVersionUID = 1L;
@Override
public String put(String oldname, String newname) {
return put0(oldname, newname);
}
@Override
public String get(Object jvmClassName) {
String n = toJavaName((String)jvmClassName);
put0(n, n);
return null;
}
@Override
public void fix(String name) {}
};
cf.getRefClasses(cm);
return cm.values();
}
return null;
}
/**
* Determines whether this object represents a class or an interface.
* It returns <code>true</code> if this object represents an interface.
*/
public boolean isInterface() {
return false;
}
/**
* Determines whether this object represents an annotation type.
* It returns <code>true</code> if this object represents an annotation type.
*
* @since 3.2
*/
public boolean isAnnotation() {
return false;
}
/**
* Determines whether this object represents an enum.
* It returns <code>true</code> if this object represents an enum.
*
* @since 3.2
*/
public boolean isEnum() {
return false;
}
/**
* Returns the modifiers for this class, encoded in an integer.
* For decoding, use <code>javassist.Modifier</code>.
*
* <p>If the class is a static nested class (a.k.a. static inner class),
* the returned modifiers include <code>Modifier.STATIC</code>.
*
* @see Modifier
*/
public int getModifiers() {
return 0;
}
/**
* Returns true if the class has the specified annotation type.
*
* @param annotationType the annotation type.
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.11
*/
public boolean hasAnnotation(Class<?> annotationType) {
return hasAnnotation(annotationType.getName());
}
/**
* Returns true if the class has the specified annotation type.
*
* @param annotationTypeName the name of annotation type.
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.21
*/
public boolean hasAnnotation(String annotationTypeName) {
return false;
}
/**
* Returns the annotation if the class has the specified annotation type.
* For example, if an annotation <code>@Author</code> is associated
* with this class, an <code>Author</code> object is returned.
* The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @param clz the annotation type.
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
public Object getAnnotation(Class<?> clz) throws ClassNotFoundException {
return null;
}
/**
* Returns the annotations associated with this class.
* For example, if an annotation <code>@Author</code> is associated
* with this class, the returned array contains an <code>Author</code>
* object. The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @return an array of annotation-type objects.
* @see CtMember#getAnnotations()
* @since 3.1
*/
public Object[] getAnnotations() throws ClassNotFoundException {
return new Object[0];
}
/**
* Returns the annotations associated with this class.
* This method is equivalent to <code>getAnnotations()</code>
* except that, if any annotations are not on the classpath,
* they are not included in the returned array.
*
* @return an array of annotation-type objects.
* @see #getAnnotations()
* @see CtMember#getAvailableAnnotations()
* @since 3.3
*/
public Object[] getAvailableAnnotations(){
return new Object[0];
}
/**
* Returns an array of nested classes declared in the class.
* Nested classes are inner classes, anonymous classes, local classes,
* and static nested classes. This simply calls <code>getNestedClasses()</code>.
*
* @see #getNestedClasses()
* @since 3.15
*/
public CtClass[] getDeclaredClasses() throws NotFoundException {
return getNestedClasses();
}
/**
* Returns an array of nested classes declared in the class.
* Nested classes are inner classes, anonymous classes, local classes,
* and static nested classes.
*
* @since 3.2
*/
public CtClass[] getNestedClasses() throws NotFoundException {
return new CtClass[0];
}
/**
* Sets the modifiers.
*
* <p>If the class is a nested class, this method also modifies
* the class declaring that nested class (i.e. the enclosing
* class is modified).
*
* @param mod modifiers encoded by
* <code>javassist.Modifier</code>
* @see Modifier
*/
public void setModifiers(int mod) {
checkModify();
}
/**
* Determines whether the class directly or indirectly extends
* the given class. If this class extends a class A and
* the class A extends a class B, then subclassof(B) returns true.
*
* <p>This method returns true if the given class is identical to
* the class represented by this object.
*/
public boolean subclassOf(CtClass superclass) {
return false;
}
/**
* Obtains the class object representing the superclass of the
* class.
* It returns null if this object represents the
* <code>java.lang.Object</code> class and thus it does not have
* the super class.
*
* <p>If this object represents an interface, this method
* always returns the <code>java.lang.Object</code> class.
* To obtain the super interfaces
* extended by that interface, call <code>getInterfaces()</code>.
*/
public CtClass getSuperclass() throws NotFoundException {
return null;
}
/**
* Changes a super class unless this object represents an interface.
* The new super class must be compatible with the old one; for example,
* it should inherit from the old super class.
*
* <p>If this object represents an interface, this method is equivalent
* to <code>addInterface()</code>; it appends <code>clazz</code> to
* the list of the super interfaces extended by that interface.
* Note that an interface can extend multiple super interfaces.
*
* @see #replaceClassName(String, String)
* @see #replaceClassName(ClassMap)
*/
public void setSuperclass(CtClass clazz) throws CannotCompileException {
checkModify();
}
/**
* Obtains the class objects representing the interfaces implemented
* by the class or, if this object represents an interface, the interfaces
* extended by that interface.
*/
public CtClass[] getInterfaces() throws NotFoundException {
return new CtClass[0];
}
/**
* Sets implemented interfaces. If this object represents an interface,
* this method sets the interfaces extended by that interface.
*
* @param list a list of the <code>CtClass</code> objects
* representing interfaces, or
* <code>null</code> if the class implements
* no interfaces.
*/
public void setInterfaces(CtClass[] list) {
checkModify();
}
/**
* Adds an interface.
*
* @param anInterface the added interface.
*/
public void addInterface(CtClass anInterface) {
checkModify();
}
/**
* If this class is a member class or interface of another class,
* then the class enclosing this class is returned.
*
* @return null if this class is a top-level class or an anonymous class.
*/
public CtClass getDeclaringClass() throws NotFoundException {
return null;
}
/**
* Returns the immediately enclosing method of this class.
* This method works only with JDK 1.5 or later.
*
* @return null if this class is not a local class or an anonymous
* class.
* @deprecated The enclosing method might be a constructor.
* Use {@link #getEnclosingBehavior()}.
* @see #getEnclosingBehavior()
*/
@Deprecated
public final CtMethod getEnclosingMethod() throws NotFoundException {
CtBehavior b = getEnclosingBehavior();
if (b == null)
return null;
else if (b instanceof CtMethod)
return (CtMethod)b;
else
throw new NotFoundException(b.getLongName() + " is enclosing " + getName());
}
/**
* Returns the immediately enclosing method of this class.
* It might be not a method but a constructor.
* This method works only with JDK 1.5 or later.
*
* @return null if this class is not a local class or an anonymous
* class.
*/
public CtBehavior getEnclosingBehavior() throws NotFoundException {
return null;
}
/**
* Makes a new public nested class. If this method is called,
* the <code>CtClass</code>, which encloses the nested class, is modified
* since a class file includes a list of nested classes.
*
* <p>The current implementation only supports a static nested class.
* <code>isStatic</code> must be true.
*
* @param name the simple name of the nested class.
* @param isStatic true if the nested class is static.
*/
public CtClass makeNestedClass(String name, boolean isStatic) {
throw new RuntimeException(getName() + " is not a class");
}
/**
* Returns an array containing <code>CtField</code> objects
* representing all the non-private fields of the class.
* That array includes non-private fields inherited from the
* superclasses.
*/
public CtField[] getFields() { return new CtField[0]; }
/**
* Returns the field with the specified name. The returned field
* may be a private field declared in a super class or interface.
*/
public CtField getField(String name) throws NotFoundException {
return getField(name, null);
}
/**
* Returns the field with the specified name and type. The returned field
* may be a private field declared in a super class or interface.
* Unlike Java, the JVM allows a class to have
* multiple fields with the same name but different types.
*
* @param name the field name.
* @param desc the type descriptor of the field. It is available by
* {@link CtField#getSignature()}.
* @see CtField#getSignature()
*/
public CtField getField(String name, String desc) throws NotFoundException {
throw new NotFoundException(name);
}
/**
* @return null if the specified field is not found.
*/
CtField getField2(String name, String desc) { return null; }
/**
* Gets all the fields declared in the class. The inherited fields
* are not included.
*
* <p>Note: the result does not include inherited fields.
*/
public CtField[] getDeclaredFields() { return new CtField[0]; }
/**
* Retrieves the field with the specified name among the fields
* declared in the class.
*
* <p>Note: this method does not search the super classes.
*/
public CtField getDeclaredField(String name) throws NotFoundException {
throw new NotFoundException(name);
}
/**
* Retrieves the field with the specified name and type among the fields
* declared in the class. Unlike Java, the JVM allows a class to have
* multiple fields with the same name but different types.
*
* <p>Note: this method does not search the super classes.
*
* @param name the field name.
* @param desc the type descriptor of the field. It is available by
* {@link CtField#getSignature()}.
* @see CtField#getSignature()
*/
public CtField getDeclaredField(String name, String desc) throws NotFoundException {
throw new NotFoundException(name);
}
/**
* Gets all the constructors and methods declared in the class.
*/
public CtBehavior[] getDeclaredBehaviors() {
return new CtBehavior[0];
}
/**
* Returns an array containing <code>CtConstructor</code> objects
* representing all the non-private constructors of the class.
*/
public CtConstructor[] getConstructors() {
return new CtConstructor[0];
}
/**
* Returns the constructor with the given signature,
* which is represented by a character string
* called method descriptor.
* For details of the method descriptor, see the JVM specification
* or <code>javassist.bytecode.Descriptor</code>.
*
* @param desc method descriptor
* @see javassist.bytecode.Descriptor
*/
public CtConstructor getConstructor(String desc)
throws NotFoundException
{
throw new NotFoundException("no such constructor");
}
/**
* Gets all the constructors declared in the class.
*
* @see javassist.CtConstructor
*/
public CtConstructor[] getDeclaredConstructors() {
return new CtConstructor[0];
}
/**
* Returns a constructor receiving the specified parameters.
*
* @param params parameter types.
*/
public CtConstructor getDeclaredConstructor(CtClass[] params)
throws NotFoundException
{
String desc = Descriptor.ofConstructor(params);
return getConstructor(desc);
}
/**
* Gets the class initializer (static constructor)
* declared in the class.
* This method returns <code>null</code> if
* no class initializer is not declared.
*
* @see #makeClassInitializer()
* @see javassist.CtConstructor
*/
public CtConstructor getClassInitializer() {
return null;
}
/**
* Returns an array containing <code>CtMethod</code> objects
* representing all the non-private methods of the class.
* That array includes non-private methods inherited from the
* superclasses.
*/
public CtMethod[] getMethods() {
return new CtMethod[0];
}
/**
* Returns the method with the given name and signature.
* The returned method may be declared in a super class.
* The method signature is represented by a character string
* called method descriptor,
* which is defined in the JVM specification.
*
* @param name method name
* @param desc method descriptor
* @see CtBehavior#getSignature()
* @see javassist.bytecode.Descriptor
*/
public CtMethod getMethod(String name, String desc)
throws NotFoundException
{
throw new NotFoundException(name);
}
/**
* Gets all methods declared in the class. The inherited methods
* are not included.
*
* @see javassist.CtMethod