-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
225 lines (189 loc) · 6.4 KB
/
Copy pathTest.java
File metadata and controls
225 lines (189 loc) · 6.4 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
import java.io.IOException;
import java.lang.annotation.*;
import java.util.*;
import java.util.function.*;
// ==========================================
// 1. ANNOTATIONS
// Triggers: RuntimeVisibleAnnotations, AnnotationDefault
// ==========================================
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface TestMetadata {
String author() default "AnvilASM"; // AnnotationDefault attribute
int version();
}
// ==========================================
// 2. INTERFACES
// Triggers: InterfaceMethodref
// ==========================================
interface MathOperation {
double execute(double a, double b);
}
// ==========================================
// 3. ENUMS
// Triggers: ACC_ENUM, Enum constant pool entries
// ==========================================
enum State {
IDLE, PROCESSING, ERROR
}
// ==========================================
// 4. MAIN CLASS
// File name must be: Test.java
// ==========================================
@TestMetadata(version = 1)
public class Test<T> implements Cloneable {
// ==========================================
// FIELDS
// Triggers: FieldInfo, ConstantValue, AccessFlags
// ==========================================
// ConstantValue attribute (Primitive)
public static final int MAGIC_CONST = 42;
// ConstantValue attribute (String)
public static final String GREETING = "Hello Bytecode";
// ACC_VOLATILE
private volatile boolean isRunning;
// ACC_TRANSIENT
private transient Object tempCache;
// Array type descriptor: [[[D
private double[][][] complexArray;
// ==========================================
// CONSTRUCTORS
// ==========================================
public Test() {
this.isRunning = true;
this.complexArray = new double[2][2][2];
}
// ==========================================
// METHODS
// ==========================================
/**
* Test 1: Control Flow & Switch
* Triggers: tableswitch, lookupswitch, StackMapTable
*/
public String testControlFlow(int value) {
// Compact sequence -> tableswitch
switch (value) {
case 0: return "Zero";
case 1: return "One";
case 2: return "Two";
}
// Sparse sequence -> lookupswitch
switch (value) {
case 100: return "Hundred";
case 5000: return "Five Thousand";
default: return "Unknown";
}
}
/**
* Test 2: Exception Handling
* Triggers: ExceptionTable, Exceptions attribute (throws)
*/
public void testExceptions() throws IOException, IllegalArgumentException {
try {
if (!isRunning) {
throw new IOException("System not running");
}
// Multi-catch (Java 7+)
} catch (IOException | NullPointerException e) {
System.err.println("IO or Null Error: " + e.getMessage());
} catch (Exception e) {
System.err.println("General Error: " + e.getMessage());
} finally {
System.out.println("Cleanup executed");
}
}
/**
* Test 3: Lambdas & Dynamic Invocation
* Triggers: invokedynamic instruction, BootstrapMethods attribute, MethodHandle
*/
public void testLambdaAndStreams() {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Lambda expression
Consumer<String> printer = s -> System.out.println("Name: " + s);
// Method Reference (System.out::println)
names.forEach(System.out::println);
// Stream pipeline
long count = names.stream()
.filter(s -> s.startsWith("A"))
.count();
System.out.println("Count: " + count);
}
/**
* Test 4: Generics
* Triggers: Signature attribute (LocalVariableTypeTable if -g is used)
*/
public <E extends Number> void testGenerics(List<E> numbers, Map<String, E> map) {
for (E num : numbers) {
System.out.println("Number: " + num.doubleValue());
}
}
/**
* Test 5: Synchronization & Strict FP
* Triggers: ACC_SYNCHRONIZED, ACC_STRICT, monitorenter/monitorexit
*/
public strictfp synchronized void testSyncAndMath() {
double result = 0.0;
synchronized (this) {
result = Math.PI * 2.0;
}
System.out.println("Strict Result: " + result);
}
/**
* Test 6: Varargs & Native declaration
* Triggers: ACC_VARARGS, ACC_NATIVE
*/
public void testVarargs(String... args) {
System.out.println("Args count: " + args.length);
}
// Native method declaration (no Code attribute)
public native void nativeMethodExample();
// ==========================================
// INNER CLASSES
// Triggers: InnerClasses attribute
// ==========================================
// 1. Static Nested Class
public static class StaticNested {
public int id;
}
// 2. Inner Member Class
private class InnerMember {
void accessOuter() {
System.out.println(isRunning); // Accessing outer field
}
}
public void testInnerClasses() {
// 3. Local Class (Triggers EnclosingMethod attribute)
class LocalClass {
void print() { System.out.println("Local"); }
}
new LocalClass().print();
// 4. Anonymous Class (Triggers EnclosingMethod attribute)
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous");
}
};
r.run();
}
// ==========================================
// MAIN ENTRY POINT
// ==========================================
public static void main(String[] args) {
System.out.println("=== Starting Bytecode Analysis Test ===");
Test<Integer> test = new Test<>();
// Run Control Flow
System.out.println("Switch: " + test.testControlFlow(1));
// Run Lambda
test.testLambdaAndStreams();
// Run Inner Classes
test.testInnerClasses();
// Run Exceptions
try {
test.testExceptions();
} catch (Exception e) {
// Ignore
}
System.out.println("=== Test Completed ===");
}
}