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, closes eclipse#320

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 7, 2022
1 parent 6680ba6 commit 17b0c22
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 53 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 @@ -38,7 +38,7 @@ public void load(SnippetRegistry registry) throws IOException {
SnippetContextForJava.TYPE_ADAPTER);
registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("mp-health.json"),
SnippetContextForJava.TYPE_ADAPTER);
registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("jax-rs.json"),
registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("rest.json"),
SnippetContextForJava.TYPE_ADAPTER);

}
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

This file was deleted.

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
@@ -0,0 +1,82 @@
{
"JAX-RS - new resource class": {
"prefix": "jaxrc",
"body": [
"package ${1:packagename};",
"",
"import javax.ws.rs.GET;",
"import javax.ws.rs.Path;",
"import javax.ws.rs.Produces;",
"import javax.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": "JAX-RS REST resource class",
"context": {
"type": "javax.ws.rs.GET",
"exclusionType": "jakarta.ws.rs.GET"
}
},
"Jakarta REST - 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",
"@Produces(MediaType.TEXT_PLAIN)",
"public String ${1:methodname}() {",
"\treturn \"hello\";",
"}"
],
"description": "JAX-RS REST resource method",
"context": {
"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"
}
}
}
Loading

0 comments on commit 17b0c22

Please sign in to comment.