Skip to content

Commit

Permalink
Added jakarta servlet API to java2flow-rest generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryszard-Trojnacki committed Jun 13, 2024
1 parent cfec5ee commit ffec1b4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
subprojects {
group = 'pl.rtprog'
description="Java classes to JavaScript Flow types converter/generator"
version="0.0.3"
version="0.0.4-SNAPSHOT"

plugins.withType(JavaPlugin) {
task javadocJar(type: Jar) {
Expand Down
3 changes: 3 additions & 0 deletions java2flow-rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ java {
dependencies {
api project(":java2flow-core")
implementation group: 'org.jboss.resteasy', name: 'jaxrs-api', version: '3.0.12.Final'
implementation group: 'jakarta.servlet', name: 'jakarta.servlet-api', version: '6.1.0'
implementation group: 'jakarta.ws.rs', name: 'jakarta.ws.rs-api', version: '4.0.0'


testImplementation group: 'junit', name: 'junit', version: '4.13.2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public FetchGenerator(JsGenerator o, Java2Flow types, String networkFunc, String
private final ArrayList<RestMethod> methods=new ArrayList<>();

public void register(Class<?> clazz) {
if(!clazz.isAnnotationPresent(Path.class)) return;
if(!clazz.isAnnotationPresent(Path.class) && !clazz.isAnnotationPresent(jakarta.ws.rs.Path.class)) return;
for(Method m: clazz.getMethods()) {
if(FLOW_IGNORE!=null && m.isAnnotationPresent(FLOW_IGNORE)) continue;
try {
Expand Down
34 changes: 28 additions & 6 deletions java2flow-rest/src/main/java/pl/rtprog/java2flow/RestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,25 @@ private static PathFragment[] computerFragments(Method method, String path) {

public static RestMethod of(Method method) throws IllegalArgumentException {
Class<?> owner=method.getDeclaringClass();
Path classPath=owner.getAnnotation(Path.class);
if(classPath==null) throw new IllegalArgumentException("Missing @Path annotation at class "+owner.getName());
Path methodPath=method.getAnnotation(Path.class);
String type= RestUtils.getMethodType(method);
if(type==null) throw new IllegalArgumentException("Missing method type (@GET, @POST, @PUT, @HEAD, @DELETE, @OPTION) annotation at method "+method.getName());
String path=classPath.value()+(methodPath!=null?"/"+methodPath.value():"");
String path;
{
Path classPath = owner.getAnnotation(Path.class);
if (classPath != null) {
Path methodPath = method.getAnnotation(Path.class);
path = classPath.value() + (methodPath != null ? "/" + methodPath.value() : "");
} else {
jakarta.ws.rs.Path jClassPath=owner.getAnnotation(jakarta.ws.rs.Path.class);
if(jClassPath!=null) {
jakarta.ws.rs.Path jMethodPath=method.getAnnotation(jakarta.ws.rs.Path.class);
path=jClassPath.value()+(jMethodPath!=null ? "/"+jMethodPath.value() : "");
} else {
throw new IllegalArgumentException("Missing @Path annotation at class " + owner.getName());
}
}
}
String type = RestUtils.getMethodType(method);
if (type == null)
throw new IllegalArgumentException("Missing method type (@GET, @POST, @PUT, @HEAD, @DELETE, @OPTION) annotation at method " + method.getName());
PathFragment[] fragments=computerFragments(method,path);

JavaTypeInfo returnType=JavaTypeInfo.returnOf(method);
Expand Down Expand Up @@ -194,10 +207,19 @@ public static RestMethod of(Method method) throws IllegalArgumentException {
query.add(NamedTypeInfo.forParameter(method, q.value(), i));
continue;
}
jakarta.ws.rs.QueryParam jq=RestUtils.find(pa, jakarta.ws.rs.QueryParam.class);
if(jq!=null) {
query.add(NamedTypeInfo.forParameter(method, jq.value(), i));
continue;
}

if(RestUtils.find(pa, Context.class)!=null ||
RestUtils.find(pa, QueryParam.class)!=null ||
RestUtils.find(pa, PathParam.class)!=null) continue;
if(RestUtils.find(pa, jakarta.ws.rs.core.Context.class)!=null ||
RestUtils.find(pa, jakarta.ws.rs.QueryParam.class)!=null ||
RestUtils.find(pa, jakarta.ws.rs.PathParam.class)!=null) continue;

body=new JavaTypeInfo(
method.getParameterTypes()[i],
method.getGenericParameterTypes()[i],
Expand Down
45 changes: 32 additions & 13 deletions java2flow-rest/src/main/java/pl/rtprog/java2flow/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ public static <T extends Annotation> T find(Annotation[] annotations, Class<T> a
*/
public static String getMethodType(Method method) {
if(method==null) return null;
if(method.isAnnotationPresent(GET.class)) return HttpMethod.GET;
if(method.isAnnotationPresent(POST.class)) return HttpMethod.POST;
if(method.isAnnotationPresent(HEAD.class)) return HttpMethod.HEAD;
if(method.isAnnotationPresent(DELETE.class)) return HttpMethod.DELETE;
if(method.isAnnotationPresent(PUT.class)) return HttpMethod.PUT;
if(method.isAnnotationPresent(OPTIONS.class)) return HttpMethod.OPTIONS;
if(method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(jakarta.ws.rs.GET.class)) return HttpMethod.GET;
if(method.isAnnotationPresent(POST.class) || method.isAnnotationPresent(jakarta.ws.rs.POST.class)) return HttpMethod.POST;
if(method.isAnnotationPresent(HEAD.class) || method.isAnnotationPresent(jakarta.ws.rs.HEAD.class)) return HttpMethod.HEAD;
if(method.isAnnotationPresent(DELETE.class) || method.isAnnotationPresent(jakarta.ws.rs.DELETE.class)) return HttpMethod.DELETE;
if(method.isAnnotationPresent(PUT.class) || method.isAnnotationPresent(jakarta.ws.rs.PUT.class)) return HttpMethod.PUT;
if(method.isAnnotationPresent(OPTIONS.class) || method.isAnnotationPresent(jakarta.ws.rs.OPTIONS.class)) return HttpMethod.OPTIONS;
return null;
}


/**
* Functions that tries to find path parameter type for given name
* @param method method to check for
Expand All @@ -69,8 +68,15 @@ public static String getMethodType(Method method) {
public static int findPathParam(Method method, String name) {
for(int i=0;i<method.getParameterCount();++i) {
PathParam pp=find(method.getParameterAnnotations()[i], PathParam.class);
if(pp==null) continue;
if(name.equals(pp.value())) return i;
if(pp!=null) {
if (name.equals(pp.value())) return i;
} else {
jakarta.ws.rs.PathParam jpp=find(method.getParameterAnnotations()[i], jakarta.ws.rs.PathParam.class);
if(jpp!=null) {
if (name.equals(jpp.value())) return i;
}
}

}
return -1;
}
Expand Down Expand Up @@ -101,8 +107,15 @@ public static List<NamedTypeInfo> getFormParams(Method method) {
ArrayList<NamedTypeInfo> res=new ArrayList<>();
for(int i=0;i<method.getParameterCount();++i) {
FormParam p=RestUtils.find(method.getParameterAnnotations()[i], FormParam.class);
if(p==null) continue; // not a FormParam
res.add(NamedTypeInfo.forParameter(method, p.value(), i));
if(p!=null) {
res.add(NamedTypeInfo.forParameter(method, p.value(), i));
} else {
jakarta.ws.rs.FormParam jp=RestUtils.find(method.getParameterAnnotations()[i], jakarta.ws.rs.FormParam.class);
if(jp!=null) {
res.add(NamedTypeInfo.forParameter(method, jp.value(), i));
}

}
}
return res;
}
Expand All @@ -111,8 +124,14 @@ public static List<NamedTypeInfo> getQueryParams(Method method) {
ArrayList<NamedTypeInfo> res=new ArrayList<>();
for(int i=0;i<method.getParameterCount();++i) {
QueryParam p=RestUtils.find(method.getParameterAnnotations()[i], QueryParam.class);
if(p==null) continue; // not a FormParam
res.add(NamedTypeInfo.forParameter(method, p.value(), i));
if(p!=null) {
res.add(NamedTypeInfo.forParameter(method, p.value(), i));
} else {
jakarta.ws.rs.QueryParam jp=RestUtils.find(method.getParameterAnnotations()[i], jakarta.ws.rs.QueryParam.class);
if(jp!=null) {
res.add(NamedTypeInfo.forParameter(method, jp.value(), i));
}
}
}
return res;
}
Expand Down

0 comments on commit ffec1b4

Please sign in to comment.