Skip to content

Commit 26ef85d

Browse files
committed
jooby-apt: MVC annotations with intellij partial build #1390
1 parent 51d6055 commit 26ef85d

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.inject.Provider;
1616
import java.io.IOException;
1717
import java.lang.reflect.Constructor;
18+
import java.lang.reflect.InvocationTargetException;
1819
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.nio.file.Paths;
@@ -283,7 +284,11 @@ public <T> Jooby mvc(@Nonnull Class<T> router, @Nonnull Provider<T> provider) {
283284
MvcFactory module = stream(modules.spliterator(), false)
284285
.filter(it -> it.supports(router))
285286
.findFirst()
286-
.orElseThrow(() -> Usage.mvcRouterNotFound(router));
287+
.orElseGet(() ->
288+
/** Make happy IDE incremental build: */
289+
mvcReflectionFallback(router, getClassLoader())
290+
.orElseThrow(() -> Usage.mvcRouterNotFound(router))
291+
);
287292
Extension extension = module.create(provider);
288293
extension.install(this);
289294
return this;
@@ -292,7 +297,8 @@ public <T> Jooby mvc(@Nonnull Class<T> router, @Nonnull Provider<T> provider) {
292297
}
293298
}
294299

295-
@Nonnull @Override public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler) {
300+
@Nonnull @Override
301+
public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler) {
296302
return router.ws(pattern, handler);
297303
}
298304

@@ -1019,4 +1025,24 @@ private void joobyRunHook(ClassLoader loader, Server server) {
10191025
}
10201026
}
10211027
}
1028+
1029+
/**
1030+
* This method exists to integrate IDE incremental build with MVC annotation processor. It
1031+
* fallback to reflection to lookup for a generated mvc factory.
1032+
*
1033+
* @param source Controller class.
1034+
* @param classLoader Class loader.
1035+
* @return Mvc factory.
1036+
*/
1037+
private Optional<MvcFactory> mvcReflectionFallback(Class source, ClassLoader classLoader) {
1038+
try {
1039+
String moduleName = source.getName() + "$Factory";
1040+
Class<?> moduleType = classLoader.loadClass(moduleName);
1041+
Constructor<?> constructor = moduleType.getDeclaredConstructor();
1042+
getLog().debug("Loading mvc using reflection: " + source);
1043+
return Optional.of((MvcFactory) constructor.newInstance());
1044+
} catch (Exception x) {
1045+
return Optional.empty();
1046+
}
1047+
}
10221048
}

0 commit comments

Comments
 (0)