From 0235e16e06d0e513d0256f12b42f306fac39cf85 Mon Sep 17 00:00:00 2001 From: azerr Date: Fri, 28 Feb 2020 17:11:29 +0100 Subject: [PATCH] Provide codeLens participant See https://github.com/redhat-developer/quarkus-ls/issues/229 Signed-off-by: azerr --- .../META-INF/MANIFEST.MF | 2 + .../plugin.xml | 6 + .../schema/javaFeatureParticipants.exsd | 23 +- .../jdt/core/PropertiesManagerForJava.java | 182 +++------------- .../jdt/core/java/AbtractJavaContext.java | 35 +++ .../core/java/IJavaCodeLensParticipant.java | 77 +++++++ .../jdt/core/java/JavaCodeLensContext.java | 49 +++++ .../jdt/core/jaxrs/JaxRsContext.java | 50 +++++ .../core/project/JDTMicroProfileProject.java | 13 -- .../core/java/JavaFeatureDefinition.java | 88 ++++++-- .../core/java/JavaFeaturesRegistry.java | 20 +- .../MicroProfileFaultToleranceConstants.java | 2 + .../MicroProfileFaultToleranceProvider.java | 2 + .../health/MicroProfileHealthConstants.java | 2 + ...roProfileHealthDiagnosticsParticipant.java | 2 + .../java/MicroProfileHealthErrorCode.java | 2 + .../jdt/internal/jaxrs/JaxRsConstants.java | 31 +++ .../jaxrs/java/JaxRsCodeLensParticipant.java | 200 ++++++++++++++++++ .../MicroProfileRestClientConstants.java | 2 + ...ofileRestClientDiagnosticsParticipant.java | 2 + ...icroProfileRegisterRestClientProvider.java | 2 + .../plugin.xml | 9 +- .../java/QuarkusJaxRsCodeLensParticipant.java | 51 +++++ 23 files changed, 657 insertions(+), 195 deletions(-) create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/IJavaCodeLensParticipant.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/JavaCodeLensContext.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/jaxrs/JaxRsContext.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/JaxRsConstants.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/java/JaxRsCodeLensParticipant.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.quarkus/src/main/java/com/redhat/microprofile/jdt/internal/quarkus/jaxrs/java/QuarkusJaxRsCodeLensParticipant.java diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/META-INF/MANIFEST.MF b/microprofile.jdt/com.redhat.microprofile.jdt.core/META-INF/MANIFEST.MF index d59f422e3..ae7af78e6 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/META-INF/MANIFEST.MF +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/META-INF/MANIFEST.MF @@ -22,6 +22,8 @@ Bundle-ActivationPolicy: lazy Export-Package: com.redhat.microprofile.commons, com.redhat.microprofile.commons.metadata, com.redhat.microprofile.jdt.core, + com.redhat.microprofile.jdt.core.java, + com.redhat.microprofile.jdt.core.jaxrs, com.redhat.microprofile.jdt.core.project, com.redhat.microprofile.jdt.core.utils, io.quarkus.runtime.util diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/plugin.xml b/microprofile.jdt/com.redhat.microprofile.jdt.core/plugin.xml index 0c2894f49..9afb0b266 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/plugin.xml +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/plugin.xml @@ -79,4 +79,10 @@ + + + + + + diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/schema/javaFeatureParticipants.exsd b/microprofile.jdt/com.redhat.microprofile.jdt.core/schema/javaFeatureParticipants.exsd index cb130e35b..93506588f 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/schema/javaFeatureParticipants.exsd +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/schema/javaFeatureParticipants.exsd @@ -19,8 +19,9 @@ - + + @@ -50,6 +51,26 @@ + + + + Java codeLens participant. + + + + + + + Name of a class that implements IJavaCodeLensParticipant. + + + + + + + + + diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java index 3fde919ab..47a6fea1e 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java @@ -9,49 +9,30 @@ *******************************************************************************/ package com.redhat.microprofile.jdt.core; -import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotation; -import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotationMemberValue; -import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.hasAnnotation; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.Socket; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.stream.Collectors; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jdt.core.Flags; -import org.eclipse.jdt.core.IAnnotatable; -import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IMethod; -import org.eclipse.jdt.core.ISourceRange; -import org.eclipse.jdt.core.ISourceReference; -import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.lsp4j.CodeLens; -import org.eclipse.lsp4j.Command; import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.Hover; import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.PublishDiagnosticsParams; -import org.eclipse.lsp4j.Range; import com.redhat.microprofile.commons.DocumentFormat; import com.redhat.microprofile.commons.MicroProfileJavaCodeLensParams; import com.redhat.microprofile.commons.MicroProfileJavaDiagnosticsParams; import com.redhat.microprofile.commons.MicroProfileJavaHoverParams; +import com.redhat.microprofile.jdt.core.java.JavaCodeLensContext; import com.redhat.microprofile.jdt.core.java.JavaDiagnosticsContext; import com.redhat.microprofile.jdt.core.java.JavaHoverContext; -import com.redhat.microprofile.jdt.core.project.JDTMicroProfileProjectManager; import com.redhat.microprofile.jdt.core.utils.IJDTUtils; import com.redhat.microprofile.jdt.internal.core.java.JavaFeatureDefinition; import com.redhat.microprofile.jdt.internal.core.java.JavaFeaturesRegistry; @@ -64,16 +45,6 @@ */ public class PropertiesManagerForJava { - private static final String LOCALHOST = "localhost"; - - private static final int PING_TIMEOUT = 2000; - - private static final String JAVAX_WS_RS_PATH_ANNOTATION = "javax.ws.rs.Path"; - - private static final String JAVAX_WS_RS_GET_ANNOTATION = "javax.ws.rs.GET"; - - private static final String PATH_VALUE = "value"; - private static final PropertiesManagerForJava INSTANCE = new PropertiesManagerForJava(); public static PropertiesManagerForJava getInstance() { @@ -99,18 +70,29 @@ public List codeLens(MicroProfileJavaCodeLensParams params, if (typeRoot == null) { return Collections.emptyList(); } - IJavaElement[] elements = typeRoot.getChildren(); - Collection lenses = new LinkedHashSet<>(elements.length); - if (params.isUrlCodeLensEnabled()) { - int serverPort = JDTMicroProfileProjectManager.getInstance() - .getJDTMicroProfileProject(typeRoot.getJavaProject()).getServerPort(); - params.setLocalServerPort(serverPort); - collectURLCodeLenses(typeRoot, elements, null, lenses, params, utils, monitor); - } + List lenses = new ArrayList<>(); + collectCodeLens(uri, typeRoot, utils, params, lenses, monitor); if (monitor.isCanceled()) { - lenses.clear(); + return Collections.emptyList(); } - return new ArrayList<>(lenses); + return lenses; + } + + private void collectCodeLens(String uri, ITypeRoot typeRoot, IJDTUtils utils, MicroProfileJavaCodeLensParams params, + List lenses, IProgressMonitor monitor) { + // Collect all adapted codeLens participant + JavaCodeLensContext context = new JavaCodeLensContext(uri, typeRoot, utils, params, lenses); + List definitions = JavaFeaturesRegistry.getInstance().getJavaFeatureDefinitions() + .stream().filter(definition -> definition.isAdaptedForCodeLens(context, monitor)) + .collect(Collectors.toList()); + if (definitions.isEmpty()) { + return; + } + + // Begin, collect, end participants + definitions.forEach(definition -> definition.beginCodeLens(context, monitor)); + definitions.forEach(definition -> definition.collectCodeLens(context, monitor)); + definitions.forEach(definition -> definition.endCodeLens(context, monitor)); } /** @@ -139,120 +121,6 @@ private static ITypeRoot resolveTypeRoot(String uri, IJDTUtils utils, IProgressM return unit != null ? unit : classFile; } - private void collectURLCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, String rootPath, - Collection lenses, MicroProfileJavaCodeLensParams params, IJDTUtils utils, - IProgressMonitor monitor) throws JavaModelException { - for (IJavaElement element : elements) { - if (monitor.isCanceled()) { - return; - } - if (element.getElementType() == IJavaElement.TYPE) { - IType type = (IType) element; - // Get value of JAX-RS @Path annotation from the class - String pathValue = getJaxRsPathValue(type); - if (pathValue != null) { - // Class is annotated with @Path - // Display code lens only if local server is available. - if (!params.isCheckServerAvailable() - || isServerAvailable(LOCALHOST, params.getLocalServerPort(), PING_TIMEOUT)) { - // Loop for each method annotated with @Path to generate URL code lens per - // method. - collectURLCodeLenses(typeRoot, type.getChildren(), pathValue, lenses, params, utils, monitor); - } - } - continue; - } else if (element.getElementType() == IJavaElement.METHOD) { - if (utils.isHiddenGeneratedElement(element)) { - continue; - } - // ignore element if method range overlaps the type range, happens for generated - // bytcode, i.e. with lombok - IJavaElement parentType = element.getAncestor(IJavaElement.TYPE); - if (parentType != null && overlaps(((ISourceReference) parentType).getNameRange(), - ((ISourceReference) element).getNameRange())) { - continue; - } - } else {// neither a type nor a method, we bail - continue; - } - - // Here java element is a method - if (rootPath != null) { - IMethod method = (IMethod) element; - // A JAX-RS method is a public method annotated with @GET @POST, @DELETE, @PUT - // JAX-RS - // annotation - if (isJaxRsRequestMethod(method) && Flags.isPublic(method.getFlags())) { - CodeLens lens = createCodeLens(element, typeRoot, utils); - if (lens != null) { - String baseURL = params.getLocalBaseURL(); - String pathValue = getJaxRsPathValue(method); - String url = buildURL(baseURL, rootPath, pathValue); - String openURICommandId = params.getOpenURICommand(); - lens.setCommand(new Command(url, openURICommandId != null ? openURICommandId : "", - Collections.singletonList(url))); - lenses.add(lens); - } - } - } - } - } - - private static String buildURL(String... paths) { - StringBuilder url = new StringBuilder(); - for (String path : paths) { - if (path != null && !path.isEmpty()) { - if (!url.toString().isEmpty() && path.charAt(0) != '/' && url.charAt(url.length() - 1) != '/') { - url.append('/'); - } - url.append(path); - } - } - return url.toString(); - } - - private static String getJaxRsPathValue(IAnnotatable annotatable) throws JavaModelException { - IAnnotation annotationPath = getAnnotation(annotatable, JAVAX_WS_RS_PATH_ANNOTATION); - return annotationPath != null ? getAnnotationMemberValue(annotationPath, PATH_VALUE) : null; - } - - private static boolean isJaxRsRequestMethod(IAnnotatable annotatable) throws JavaModelException { - return hasAnnotation(annotatable, JAVAX_WS_RS_GET_ANNOTATION); - } - - private boolean overlaps(ISourceRange typeRange, ISourceRange methodRange) { - if (typeRange == null || methodRange == null) { - return false; - } - // method range is overlapping if it appears before or actually overlaps the - // type's range - return methodRange.getOffset() < typeRange.getOffset() || methodRange.getOffset() >= typeRange.getOffset() - && methodRange.getOffset() <= (typeRange.getOffset() + typeRange.getLength()); - } - - private static CodeLens createCodeLens(IJavaElement element, ITypeRoot typeRoot, IJDTUtils utils) - throws JavaModelException { - ISourceRange r = ((ISourceReference) element).getNameRange(); - if (r == null) { - return null; - } - CodeLens lens = new CodeLens(); - final Range range = utils.toRange(typeRoot, r.getOffset(), r.getLength()); - lens.setRange(range); - String uri = utils.toClientUri(utils.toUri(typeRoot)); - lens.setData(Arrays.asList(uri, range.getStart())); - return lens; - } - - private static boolean isServerAvailable(String host, int port, int timeout) { - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress(host, port), timeout); - return true; - } catch (IOException e) { - return false; - } - } - /** * Returns the hover information according to the given params * @@ -282,6 +150,9 @@ public Hover hover(MicroProfileJavaHoverParams params, IJDTUtils utils, IProgres if (hovers.isEmpty()) { return null; } + if (monitor.isCanceled()) { + return null; + } // TODO : aggregate the hover return hovers.get(0); } @@ -331,6 +202,9 @@ public List diagnostics(MicroProfileJavaDiagnosticsPar publishDiagnostics.add(publishDiagnostic); collectDiagnostics(uri, utils, documentFormat, diagnostics, monitor); } + if (monitor.isCanceled()) { + return Collections.emptyList(); + } return publishDiagnostics; } diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/AbtractJavaContext.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/AbtractJavaContext.java index 57e27fb1a..67036d53a 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/AbtractJavaContext.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/AbtractJavaContext.java @@ -11,6 +11,9 @@ *******************************************************************************/ package com.redhat.microprofile.jdt.core.java; +import java.util.HashMap; +import java.util.Map; + import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.ITypeRoot; @@ -30,6 +33,8 @@ public abstract class AbtractJavaContext { private final IJDTUtils utils; + private Map cache; + public AbtractJavaContext(String uri, ITypeRoot typeRoot, IJDTUtils utils) { this.uri = uri; this.typeRoot = typeRoot; @@ -51,4 +56,34 @@ public IJavaProject getJavaProject() { public IJDTUtils getUtils() { return utils; } + + + /** + * Associates the specified value with the specified key in the cache. + * + * @param key the key. + * @param value the value. + */ + public void put(String key, Object value) { + if (cache == null) { + cache = new HashMap<>(); + } + cache.put(key, value); + } + + /** + * Returns the value to which the specified key is mapped, or {@code null} if + * this map contains no mapping for the key. + * + * @param key the key. + * @return the value to which the specified key is mapped, or {@code null} if + * this map contains no mapping for the key. + */ + public Object get(String key) { + if (cache == null) { + return null; + } + return cache.get(key); + } + } diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/IJavaCodeLensParticipant.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/IJavaCodeLensParticipant.java new file mode 100644 index 000000000..3337eb430 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/IJavaCodeLensParticipant.java @@ -0,0 +1,77 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.core.java; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; + +/** + * Java codeLens participants API. + * + * @author Angelo ZERR + * + */ +public interface IJavaCodeLensParticipant { + + /** + * Returns true if codeLens must be collected for the given context and false + * otherwise. + * + *

