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
|
package org.aspectj.apache.bcel.classfile;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
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.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
import org.aspectj.apache.bcel.Constants;
import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations;
import org.aspectj.apache.bcel.generic.Type;
import org.aspectj.apache.bcel.util.SyntheticRepository;
/**
* Represents a Java class, i.e., the data structures, constant pool, fields, methods and commands contained in a Java .class file.
* See <a href="ftp://java.sun.com/docs/specs/">JVM specification</a> for details.
*
* The intent of this class is to represent a parsed or otherwise existing class file. Those interested in programatically
* generating classes should see the <a href="../generic/ClassGen.html">ClassGen</a> class.
*
* @version $Id: JavaClass.java,v 1.17 2009/09/09 22:18:20 aclement Exp $
* @see org.aspectj.apache.bcel.generic.ClassGen
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
*/
public class JavaClass extends Modifiers implements Cloneable, Node {
private static final String[] NO_INTERFACE_NAMES = new String[0];
private String fileName;
private String packageName;
private String sourcefileName;
private int classnameIdx;
private int superclassnameIdx;
private String classname;
private String superclassname;
private int major, minor;
private ConstantPool cpool;
private int[] interfaces;
private String[] interfacenames;
private Field[] fields;
private Method[] methods;
private Attribute[] attributes;
private AnnotationGen[] annotations;
private boolean isGeneric = false;
private boolean isAnonymous = false;
private boolean isNested = false;
private boolean computedNestedTypeStatus = false;
// Annotations are collected from certain attributes, don't do it more than necessary!
private boolean annotationsOutOfDate = true;
// state for dealing with generic signature string
private String signatureAttributeString = null;
private Signature signatureAttribute = null;
private boolean searchedForSignatureAttribute = false;
/**
* In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.
*/
private transient org.aspectj.apache.bcel.util.Repository repository = null;
/**
* Constructor gets all contents as arguments.
*
* @param class_name_index Index into constant pool referencing a ConstantClass that represents this class.
* @param superclass_name_index Index into constant pool referencing a ConstantClass that represents this class's superclass.
* @param file_name File name
* @param major Major compiler version
* @param minor Minor compiler version
* @param access_flags Access rights defined by bit flags
* @param constant_pool Array of constants
* @param interfaces Implemented interfaces
* @param fields Class fields
* @param methods Class methods
* @param attributes Class attributes
* @param source Read from file or generated in memory?
*/
public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags,
ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) {
if (interfaces == null) {
interfaces = new int[0];
}
if (attributes == null) {
this.attributes = Attribute.NoAttributes;
}
if (fields == null) {
fields = new Field[0]; // TODO create a constant for no fields
}
if (methods == null) {
methods = new Method[0]; // TODO create a constant for no methods
}
this.classnameIdx = class_name_index;
this.superclassnameIdx = superclass_name_index;
this.fileName = file_name;
this.major = major;
this.minor = minor;
this.modifiers = access_flags;
this.cpool = constant_pool;
this.interfaces = interfaces;
this.fields = fields;
this.methods = methods;
this.attributes = attributes;
annotationsOutOfDate = true;
// Get source file name if available
SourceFile sfAttribute = AttributeUtils.getSourceFileAttribute(attributes);
sourcefileName = sfAttribute == null ? "<Unknown>" : sfAttribute.getSourceFileName();
/*
* According to the specification the following entries must be of type `ConstantClass' but we check that anyway via the
* `ConstPool.getConstant' method.
*/
classname = constant_pool.getConstantString(class_name_index, Constants.CONSTANT_Class);
classname = Utility.compactClassName(classname, false);
int index = classname.lastIndexOf('.');
if (index < 0) {
packageName = "";
} else {
packageName = classname.substring(0, index);
}
if (superclass_name_index > 0) { // May be zero -> class is java.lang.Object
superclassname = constant_pool.getConstantString(superclass_name_index, Constants.CONSTANT_Class);
superclassname = Utility.compactClassName(superclassname, false);
} else {
superclassname = "java.lang.Object";
}
if (interfaces.length == 0) {
interfacenames = NO_INTERFACE_NAMES;
} else {
interfacenames = new String[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
String str = constant_pool.getConstantString(interfaces[i], Constants.CONSTANT_Class);
interfacenames[i] = Utility.compactClassName(str, false);
}
}
}
/**
* Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
* hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
*
* @param v Visitor object
*/
public void accept(ClassVisitor v) {
v.visitJavaClass(this);
}
/**
* Dump class to a file.
*
* @param file Output file
* @throws IOException
*/
public void dump(File file) throws IOException {
String parent = file.getParent();
if (parent != null) {
File dir = new File(parent);
dir.mkdirs();
}
dump(new DataOutputStream(new FileOutputStream(file)));
}
/**
* Dump class to a file named file_name.
*
* @param file_name Output file name
* @exception IOException
*/
public void dump(String file_name) throws IOException {
dump(new File(file_name));
}
/**
* @return class in binary format
*/
public byte[] getBytes() {
ByteArrayOutputStream s = new ByteArrayOutputStream();
DataOutputStream ds = new DataOutputStream(s);
try {
dump(ds);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return s.toByteArray();
}
/**
* Dump Java class to output stream in binary format.
*
* @param file Output stream
* @exception IOException
*/
public void dump(OutputStream file) throws IOException {
dump(new DataOutputStream(file));
}
/**
* Dump Java class to output stream in binary format.
*
* @param file Output stream
* @exception IOException
*/
public void dump(DataOutputStream file) throws IOException {
file.writeInt(0xcafebabe);
file.writeShort(minor);
file.writeShort(major);
cpool.dump(file);
file.writeShort(modifiers);
file.writeShort(classnameIdx);
file.writeShort(superclassnameIdx);
file.writeShort(interfaces.length);
for (int i = 0; i < interfaces.length; i++) {
file.writeShort(interfaces[i]);
}
file.writeShort(fields.length);
for (int i = 0; i < fields.length; i++) {
fields[i].dump(file);
}
file.writeShort(methods.length);
for (int i = 0; i < methods.length; i++) {
methods[i].dump(file);
}
AttributeUtils.writeAttributes(attributes, file);
file.close();
}
/**
* @return Attributes of the class.
*/
public Attribute[] getAttributes() {
return attributes;
}
public AnnotationGen[] getAnnotations() {
if (annotationsOutOfDate) {
// Find attributes that contain annotation data
List<AnnotationGen> accumulatedAnnotations = new ArrayList<AnnotationGen>();
for (int i = 0; i < attributes.length; i++) {
Attribute attribute = attributes[i];
if (attribute instanceof RuntimeAnnotations) {
RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) attribute;
accumulatedAnnotations.addAll(runtimeAnnotations.getAnnotations());
}
}
annotations = accumulatedAnnotations.toArray(new AnnotationGen[] {});
annotationsOutOfDate = false;
}
return annotations;
}
/**
* @return Class name.
*/
public String getClassName() {
return classname;
}
/**
* @return Package name.
*/
public String getPackageName() {
return packageName;
}
/**
* @return Class name index.
*/
public int getClassNameIndex() {
return classnameIdx;
}
/**
* @return Constant pool.
*/
public ConstantPool getConstantPool() {
return cpool;
}
/**
* @return Fields, i.e., variables of the class. Like the JVM spec mandates for the classfile format, these fields are those
* specific to this class, and not those of the superclass or superinterfaces.
*/
public Field[] getFields() {
return fields;
}
/**
* @return File name of class, aka SourceFile attribute value
*/
public String getFileName() {
return fileName;
}
/**
* @return Names of implemented interfaces.
*/
public String[] getInterfaceNames() {
return interfacenames;
}
/**
* @return Indices in constant pool of implemented interfaces.
*/
public int[] getInterfaceIndices() {
return interfaces;
}
/**
* @return Major number of class file version.
*/
public int getMajor() {
return major;
}
/**
* @return Methods of the class.
*/
public Method[] getMethods() {
return methods;
}
/**
* @return A org.aspectj.apache.bcel.classfile.Method corresponding to java.lang.reflect.Method if any
*/
public Method getMethod(java.lang.reflect.Method m) {
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (m.getName().equals(method.getName()) && m.getModifiers() == method.getModifiers()
&& Type.getSignature(m).equals(method.getSignature())) {
return method;
}
}
return null;
}
public Method getMethod(java.lang.reflect.Constructor c) {
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().equals("<init>") && c.getModifiers() == method.getModifiers()
&& Type.getSignature(c).equals(method.getSignature())) {
return method;
}
}
return null;
}
public Field getField(java.lang.reflect.Field field) {
for (int i = 0; i < fields.length; i++) {
if (fields[i].getName().equals(field.getName())) {
return fields[i];
}
}
return null;
}
/**
* @return Minor number of class file version.
*/
public int getMinor() {
return minor;
}
/**
* @return sbsolute path to file where this class was read from
*/
public String getSourceFileName() {
return sourcefileName;
}
/**
* @return Superclass name.
*/
public String getSuperclassName() {
return superclassname;
}
/**
* @return Class name index.
*/
public int getSuperclassNameIndex() {
return superclassnameIdx;
}
/**
* @param attributes .
*/
public void setAttributes(Attribute[] attributes) {
this.attributes = attributes;
annotationsOutOfDate = true;
}
/**
* @param class_name .
*/
public void setClassName(String class_name) {
this.classname = class_name;
}
/**
* @param class_name_index .
*/
public void setClassNameIndex(int class_name_index) {
this.classnameIdx = class_name_index;
}
/**
* @param constant_pool .
*/
public void setConstantPool(ConstantPool constant_pool) {
this.cpool = constant_pool;
}
/**
* @param fields .
*/
public void setFields(Field[] fields) {
this.fields = fields;
}
/**
* Set File name of class, aka SourceFile attribute value
*/
public void setFileName(String file_name) {
this.fileName = file_name;
}
/**
* @param interface_names .
*/
public void setInterfaceNames(String[] interface_names) {
this.interfacenames = interface_names;
}
/**
* @param interfaces .
*/
public void setInterfaces(int[] interfaces) {
this.interfaces = interfaces;
}
/**
* @param major .
*/
public void setMajor(int major) {
this.major = major;
}
/**
* @param methods .
*/
public void setMethods(Method[] methods) {
this.methods = methods;
}
/**
* @param minor .
*/
public void setMinor(int minor) {
this.minor = minor;
}
/**
* Set absolute path to file this class was read from.
*/
public void setSourceFileName(String source_file_name) {
this.sourcefileName = source_file_name;
}
/**
* @param superclass_name .
*/
public void setSuperclassName(String superclass_name) {
this.superclassname = superclass_name;
}
/**
* @param superclass_name_index .
*/
public void setSuperclassNameIndex(int superclass_name_index) {
this.superclassnameIdx = superclass_name_index;
}
/**
* @return String representing class contents.
*/
@Override
public String toString() {
String access = Utility.accessToString(modifiers, true);
access = access.equals("") ? "" : access + " ";
StringBuffer buf = new StringBuffer(access + Utility.classOrInterface(modifiers) + " " + classname + " extends "
+ Utility.compactClassName(superclassname, false) + '\n');
int size = interfaces.length;
if (size > 0) {
buf.append("implements\t\t");
for (int i = 0; i < size; i++) {
buf.append(interfacenames[i]);
if (i < size - 1) {
buf.append(", ");
}
}
buf.append('\n');
}
buf.append("filename\t\t" + fileName + '\n');
buf.append("compiled from\t\t" + sourcefileName + '\n');
buf.append("compiler version\t" + major + "." + minor + '\n');
buf.append("access flags\t\t" + modifiers + '\n');
buf.append("constant pool\t\t" + cpool.getLength() + " entries\n");
buf.append("ACC_SUPER flag\t\t" + isSuper() + "\n");
if (attributes.length > 0) {
buf.append("\nAttribute(s):\n");
for (int i = 0; i < attributes.length; i++) {
buf.append(indent(attributes[i]));
}
}
if (annotations != null && annotations.length > 0) {
buf.append("\nAnnotation(s):\n");
for (int i = 0; i < annotations.length; i++) {
buf.append(indent(annotations[i]));
}
}
if (fields.length > 0) {
buf.append("\n" + fields.length + " fields:\n");
for (int i = 0; i < fields.length; i++) {
buf.append("\t" + fields[i] + '\n');
}
}
if (methods.length > 0) {
buf.append("\n" + methods.length + " methods:\n");
for (int i = 0; i < methods.length; i++) {
buf.append("\t" + methods[i] + '\n');
}
}
return buf.toString();
}
private static final String indent(Object obj) {
StringTokenizer tok = new StringTokenizer(obj.toString(), "\n");
StringBuffer buf = new StringBuffer();
while (tok.hasMoreTokens()) {
buf.append("\t" + tok.nextToken() + "\n");
}
return buf.toString();
}
/**
* @return deep copy of this class
*/
public JavaClass copy() {
JavaClass c = null;
try {
c = (JavaClass) clone();
} catch (CloneNotSupportedException e) {
}
c.cpool = cpool.copy();
c.interfaces = interfaces.clone();
c.interfacenames = interfacenames.clone();
c.fields = new Field[fields.length];
for (int i = 0; i < fields.length; i++) {
c.fields[i] = fields[i].copy(c.cpool);
}
c.methods = new Method[methods.length];
for (int i = 0; i < methods.length; i++) {
c.methods[i] = methods[i].copy(c.cpool);
}
c.attributes = AttributeUtils.copy(attributes, c.cpool);
// J5SUPPORT: As the annotations exist as attributes against the class, copying
// the attributes will copy the annotations across, so we don't have to
// also copy them individually.
return c;
}
public final boolean isSuper() {
return (modifiers & Constants.ACC_SUPER) != 0;
}
public final boolean isClass() {
return (modifiers & Constants.ACC_INTERFACE) == 0;
}
public final boolean isAnonymous() {
computeNestedTypeStatus();
return this.isAnonymous;
}
public final boolean isNested() {
computeNestedTypeStatus();
return this.isNested;
}
private final void computeNestedTypeStatus() {
if (computedNestedTypeStatus) {
return;
}
// Attribute[] attrs = attributes.getAttributes();
for (int i = 0; i < attributes.length; i++) {
if (attributes[i] instanceof InnerClasses) {
InnerClass[] innerClasses = ((InnerClasses) attributes[i]).getInnerClasses();
for (int j = 0; j < innerClasses.length; j++) {
boolean innerClassAttributeRefersToMe = false;
String inner_class_name = cpool.getConstantString(innerClasses[j].getInnerClassIndex(),
Constants.CONSTANT_Class);
inner_class_name = Utility.compactClassName(inner_class_name);
if (inner_class_name.equals(getClassName())) {
innerClassAttributeRefersToMe = true;
}
if (innerClassAttributeRefersToMe) {
this.isNested = true;
if (innerClasses[j].getInnerNameIndex() == 0) {
this.isAnonymous = true;
}
}
}
}
}
this.computedNestedTypeStatus = true;
}
// J5SUPPORT:
/**
* Returns true if this class represents an annotation, i.e. it was a 'public @interface blahblah' declaration
*/
public final boolean isAnnotation() {
return (modifiers & Constants.ACC_ANNOTATION) != 0;
}
/**
* Returns true if this class represents an enum type
*/
public final boolean isEnum() {
return (modifiers & Constants.ACC_ENUM) != 0;
}
/********************* New repository functionality *********************/
/**
* Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance();
*/
public org.aspectj.apache.bcel.util.Repository getRepository() {
if (repository == null) {
repository = SyntheticRepository.getInstance();
}
return repository;
}
/**
* Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.
*/
public void setRepository(org.aspectj.apache.bcel.util.Repository repository) {
this.repository = repository;
}
/**
* Equivalent to runtime "instanceof" operator.
*
* @return true if this JavaClass is derived from teh super class
*/
public final boolean instanceOf(JavaClass super_class) {
if (this.equals(super_class)) {
return true;
}
JavaClass[] super_classes = getSuperClasses();
for (int i = 0; i < super_classes.length; i++) {
if (super_classes[i].equals(super_class)) {
return true;
}
}
if (super_class.isInterface()) {
return implementationOf(super_class);
}
return false;
}
/**
* @return true, if clazz is an implementation of interface inter
*/
public boolean implementationOf(JavaClass inter) {
if (!inter.isInterface()) {
throw new IllegalArgumentException(inter.getClassName() + " is no interface");
}
if (this.equals(inter)) {
return true;
}
Collection<JavaClass> superInterfaces = getAllInterfaces();
for (JavaClass superInterface : superInterfaces) {
if (superInterface.equals(inter)) {
return true;
}
}
// for (int i = 0; i < super_interfaces.length; i++) {
// if (super_interfaces[i].equals(inter)) {
// return true;
// }
// }
return false;
}
/**
* @return the superclass for this JavaClass object, or null if this is java.lang.Object
*/
public JavaClass getSuperClass() {
if ("java.lang.Object".equals(getClassName())) {
return null;
}
try {
return getRepository().loadClass(getSuperclassName());
} catch (ClassNotFoundException e) {
System.err.println(e);
return null;
}
}
/**
* @return list of super classes of this class in ascending order, i.e., java.lang.Object is always the last element
*/
public JavaClass[] getSuperClasses() {
JavaClass clazz = this;
List<JavaClass> vec = new ArrayList<JavaClass>();
for (clazz = clazz.getSuperClass(); clazz != null; clazz = clazz.getSuperClass()) {
vec.add(clazz);
}
return vec.toArray(new JavaClass[vec.size()]);
}
/**
* Get interfaces directly implemented by this JavaClass.
*/
public JavaClass[] getInterfaces() {
String[] interfaces = getInterfaceNames();
JavaClass[] classes = new JavaClass[interfaces.length];
try {
for (int i = 0; i < interfaces.length; i++) {
classes[i] = getRepository().loadClass(interfaces[i]);
}
} catch (ClassNotFoundException e) {
System.err.println(e);
return null;
}
return classes;
}
/**
* Get all interfaces implemented by this JavaClass (transitively).
*/
public Collection<JavaClass> getAllInterfaces() {
Queue<JavaClass> queue = new LinkedList<JavaClass>();
List<JavaClass> interfaceList = new ArrayList<JavaClass>();
queue.add(this);
while (!queue.isEmpty()) {
JavaClass clazz = queue.remove();
JavaClass souper = clazz.getSuperClass();
JavaClass[] interfaces = clazz.getInterfaces();
if (clazz.isInterface()) {
interfaceList.add(clazz);
} else {
if (souper != null) {
queue.add(souper);
}
}
for (int i = 0; i < interfaces.length; i++) {
queue.add(interfaces[i]);
}
}
return interfaceList;
// return interfaceList.toArray(new JavaClass[interfaceList.size()]);
}
/**
* Hunts for a signature attribute on the member and returns its contents. So where the 'regular' signature may be
* Ljava/util/Vector; the signature attribute will tell us e.g. "<E:>Ljava/lang/Object". We can learn the type variable names,
* their bounds, and the true superclass and superinterface types (including any parameterizations) Coded for performance -
* searches for the attribute only when requested - only searches for it once.
*/
public final String getGenericSignature() {
loadGenericSignatureInfoIfNecessary();
return signatureAttributeString;
}
public boolean isGeneric() {
loadGenericSignatureInfoIfNecessary();
return isGeneric;
}
private void loadGenericSignatureInfoIfNecessary() {
if (!searchedForSignatureAttribute) {
signatureAttribute = AttributeUtils.getSignatureAttribute(attributes);
signatureAttributeString = signatureAttribute == null ? null : signatureAttribute.getSignature();
isGeneric = signatureAttribute != null && signatureAttributeString.charAt(0) == '<';
searchedForSignatureAttribute = true;
}
}
public final Signature getSignatureAttribute() {
loadGenericSignatureInfoIfNecessary();
return signatureAttribute;
}
}
|