-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1114 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
3fbeb41
commit 9736652
Showing
6 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/redhat/microprofile/psi/internal/quarkus/vertx/java/VertxWebConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.psi.internal.quarkus.vertx.java; | ||
|
||
/** | ||
* Constants important to working with Vertx Web reactive applications. | ||
*/ | ||
public class VertxWebConstants { | ||
|
||
public static final String ROUTE_BASE_FQN = "io.quarkus.vertx.web.RouteBase"; | ||
|
||
public static final String ROUTE_BASE_PATH = "path"; | ||
|
||
public static final String ROUTE_FQN = "io.quarkus.vertx.web.Route"; | ||
|
||
public static final String ROUTE_PATH = "path"; | ||
|
||
} |
145 changes: 145 additions & 0 deletions
145
...va/com/redhat/microprofile/psi/internal/quarkus/vertx/java/VertxWebJaxRsInfoProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.psi.internal.quarkus.vertx.java; | ||
|
||
|
||
import com.intellij.openapi.module.Module; | ||
import com.intellij.openapi.progress.ProcessCanceledException; | ||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.psi.*; | ||
import com.intellij.util.KeyedLazyInstanceEP; | ||
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.*; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils.hasAnnotation; | ||
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils.overlaps; | ||
import static com.redhat.microprofile.psi.internal.quarkus.vertx.java.VertxWebConstants.ROUTE_FQN; | ||
import static com.redhat.microprofile.psi.internal.quarkus.vertx.java.VertxWebUtils.*; | ||
|
||
/** | ||
* Use custom logic for all JAX-RS features in classes that extends Renarde's | ||
* <code>Controller</code> class. | ||
*/ | ||
public class VertxWebJaxRsInfoProvider extends KeyedLazyInstanceEP<IJaxRsInfoProvider> implements IJaxRsInfoProvider { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(VertxWebJaxRsInfoProvider.class.getName()); | ||
|
||
@Override | ||
public boolean canProvideJaxRsMethodInfoForClass(PsiFile typeRoot, Module javaProject, ProgressIndicator monitor) { | ||
return PsiTypeUtils.findType(javaProject, ROUTE_FQN) != null; | ||
} | ||
|
||
@Override | ||
public Set<PsiClass> getAllJaxRsClasses(Module javaProject, ProgressIndicator monitor) { | ||
// TODO: implement when LSP4IJ will support workspace symbols | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public List<JaxRsMethodInfo> getJaxRsMethodInfo(PsiFile typeRoot, JaxRsContext jaxrsContext, IPsiUtils utils, | ||
ProgressIndicator monitor) { | ||
try { | ||
PsiClass type = findFirstClass(typeRoot); | ||
if (type == null) { | ||
return Collections.emptyList(); | ||
} | ||
PsiAnnotation routeBaseAnnotation = getRouteBaseAnnotation(type); | ||
String pathSegment = routeBaseAnnotation != null ? getRouteBasePath(routeBaseAnnotation) : null; | ||
|
||
List<JaxRsMethodInfo> methodInfos = new ArrayList<>(); | ||
for (PsiMethod method : type.getMethods()) { | ||
|
||
if (method.isConstructor() || utils.isHiddenGeneratedElement(method)) { | ||
continue; | ||
} | ||
// ignore element if method range overlaps the type range, | ||
// happens for generated | ||
// bytecode, i.e. with lombok | ||
if (overlaps(type.getNameIdentifier().getTextRange(), method.getNameIdentifier().getTextRange())) { | ||
continue; | ||
} | ||
|
||
//if (method.getModifierList().hasExplicitModifier(PsiModifier.PUBLIC)) { | ||
|
||
List<PsiAnnotation> routeAnnotations = getRouteAnnotations(method); | ||
if (routeAnnotations.isEmpty()) { | ||
continue; | ||
} | ||
|
||
for (PsiAnnotation routeAnnotation : routeAnnotations) { | ||
// @Route(path="/foo") | ||
String methodSegment = VertxWebUtils.getRoutePath(routeAnnotation); | ||
if (methodSegment == null) { | ||
methodSegment = method.getName(); | ||
} | ||
String path; | ||
if (pathSegment == null) { | ||
path = methodSegment; | ||
} else { | ||
path = JaxRsUtils.buildURL(pathSegment, methodSegment); | ||
} | ||
String url = JaxRsUtils.buildURL(jaxrsContext.getLocalBaseURL(), path); | ||
|
||
JaxRsMethodInfo methodInfo = createMethodInfo(method, routeAnnotation, url); | ||
if (methodInfo != null) { | ||
methodInfos.add(methodInfo); | ||
} | ||
} | ||
//} | ||
} | ||
return methodInfos; | ||
} catch (ProcessCanceledException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Error while collecting JAX-RS methods for Vertx Web @Route", e); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
private PsiClass findFirstClass(PsiFile typeRoot) { | ||
for (PsiElement element:typeRoot.getChildren()) { | ||
if (element instanceof PsiClass) { | ||
return (PsiClass) element; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static JaxRsMethodInfo createMethodInfo(PsiMethod method, PsiAnnotation routeAnnotation, String url) { | ||
|
||
PsiFile resource = method.getContainingFile(); | ||
if (resource == null) { | ||
return null; | ||
} | ||
String documentUri = LSPIJUtils.toUriAsString(resource); | ||
|
||
HttpMethod httpMethod = HttpMethod.GET; | ||
for (String methodAnnotationFQN : JaxRsConstants.HTTP_METHOD_ANNOTATIONS) { | ||
if (hasAnnotation(method, methodAnnotationFQN)) { | ||
httpMethod = VertxWebUtils.getHttpMethodForAnnotation(methodAnnotationFQN); | ||
break; | ||
} | ||
} | ||
|
||
return new JaxRsMethodInfo(url, httpMethod, method, documentUri); | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/redhat/microprofile/psi/internal/quarkus/vertx/java/VertxWebUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.psi.internal.quarkus.vertx.java; | ||
|
||
import com.intellij.psi.PsiAnnotation; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiMethod; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.HttpMethod; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.JaxRsConstants; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.JaxRsConstants.*; | ||
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils.*; | ||
import static com.redhat.microprofile.psi.internal.quarkus.vertx.java.VertxWebConstants.*; | ||
|
||
|
||
/** | ||
* JAX-RS utilities. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class VertxWebUtils { | ||
|
||
private VertxWebUtils() { | ||
|
||
} | ||
|
||
public static PsiAnnotation getRouteBaseAnnotation(PsiElement annotatable) { | ||
return getFirstAnnotation(annotatable, ROUTE_BASE_FQN); | ||
} | ||
|
||
public static String getRouteBasePath(PsiAnnotation routeBaseAnnotation) { | ||
return getAnnotationMemberValue(routeBaseAnnotation, ROUTE_BASE_PATH); | ||
} | ||
|
||
public static List<PsiAnnotation> getRouteAnnotations(PsiElement annotatable) { | ||
return getAllAnnotations(annotatable, ROUTE_FQN); | ||
} | ||
|
||
public static String getRoutePath(PsiAnnotation routeAnnotation) { | ||
return getAnnotationMemberValue(routeAnnotation, ROUTE_PATH); | ||
} | ||
|
||
/** | ||
* Returns an HttpMethod given the FQN of a JAX-RS or Jakarta RESTful | ||
* annotation, nor null if the FQN doesn't match any HttpMethod. | ||
* | ||
* @param annotationFQN the FQN of the annotation to convert into a HttpMethod | ||
* @return an HttpMethod given the FQN of a JAX-RS or Jakarta RESTful | ||
* annotation, nor null if the FQN doesn't match any HttpMethod | ||
*/ | ||
public static HttpMethod getHttpMethodForAnnotation(String annotationFQN) { | ||
switch (annotationFQN) { | ||
case JaxRsConstants.JAKARTA_WS_RS_GET_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_GET_ANNOTATION: | ||
return HttpMethod.GET; | ||
case JaxRsConstants.JAKARTA_WS_RS_HEAD_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_HEAD_ANNOTATION: | ||
return HttpMethod.HEAD; | ||
case JaxRsConstants.JAKARTA_WS_RS_POST_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_POST_ANNOTATION: | ||
return HttpMethod.POST; | ||
case JaxRsConstants.JAKARTA_WS_RS_PUT_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_PUT_ANNOTATION: | ||
return HttpMethod.PUT; | ||
case JaxRsConstants.JAKARTA_WS_RS_DELETE_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_DELETE_ANNOTATION: | ||
return HttpMethod.DELETE; | ||
case JaxRsConstants.JAKARTA_WS_RS_PATCH_ANNOTATION: | ||
case JaxRsConstants.JAVAX_WS_RS_PATCH_ANNOTATION: | ||
return HttpMethod.PATCH; | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters