My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package com.hypefree.blogspot.dyncompile;

import javax.tools.*;
import java.util.*;
import java.util.concurrent.atomic.*;

public class DynCompile {
private static AtomicInteger counter = new AtomicInteger(0);

private static int getUniqueID() {
return counter.incrementAndGet();
}

private static String getSource(int printVal, int uniqueId) {
return "package com.hypefree.blogspot.dyncompile;\n"
+ "public final class TestClass" + uniqueId + " implements DoIt { "
+ "public void doIt() { System.out.println(" + printVal + "); } }";
}

static void doTest() throws Exception {
// based on: http://www.ibm.com/developerworks/java/library/j-jcomp/index.html

CharSequenceCompiler<DoIt> compiler
= new CharSequenceCompiler<DoIt>(DynCompile.class.getClassLoader(), Arrays.asList(new String[] {}));
final DiagnosticCollector<JavaFileObject> errs = new DiagnosticCollector<JavaFileObject>();

DoIt[] testCases = new DoIt[10];
for (int i = 0; i < testCases.length; ++i) {
final int id = getUniqueID();
final String src = getSource(i, id);
Class<DoIt> compiledFunction = compiler.compile("com.hypefree.blogspot.dyncompile.TestClass" + id,
src, errs, new Class<?>[] { DoIt.class });
testCases[i] = compiledFunction.newInstance();
}

for (int i = 0; i < 5; ++i) {
for (int j = 0; j < testCases.length; ++j) {
testCases[j].doIt();
}
}
}

public static void main(String[] args) throws Exception {
doTest();
}

}

Change log

r47 by dify.ltd on Apr 18, 2010   Diff
Tests for dynamic compilation.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1600 bytes, 47 lines
Powered by Google Project Hosting