Skip to content

Commit

Permalink
fix: show @scheduled methods as implicitly used
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Oct 16, 2023
1 parent f1f19b8 commit 2dd6d5a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class QuarkusConstants {

public static final String QUARKUS_DEPLOYMENT_BUILDSTEP_ANNOTATION = "io.quarkus.deployment.annotations.BuildStep";

public static final String QUARKUS_SCHEDULED_ANNOTATION = "io.quarkus.scheduler.Scheduled";

public static final String QUARKUS_CORE_PREFIX = "io.quarkus:quarkus-core:";
public static final String QUARKUS_PREFIX = "quarkus";
public static final String QUARKUS_JAVADOC_PROPERTIES_FILE = "quarkus-javadoc.properties";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2023 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.devtools.intellij.quarkus.psi.internal;

import com.intellij.codeInsight.daemon.ImplicitUsageProvider;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.metrics.MicroProfileMetricsConstants;
import org.jetbrains.annotations.NotNull;

import static com.redhat.devtools.intellij.quarkus.QuarkusConstants.QUARKUS_DEPLOYMENT_BUILDSTEP_ANNOTATION;
import static com.redhat.devtools.intellij.quarkus.QuarkusConstants.QUARKUS_SCHEDULED_ANNOTATION;

/**
* Automatically declares as used, methods annotated with {@link io.quarkus.scheduler.Scheduled} annotations, on <code>@ApplicationScoped</code>-annotated classes.
* <p>
* {@code
* @Scheduled(cron="0 15 10 * * ?")
* void cronJob(ScheduledExecution execution) {
* counter.incrementAndGet();
* System.out.println(execution.getScheduledFireTime());
* }
* }
* </p>
*/
public class ScheduledImplicitUsageProvider implements ImplicitUsageProvider {

@Override
public boolean isImplicitUsage(@NotNull PsiElement element) {
return isImplicitRead(element) || isImplicitWrite(element);
}

@Override
public boolean isImplicitRead(@NotNull PsiElement element) {
if (!(element instanceof PsiMethod)) {
return false;
}
PsiMethod method = (PsiMethod) element;
return !method.isConstructor() &&
AnnotationUtils.hasAnyAnnotation(element, QUARKUS_SCHEDULED_ANNOTATION) &&
isClassApplicationScoped(method.getContainingClass());
}

private boolean isClassApplicationScoped(PsiClass containingClass) {
return (containingClass != null) && AnnotationUtils.hasAnyAnnotation(containingClass, MicroProfileMetricsConstants.APPLICATION_SCOPED_JAKARTA_ANNOTATION,
MicroProfileMetricsConstants.APPLICATION_SCOPED_JAVAX_ANNOTATION);
}

@Override
public boolean isImplicitWrite(@NotNull PsiElement element) {
return false;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/lsp4ij-quarkus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

<implicitUsageProvider implementation="com.redhat.devtools.intellij.lsp4mp4ij.psi.core.JavaEEImplicitUsageProvider"/>
<implicitUsageProvider implementation="com.redhat.devtools.intellij.quarkus.psi.internal.QuarkusBuildImplicitUsageProvider"/>
<implicitUsageProvider implementation="com.redhat.devtools.intellij.quarkus.psi.internal.ScheduledImplicitUsageProvider"/>
<implicitUsageProvider implementation="com.redhat.microprofile.psi.internal.quarkus.route.java.ReactiveRouteImplicitUsageProvider"/>

<!-- Quarkus -->
Expand Down

0 comments on commit 2dd6d5a

Please sign in to comment.