forked from Konloch/bytecode-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava.java
More file actions
4655 lines (3665 loc) · 165 KB
/
Copy pathJava.java
File metadata and controls
4655 lines (3665 loc) · 165 KB
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
/*
* Janino - An embedded Java[TM] compiler
*
* Copyright (c) 2001-2010, Arno Unkrig
* 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 name of the author may not be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 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 AUTHOR 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.
*/
package org.codehaus.janino;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.Location;
import org.codehaus.janino.CodeContext.Offset;
import org.codehaus.janino.IClass.IMethod;
import org.codehaus.janino.Java.FunctionDeclarator.FormalParameter;
import org.codehaus.janino.Java.FunctionDeclarator.FormalParameters;
import org.codehaus.janino.Visitor.BlockStatementVisitor;
import org.codehaus.janino.Visitor.ElementValueVisitor;
import org.codehaus.janino.Visitor.TypeArgumentVisitor;
import org.codehaus.janino.util.Traverser;
import org.codehaus.janino.util.iterator.ReverseListIterator;
/**
* This wrapper class defines classes that represent the elements of the
* Java™ programming language.
* <p>
* Notice:
* <p>
* 'JLS7' refers to the <a href="http://docs.oracle.com/javase/specs/">Java Language Specification, Java SE 7
* Edition</a>.
*/
@SuppressWarnings({ "rawtypes", "unchecked" }) public final
class Java {
private Java() {} // Don't instantiate me.
/** Representation of a Java™ 'scope', e.g. a compilation unit, type, method or block. */
public
interface Scope {
/** @return The scope that encloses this scope, or {@code null} */
Scope getEnclosingScope();
}
/** This interface is implemented by objects which are associated with a location in the source code. */
public
interface Locatable {
/** @return The location of this object */
Location getLocation();
/**
* Throw a {@link CompileException} with the given message and this
* object's location.
*
* @param message The message to report
*/
void throwCompileException(String message) throws CompileException;
}
/** Abstract implementation of {@link Locatable}. */
public abstract static
class Located implements Locatable {
/** Indication of 'no' or 'unknown' location. */
public static final Located NOWHERE = new Located(Location.NOWHERE) {};
private final Location location;
protected
Located(Location location) {
//assert location != null;
this.location = location;
}
// Implement "Locatable".
@Override public Location
getLocation() { return this.location; }
@Override public void
throwCompileException(String message) throws CompileException {
throw new CompileException(message, this.location);
}
}
/** Holds the result of {@link Parser#parseCompilationUnit}. */
public static final
class CompilationUnit implements Scope {
/** A string that explains the 'file' (or similar resource) where this CU was loaded from. */
public final String optionalFileName;
/** The package declaration at the very top of this CU (if any). */
public PackageDeclaration optionalPackageDeclaration;
/** The IMPORT declarations in this CU. */
public final List<ImportDeclaration> importDeclarations = new ArrayList();
/** The top-level declarations in this CU. */
public final List<PackageMemberTypeDeclaration> packageMemberTypeDeclarations = new ArrayList();
public
CompilationUnit(String optionalFileName) { this.optionalFileName = optionalFileName; }
// Implement "Scope".
@Override public Scope
getEnclosingScope() { throw new JaninoRuntimeException("A compilation unit has no enclosing scope"); }
/** Sets the package declaration of this CU. */
public void
setPackageDeclaration(PackageDeclaration packageDeclaration) {
this.optionalPackageDeclaration = packageDeclaration;
}
/** Adds one IMPORT declaration to this CU. */
public void
addImportDeclaration(CompilationUnit.ImportDeclaration id) {
// Conflicting imports are checked in UnitCompiler, not here.
this.importDeclarations.add(id);
}
/** Adds one top-level type declaration to this CU. */
public void
addPackageMemberTypeDeclaration(PackageMemberTypeDeclaration pmtd) {
this.packageMemberTypeDeclarations.add(pmtd);
pmtd.setDeclaringCompilationUnit(this);
}
/** Gets all classes and interfaces declared in this compilation unit. */
public PackageMemberTypeDeclaration[]
getPackageMemberTypeDeclarations() {
return (PackageMemberTypeDeclaration[]) this.packageMemberTypeDeclarations.toArray(
new PackageMemberTypeDeclaration[this.packageMemberTypeDeclarations.size()]
);
}
/**
* Return the package member class or interface declared with the given name.
* @param name Declared (i.e. not the fully qualified) name
* @return <code>null</code> if a package member type with that name is not declared in this compilation unit
*/
public PackageMemberTypeDeclaration
getPackageMemberTypeDeclaration(String name) {
for (PackageMemberTypeDeclaration pmtd : this.packageMemberTypeDeclarations) {
if (pmtd.getName().equals(name)) return pmtd;
}
return null;
}
/** Represents a 'single-type import declaration' like '{@code import java.util.Map;}'. */
public static
class SingleTypeImportDeclaration extends ImportDeclaration {
/** The identifiers that constitute the type to be imported, e.g. 'java', 'util', 'Map'. */
public final String[] identifiers;
public
SingleTypeImportDeclaration(Location location, String[] identifiers) {
super(location);
this.identifiers = identifiers;
}
@Override public final void
accept(Visitor.ImportVisitor visitor) { visitor.visitSingleTypeImportDeclaration(this); }
@Override public String
toString() { return "import " + Java.join(this.identifiers, ".") + ';'; }
}
/** Represents a type-import-on-demand declaration like {@code import java.util.*;}. */
public static
class TypeImportOnDemandDeclaration extends ImportDeclaration {
/** The identifiers that constitute the package or type to import from, e.g. 'java', 'util'. */
public final String[] identifiers;
public
TypeImportOnDemandDeclaration(Location location, String[] identifiers) {
super(location);
this.identifiers = identifiers;
}
@Override public final void
accept(Visitor.ImportVisitor visitor) {
visitor.visitTypeImportOnDemandDeclaration(this);
}
@Override public String
toString() { return "import " + Java.join(this.identifiers, ".") + ".*;"; }
}
/**
* Represents a single static import declaration like<pre>
* import java.util.Collections.EMPTY_MAP;</pre>
*/
public static
class SingleStaticImportDeclaration extends ImportDeclaration {
/**
* The identifiers that constitute the member to be imported, e.g. 'java', 'util', 'Collections',
* 'EMPTY_MAP'.
*/
public final String[] identifiers;
public
SingleStaticImportDeclaration(Location location, String[] identifiers) {
super(location);
this.identifiers = identifiers;
}
@Override public final void
accept(Visitor.ImportVisitor visitor) {
visitor.visitSingleStaticImportDeclaration(this);
}
}
/**
* Represents a static-import-on-demand declaration like<pre>
* import java.util.Collections.*;</pre>
*/
public static
class StaticImportOnDemandDeclaration extends ImportDeclaration {
/** The identifiers that constitute the type to import from, e.g. 'java', 'util', 'Collections'. */
public final String[] identifiers;
public
StaticImportOnDemandDeclaration(Location location, String[] identifiers) {
super(location);
this.identifiers = identifiers;
}
@Override public final void
accept(Visitor.ImportVisitor visitor) {
visitor.visitStaticImportOnDemandDeclaration(this);
}
}
/** Base class for the various IMPORT declarations. */
public abstract static
class ImportDeclaration extends Java.Located {
public
ImportDeclaration(Location location) { super(location); }
/**
* Invokes the '{@code visit...()}' method of {@link Visitor.ImportVisitor} for the concrete {@link
* ImportDeclaration} type.
*/
public abstract void accept(Visitor.ImportVisitor visitor);
}
}
/** Representation of a Java ™ annotation. */
public
interface Annotation extends ElementValue {
/**
* Invokes the '{@code visit...()}' method of {@link Visitor.AnnotationVisitor} for the concrete {@link
* Annotation} type.
*/
void accept(Visitor.AnnotationVisitor visitor);
/** Sets the enclosing scope for this annotation. */
void setEnclosingScope(Scope enclosingScope);
/** @return The type of this annotation */
Type getType();
}
/** Repreentation of a 'marker annotation', i.e. an annotation without any elements in parentheses. */
public static final
class MarkerAnnotation implements Annotation {
/** The type of this marker annotation. */
public final Type type;
public
MarkerAnnotation(Type type) { this.type = type; }
@Override public void
setEnclosingScope(Scope enclosingScope) { this.type.setEnclosingScope(enclosingScope); }
@Override public String toString() { return "@" + this.type; }
@Override public Type
getType() { return this.type; }
@Override public void
accept(Visitor.AnnotationVisitor visitor) { visitor.visitMarkerAnnotation(this); }
@Override public void
accept(Visitor.ElementValueVisitor visitor) { visitor.visitMarkerAnnotation(this); }
}
/**
* Representation of a 'single-element annotation', i.e. an annotation followed by a single element in parentheses.
*/
public static final
class SingleElementAnnotation implements Annotation {
/** The type of this single-element annotation. */
public final Type type;
/** The element value associated with this single-element annotation. */
public final ElementValue elementValue;
public
SingleElementAnnotation(Type type, ElementValue elementValue) {
this.type = type;
this.elementValue = elementValue;
}
@Override public void
setEnclosingScope(Scope enclosingScope) { this.type.setEnclosingScope(enclosingScope); }
@Override public String toString() { return "@" + this.type + '(' + this.elementValue + ')'; }
@Override public Type
getType() { return this.type; }
@Override public void
accept(Visitor.AnnotationVisitor visitor) { visitor.visitSingleElementAnnotation(this); }
@Override public void
accept(Visitor.ElementValueVisitor visitor) { visitor.visitSingleElementAnnotation(this); }
}
/** A 'normal annotation', i.e. an annotation with multiple elements in parentheses and curly braces. */
public static final
class NormalAnnotation implements Annotation {
/** The type of this normal annotation. */
public final Type type;
/** The element-value-pairs associated with this annotation. */
public final ElementValuePair[] elementValuePairs;
public
NormalAnnotation(Type type, ElementValuePair[] elementValuePairs) {
this.type = type;
this.elementValuePairs = elementValuePairs;
}
@Override public Type
getType() { return this.type; }
@Override public String
toString() {
switch (this.elementValuePairs.length) {
case 0: return "@" + this.type + "()";
case 1: return "@" + this.type + "(" + this.elementValuePairs[0] + ")";
default: return "@" + this.type + "(" + this.elementValuePairs[0] + ", ...)";
}
}
@Override public void
setEnclosingScope(Scope enclosingScope) { this.setEnclosingScope(enclosingScope); }
@Override public void
accept(Visitor.AnnotationVisitor visitor) { visitor.visitNormalAnnotation(this); }
@Override public void
accept(Visitor.ElementValueVisitor visitor) { visitor.visitNormalAnnotation(this); }
}
/** Representation of the modifier flags and annotations that are associated with a declaration. */
public static
class Modifiers {
/** The or'ed constants declared in {@link Mod}. */
public final short flags;
/** The annotations. */
public final Annotation[] annotations;
/** A 'blank' {@link Modifiers} object: No flags, no annotations. */
public
Modifiers() {
this.flags = Mod.NONE;
this.annotations = new Annotation[0];
}
public
Modifiers(short modifiers) {
this.flags = modifiers;
this.annotations = new Annotation[0];
}
public
Modifiers(short modifiers, Annotation[] annotations) {
this.flags = modifiers;
this.annotations = annotations;
}
/** @return This object, with the given {@code modifiersToAdd} added. */
public Modifiers
add(int modifiersToAdd) {
return new Modifiers((short) (this.flags | modifiersToAdd), this.annotations);
}
/** @return This object, with the given {@code modifiersToRemove} removed. */
public Modifiers
remove(int modifiersToRemove) {
return new Modifiers((short) (this.flags & ~modifiersToRemove), this.annotations);
}
/**
* @param newAccess One of {@link Mod#PUBLIC}, {@link Mod#PRIVATE}, {@link Mod#PROTECTED}, {@link Mod#PACKAGE}
* @return This object, with the access changed to {@code newAccess}
*/
public Modifiers
changeAccess(int newAccess) {
return new Modifiers((short) (this.flags & ~Mod.PPP | newAccess), this.annotations);
}
}
/** Representation of a 'name = value' element in a {@link NormalAnnotation}. */
public static
class ElementValuePair {
/** The element name. */
public final String identifier;
/** The element value. */
public final ElementValue elementValue;
public
ElementValuePair(String identifier, ElementValue elementValue) {
this.identifier = identifier;
this.elementValue = elementValue;
}
@Override public String
toString() { return this.identifier + " = " + this.elementValue; }
}
/** Base of the possible element values in a {@link NormalAnnotation}. */
public
interface ElementValue {
/**
* Invokes the '{@code visit...()}' method of {@link Visitor.ElementValueVisitor} for the concrete {@link
* ElementValue} type.
*/
void accept(Visitor.ElementValueVisitor visitor);
}
/**
* An element value in the form of an array initializer, e.g. '<code>SuppressWarnings({ "null", "unchecked"
* })</code>'.
*/
public static
class ElementValueArrayInitializer implements ElementValue {
/** The element values in the body of the array initializer. */
public final ElementValue[] elementValues;
public
ElementValueArrayInitializer(ElementValue[] elementValues) {
this.elementValues = elementValues;
}
@Override public String
toString() {
switch (this.elementValues.length) {
case 0: return "{}";
case 1: return "{ " + this.elementValues[0] + " }";
default: return "{ " + this.elementValues[0] + ", ... }";
}
}
@Override public void
accept(Visitor.ElementValueVisitor visitor) { visitor.visitElementValueArrayInitializer(this); }
}
/** Representation of a package declaration like {@code package com.acme.tools;}. */
public static
class PackageDeclaration extends Located {
/** The package name, e.g. '{@code com.acme.tools}'. */
public final String packageName;
public
PackageDeclaration(Location location, String packageName) {
super(location);
this.packageName = packageName;
}
}
/** Base for the various kinds of type declarations, e.g. top-level class, member interface, local class. */
public
interface TypeDeclaration extends Locatable, Scope {
/** @return The or'ed modifier flags of the type, as defined in {@link Mod} */
short getModifierFlags();
/** @return The annotations of this {@link TypeDeclaration} */
Annotation[] getAnnotations();
/**
* Return the member type with the given name.
* @return <code>null</code> if a member type with that name is not declared
*/
MemberTypeDeclaration getMemberTypeDeclaration(String name);
/** @return The (possibly empty) set of member types declared inside this {@link TypeDeclaration} */
Collection<MemberTypeDeclaration> getMemberTypeDeclarations();
/**
* Return the first method declared with the given name. (Does not honor inherited methods.)
*
* @return <code>null</code> if a method with this name is not declared
*/
MethodDeclarator getMethodDeclaration(String name);
/**
* @return The list of methods declared in this {@link TypeDeclaration}, not including methods declared in
* supertypes
*/
List<MethodDeclarator> getMethodDeclarations();
/** Determines the effective class name, e.g. "pkg.Outer$Inner". */
String getClassName();
/** Creates a unique name for a local class or interface. */
String createLocalTypeName(String localTypeName);
/** Creates a unique name for an anonymous class. */
String createAnonymousClassName();
/**
* Invokes the '{@code visit...()}' method of {@link Visitor.TypeDeclarationVisitor} for the concrete {@link
* TypeDeclaration} type.
*/
void accept(Visitor.TypeDeclarationVisitor visitor);
}
/**
* Representation of a Java™ element that can be annotated with a DOC comment ('<code>/** ...
* */</code>').
*/
public
interface DocCommentable {
/** @return The doc comment of the object or {@code null} */
String getDocComment();
/**
* Returns <code>true</code> if the object has a doc comment and
* the <code>@deprecated</code> tag appears in the doc
* comment.
*/
boolean hasDeprecatedDocTag();
}
/**
* Represents a class or interface declaration on compilation unit level. These are called "package member types"
* because they are immediate members of a package.
*/
public
interface PackageMemberTypeDeclaration extends NamedTypeDeclaration {
/** Sets the {@link CompilationUnit} in which this top-level type is declared. */
void setDeclaringCompilationUnit(CompilationUnit declaringCompilationUnit);
/** @return The {@link CompilationUnit} in which this top-level type is declared. */
CompilationUnit getDeclaringCompilationUnit();
}
/**
* Represents a class or interface declaration where the immediately enclosing scope is
* another class or interface declaration.
*/
public interface MemberTypeDeclaration extends NamedTypeDeclaration, TypeBodyDeclaration {}
/**
* Represents the declaration of a class or an interface that has a name. (All type
* declarations are named, except for anonymous classes.)
*/
public
interface NamedTypeDeclaration extends TypeDeclaration {
/** @return The declared (not the fully qualified) name of the class or interface */
String getName();
/** @return The declared type parameters */
TypeParameter[] getOptionalTypeParameters();
}
/**
* Represents the declaration of an inner class, i.e. a class that exists in the context of
* zero or more "enclosing instances". These are anonymous classes, local classes and member
* classes.
*/
interface InnerClassDeclaration extends TypeDeclaration {
/**
* Inner classes have zero or more synthetic fields that hold references to their enclosing
* context:
* <dl>
* <dt><code>this$<i>n</i></code></dt>
* <dd>
* (Mandatory for non-private non-static member classes; optional for private non-static
* member classes, local classes in non-static context, and anonymous classes in
* non-static context; forbidden for static member classes, local classes in static
* context, and anonymous classes in static context)
* Holds a reference to the immediately enclosing instance. <code><i>n</i></code> is
* N-1 for the Nth nesting level; e.g. the public non-static member class of a
* package member class has a synthetic field <code>this$0</code>.
* </dd>
* <dt><code>val$<i>local-variable-name</i></code></dt>
* <dd>
* (Allowed for local classes and anonymous classes; forbidden for member classes)
* Hold copies of <code>final</code> local variables of the defining context.
* </dd>
* </dl>
* Notice that these fields are not included in the {@link IClass.IField} array returned
* by {@link IClass#getDeclaredIFields2()}.
* <p>
* If a synthetic field with the same name exists already, then it must have the same
* type and the redefinition is ignored.
* @param iField
*/
void defineSyntheticField(IClass.IField iField) throws CompileException;
}
/** Abstract implementation of {@link TypeDeclaration}. */
public abstract static
class AbstractTypeDeclaration implements TypeDeclaration {
private final Location location;
private final Modifiers modifiers;
private final List<MethodDeclarator> declaredMethods = new ArrayList();
private final List<MemberTypeDeclaration> declaredClassesAndInterfaces = new ArrayList();
private Scope enclosingScope;
/** Holds the resolved type during compilation. */
IClass resolvedType;
public
AbstractTypeDeclaration(Location location, Modifiers modifiers) {
this.location = location;
this.modifiers = modifiers;
}
@Override public short
getModifierFlags() { return this.modifiers.flags; }
@Override public Annotation[]
getAnnotations() { return this.modifiers.annotations; }
/** Sets the enclosing scope of this {@link TypeDeclaration}. */
public void
setEnclosingScope(Scope enclosingScope) {
if (this.enclosingScope != null && enclosingScope != this.enclosingScope) {
throw new JaninoRuntimeException(
"Enclosing scope is already set for type declaration \""
+ this.toString()
+ "\" at "
+ this.getLocation()
);
}
this.enclosingScope = enclosingScope;
}
@Override public Scope
getEnclosingScope() { return this.enclosingScope; }
/**
* Invalidates the method cache of the {@link #resolvedType}. This is necessary when methods are added
* <i>during</i> compilation
*/
public void
invalidateMethodCaches() {
if (this.resolvedType != null) {
this.resolvedType.invalidateMethodCaches();
}
}
/** Adds one {@link MemberTypeDeclaration} to this type. */
public void
addMemberTypeDeclaration(MemberTypeDeclaration mcoid) {
this.declaredClassesAndInterfaces.add(mcoid);
mcoid.setDeclaringType(this);
}
/** Adds one {@link MethodDeclarator} to this type. */
public void
addDeclaredMethod(MethodDeclarator method) {
this.declaredMethods.add(method);
method.setDeclaringType(this);
}
// Implement TypeDeclaration.
@Override public Collection<MemberTypeDeclaration>
getMemberTypeDeclarations() { return this.declaredClassesAndInterfaces; }
@Override public MemberTypeDeclaration
getMemberTypeDeclaration(String name) {
for (MemberTypeDeclaration mtd : this.declaredClassesAndInterfaces) {
if (mtd.getName().equals(name)) return mtd;
}
return null;
}
@Override public MethodDeclarator
getMethodDeclaration(String name) {
for (MethodDeclarator md : this.declaredMethods) {
if (md.name.equals(name)) return md;
}
return null;
}
@Override public List<MethodDeclarator>
getMethodDeclarations() { return this.declaredMethods; }
@Override public String
createLocalTypeName(String localTypeName) {
return (
this.getClassName()
+ '$'
+ ++this.localClassCount
+ '$'
+ localTypeName
);
}
@Override public String
createAnonymousClassName() {
return (
this.getClassName()
+ '$'
+ ++this.anonymousClassCount
);
}
// Implement "Locatable".
@Override public Location
getLocation() { return this.location; }
@Override public void
throwCompileException(String message) throws CompileException {
throw new CompileException(message, this.location);
}
@Override public abstract String
toString();
/** For naming anonymous classes. */
public int anonymousClassCount;
/** For naming local classes. */
public int localClassCount;
}
/** Base for the various class declaration kinds. */
public abstract static
class ClassDeclaration extends AbstractTypeDeclaration {
/** List of {@link ConstructorDeclarator}s of this class. */
public final List<ConstructorDeclarator> constructors = new ArrayList();
/**
* List of {@link TypeBodyDeclaration}s of this class: Field declarations (both static and non-static),
* (static and non-static) initializers (a.k.a. "class initializers" and "instance initializers").
*/
public final List<BlockStatement> variableDeclaratorsAndInitializers = new ArrayList();
public
ClassDeclaration(Location location, Modifiers modifiers) {
super(location, modifiers);
}
/** Adds one {@link ConstructorDeclarator} to this class. */
public void
addConstructor(ConstructorDeclarator cd) {
this.constructors.add(cd);
cd.setDeclaringType(this);
}
/** Adds one field declaration to this class. */
public void
addFieldDeclaration(FieldDeclaration fd) {
this.variableDeclaratorsAndInitializers.add(fd);
fd.setDeclaringType(this);
// Clear resolved type cache.
if (this.resolvedType != null) this.resolvedType.clearIFieldCaches();
}
/** Adds one initializer to this class. */
public void
addInitializer(Initializer i) {
this.variableDeclaratorsAndInitializers.add(i);
i.setDeclaringType(this);
// Clear resolved type cache.
if (this.resolvedType != null) this.resolvedType.clearIFieldCaches();
}
// Compile time members.
// Forward-implement InnerClassDeclaration.
/** @see Java.InnerClassDeclaration#defineSyntheticField(IClass.IField) */
public void
defineSyntheticField(IClass.IField iField) throws CompileException {
if (!(this instanceof InnerClassDeclaration)) throw new JaninoRuntimeException();
IClass.IField if2 = (IClass.IField) this.syntheticFields.get(iField.getName());
if (if2 != null) {
if (iField.getType() != if2.getType()) throw new JaninoRuntimeException();
return;
}
this.syntheticFields.put(iField.getName(), iField);
}
/** @return The declared constructors, or the default constructor */
ConstructorDeclarator[]
getConstructors() {
if (this.constructors.isEmpty()) {
ConstructorDeclarator defaultConstructor = new ConstructorDeclarator(
this.getLocation(), // location
null, // optionalDocComment
new Java.Modifiers(Mod.PUBLIC), // modifiers
new FormalParameters(), // formalParameters
new Type[0], // thrownExceptions
null, // optionalExplicitConstructorInvocation
Collections.EMPTY_LIST // optionalStatements
);
defaultConstructor.setDeclaringType(this);
return new ConstructorDeclarator[] { defaultConstructor };
}
return (ConstructorDeclarator[]) this.constructors.toArray(
new ConstructorDeclarator[this.constructors.size()]
);
}
/** All field names start with "this$" or "val$". */
final SortedMap<String, IClass.IField> syntheticFields = new TreeMap();
}
/** Representation of a JLS7 15.9.5 'anonymous class declaration'. */
public static final
class AnonymousClassDeclaration extends ClassDeclaration implements InnerClassDeclaration {
/** Base class or interface. */
public final Type baseType;
public
AnonymousClassDeclaration(Location location, Type baseType) {
super(
location, // location
new Modifiers((short) (Mod.PRIVATE | Mod.FINAL)) // modifiers
);
(this.baseType = baseType).setEnclosingScope(new EnclosingScopeOfTypeDeclaration(this));
}
@Override public void
accept(Visitor.TypeDeclarationVisitor visitor) { visitor.visitAnonymousClassDeclaration(this); }
// Implement TypeDeclaration.
@Override public String
getClassName() {
if (this.myName == null) {
Scope s = this.getEnclosingScope();
for (; !(s instanceof TypeDeclaration); s = s.getEnclosingScope());
this.myName = ((TypeDeclaration) s).createAnonymousClassName();
}
return this.myName;
}
private String myName;
@Override public String
toString() { return this.getClassName(); }
}
/** Base for the various named class declarations. */
public abstract static
class NamedClassDeclaration extends ClassDeclaration implements NamedTypeDeclaration, DocCommentable {
private final String optionalDocComment;
/** The simple name of this class. */
public final String name;
/** The optional type parameters of this interface. */
public final TypeParameter[] optionalTypeParameters;
/** The type of the extended class. */
public final Type optionalExtendedType;
/** The types of the implemented interfaces. */
public final Type[] implementedTypes;
public
NamedClassDeclaration(
Location location,
String optionalDocComment,
Modifiers modifiers,
String name,
TypeParameter[] optionalTypeParameters,
Type optionalExtendedType,
Type[] implementedTypes
) {
super(location, modifiers);
this.optionalDocComment = optionalDocComment;
this.name = name;
this.optionalTypeParameters = optionalTypeParameters;
this.optionalExtendedType = optionalExtendedType;
if (optionalExtendedType != null) {
optionalExtendedType.setEnclosingScope(new EnclosingScopeOfTypeDeclaration(this));
}
this.implementedTypes = implementedTypes;
for (Type implementedType : implementedTypes) {
implementedType.setEnclosingScope(new EnclosingScopeOfTypeDeclaration(this));
}
}
@Override public String
toString() { return this.name; }
// Implement NamedTypeDeclaration.
@Override public String
getName() { return this.name; }
@Override public TypeParameter[]
getOptionalTypeParameters() { return this.optionalTypeParameters; }
// Implement DocCommentable.
@Override public String
getDocComment() { return this.optionalDocComment; }
@Override public boolean
hasDeprecatedDocTag() {
return this.optionalDocComment != null && this.optionalDocComment.indexOf("@deprecated") != -1;
}
}
/** Lazily determines and returns the enclosing {@link Java.Scope} of the given {@link Java.TypeDeclaration}. */
public static final
class EnclosingScopeOfTypeDeclaration implements Scope {
/** The specific type declaration. */
public final TypeDeclaration typeDeclaration;
public
EnclosingScopeOfTypeDeclaration(TypeDeclaration typeDeclaration) { this.typeDeclaration = typeDeclaration; }
@Override public Scope
getEnclosingScope() { return this.typeDeclaration.getEnclosingScope(); }
}
/**
* Representation of a 'member class declaration', i.e. a class declaration that appears inside another class
* declaration.
*/
public static final
class MemberClassDeclaration extends NamedClassDeclaration implements MemberTypeDeclaration, InnerClassDeclaration {
public
MemberClassDeclaration(
Location location,
String optionalDocComment,