From 4ecab363053ea2085e18334706d745531f4610ef Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Fri, 1 Nov 2024 20:49:54 +0530 Subject: [PATCH 1/8] Added a log to understand how the Diagnostic data is coming in lsp4Jakarta --- .../RemoveDynamicConstraintAnnotationQuickFix.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java index 137c5750..22212262 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java @@ -64,6 +64,7 @@ public String getParticipantId() { @Override public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic, IProgressMonitor monitor) throws CoreException { + LOGGER.log(Level.SEVERE, "Diagnostics data ============",diagnostic); String annotationName = diagnostic.getData().toString().replace("\"", ""); String label = getLabel(annotationName); ASTNode node = context.getCoveredNode(); From 6feee395b40513634ae0d0eca67473ff2d1cdb0e Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Wed, 13 Nov 2024 16:56:04 +0530 Subject: [PATCH 2/8] Updated the Argument utils to fix the null pointer exception --- .../jdt/internal/core/ls/ArgumentUtils.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 93d22743..7d094526 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; +import java.util.logging.Logger; import java.util.stream.Collectors; import org.eclipse.lsp4j.CodeActionContext; @@ -22,9 +23,12 @@ import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.TextDocumentIdentifier; +import org.eclipse.lsp4jakarta.jdt.internal.beanvalidation.RemoveDynamicConstraintAnnotationQuickFix; import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; /** * Arguments utilities. @@ -49,6 +53,8 @@ public class ArgumentUtils { private static final String LINE_PROPERTY = "line"; private static final String URI_PROPERTY = "uri"; + private static final Logger LOGGER = Logger.getLogger(ArgumentUtils.class.getName()); + public static Map getFirst(List arguments) { return arguments.isEmpty() ? null : (Map) arguments.get(0); } @@ -116,6 +122,8 @@ public static CodeActionContext getCodeActionContext(Map obj, St // In Eclipse IDE (LSP client), the data is JsonObject, and in JDT-LS (ex : // vscode as LSP client) the data is a Map, we // convert the Map to a JsonObject to be consistent with any LSP clients. + LOGGER.info("diagnosticObj from vscode ============ " + diagnosticObj.toString()); + diagnostic.setData(getObjectAsJson(diagnosticObj, DATA_PROPERTY)); return diagnostic; }).collect(Collectors.toList()); @@ -147,12 +155,22 @@ public static Map getObject(Map obj, String key) * @return the child as a JsonObject if it exists and is an object, and null * otherwise */ - public static JsonObject getObjectAsJson(Map obj, String key) { + public static Object getObjectAsJson(Map obj, String key) { + +// LOGGER.info(" object for " + key + "value = " + child.toString()); + Object child = obj.get(key); - if (child != null && child instanceof Map) { + if (child != null && child instanceof String) { + return child; + } else if (child instanceof List) { Gson gson = new Gson(); - return (JsonObject) gson.toJsonTree(child); +// String[] childArray = ((List) child).toArray(new String[0]); + JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); + return jsonArray; + } else { + return getObject(obj, key); } - return null; + +// return child; } } From 0471793bfb77aa6a5b415773285a8bff200b6644 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Wed, 13 Nov 2024 17:44:02 +0530 Subject: [PATCH 3/8] Removed the unwanted log statements and added comments for better readability. --- ...veDynamicConstraintAnnotationQuickFix.java | 1 - .../jdt/internal/core/ls/ArgumentUtils.java | 39 +++++++++++-------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java index 22212262..137c5750 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/beanvalidation/RemoveDynamicConstraintAnnotationQuickFix.java @@ -64,7 +64,6 @@ public String getParticipantId() { @Override public List getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic, IProgressMonitor monitor) throws CoreException { - LOGGER.log(Level.SEVERE, "Diagnostics data ============",diagnostic); String annotationName = diagnostic.getData().toString().replace("\"", ""); String label = getLabel(annotationName); ASTNode node = context.getCoveredNode(); diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 7d094526..99856038 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -114,17 +114,20 @@ public static CodeActionContext getCodeActionContext(Map obj, St } List> diagnosticsObj = (List>) contextObj.get(DIAGNOSTICS_PROPERTY); List diagnostics = diagnosticsObj.stream().map(diagnosticObj -> { + LOGGER.info("Received diagnostic data" + diagnosticObj.toString()); Diagnostic diagnostic = new Diagnostic(); diagnostic.setRange(getRange(diagnosticObj, RANGE_PROPERTY)); diagnostic.setCode(getString(diagnosticObj, CODE_PROPERTY)); diagnostic.setMessage(getString(diagnosticObj, MESSAGE_PROPERTY)); diagnostic.setSource(getString(diagnosticObj, SOURCE_PROPERTY)); // In Eclipse IDE (LSP client), the data is JsonObject, and in JDT-LS (ex : - // vscode as LSP client) the data is a Map, we - // convert the Map to a JsonObject to be consistent with any LSP clients. - LOGGER.info("diagnosticObj from vscode ============ " + diagnosticObj.toString()); - - diagnostic.setData(getObjectAsJson(diagnosticObj, DATA_PROPERTY)); + // vscode as LSP client) the data is a Map. + + // In Vscode we are sending data in two different formats either as a string or + // as an array of strings. eg: data = “AssertTrue” or data =[“ApplicationScoped", "RequestScoped”] + // if it is a string -> set data as an Object. + // if ite is an array of strings - > set data as an JsonArray + diagnostic.setData(getValueFromDataParameter(diagnosticObj, DATA_PROPERTY)); return diagnostic; }).collect(Collectors.toList()); List only = null; @@ -147,30 +150,32 @@ public static Map getObject(Map obj, String key) } /** - * Returns the child as a JsonObject if it exists and is an object, and null - * otherwise + * Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns it as an object if present, or null if not. * - * @param obj the object to get the child of + * + * @param data the object to get the child of * @param key the key of the child - * @return the child as a JsonObject if it exists and is an object, and null - * otherwise + * @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns it as an object if present, or null if not. */ - public static Object getObjectAsJson(Map obj, String key) { + public static Object getValueFromDataParameter(Map data, String key) { -// LOGGER.info(" object for " + key + "value = " + child.toString()); - - Object child = obj.get(key); + Object child = data.get(key); if (child != null && child instanceof String) { + // if the value in the 'data' is a string, we string the object. + // eg: data = “AssertTrue” return child; } else if (child instanceof List) { + // if the value in the 'data' is an array, we will convert it to an JsonArray. + // eg: data =[“ApplicationScoped", "RequestScoped”] Gson gson = new Gson(); -// String[] childArray = ((List) child).toArray(new String[0]); JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); return jsonArray; } else { - return getObject(obj, key); + //Returns the object if it exists and is an object, and null otherwise + return getObject(data, key); } -// return child; } } From 4286c3cea5d04c4beabe459aeced24fa244181c4 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 14 Nov 2024 12:56:03 +0530 Subject: [PATCH 4/8] Removed unused imports and updated copyright header. --- .../lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 99856038..4156a0b2 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2019, 2023 Red Hat Inc. and others. +* Copyright (c) 2019, 2024 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 @@ -23,12 +23,9 @@ import org.eclipse.lsp4j.Position; import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.TextDocumentIdentifier; -import org.eclipse.lsp4jakarta.jdt.internal.beanvalidation.RemoveDynamicConstraintAnnotationQuickFix; import com.google.gson.Gson; import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; /** * Arguments utilities. From f9662b2a37ce08854f642725182aeb626feee360 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 14 Nov 2024 13:02:11 +0530 Subject: [PATCH 5/8] Corrected the method Indentation. --- .../jdt/internal/core/ls/ArgumentUtils.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 4156a0b2..f30b30f9 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -147,32 +147,34 @@ public static Map getObject(Map obj, String key) } /** - * Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, - * returns it as an object if present, or null if not. - * - * - * @param data the object to get the child of - * @param key the key of the child - * @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, - * returns it as an object if present, or null if not. - */ + * Returns the child as a JSON array if the data parameter contains an array of + * strings; otherwise, + * returns it as an object if present, or null if not. + * + * + * @param data the object to get the child of + * @param key the key of the child + * @return Returns the child as a JSON array if the data parameter contains an + * array of strings; otherwise, + * returns it as an object if present, or null if not. + */ public static Object getValueFromDataParameter(Map data, String key) { - Object child = data.get(key); - if (child != null && child instanceof String) { - // if the value in the 'data' is a string, we string the object. - // eg: data = “AssertTrue” - return child; - } else if (child instanceof List) { - // if the value in the 'data' is an array, we will convert it to an JsonArray. - // eg: data =[“ApplicationScoped", "RequestScoped”] - Gson gson = new Gson(); - JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); - return jsonArray; - } else { - //Returns the object if it exists and is an object, and null otherwise - return getObject(data, key); - } + Object child = data.get(key); + if (child instanceof String) { + // if the value in the 'data' is a string, we string the object. + // eg: data = “AssertTrue” + return child; + } else if (child instanceof List) { + // if the value in the 'data' is an array, we will convert it to an JsonArray. + // eg: data =[“ApplicationScoped", "RequestScoped”] + Gson gson = new Gson(); + JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); + return jsonArray; + } else { + // Returns the object if it exists and is an object, and null otherwise + return getObject(data, key); + } } } From 8c4c4f442d32b7c9874db083fcb51318a0539fcb Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 14 Nov 2024 19:27:36 +0530 Subject: [PATCH 6/8] returns null, in any other kind of value in data. --- .../lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index f30b30f9..a4c38197 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -172,8 +172,8 @@ public static Object getValueFromDataParameter(Map data, String JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); return jsonArray; } else { - // Returns the object if it exists and is an object, and null otherwise - return getObject(data, key); + // Returns null if it is in any other format. + return null; } } From 473976d2c0f303b4986d96d4d06dc45f0dd8d828 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 14 Nov 2024 22:29:39 +0530 Subject: [PATCH 7/8] Fix the indentation --- .../jdt/internal/core/ls/ArgumentUtils.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index a4c38197..34a93c3f 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -147,34 +147,32 @@ public static Map getObject(Map obj, String key) } /** - * Returns the child as a JSON array if the data parameter contains an array of - * strings; otherwise, - * returns it as an object if present, or null if not. - * - * - * @param data the object to get the child of - * @param key the key of the child - * @return Returns the child as a JSON array if the data parameter contains an - * array of strings; otherwise, - * returns it as an object if present, or null if not. - */ + * Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns it as an object if present, or null if not. + * + * + * @param data the object to get the child of + * @param key the key of the child + * @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise, + * returns null. + */ public static Object getValueFromDataParameter(Map data, String key) { - Object child = data.get(key); - if (child instanceof String) { - // if the value in the 'data' is a string, we string the object. - // eg: data = “AssertTrue” - return child; - } else if (child instanceof List) { - // if the value in the 'data' is an array, we will convert it to an JsonArray. - // eg: data =[“ApplicationScoped", "RequestScoped”] - Gson gson = new Gson(); - JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); - return jsonArray; - } else { - // Returns null if it is in any other format. - return null; - } + Object child = data.get(key); + if (child instanceof String) { + // if the value in the 'data' is a string, we string the object. + // eg: data = “AssertTrue” + return child; + } else if (child instanceof List) { + // if the value in the 'data' is an array, we will convert it to an JsonArray. + // eg: data =[“ApplicationScoped", "RequestScoped”] + Gson gson = new Gson(); + JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); + return jsonArray; + } else { + // Returns null if it is in any other format. + return null; + } } } From 4f99565eb1e515e32e13e9715fda87593ca6eea8 Mon Sep 17 00:00:00 2001 From: Aparna Michael Date: Thu, 14 Nov 2024 22:33:00 +0530 Subject: [PATCH 8/8] Removed the tabs and added spaces to correct the alignment of comments --- .../lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java index 34a93c3f..b806db19 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/internal/core/ls/ArgumentUtils.java @@ -161,16 +161,16 @@ public static Object getValueFromDataParameter(Map data, String Object child = data.get(key); if (child instanceof String) { // if the value in the 'data' is a string, we string the object. - // eg: data = “AssertTrue” + // eg: data = “AssertTrue” return child; } else if (child instanceof List) { - // if the value in the 'data' is an array, we will convert it to an JsonArray. - // eg: data =[“ApplicationScoped", "RequestScoped”] + // if the value in the 'data' is an array, we will convert it to an JsonArray. + // eg: data =[“ApplicationScoped", "RequestScoped”] Gson gson = new Gson(); JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray(); return jsonArray; } else { - // Returns null if it is in any other format. + // Returns null if it is in any other format. return null; }