Skip to content

Commit

Permalink
feat: Validate if a BuildItem is final or abstract
Browse files Browse the repository at this point in the history
Fixes redhat-developer#926

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jul 29, 2024
1 parent 009ea17 commit 89fdddf
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
.flattened-pom.xml

# Eclipse
#.project
#.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode
.factorypath

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Local environment
.env

# Plugin directory
/.quarkus/cli/plugins/
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>quarkus-builditems</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>quarkus-builditems</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.11.0</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.3.1</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.1.2</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.acme.builditems;

import io.quarkus.builder.item.MultiBuildItem;

public class BadBuildItem extends MultiBuildItem {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.acme.builditems;

import io.quarkus.builder.item.SimpleBuildItem;

public final class GoodBuildItem extends SimpleBuildItem {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class QuarkusMavenProjectName extends MicroProfileMavenProjectName {
public static String scheduler_quickstart = "scheduler-quickstart";
public static String scheduler_diagnostic = "scheduler-diagnostic";
public static final String quarkus_route = "quarkus-route";

public static final String quarkus_builditems = "quarkus-builditems";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.redhat.microprofile.jdt.quarkus.builditems;

import static org.eclipse.lsp4mp.jdt.core.MicroProfileForJavaAssert.assertJavaDiagnostics;
import static org.eclipse.lsp4mp.jdt.core.MicroProfileForJavaAssert.d;

import java.util.Arrays;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4mp.commons.DocumentFormat;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams;
import org.eclipse.lsp4mp.jdt.core.BasePropertiesManagerTest;
import org.junit.Test;

import com.redhat.microprofile.jdt.internal.quarkus.QuarkusConstants;
import com.redhat.microprofile.jdt.internal.quarkus.builditems.QuarkusBuildItemErrorCode;
import com.redhat.microprofile.jdt.quarkus.QuarkusMavenProjectName;

public class QuarkusBuildItemASTVisitorTest extends BasePropertiesManagerTest {

@Test
public void buildItemClassifier() throws Exception {
IJavaProject javaProject = loadMavenProject(QuarkusMavenProjectName.quarkus_builditems);

{ // Bad BuildItem shows error
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
IFile javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/builditems/BadBuildItem.java"));
String uri = javaFile.getLocation().toFile().toURI().toASCIIString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);

Diagnostic d = d(4, 13, 25,
"BuildItem class `org.acme.builditems.BadBuildItem` must either be declared final or abstract",
DiagnosticSeverity.Error, QuarkusConstants.QUARKUS_DIAGNOSTIC_SOURCE,
QuarkusBuildItemErrorCode.InvalidModifierBuildItem);
assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d);
}

{ // Good BuildItem shows no error
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
IFile javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/builditems/GoodBuildItem.java"));
String uri = javaFile.getLocation().toFile().toURI().toASCIIString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);
assertJavaDiagnostics(diagnosticsParams, JDT_UTILS);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Export-Package: com.redhat.microprofile.jdt.internal.quarkus;x-friends:="com.red
com.redhat.microprofile.jdt.internal.quarkus.providers;x-friends:="com.redhat.microprofile.jdt.quarkus.test",
com.redhat.microprofile.jdt.internal.quarkus.renarde.java;x-friends:="com.redhat.microprofile.jdt.quarkus.test",
com.redhat.microprofile.jdt.internal.quarkus.scheduler;x-friends:="com.redhat.microprofile.jdt.quarkus.test",
com.redhat.microprofile.jdt.internal.quarkus.builditems;x-friends:="com.redhat.microprofile.jdt.quarkus.test",
com.redhat.microprofile.jdt.internal.quarkus.utils;x-friends:="com.redhat.microprofile.jdt.quarkus.test",
com.redhat.microprofile.jdt.quarkus,
org.yaml.snakeyaml;x-friends:="com.redhat.microprofile.jdt.quarkus.test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
<provider class="com.redhat.microprofile.jdt.internal.quarkus.core.properties.QuarkusConfigMappingProvider" />
</extension>

<!-- Quarkus Builditems support -->

<extension point="org.eclipse.lsp4mp.jdt.core.javaASTValidators">
<!-- Java validation for BuildItem: checks if the BuildItem is final or abstract-->
<validator class="com.redhat.microprofile.jdt.internal.quarkus.builditems.java.QuarkusBuildItemASTVisitor" />
</extension>

<extension point="org.eclipse.lsp4mp.jdt.core.javaASTValidators">
<!-- Java validation for the Quarkus @ConfigMapping annotation -->
<validator class="com.redhat.microprofile.jdt.internal.quarkus.core.java.QuarkusConfigMappingASTVisitor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public class QuarkusConstants {
public static final String SCHEDULED_ANNOTATION_DELAY_UNIT = "delayUnit";
public static final String SCHEDULED_ANNOTATION_DELAYED = "delayed";

public static final String[] SCHEDULED_SUPPORTED_PARTICIPANT_MEMBERS = {
SCHEDULED_ANNOTATION_CRON, SCHEDULED_ANNOTATION_EVERY, SCHEDULED_ANNOTATION_DELAY,
SCHEDULED_ANNOTATION_DELAYED, SCHEDULED_ANNOTATION_DELAY_UNIT
};
public static final String[] SCHEDULED_SUPPORTED_PARTICIPANT_MEMBERS = { SCHEDULED_ANNOTATION_CRON,
SCHEDULED_ANNOTATION_EVERY, SCHEDULED_ANNOTATION_DELAY, SCHEDULED_ANNOTATION_DELAYED,
SCHEDULED_ANNOTATION_DELAY_UNIT };
public static final String SCHEDULED_ANNOTATION_CONCURRENT_EXECUTION = "concurrentExecution";
public static final String SCHEDULED_ANNOTATION_SKIP_EXECUTION_IF = "skipExecutionIf";

Expand Down Expand Up @@ -132,6 +131,10 @@ public class QuarkusConstants {

public static final String QUARKUS_RUNTIME_CLASS_NAME = "io.quarkus.runtime.LaunchMode";

public static final String QUARKUS_BUILD_ITEM_CLASS_NAME = "io.quarkus.builder.item.BuildItem";

public static final String QUARKUS_DIAGNOSTIC_SOURCE = "quarkus";

private QuarkusConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2024 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.builditems;

import org.eclipse.lsp4mp.jdt.core.java.diagnostics.IJavaErrorCode;

/**
* Represents error codes for validation issues in classes inheriting
* <code>io.quarkus.builder.item.BuildItem</code>.
*/
public enum QuarkusBuildItemErrorCode implements IJavaErrorCode {

InvalidModifierBuildItem;

@Override
public String getCode() {
return name();
}
}
Loading

0 comments on commit 89fdddf

Please sign in to comment.