Skip to content

Commit 4bb3c16

Browse files
committed
APT: write class file
1 parent 067663b commit 4bb3c16

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

modules/jooby-apt/src/main/java/io/jooby/compiler/MvcHandlerCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ private void processReturnType(MethodVisitor visitor) throws Exception {
272272
}
273273

274274
public String getGeneratedClass() {
275-
return getController().getName() + "$" + httpMethod.toUpperCase() + "$" + executable
276-
.getSimpleName();
275+
return getController().getName() + "$" + httpMethod.toUpperCase() + pattern
276+
.replace('/', '_');
277277
}
278278

279279
public String getGeneratedInternalClass() {

modules/jooby-apt/src/main/java/io/jooby/compiler/MvcProcessor.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.objectweb.asm.util.TraceClassVisitor;
1414

1515
import javax.annotation.processing.Completion;
16+
import javax.annotation.processing.Filer;
1617
import javax.annotation.processing.ProcessingEnvironment;
1718
import javax.annotation.processing.Processor;
1819
import javax.annotation.processing.RoundEnvironment;
@@ -23,7 +24,10 @@
2324
import javax.lang.model.element.ExecutableElement;
2425
import javax.lang.model.element.TypeElement;
2526
import javax.lang.model.type.TypeMirror;
27+
import javax.tools.JavaFileObject;
2628
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.OutputStream;
2731
import java.io.PrintWriter;
2832
import java.util.Collections;
2933
import java.util.HashMap;
@@ -76,16 +80,23 @@ public boolean process(Set<? extends TypeElement> annotations,
7680
}
7781
}
7882
}
83+
Filer filer = processingEnvironment.getFiler();
7984
Map<String, List<Map.Entry<String, MvcHandlerCompiler>>> classes = result.entrySet().stream()
8085
.collect(Collectors.groupingBy(e -> e.getValue().getController().getName()));
8186
for (Map.Entry<String, List<Map.Entry<String, MvcHandlerCompiler>>> entry : classes
8287
.entrySet()) {
8388
try {
8489
List<Map.Entry<String, MvcHandlerCompiler>> handlers = entry.getValue();
8590
MvcModuleCompiler module = new MvcModuleCompiler(entry.getKey());
86-
modules.put(entry.getKey() + "$Module", module.compile(handlers));
91+
String moduleClass = entry.getKey() + "$Module";
92+
byte[] moduleBin = module.compile(handlers);
93+
writeClass(filer.createClassFile(moduleClass), moduleBin);
94+
modules.put(moduleClass, moduleBin);
8795
for (Map.Entry<String, MvcHandlerCompiler> handler : handlers) {
88-
modules.put(handler.getValue().getGeneratedClass(), handler.getValue().compile());
96+
String handleClass = handler.getValue().getGeneratedClass();
97+
byte[] handleBin = handler.getValue().compile();
98+
writeClass(filer.createClassFile(handleClass), handleBin);
99+
modules.put(handleClass, handleBin);
89100
}
90101
} catch (Exception x) {
91102
x.printStackTrace();
@@ -95,6 +106,12 @@ public boolean process(Set<? extends TypeElement> annotations,
95106
return true;
96107
}
97108

109+
private void writeClass(JavaFileObject javaFileObject, byte[] bytecode) throws IOException {
110+
try (OutputStream output = javaFileObject.openOutputStream()) {
111+
output.write(bytecode);
112+
}
113+
}
114+
98115
/*package*/ MvcHandlerCompiler compilerFor(String methodDescriptor) {
99116
return result.get(methodDescriptor);
100117
}

0 commit comments

Comments
 (0)