Skip to content

Commit

Permalink
Merge pull request #3444 from jooby-project/2968
Browse files Browse the repository at this point in the history
jooby apt: generate source code
  • Loading branch information
jknack authored Jun 7, 2024
2 parents 4aba526 + 59e9b40 commit 7d28303
Show file tree
Hide file tree
Showing 112 changed files with 2,408 additions and 4,935 deletions.
10 changes: 10 additions & 0 deletions jooby/src/main/java/io/jooby/Jooby.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,16 @@ public Jooby mount(@NonNull Router router) {
return mount("/", router);
}

@NonNull @Override
public Jooby mvc(@NonNull MvcExtension router) {
try {
router.install(this);
return this;
} catch (Exception cause) {
throw SneakyThrows.propagate(cause);
}
}

@NonNull @Override
public Jooby mvc(@NonNull Object router) {
Provider provider = () -> router;
Expand Down
14 changes: 14 additions & 0 deletions jooby/src/main/java/io/jooby/MvcExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;

/**
* Marker interface for generated MVC router.
*
* @author edgar
* @since 3.2.0
*/
public interface MvcExtension extends Extension {}
6 changes: 3 additions & 3 deletions jooby/src/main/java/io/jooby/MvcFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
*
* @since 2.1.0
*/
public interface MvcFactory {
public interface MvcFactory<T> {
/**
* Check if the factory applies for the given MVC route.
*
* @param type MVC route.
* @return True for matching factory.
*/
boolean supports(@NonNull Class type);
boolean supports(@NonNull Class<T> type);

/**
* Creates an extension module. The extension module are created at compilation time by Jooby APT.
*
* @param provider MVC route instance provider.
* @return All mvc route as extension module.
*/
@NonNull Extension create(@NonNull Provider provider);
@NonNull Extension create(@NonNull Provider<T> provider);
}
8 changes: 4 additions & 4 deletions jooby/src/main/java/io/jooby/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,11 @@ public boolean isNonBlockingSet() {
* @return This route.
*/
public @NonNull Route setProduces(@NonNull Collection<MediaType> produces) {
if (produces.size() > 0) {
if (!produces.isEmpty()) {
if (this.produces == EMPTY_LIST) {
this.produces = new ArrayList<>();
}
produces.forEach(this.produces::add);
this.produces.addAll(produces);
}
return this;
}
Expand Down Expand Up @@ -724,11 +724,11 @@ public boolean isNonBlockingSet() {
* @return This route.
*/
public @NonNull Route setConsumes(@NonNull Collection<MediaType> consumes) {
if (consumes.size() > 0) {
if (!consumes.isEmpty()) {
if (this.consumes == EMPTY_LIST) {
this.consumes = new ArrayList<>();
}
consumes.forEach(this.consumes::add);
this.consumes.addAll(consumes);
}
return this;
}
Expand Down
35 changes: 22 additions & 13 deletions jooby/src/main/java/io/jooby/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package io.jooby;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -109,8 +108,7 @@ default Object execute(@NonNull Context context) {
String TRACE = "TRACE";

/** HTTP Methods. */
List<String> METHODS =
unmodifiableList(asList(GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE));
List<String> METHODS = List.of(GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE);

/** Web socket. */
String WS = "WS";
Expand Down Expand Up @@ -410,13 +408,23 @@ default Object execute(@NonNull Context context) {
* ***********************************************************************************************
*/

/**
* Import all route method from the given controller class.
*
* @param router Router extension.
* @return This router.
*/
@NonNull Router mvc(@NonNull MvcExtension router);

/**
* Import all route method from the given controller class. At runtime the controller instance is
* resolved by calling {@link Jooby#require(Class)}.
*
* @param router Controller class.
* @return This router.
* @deprecated See {{@link #mvc(MvcExtension)}}
*/
@Deprecated
@NonNull Router mvc(@NonNull Class router);

/**
Expand All @@ -426,15 +434,19 @@ default Object execute(@NonNull Context context) {
* @param provider Controller provider.
* @param <T> Controller type.
* @return This router.
* @deprecated See {{@link #mvc(MvcExtension)}}
*/
@Deprecated
@NonNull <T> Router mvc(@NonNull Class<T> router, @NonNull Provider<T> provider);

/**
* Import all route methods from given controller instance.
*
* @param router Controller instance.
* @return This routes.
* @deprecated See {{@link #mvc(MvcExtension)}}
*/
@Deprecated
@NonNull Router mvc(@NonNull Object router);

/**
Expand Down Expand Up @@ -1105,14 +1117,11 @@ default Object execute(@NonNull Context context) {
i = len;
}
}
switch (result.size()) {
case 0:
return Collections.emptyList();
case 1:
return Collections.singletonList(result.get(0));
default:
return unmodifiableList(result);
}
return switch (result.size()) {
case 0 -> Collections.emptyList();
case 1 -> Collections.singletonList(result.get(0));
default -> unmodifiableList(result);
};
}

/**
Expand Down Expand Up @@ -1207,7 +1216,7 @@ default Object execute(@NonNull Context context) {
if (paths.isEmpty()) {
return Collections.singletonList(pattern);
}
if (segment.length() > 0) {
if (!segment.isEmpty()) {
pathAppender.accept(key.get(), segment);
if (isLastOptional) {
paths.put(key.incrementAndGet(), segment);
Expand Down Expand Up @@ -1273,7 +1282,7 @@ default Object execute(@NonNull Context context) {
i = len;
}
}
if (path.length() == 0) {
if (path.isEmpty()) {
return pattern;
}
if (start > 0) {
Expand Down
17 changes: 17 additions & 0 deletions jooby/src/main/java/io/jooby/annotation/Generated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Generated {
Class<?> value();
}
5 changes: 5 additions & 0 deletions jooby/src/main/java/io/jooby/internal/RouterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ public Router mount(@NonNull Router router) {
return mount("/", router);
}

@NonNull @Override
public Router mvc(@NonNull MvcExtension router) {
throw new UnsupportedOperationException();
}

@NonNull @Override
public Router mvc(@NonNull Object router) {
throw new UnsupportedOperationException();
Expand Down
8 changes: 4 additions & 4 deletions modules/jooby-apt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

<dependencies>

<!-- ASM -->
<!-- https://mvnrepository.com/artifact/com.squareup/javapoet -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<optional>true</optional>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>

<dependency>
Expand Down
Loading

0 comments on commit 7d28303

Please sign in to comment.