+ * Collection is done by default. Participants can override this to check if + * some classes are on the classpath before deciding to process the collection. + *

+ * + * @param the java codeLens context + * @param monitor the progress monitor + * @return true if codeLens must be collected for the given context and false + * otherwise. + * + */ + default boolean isAdaptedForCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + return true; + } + + /** + * Begin codeLens collection. + * + * @param context the java codeLens context + * @param monitor the progress monitor + * + * @throws CoreException + */ + default void beginCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + + } + + /** + * Collect codeLens according to the context. + * + * @param context the java codeLens context + * @param monitor the progress monitor + * + * @throws CoreException + */ + void collectCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException; + + /** + * End codeLens collection. + * + * @param context the java codeLens context + * @param monitor the progress monitor + * + * @throws CoreException + */ + default void endCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + + } +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/JavaCodeLensContext.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/JavaCodeLensContext.java new file mode 100644 index 000000000..338c86d23 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/java/JavaCodeLensContext.java @@ -0,0 +1,49 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.core.java; + +import java.util.List; + +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.lsp4j.CodeLens; + +import com.redhat.microprofile.commons.MicroProfileJavaCodeLensParams; +import com.redhat.microprofile.jdt.core.utils.IJDTUtils; + +/** + * Java codeLens context for a given compilation unit. + * + * @author Angelo ZERR + * + */ +public class JavaCodeLensContext extends AbtractJavaContext { + + private final MicroProfileJavaCodeLensParams params; + + private final List codeLens; + + public JavaCodeLensContext(String uri, ITypeRoot typeRoot, IJDTUtils utils, MicroProfileJavaCodeLensParams params, + List codeLens) { + super(uri, typeRoot, utils); + this.params = params; + this.codeLens = codeLens; + } + + public MicroProfileJavaCodeLensParams getParams() { + return params; + } + + public List getCodeLens() { + return codeLens; + } + +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/jaxrs/JaxRsContext.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/jaxrs/JaxRsContext.java new file mode 100644 index 000000000..411ace297 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/jaxrs/JaxRsContext.java @@ -0,0 +1,50 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.core.jaxrs; + +import com.redhat.microprofile.jdt.core.java.JavaCodeLensContext; + +/** + * JAX-RS context. + * + * @author Angelo ZERR + * + */ +public class JaxRsContext { + + public static final int DEFAULT_PORT = 8080; + + private static final String CONTEXT_KEY = JaxRsContext.class.getName(); + + private int serverPort; + + public JaxRsContext() { + setServerPort(DEFAULT_PORT); + } + + public int getServerPort() { + return serverPort; + } + + public void setServerPort(int serverPort) { + this.serverPort = serverPort; + } + + public static JaxRsContext getJaxRsContext(JavaCodeLensContext context) { + JaxRsContext jaxRsContext = (JaxRsContext) context.get(CONTEXT_KEY); + if (jaxRsContext == null) { + jaxRsContext = new JaxRsContext(); + context.put(CONTEXT_KEY, jaxRsContext); + } + return jaxRsContext; + } +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProject.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProject.java index 68388eaa9..00ba4bc05 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProject.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/project/JDTMicroProfileProject.java @@ -32,10 +32,6 @@ public class JDTMicroProfileProject { private final List configSources; - private static final String QUARKUS_HTTP_PORT = "quarkus.http.port"; - - private static final int DEFAULT_PORT = 8080; - public JDTMicroProfileProject(IJavaProject javaProject) { this.configSources = new ArrayList(3); configSources.add(new YamlConfigSource(APPLICATION_YAML_FILE, javaProject)); @@ -43,15 +39,6 @@ public JDTMicroProfileProject(IJavaProject javaProject) { configSources.add(new PropertiesConfigSource(MICROPROFILE_CONFIG_PROPERTIES_FILE, javaProject)); } - /** - * Returns the server port. - * - * @return the server port. - */ - public int getServerPort() { - return getPropertyAsInteger(QUARKUS_HTTP_PORT, DEFAULT_PORT); - } - public String getProperty(String key, String defaultValue) { for (IConfigSource configSource : configSources) { String property = configSource.getProperty(key); diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeatureDefinition.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeatureDefinition.java index 4cbdc477c..9253891b1 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeatureDefinition.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeatureDefinition.java @@ -17,8 +17,10 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.lsp4j.Hover; +import com.redhat.microprofile.jdt.core.java.IJavaCodeLensParticipant; import com.redhat.microprofile.jdt.core.java.IJavaDiagnosticsParticipant; import com.redhat.microprofile.jdt.core.java.IJavaHoverParticipant; +import com.redhat.microprofile.jdt.core.java.JavaCodeLensContext; import com.redhat.microprofile.jdt.core.java.JavaDiagnosticsContext; import com.redhat.microprofile.jdt.core.java.JavaHoverContext; @@ -27,62 +29,65 @@ * *
    *
  • {@link IJavaHoverParticipant}.
  • + *
  • {@link IJavaCodeLensParticipant}.
  • *
  • {@link IJavaDiagnosticsParticipant}.
  • *
* */ -public class JavaFeatureDefinition implements IJavaHoverParticipant, IJavaDiagnosticsParticipant { +public class JavaFeatureDefinition + implements IJavaHoverParticipant, IJavaCodeLensParticipant, IJavaDiagnosticsParticipant { private static final Logger LOGGER = Logger.getLogger(JavaFeatureDefinition.class.getName()); - private final IJavaHoverParticipant hoverParticipant; + private final IJavaCodeLensParticipant codeLensParticipant; private final IJavaDiagnosticsParticipant diagnosticsParticipant; + private final IJavaHoverParticipant hoverParticipant; - public JavaFeatureDefinition(IJavaHoverParticipant hoverParticipant, - IJavaDiagnosticsParticipant diagnosticsParticipant) { - this.hoverParticipant = hoverParticipant; + public JavaFeatureDefinition(IJavaCodeLensParticipant codeLensParticipant, + IJavaDiagnosticsParticipant diagnosticsParticipant, IJavaHoverParticipant hoverParticipant) { + this.codeLensParticipant = codeLensParticipant; this.diagnosticsParticipant = diagnosticsParticipant; + this.hoverParticipant = hoverParticipant; } - // -------------- Hover + // -------------- CodeLens @Override - public boolean isAdaptedForHover(JavaHoverContext context, IProgressMonitor monitor) { - if (hoverParticipant == null) { + public boolean isAdaptedForCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) { + if (codeLensParticipant == null) { return false; } try { - return hoverParticipant.isAdaptedForHover(context, monitor); + return codeLensParticipant.isAdaptedForCodeLens(context, monitor); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error while calling isAdaptedForHover", e); + LOGGER.log(Level.SEVERE, "Error while calling isAdaptedForCodeLens", e); return false; } } @Override - public void beginHover(JavaHoverContext context, IProgressMonitor monitor) { + public void beginCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) { try { - hoverParticipant.beginHover(context, monitor); + codeLensParticipant.beginCodeLens(context, monitor); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error while calling beginHover", e); + LOGGER.log(Level.SEVERE, "Error while calling beginCodeLens", e); } } @Override - public Hover collectHover(JavaHoverContext context, IProgressMonitor monitor) { + public void collectCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) { try { - return hoverParticipant.collectHover(context, monitor); + codeLensParticipant.collectCodeLens(context, monitor); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error while collecting hover", e); - return null; + LOGGER.log(Level.SEVERE, "Error while collecting codeLens", e); } } @Override - public void endHover(JavaHoverContext context, IProgressMonitor monitor) { + public void endCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) { try { - hoverParticipant.endHover(context, monitor); + codeLensParticipant.endCodeLens(context, monitor); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error while calling endHover", e); + LOGGER.log(Level.SEVERE, "Error while calling endCodeLens", e); } } @@ -128,4 +133,47 @@ public void endDiagnostics(JavaDiagnosticsContext context, IProgressMonitor moni } } + // -------------- Hover + + @Override + public boolean isAdaptedForHover(JavaHoverContext context, IProgressMonitor monitor) { + if (hoverParticipant == null) { + return false; + } + try { + return hoverParticipant.isAdaptedForHover(context, monitor); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Error while calling isAdaptedForHover", e); + return false; + } + } + + @Override + public void beginHover(JavaHoverContext context, IProgressMonitor monitor) { + try { + hoverParticipant.beginHover(context, monitor); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Error while calling beginHover", e); + } + } + + @Override + public Hover collectHover(JavaHoverContext context, IProgressMonitor monitor) { + try { + return hoverParticipant.collectHover(context, monitor); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Error while collecting hover", e); + return null; + } + } + + @Override + public void endHover(JavaHoverContext context, IProgressMonitor monitor) { + try { + hoverParticipant.endHover(context, monitor); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Error while calling endHover", e); + } + } + } diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeaturesRegistry.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeaturesRegistry.java index eb161e5af..767b3b86d 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeaturesRegistry.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/java/JavaFeaturesRegistry.java @@ -22,6 +22,7 @@ import org.eclipse.core.runtime.Platform; import com.redhat.microprofile.jdt.core.MicroProfileCorePlugin; +import com.redhat.microprofile.jdt.core.java.IJavaCodeLensParticipant; import com.redhat.microprofile.jdt.core.java.IJavaDiagnosticsParticipant; import com.redhat.microprofile.jdt.core.java.IJavaHoverParticipant; @@ -33,6 +34,7 @@ public class JavaFeaturesRegistry { private static final String EXTENSION_JAVA_FEATURE_PARTICIPANTS = "javaFeatureParticipants"; + private static final String CODELENS_ELT = "codeLens"; private static final String DIAGNOSTICS_ELT = "diagnostics"; private static final String HOVER_ELT = "hover"; private static final String CLASS_ATTR = "class"; @@ -82,8 +84,10 @@ private void addJavaFeatureDefinition(IConfigurationElement[] cf) { for (IConfigurationElement ce : cf) { try { JavaFeatureDefinition definition = createDefinition(ce); - synchronized (javaFeatureDefinitions) { - this.javaFeatureDefinitions.add(definition); + if (definition != null) { + synchronized (javaFeatureDefinitions) { + this.javaFeatureDefinitions.add(definition); + } } } catch (Throwable t) { LOGGER.log(Level.SEVERE, "Error while collecting java features extension contributions", t); @@ -93,13 +97,17 @@ private void addJavaFeatureDefinition(IConfigurationElement[] cf) { private static JavaFeatureDefinition createDefinition(IConfigurationElement ce) throws CoreException { switch (ce.getName()) { - case HOVER_ELT: - IJavaHoverParticipant hoverParticipant = (IJavaHoverParticipant) ce.createExecutableExtension(CLASS_ATTR); - return new JavaFeatureDefinition(hoverParticipant, null); + case CODELENS_ELT: + IJavaCodeLensParticipant codeLensParticipant = (IJavaCodeLensParticipant) ce + .createExecutableExtension(CLASS_ATTR); + return new JavaFeatureDefinition(codeLensParticipant, null, null); case DIAGNOSTICS_ELT: IJavaDiagnosticsParticipant diagnosticsParticipant = (IJavaDiagnosticsParticipant) ce .createExecutableExtension(CLASS_ATTR); - return new JavaFeatureDefinition(null, diagnosticsParticipant); + return new JavaFeatureDefinition(null, diagnosticsParticipant, null); + case HOVER_ELT: + IJavaHoverParticipant hoverParticipant = (IJavaHoverParticipant) ce.createExecutableExtension(CLASS_ATTR); + return new JavaFeatureDefinition(null, null, hoverParticipant); default: return null; } diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/MicroProfileFaultToleranceConstants.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/MicroProfileFaultToleranceConstants.java index 27cadd105..6ae13b233 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/MicroProfileFaultToleranceConstants.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/MicroProfileFaultToleranceConstants.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/properties/MicroProfileFaultToleranceProvider.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/properties/MicroProfileFaultToleranceProvider.java index 218b8855d..37c74cee7 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/properties/MicroProfileFaultToleranceProvider.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/faulttolerance/properties/MicroProfileFaultToleranceProvider.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/MicroProfileHealthConstants.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/MicroProfileHealthConstants.java index 39a1fe4e3..3fefa5509 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/MicroProfileHealthConstants.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/MicroProfileHealthConstants.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthDiagnosticsParticipant.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthDiagnosticsParticipant.java index e723f0640..494e0bc0b 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthDiagnosticsParticipant.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthDiagnosticsParticipant.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthErrorCode.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthErrorCode.java index 0ead5f1c0..9b940182f 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthErrorCode.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/health/java/MicroProfileHealthErrorCode.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/JaxRsConstants.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/JaxRsConstants.java new file mode 100644 index 000000000..d0d8f225a --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/JaxRsConstants.java @@ -0,0 +1,31 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.internal.jaxrs; + +/** + * JAX-RS constants + * + * @author Angelo ZERR + * + */ +public class JaxRsConstants { + + public JaxRsConstants() { + } + + public static final String JAVAX_WS_RS_PATH_ANNOTATION = "javax.ws.rs.Path"; + + public static final String JAVAX_WS_RS_GET_ANNOTATION = "javax.ws.rs.GET"; + + public static final String PATH_VALUE = "value"; + +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/java/JaxRsCodeLensParticipant.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/java/JaxRsCodeLensParticipant.java new file mode 100644 index 000000000..ee980b5ce --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/jaxrs/java/JaxRsCodeLensParticipant.java @@ -0,0 +1,200 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.internal.jaxrs.java; + +import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotation; +import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotationMemberValue; +import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.hasAnnotation; +import static com.redhat.microprofile.jdt.internal.jaxrs.JaxRsConstants.JAVAX_WS_RS_GET_ANNOTATION; +import static com.redhat.microprofile.jdt.internal.jaxrs.JaxRsConstants.JAVAX_WS_RS_PATH_ANNOTATION; +import static com.redhat.microprofile.jdt.internal.jaxrs.JaxRsConstants.PATH_VALUE; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.Flags; +import org.eclipse.jdt.core.IAnnotatable; +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.ISourceRange; +import org.eclipse.jdt.core.ISourceReference; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.lsp4j.CodeLens; +import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.Range; + +import com.redhat.microprofile.commons.MicroProfileJavaCodeLensParams; +import com.redhat.microprofile.jdt.core.java.IJavaCodeLensParticipant; +import com.redhat.microprofile.jdt.core.java.JavaCodeLensContext; +import com.redhat.microprofile.jdt.core.jaxrs.JaxRsContext; +import com.redhat.microprofile.jdt.core.utils.IJDTUtils; +import com.redhat.microprofile.jdt.core.utils.JDTTypeUtils; + +/** + * + * JAX-RS CodeLens participant + * + * @author Angelo ZERR + * + */ +public class JaxRsCodeLensParticipant implements IJavaCodeLensParticipant { + + private static final String LOCALHOST = "localhost"; + + private static final int PING_TIMEOUT = 2000; + + @Override + public boolean isAdaptedForCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + MicroProfileJavaCodeLensParams params = context.getParams(); + if (!params.isUrlCodeLensEnabled()) { + return false; + } + // Collection of URL codeLens is done only if JAX-RS is on the classpath + IJavaProject javaProject = context.getJavaProject(); + return JDTTypeUtils.findType(javaProject, JAVAX_WS_RS_PATH_ANNOTATION) != null; + } + + @Override + public void collectCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + ITypeRoot typeRoot = context.getTypeRoot(); + IJavaElement[] elements = typeRoot.getChildren(); + int serverPort = JaxRsContext.getJaxRsContext(context).getServerPort(); + IJDTUtils utils = context.getUtils(); + MicroProfileJavaCodeLensParams params = context.getParams(); + params.setLocalServerPort(serverPort); + collectURLCodeLenses(typeRoot, elements, null, context.getCodeLens(), params, utils, monitor); + } + + private static void collectURLCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, String rootPath, + Collection lenses, MicroProfileJavaCodeLensParams params, IJDTUtils utils, + IProgressMonitor monitor) throws JavaModelException { + for (IJavaElement element : elements) { + if (monitor.isCanceled()) { + return; + } + if (element.getElementType() == IJavaElement.TYPE) { + IType type = (IType) element; + // Get value of JAX-RS @Path annotation from the class + String pathValue = getJaxRsPathValue(type); + if (pathValue != null) { + // Class is annotated with @Path + // Display code lens only if local server is available. + if (!params.isCheckServerAvailable() + || isServerAvailable(LOCALHOST, params.getLocalServerPort(), PING_TIMEOUT)) { + // Loop for each method annotated with @Path to generate URL code lens per + // method. + collectURLCodeLenses(typeRoot, type.getChildren(), pathValue, lenses, params, utils, monitor); + } + } + continue; + } else if (element.getElementType() == IJavaElement.METHOD) { + if (utils.isHiddenGeneratedElement(element)) { + continue; + } + // ignore element if method range overlaps the type range, happens for generated + // bytcode, i.e. with lombok + IJavaElement parentType = element.getAncestor(IJavaElement.TYPE); + if (parentType != null && overlaps(((ISourceReference) parentType).getNameRange(), + ((ISourceReference) element).getNameRange())) { + continue; + } + } else {// neither a type nor a method, we bail + continue; + } + + // Here java element is a method + if (rootPath != null) { + IMethod method = (IMethod) element; + // A JAX-RS method is a public method annotated with @GET @POST, @DELETE, @PUT + // JAX-RS + // annotation + if (isJaxRsRequestMethod(method) && Flags.isPublic(method.getFlags())) { + CodeLens lens = createURLCodeLens(element, typeRoot, utils); + if (lens != null) { + String baseURL = params.getLocalBaseURL(); + String pathValue = getJaxRsPathValue(method); + String url = buildURL(baseURL, rootPath, pathValue); + String openURICommandId = params.getOpenURICommand(); + lens.setCommand(new Command(url, openURICommandId != null ? openURICommandId : "", + Collections.singletonList(url))); + lenses.add(lens); + } + } + } + } + } + + private static String buildURL(String... paths) { + StringBuilder url = new StringBuilder(); + for (String path : paths) { + if (path != null && !path.isEmpty()) { + if (!url.toString().isEmpty() && path.charAt(0) != '/' && url.charAt(url.length() - 1) != '/') { + url.append('/'); + } + url.append(path); + } + } + return url.toString(); + } + + private static String getJaxRsPathValue(IAnnotatable annotatable) throws JavaModelException { + IAnnotation annotationPath = getAnnotation(annotatable, JAVAX_WS_RS_PATH_ANNOTATION); + return annotationPath != null ? getAnnotationMemberValue(annotationPath, PATH_VALUE) : null; + } + + private static boolean isJaxRsRequestMethod(IAnnotatable annotatable) throws JavaModelException { + return hasAnnotation(annotatable, JAVAX_WS_RS_GET_ANNOTATION); + } + + private static boolean overlaps(ISourceRange typeRange, ISourceRange methodRange) { + if (typeRange == null || methodRange == null) { + return false; + } + // method range is overlapping if it appears before or actually overlaps the + // type's range + return methodRange.getOffset() < typeRange.getOffset() || methodRange.getOffset() >= typeRange.getOffset() + && methodRange.getOffset() <= (typeRange.getOffset() + typeRange.getLength()); + } + + private static CodeLens createURLCodeLens(IJavaElement element, ITypeRoot typeRoot, IJDTUtils utils) + throws JavaModelException { + ISourceRange r = ((ISourceReference) element).getNameRange(); + if (r == null) { + return null; + } + CodeLens lens = new CodeLens(); + final Range range = utils.toRange(typeRoot, r.getOffset(), r.getLength()); + lens.setRange(range); + String uri = utils.toClientUri(utils.toUri(typeRoot)); + lens.setData(Arrays.asList(uri, range.getStart())); + return lens; + } + + private static boolean isServerAvailable(String host, int port, int timeout) { + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(host, port), timeout); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/MicroProfileRestClientConstants.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/MicroProfileRestClientConstants.java index f338a6990..079bd75b2 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/MicroProfileRestClientConstants.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/MicroProfileRestClientConstants.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/java/MicroProfileRestClientDiagnosticsParticipant.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/java/MicroProfileRestClientDiagnosticsParticipant.java index c5b27ec47..7b6239eb0 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/java/MicroProfileRestClientDiagnosticsParticipant.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/java/MicroProfileRestClientDiagnosticsParticipant.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/properties/MicroProfileRegisterRestClientProvider.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/properties/MicroProfileRegisterRestClientProvider.java index c34a29216..4159df890 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/properties/MicroProfileRegisterRestClientProvider.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/restclient/properties/MicroProfileRegisterRestClientProvider.java @@ -4,6 +4,8 @@ * 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 *******************************************************************************/ diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/plugin.xml b/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/plugin.xml index 798f38bb2..75bba0f2b 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/plugin.xml +++ b/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/plugin.xml @@ -11,5 +11,12 @@ - + + + + + + + + diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/src/main/java/com/redhat/microprofile/jdt/internal/quarkus/jaxrs/java/QuarkusJaxRsCodeLensParticipant.java b/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/src/main/java/com/redhat/microprofile/jdt/internal/quarkus/jaxrs/java/QuarkusJaxRsCodeLensParticipant.java new file mode 100644 index 000000000..50deb7b2a --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.quarkus/src/main/java/com/redhat/microprofile/jdt/internal/quarkus/jaxrs/java/QuarkusJaxRsCodeLensParticipant.java @@ -0,0 +1,51 @@ +/******************************************************************************* +* Copyright (c) 2020 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.jdt.internal.quarkus.jaxrs.java; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IJavaProject; + +import com.redhat.microprofile.jdt.core.java.IJavaCodeLensParticipant; +import com.redhat.microprofile.jdt.core.java.JavaCodeLensContext; +import com.redhat.microprofile.jdt.core.jaxrs.JaxRsContext; +import com.redhat.microprofile.jdt.core.project.JDTMicroProfileProject; +import com.redhat.microprofile.jdt.core.project.JDTMicroProfileProjectManager; + +/** + * + * Quarkus JAX-RS CodeLens participant used to update the server port declared + * with "quarkus.http.port" property. + * + * @author Angelo ZERR + * + */ +public class QuarkusJaxRsCodeLensParticipant implements IJavaCodeLensParticipant { + + private static final String QUARKUS_HTTP_PORT = "quarkus.http.port"; + + @Override + public void beginCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + // Update the JAX-RS server port from the declared quarkus property + // "quarkus.http.port" + IJavaProject javaProject = context.getJavaProject(); + JDTMicroProfileProject mpProject = JDTMicroProfileProjectManager.getInstance() + .getJDTMicroProfileProject(javaProject); + int serverPort = mpProject.getPropertyAsInteger(QUARKUS_HTTP_PORT, JaxRsContext.DEFAULT_PORT); + JaxRsContext.getJaxRsContext(context).setServerPort(serverPort); + } + + @Override + public void collectCodeLens(JavaCodeLensContext context, IProgressMonitor monitor) throws CoreException { + // Do nothing + } +}