From 0f29bccb75212fd3fb1960bb1530d1cf6031f4f5 Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Mon, 27 May 2019 14:44:53 +0200 Subject: [PATCH] fixed sonar issues (#244) Signed-off-by: Andre Dietisheim --- .../org/jboss/tools/rsp/api/RSPClient.java | 2 - .../rsp/api/schema/JavadocUtilities.java | 34 +++++-- .../rsp/api/schema/TypescriptUtility.java | 94 +++++++++++-------- 3 files changed, 82 insertions(+), 48 deletions(-) diff --git a/api/bundles/org.jboss.tools.rsp.api/src/main/java/org/jboss/tools/rsp/api/RSPClient.java b/api/bundles/org.jboss.tools.rsp.api/src/main/java/org/jboss/tools/rsp/api/RSPClient.java index c94826d05..6add5b1c3 100644 --- a/api/bundles/org.jboss.tools.rsp.api/src/main/java/org/jboss/tools/rsp/api/RSPClient.java +++ b/api/bundles/org.jboss.tools.rsp.api/src/main/java/org/jboss/tools/rsp/api/RSPClient.java @@ -32,8 +32,6 @@ public interface RSPClient { @JsonRequest CompletableFuture promptString(StringPrompt prompt); - - /** * The `client/discoveryPathAdded` notification is sent by the server to all * clients in response to the `server/addDiscoveryPath` notification. diff --git a/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/JavadocUtilities.java b/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/JavadocUtilities.java index 05aa0a331..bf2af4154 100644 --- a/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/JavadocUtilities.java +++ b/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/JavadocUtilities.java @@ -1,9 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2018-2019 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ package org.jboss.tools.rsp.api.schema; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.Map; import java.util.Optional; import com.github.javaparser.JavaParser; @@ -17,19 +26,22 @@ public class JavadocUtilities { - public static HashMap methodToJavadocMap(File f) { + private JavadocUtilities() { + // inhibit instantiation + } + + public static Map methodToJavadocMap(File f) { HashMap map = new LinkedHashMap<>(); - VoidVisitorAdapter adapter = new VoidVisitorAdapter() { + VoidVisitorAdapter adapter = new VoidVisitorAdapter() { @Override public void visit(JavadocComment comment, Object arg) { super.visit(comment, arg); Optional o = comment.getCommentedNode(); - if (o.get() != null) { - if (!(o.get() instanceof CompilationUnit)) { - Node n = o.get(); - if (n instanceof MethodDeclaration) { - map.put(((MethodDeclaration)n).getNameAsString(), comment); - } + if (o.isPresent() + && (!(o.get() instanceof CompilationUnit))) { + Node n = o.get(); + if (n instanceof MethodDeclaration) { + map.put(((MethodDeclaration)n).getNameAsString(), comment); } } } @@ -44,6 +56,9 @@ public void visit(JavadocComment comment, Object arg) { } public static boolean isNotification(MethodDeclaration dec) { + if (dec == null) { + return false; + } NodeList annotations = dec.getAnnotations(); for( AnnotationExpr a : annotations) { String annotName = annotations.get(0).getNameAsString(); @@ -55,6 +70,9 @@ public static boolean isNotification(MethodDeclaration dec) { } public static boolean isRequest(MethodDeclaration dec) { + if (dec == null) { + return false; + } NodeList annotations = dec.getAnnotations(); for( AnnotationExpr a : annotations) { String annotName = annotations.get(0).getNameAsString(); diff --git a/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/TypescriptUtility.java b/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/TypescriptUtility.java index cd964bd7a..18adb07d1 100644 --- a/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/TypescriptUtility.java +++ b/api/docs/org.jboss.tools.rsp.schema/src/main/java/org/jboss/tools/rsp/api/schema/TypescriptUtility.java @@ -133,7 +133,7 @@ private void generateProtocolTs(String dir) { try { Files.write(destination.toPath(), total.getBytes()); } catch(IOException ioe) { - + System.err.println(ioe); } } @@ -143,7 +143,7 @@ private void generateMessageTs(String dir) { try { Files.write(destination.toPath(), fileContents.getBytes()); } catch(IOException ioe) { - + System.err.println(ioe); } } @@ -153,7 +153,7 @@ private void generateIncomingTs(String dir) { try { Files.write(destination.toPath(), fileContents.getBytes()); } catch(IOException ioe) { - + System.err.println(ioe); } } @@ -163,7 +163,7 @@ private void generateOutgoingTs(String dir) { try { Files.write(destination.toPath(), fileContents.getBytes()); } catch(IOException ioe) { - + System.err.println(ioe); } } @@ -174,23 +174,26 @@ private String outgoingTsErrors() { " * Error messages\n" + JAVA_END + "export namespace ErrorMessages {\n"); - List mNames = null; try { - mNames = getMethodNames(getServerInterfaceFile()); + List mNames = getMethodNames(getServerInterfaceFile()); + for( String mName : mNames ) { + printOneErrorMessageConstant(sb, mName); + } + + sb.append(emptyFooter()); + return sb.toString(); } catch(IOException ioe) { throw new RuntimeException(ioe); - } - - for( String n : mNames ) { - sb.append(" "); - sb.append("export const "); - sb.append(methodNameToTimeoutErrorName(n)); - sb.append(" = 'Failed to "); - sb.append(camelCaseToSpaces(n)); - sb.append(" in time';\n"); - } - sb.append(emptyFooter()); - return sb.toString(); + } + } + + private void printOneErrorMessageConstant(StringBuilder sb, String methodName) { + sb.append(" "); + sb.append("export const "); + sb.append(methodNameToTimeoutErrorName(methodName)); + sb.append(" = 'Failed to "); + sb.append(camelCaseToSpaces(methodName)); + sb.append(" in time';\n"); } private String camelCaseToSpaces(String n) { @@ -320,6 +323,9 @@ private String outgoingTsServer() { private void outgoingTsServerMethod(StringBuilder sb, Map map, String[] methods, int i) { JavadocComment jdc = map.get(methods[i]); MethodDeclaration md = getMethodDeclaration(jdc); + if (md == null) { + return; + } String methodName = md.getNameAsString(); int paramCount = md.getParameters().size(); @@ -363,6 +369,7 @@ private void outgoingTsServerMethod(StringBuilder sb, Map names = new ArrayList<>(map.keySet()); String[] methods = names.toArray(new String[names.size()]); for( int i = 0; i < methods.length; i++ ) { - JavadocComment jdc = map.get(methods[i]); - MethodDeclaration md = getMethodDeclaration(jdc); - - String methodName = md.getNameAsString(); - String capName = capFirstLetter(methodName); - int paramCount = md.getParameters().size(); - String paramType = paramCount > 0 ? convertReturnType(md.getParameter(0).getType().toString()) : null; - Type retType = md.getType(); - String retTypeName = convertReturnType(retType.toString()); - - if( JavadocUtilities.isNotification(md) ) { - sb.append(" on" + capName + "(listener: (arg: " + paramType + ") => " + retTypeName + "): void {\n"); - sb.append(" this.emitter.on('" + methodName + "', listener);\n"); - sb.append(" }\n"); - } else { - String requestName = methodNameToRequestName(methodName); - sb.append(" on" + capName + "(listener: (arg: " + paramType + ") => Promise<" + retTypeName + ">): void {\n"); - sb.append(" this.connection.onRequest(Messages.Client." + requestName + ".type, listener);\n"); - sb.append(" }\n"); - } + printOneListenerMethod(sb, map, methods[i]); } } catch(IOException ioe) { throw new RuntimeException(ioe); @@ -397,6 +385,32 @@ private String incomingTsRegisterListeners() { return sb.toString(); } + + private void printOneListenerMethod(StringBuilder sb, Map map, String method) { + JavadocComment jdc = map.get(method); + MethodDeclaration md = getMethodDeclaration(jdc); + if (md == null) { + System.err.println("Method declaration for method '" + method + "' not found. No listener was created."); + return; + } + String methodName = md.getNameAsString(); + String capName = capFirstLetter(methodName); + int paramCount = md.getParameters().size(); + String paramType = paramCount > 0 ? convertReturnType(md.getParameter(0).getType().toString()) : null; + Type retType = md.getType(); + String retTypeName = convertReturnType(retType.toString()); + + if( JavadocUtilities.isNotification(md) ) { + sb.append(" on" + capName + "(listener: (arg: " + paramType + ") => " + retTypeName + "): void {\n"); + sb.append(" this.emitter.on('" + methodName + "', listener);\n"); + sb.append(" }\n"); + } else { + String requestName = methodNameToRequestName(methodName); + sb.append(" on" + capName + "(listener: (arg: " + paramType + ") => Promise<" + retTypeName + ">): void {\n"); + sb.append(" this.connection.onRequest(Messages.Client." + requestName + ".type, listener);\n"); + sb.append(" }\n"); + } + } private String incomingTsClient() { return incomingTsListen() + "\n\n" + incomingTsRegisterListeners(); @@ -534,6 +548,10 @@ private void printOneNotification(String methodName, JavadocComment jdc, // TODO body MethodDeclaration md = getMethodDeclaration(jdc); + if (md == null) { + System.err.println("No method declaration for '" + jdc.toString() + "' was found. Not creating notification type."); + return; + } sb.append(" export const type = new NotificationType<"); NodeList params = md.getParameters(); if( params.size() == 0 ) {