Skip to content

Commit

Permalink
Use jakarta imports in snippets when possible
Browse files Browse the repository at this point in the history
If the jakarta versions of the javax classes referenced in the current
snippets are on the classpath, hide the old snippet and display a new
snippet that imports the jakarta classes instead.

Closes eclipse#229

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 7, 2022
1 parent 6680ba6 commit 0c5b5b3
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ private synchronized List<String> collectTypes() {
}
}
}
List<String> snippetExclusionTypes = ((SnippetContextForJava) snippet.getContext()).getExcludedTypes();
if (snippetExclusionTypes != null) {
for (String snippetExclusionType : snippetExclusionTypes) {
if (!types.contains(snippetExclusionType)) {
types.add(snippetExclusionType);
}
}
}
}
}
return types;
Expand All @@ -92,7 +100,7 @@ public void registerSnippet(Snippet snippet) {

/**
* Preprocess Snippet body for managing package name.
*
*
* @param snippet
*/
private void preprocessSnippetBody(Snippet snippet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,46 @@
public class SnippetContextForJava implements ISnippetContext<ProjectLabelInfoEntry> {

public static final TypeAdapter<SnippetContextForJava> TYPE_ADAPTER = new SnippetContextForJavaAdapter();

/**
* A list of all types whose presence indicates the snippet should be displayed.
*/
private List<String> types;

public SnippetContextForJava(List<String> types) {
/**
* A list of all types whose presence indicates the snippet shouldn't be
* displayed.
*
* Takes precedence over {@link #types} i.e. if a type is present in
* {@link #exclusionTypes}, then the snippet won't be shown, and {@link #types} won't be considered.
*/
private List<String> exclusionTypes;

public SnippetContextForJava(List<String> types, List<String> exclusionTypes) {
this.types = types;
this.exclusionTypes = exclusionTypes;
}

public List<String> getTypes() {
return types;
}

public List<String> getExcludedTypes() {
return this.exclusionTypes;
}

@Override
public boolean isMatch(ProjectLabelInfoEntry context) {
if (context == null) {
return true;
}
if (exclusionTypes != null && !exclusionTypes.isEmpty()) {
for (String type : exclusionTypes) {
if (context.hasLabel(type)) {
return false;
}
}
}
if (types == null || types.isEmpty()) {
return true;
}
Expand All @@ -70,6 +95,7 @@ public SnippetContextForJava read(final JsonReader in) throws IOException {
}

List<String> types = new ArrayList<>();
List<String> exclusionTypes = new ArrayList<>();
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
Expand All @@ -85,12 +111,23 @@ public SnippetContextForJava read(final JsonReader in) throws IOException {
types.add(in.nextString());
}
break;
case "exclusionType":
if (in.peek() == JsonToken.BEGIN_ARRAY) {
in.beginArray();
while (in.peek() != JsonToken.END_ARRAY) {
exclusionTypes.add(in.nextString());
}
in.endArray();
} else {
exclusionTypes.add(in.nextString());
}
break;
default:
in.skipValue();
}
}
in.endObject();
return new SnippetContextForJava(types);
return new SnippetContextForJava(types, exclusionTypes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,36 @@
],
"description": "JAX-RS REST resource class",
"context": {
"type": "javax.ws.rs.GET"
"type": "javax.ws.rs.GET",
"exclusionType": "jakarta.ws.rs.GET"
}
},
"JAX-RS - new resource method": {
"Jakarta RESTful Web Services - new resource class": {
"prefix": "rest_class",
"body": [
"package ${1:packagename};",
"",
"import jakarta.ws.rs.GET;",
"import jakarta.ws.rs.Path;",
"import jakarta.ws.rs.Produces;",
"import jakarta.ws.rs.core.MediaType;",
"",
"@Path(\"${2:/path}\")",
"public class ${TM_FILENAME_BASE} {",
"",
"\t@GET",
"\t@Produces(MediaType.TEXT_PLAIN)",
"\tpublic String ${3:methodname}() {",
"\t\treturn \"hello\";",
"\t}",
"}"
],
"description": "Jakarta REST resource class",
"context": {
"type": "jakarta.ws.rs.GET"
}
},
"JAX-RS - new GET resource method": {
"prefix": "jaxrm",
"body": [
"@GET",
Expand All @@ -35,7 +61,22 @@
],
"description": "JAX-RS REST resource method",
"context": {
"type": "javax.ws.rs.GET"
"type": "javax.ws.rs.GET",
"exclusionType": "jakarta.ws.rs.GET"
}
},
"Jakarta REST - new GET resource method": {
"prefix": "rest_get",
"body": [
"@GET",
"@Produces(MediaType.TEXT_PLAIN)",
"public String ${1:methodname}() {",
"\treturn \"hello\";",
"}"
],
"description": "Jakarta REST GET resource method",
"context": {
"type": "jakarta.ws.rs.GET"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"MicroProfile readiness check": {
"MicroProfile readiness check (javax)": {
"prefix": "mpreadiness",
"body": [
"package ${1:packagename};",
Expand All @@ -21,11 +21,41 @@
"}"
],
"context": {
"type": "org.eclipse.microprofile.health.Readiness"
"type": "org.eclipse.microprofile.health.Readiness",
"exclusionType": "jakarta.enterprise.context.ApplicationScoped"
},
"description": "MicroProfile Health readiness check"
},
"MicroProfile liveness check": {
"MicroProfile readiness check": {
"prefix": "mpreadiness",
"body": [
"package ${1:packagename};",
"",
"import org.eclipse.microprofile.health.HealthCheck;",
"import org.eclipse.microprofile.health.HealthCheckResponse;",
"import org.eclipse.microprofile.health.Readiness;",
"",
"import jakarta.enterprise.context.ApplicationScoped;",
"",
"@Readiness",
"@ApplicationScoped",
"public class ${TM_FILENAME_BASE} implements HealthCheck {",
"",
"\t@Override",
"\tpublic HealthCheckResponse call() {",
"\t\treturn HealthCheckResponse.named(${TM_FILENAME_BASE}.class.getSimpleName()).withData(\"ready\",true).up().build();",
"\t}",
"}"
],
"context": {
"type": [
"org.eclipse.microprofile.health.Readiness",
"jakarta.enterprise.context.ApplicationScoped"
]
},
"description": "MicroProfile Health readiness check"
},
"MicroProfile liveness check (javax)": {
"prefix": "mpliveness",
"body": [
"package ${1:packagename};",
Expand All @@ -47,7 +77,37 @@
"}"
],
"context": {
"type": "org.eclipse.microprofile.health.Liveness"
"type": "org.eclipse.microprofile.health.Liveness",
"exclusionType": "jakarta.enterprise.context.ApplicationScoped"
},
"description": "MicroProfile Health liveness check"
},
"MicroProfile liveness check": {
"prefix": "mpliveness",
"body": [
"package ${1:packagename};",
"",
"import org.eclipse.microprofile.health.HealthCheck;",
"import org.eclipse.microprofile.health.HealthCheckResponse;",
"import org.eclipse.microprofile.health.Liveness;",
"",
"import jakarta.enterprise.context.ApplicationScoped;",
"",
"@Liveness",
"@ApplicationScoped",
"public class ${TM_FILENAME_BASE} implements HealthCheck {",
"",
"\t@Override",
"\tpublic HealthCheckResponse call() {",
"\t\treturn HealthCheckResponse.named(${TM_FILENAME_BASE}.class.getSimpleName()).withData(\"live\",true).up().build();",
"\t}",
"}"
],
"context": {
"type": [
"org.eclipse.microprofile.health.Liveness",
"jakarta.enterprise.context.ApplicationScoped"
]
},
"description": "MicroProfile Health liveness check"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"MicroProfile - new rest client": {
"MicroProfile - new rest client (javax)": {
"prefix": "mpnrc",
"body": [
"package ${1:packagename};",
Expand All @@ -24,7 +24,39 @@
],
"description": "MicroProfile - new rest client",
"context": {
"type": "org.eclipse.microprofile.rest.client.inject.RegisterRestClient"
"type": "org.eclipse.microprofile.rest.client.inject.RegisterRestClient",
"exclusionType": "jakarta.ws.rs.GET"
}
},
"MicroProfile - new rest client": {
"prefix": "mpnrc",
"body": [
"package ${1:packagename};",
"",
"import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;",
"",
"import jakarta.enterprise.context.ApplicationScoped;",
"import jakarta.ws.rs.GET;",
"import jakarta.ws.rs.Path;",
"import jakarta.ws.rs.PathParam;",
"",
"@RegisterRestClient",
"@ApplicationScoped",
"public interface ${TM_FILENAME_BASE} {",
"",
"\t@GET",
"\t@Path(\"${2:/path}\")",
"\tString ${3:methodname}();",
"",
"}",
""
],
"description": "MicroProfile - new rest client",
"context": {
"type": [
"org.eclipse.microprofile.rest.client.inject.RegisterRestClient",
"jakarta.ws.rs.GET"
]
}
},
"MicroProfile - inject rest client": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
Expand Down Expand Up @@ -33,7 +33,7 @@

/**
* Test for Java snippet registry.
*
*
* @author Angelo ZERR
*
*/
Expand Down Expand Up @@ -62,6 +62,36 @@ public void jaxrsSnippets() {
ProjectLabelInfoEntry projectInfo2 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET"));
boolean match2 = ((SnippetContextForJava) context).isMatch(projectInfo2);
assertTrue("Project has javax.ws.rs.GET type", match2);

ProjectLabelInfoEntry projectInfo3 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET", "jakarta.ws.rs.GET"));
boolean match3= ((SnippetContextForJava) context).isMatch(projectInfo3);
assertFalse("Project has javax.ws.rs.GET and jakarta.ws.rs.GET types, so the snippet won't display", match3);
}

@Test
public void jakartaRESTSnippets() {
Optional<Snippet> jakartaRESTResourceClassSnippet = findByPrefix("rest_class", registry);
assertTrue("Tests has rest_class Java snippets", jakartaRESTResourceClassSnippet.isPresent());

ISnippetContext<?> context = jakartaRESTResourceClassSnippet.get().getContext();
assertNotNull("rest_class snippet has context", context);
assertTrue("rest_class snippet context is Java context", context instanceof SnippetContextForJava);

ProjectLabelInfoEntry projectInfo = new ProjectLabelInfoEntry("", "", new ArrayList<>());
boolean match = ((SnippetContextForJava) context).isMatch(projectInfo);
assertFalse("Project has neither javax.ws.rs.GET nor jakarta.ws.rs.GET", match);

ProjectLabelInfoEntry projectInfo2 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET"));
boolean match2 = ((SnippetContextForJava) context).isMatch(projectInfo2);
assertFalse("Project has javax.ws.rs.GET", match2);

ProjectLabelInfoEntry projectInfo3 = new ProjectLabelInfoEntry("", "", Arrays.asList("jakarta.ws.rs.GET"));
boolean match3 = ((SnippetContextForJava) context).isMatch(projectInfo3);
assertTrue("Project has jakarta.ws.rs.GET", match3);

ProjectLabelInfoEntry projectInfo4 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET", "jakarta.ws.rs.GET"));
boolean match4 = ((SnippetContextForJava) context).isMatch(projectInfo4);
assertTrue("Project has javax.ws.rs.GET and jakarta.ws.rs.GET types", match4);
}

@Test
Expand Down

0 comments on commit 0c5b5b3

Please sign in to comment.