forked from getgauge/gauge-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStubImplementationCodeProcessor.java
More file actions
140 lines (122 loc) · 5.58 KB
/
StubImplementationCodeProcessor.java
File metadata and controls
140 lines (122 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE.txt in the project root for license information.
*----------------------------------------------------------------*/
package com.thoughtworks.gauge.connection;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.Range;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.google.protobuf.ProtocolStringList;
import com.thoughtworks.gauge.FileHelper;
import com.thoughtworks.gauge.Logger;
import gauge.messages.Messages;
import gauge.messages.Spec;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class StubImplementationCodeProcessor implements com.thoughtworks.gauge.processor.IMessageProcessor {
private static final String NEW_LINE = "\n";
private static ArrayList<MethodDeclaration> methodDeclarations = new ArrayList<>();
private static Range classRange;
@Override
public Messages.Message process(Messages.Message message) {
ProtocolStringList stubs = message.getStubImplementationCodeRequest().getCodesList();
String filePath = message.getStubImplementationCodeRequest().getImplementationFilePath();
File file = new File(filePath);
Messages.FileDiff fileDiff;
if (file.exists()) {
fileDiff = implementInExistingFile(stubs, file);
} else {
File fileName = FileHelper.getDefaultImplFileName("", 0);
fileDiff = implementInNewClass(stubs, fileName);
}
return Messages.Message.newBuilder()
.setMessageId(message.getMessageId())
.setFileDiff(fileDiff)
.setMessageType(Messages.Message.MessageType.StubImplementationCodeRequest)
.build();
}
private Messages.FileDiff implementInExistingFile(ProtocolStringList stubs, File file) {
try {
if (new FileReader(file).read() != -1) {
return implementInExistingClass(stubs, file);
}
return implementInNewClass(stubs, file);
} catch (IOException e) {
Logger.error("Unable to implement method", e);
}
return null;
}
private Messages.FileDiff implementInNewClass(ProtocolStringList stubs, File file) {
String className = FileHelper.getClassName(file);
String contents = getNewClassContents(className, stubs);
Spec.Span.Builder span = Spec.Span.newBuilder()
.setStart(0)
.setStartChar(0)
.setEnd(0)
.setEndChar(0);
Messages.TextDiff textDiff = Messages.TextDiff.newBuilder().setSpan(span).setContent(contents).build();
return Messages.FileDiff.newBuilder().setFilePath(file.toString()).addTextDiffs(textDiff).build();
}
private String getNewClassContents(String className, ProtocolStringList stubs) {
return "import com.thoughtworks.gauge.Step;"
+ NEW_LINE
+ NEW_LINE
+ "public class " + className + " {"
+ NEW_LINE
+ String.join(NEW_LINE, stubs)
+ NEW_LINE
+ "}"
+ NEW_LINE;
}
private Messages.FileDiff implementInExistingClass(ProtocolStringList stubs, File file) {
try {
JavaParser javaParser = new JavaParser();
ParseResult<CompilationUnit> compilationUnit = javaParser.parse(file);
String contents = String.join(NEW_LINE, stubs);
int lastLine;
int column;
MethodVisitor methodVisitor = new MethodVisitor();
methodVisitor.visit(compilationUnit.getResult().get(), null);
if (!methodDeclarations.isEmpty()) {
MethodDeclaration methodDeclaration = methodDeclarations.get(methodDeclarations.size() - 1);
lastLine = methodDeclaration.getRange().get().end.line - 1;
column = methodDeclaration.getRange().get().end.column + 1;
contents = NEW_LINE + contents;
} else {
new ClassVisitor().visit(compilationUnit.getResult().get(), null);
lastLine = classRange.end.line - 1;
column = 0;
contents = contents + NEW_LINE;
}
Spec.Span.Builder span = Spec.Span.newBuilder()
.setStart(lastLine)
.setStartChar(column)
.setEnd(lastLine)
.setEndChar(column);
Messages.TextDiff textDiff = Messages.TextDiff.newBuilder().setSpan(span).setContent(contents).build();
return Messages.FileDiff.newBuilder().setFilePath(file.toString()).addTextDiffs(textDiff).build();
} catch (IOException e) {
Logger.error("Unable to implement method", e);
}
return null;
}
private static final class MethodVisitor extends VoidVisitorAdapter {
@Override
public void visit(MethodDeclaration methodDeclaration, Object arg) {
methodDeclarations.add(methodDeclaration);
}
}
private static final class ClassVisitor extends VoidVisitorAdapter {
@Override
public void visit(ClassOrInterfaceDeclaration node, Object arg) {
classRange = node.getRange().get();
}
}
}