Skip to content

Commit

Permalink
fixed sonar issues (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish authored and robstryker committed May 28, 2019
1 parent 0dabdb4 commit b9b4a88
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public interface RSPClient {
@JsonRequest
CompletableFuture<String> promptString(StringPrompt prompt);



/**
* The `client/discoveryPathAdded` notification is sent by the server to all
* clients in response to the `server/addDiscoveryPath` notification.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,19 +26,22 @@

public class JavadocUtilities {

public static HashMap<String, JavadocComment> methodToJavadocMap(File f) {
private JavadocUtilities() {
// inhibit instantiation
}

public static Map<String, JavadocComment> methodToJavadocMap(File f) {
HashMap<String, JavadocComment> map = new LinkedHashMap<>();
VoidVisitorAdapter adapter = new VoidVisitorAdapter<Object>() {
VoidVisitorAdapter<Object> adapter = new VoidVisitorAdapter<Object>() {
@Override
public void visit(JavadocComment comment, Object arg) {
super.visit(comment, arg);
Optional<Node> 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);
}
}
}
Expand All @@ -44,6 +56,9 @@ public void visit(JavadocComment comment, Object arg) {
}

public static boolean isNotification(MethodDeclaration dec) {
if (dec == null) {
return false;
}
NodeList<AnnotationExpr> annotations = dec.getAnnotations();
for( AnnotationExpr a : annotations) {
String annotName = annotations.get(0).getNameAsString();
Expand All @@ -55,6 +70,9 @@ public static boolean isNotification(MethodDeclaration dec) {
}

public static boolean isRequest(MethodDeclaration dec) {
if (dec == null) {
return false;
}
NodeList<AnnotationExpr> annotations = dec.getAnnotations();
for( AnnotationExpr a : annotations) {
String annotName = annotations.get(0).getNameAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void generateProtocolTs(String dir) {
try {
Files.write(destination.toPath(), total.getBytes());
} catch(IOException ioe) {

System.err.println(ioe);
}
}

Expand All @@ -143,7 +143,7 @@ private void generateMessageTs(String dir) {
try {
Files.write(destination.toPath(), fileContents.getBytes());
} catch(IOException ioe) {

System.err.println(ioe);
}
}

Expand All @@ -153,7 +153,7 @@ private void generateIncomingTs(String dir) {
try {
Files.write(destination.toPath(), fileContents.getBytes());
} catch(IOException ioe) {

System.err.println(ioe);
}
}

Expand All @@ -163,7 +163,7 @@ private void generateOutgoingTs(String dir) {
try {
Files.write(destination.toPath(), fileContents.getBytes());
} catch(IOException ioe) {

System.err.println(ioe);
}
}

Expand All @@ -174,23 +174,26 @@ private String outgoingTsErrors() {
" * Error messages\n" +
JAVA_END +
"export namespace ErrorMessages {\n");
List<String> mNames = null;
try {
mNames = getMethodNames(getServerInterfaceFile());
List<String> 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) {
Expand Down Expand Up @@ -320,6 +323,9 @@ private String outgoingTsServer() {
private void outgoingTsServerMethod(StringBuilder sb, Map<String, JavadocComment> 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();
Expand Down Expand Up @@ -363,40 +369,48 @@ private void outgoingTsServerMethod(StringBuilder sb, Map<String, JavadocComment
private String methodNameToTimeoutErrorName(String name) {
return name.toUpperCase() + "_TIMEOUT";
}

private String incomingTsRegisterListeners() {
StringBuilder sb = new StringBuilder();
try {
Map<String, JavadocComment> map = JavadocUtilities.methodToJavadocMap(getClientInterfaceFile());
List<String> 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);
}

return sb.toString();
}

private void printOneListenerMethod(StringBuilder sb, Map<String, JavadocComment> 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();
Expand All @@ -419,7 +433,7 @@ private String messageTsServer() {
" */\n" +
" export namespace Server {\n\n";
String footer = " }\n";

StringBuilder sb = new StringBuilder();
try {
Map<String, JavadocComment> map = JavadocUtilities.methodToJavadocMap(getServerInterfaceFile());
Expand Down Expand Up @@ -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<Parameter> params = md.getParameters();
if( params.size() == 0 ) {
Expand Down Expand Up @@ -590,14 +608,16 @@ private String emptyFooter() {
}

private File getClientInterfaceFile() throws IOException {
File f2 = new File(baseDir);
File f = new File(f2, CLIENT_INTERFACE_PATH).getCanonicalFile();
return f;
return getCanonicalFile(CLIENT_INTERFACE_PATH);
}

private File getServerInterfaceFile() throws IOException {
return getCanonicalFile(SERVER_INTERFACE_PATH);
}

private File getCanonicalFile(String baseRelativePath) throws IOException {
File f2 = new File(baseDir);
File f = new File(f2, SERVER_INTERFACE_PATH).getCanonicalFile();
File f = new File(f2, baseRelativePath).getCanonicalFile();
return f;
}

Expand Down

0 comments on commit b9b4a88

Please sign in to comment.