Skip to content

Commit a02974e

Browse files
committed
Java9 compatibility, fixes #27
1 parent 3f9d67b commit a02974e

File tree

2 files changed

+100
-58
lines changed

2 files changed

+100
-58
lines changed

compiler/src/main/java/net/openhft/compiler/MyJavaFileManager.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,20 @@
2020

2121
import org.jetbrains.annotations.NotNull;
2222

23-
import javax.tools.*;
23+
import javax.tools.FileObject;
24+
import javax.tools.JavaFileManager;
25+
import javax.tools.JavaFileObject;
2426
import javax.tools.JavaFileObject.Kind;
25-
import java.io.*;
27+
import javax.tools.SimpleJavaFileObject;
28+
import javax.tools.StandardJavaFileManager;
29+
import javax.tools.StandardLocation;
30+
import java.io.ByteArrayInputStream;
31+
import java.io.ByteArrayOutputStream;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.io.OutputStream;
35+
import java.lang.reflect.InvocationTargetException;
36+
import java.lang.reflect.Method;
2637
import java.net.URI;
2738
import java.util.Iterator;
2839
import java.util.LinkedHashMap;
@@ -38,6 +49,14 @@ class MyJavaFileManager implements JavaFileManager {
3849
this.fileManager = fileManager;
3950
}
4051

52+
public Iterable<Set<Location>> listLocationsForModules(final Location location) throws IOException {
53+
return invokeNamedMethodIfAvailable(location, "listLocationsForModules");
54+
}
55+
56+
public String inferModuleName(final Location location) throws IOException {
57+
return invokeNamedMethodIfAvailable(location, "inferModuleName");
58+
}
59+
4160
public ClassLoader getClassLoader(Location location) {
4261
return fileManager.getClassLoader(location);
4362
}
@@ -119,4 +138,22 @@ public Map<String, byte[]> getAllBuffers() {
119138
}
120139
return ret;
121140
}
122-
}
141+
142+
@SuppressWarnings("unchecked")
143+
private <T> T invokeNamedMethodIfAvailable(final Location location, final String name) {
144+
final Method[] methods = fileManager.getClass().getDeclaredMethods();
145+
for (Method method : methods) {
146+
if (method.getName().equals(name) && method.getParameterTypes().length == 1 &&
147+
method.getParameterTypes()[0] == Location.class) {
148+
try {
149+
return (T) method.invoke(fileManager, location);
150+
} catch (IllegalAccessException e) {
151+
throw new UnsupportedOperationException("Unable to invoke method " + name);
152+
} catch (InvocationTargetException e) {
153+
throw new UnsupportedOperationException("Unable to invoke method " + name);
154+
}
155+
}
156+
}
157+
throw new UnsupportedOperationException("Unable to find method " + name);
158+
}
159+
}

compiler/src/test/java/net/openhft/compiler/CompilerTest.java

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -47,65 +47,70 @@ public class CompilerTest extends TestCase {
4747
}
4848
}
4949

50-
public static void test_compiler() throws ClassNotFoundException {
51-
// CompilerUtils.setDebug(true);
52-
// added so the test passes in Maven.
53-
CompilerUtils.addClassPath("target/test-classes");
50+
public void test_compiler() throws ClassNotFoundException {
51+
try {
52+
// CompilerUtils.setDebug(true);
53+
// added so the test passes in Maven.
54+
CompilerUtils.addClassPath("target/test-classes");
5455
// ClassLoader loader = CompilerTest.class.getClassLoader();
5556
// URLClassLoader urlClassLoader = new URLClassLoader(((URLClassLoader)loader).getURLs(), null);
5657
// Class fooBarTee1 = urlClassLoader.loadClass("eg.FooBarTee");
5758

58-
// this writes the file to disk only when debugging is enabled.
59-
CachedCompiler cc = CompilerUtils.DEBUGGING ?
60-
new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
61-
CompilerUtils.CACHED_COMPILER;
62-
63-
String text = "generated test " + new Date();
64-
cc.loadFromJava(EG_FOO_BAR_TEE, "package eg;\n" +
65-
'\n' +
66-
"import eg.components.BarImpl;\n" +
67-
"import eg.components.TeeImpl;\n" +
68-
"import eg.components.Foo;\n" +
69-
'\n' +
70-
"public class FooBarTee{\n" +
71-
" public final String name;\n" +
72-
" public final TeeImpl tee;\n" +
73-
" public final BarImpl bar;\n" +
74-
" public final BarImpl copy;\n" +
75-
" public final Foo foo;\n" +
76-
'\n' +
77-
" public FooBarTee(String name) {\n" +
78-
" // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
79-
" System.out.println(\"" + text + "\");\n" +
80-
" this.name = name;\n" +
81-
'\n' +
82-
" tee = new TeeImpl(\"test\");\n" +
83-
'\n' +
84-
" bar = new BarImpl(tee, 55);\n" +
85-
'\n' +
86-
" copy = new BarImpl(tee, 555);\n" +
87-
'\n' +
88-
" // you should see the current date here after synchronisation.\n" +
89-
" foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
90-
" }\n" +
91-
'\n' +
92-
" public void start() {\n" +
93-
" }\n" +
94-
'\n' +
95-
" public void stop() {\n" +
96-
" }\n" +
97-
'\n' +
98-
" public void close() {\n" +
99-
" stop();\n" +
100-
'\n' +
101-
" }\n" +
102-
"}\n");
103-
104-
// add a debug break point here and step into this method.
105-
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
106-
Foo foo = fooBarTee.foo;
107-
assertNotNull(foo);
108-
assertEquals(text, foo.s);
59+
// this writes the file to disk only when debugging is enabled.
60+
CachedCompiler cc = CompilerUtils.DEBUGGING ?
61+
new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
62+
CompilerUtils.CACHED_COMPILER;
63+
64+
String text = "generated test " + new Date();
65+
cc.loadFromJava(EG_FOO_BAR_TEE, "package eg;\n" +
66+
'\n' +
67+
"import eg.components.BarImpl;\n" +
68+
"import eg.components.TeeImpl;\n" +
69+
"import eg.components.Foo;\n" +
70+
'\n' +
71+
"public class FooBarTee{\n" +
72+
" public final String name;\n" +
73+
" public final TeeImpl tee;\n" +
74+
" public final BarImpl bar;\n" +
75+
" public final BarImpl copy;\n" +
76+
" public final Foo foo;\n" +
77+
'\n' +
78+
" public FooBarTee(String name) {\n" +
79+
" // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
80+
" System.out.println(\"" + text + "\");\n" +
81+
" this.name = name;\n" +
82+
'\n' +
83+
" tee = new TeeImpl(\"test\");\n" +
84+
'\n' +
85+
" bar = new BarImpl(tee, 55);\n" +
86+
'\n' +
87+
" copy = new BarImpl(tee, 555);\n" +
88+
'\n' +
89+
" // you should see the current date here after synchronisation.\n" +
90+
" foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
91+
" }\n" +
92+
'\n' +
93+
" public void start() {\n" +
94+
" }\n" +
95+
'\n' +
96+
" public void stop() {\n" +
97+
" }\n" +
98+
'\n' +
99+
" public void close() {\n" +
100+
" stop();\n" +
101+
'\n' +
102+
" }\n" +
103+
"}\n");
104+
105+
// add a debug break point here and step into this method.
106+
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
107+
Foo foo = fooBarTee.foo;
108+
assertNotNull(foo);
109+
assertEquals(text, foo.s);
110+
} catch (Throwable t) {
111+
t.printStackTrace(System.out);
112+
fail(t.getMessage());
113+
}
109114
}
110115

111116
public void test_fromFile()

0 commit comments

Comments
 (0)