Skip to content

Commit b9eb480

Browse files
committed
Fail on warnings when building. Starting to use keywords to extract from responses
1 parent 4c6b01d commit b9eb480

25 files changed

+176
-130
lines changed

build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ apply plugin: 'java'
88
apply plugin: 'maven-publish'
99

1010
def projectVersion = System.getenv('PROJECT_VERSION') != null ? System.getenv('PROJECT_VERSION') : 'dev'
11+
def projectCompilerArgs = ["-Xlint:all", "-Xlint:-processing", "-Xlint:-serial", "-Werror"]
12+
13+
14+
compileJava {
15+
options.compilerArgs = projectCompilerArgs
16+
}
17+
18+
compileTestJava {
19+
options.compilerArgs = projectCompilerArgs
20+
}
1121

1222
repositories {
1323
mavenCentral()

src/javarepl/EvaluationContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ public EvaluationContext addExpression(Expression expression) {
102102
}
103103

104104
public EvaluationContext removeExpressionWithKey(String key) {
105-
return new EvaluationContext(outputDirectory, expressions.filter(where(Expression.functions.key(), not(key))), results, lastSource);
105+
return new EvaluationContext(outputDirectory, expressions.filter(where(Expression::key, not(key))), results, lastSource);
106106
}
107107
}

src/javarepl/Evaluator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public final Option<java.lang.reflect.Type> typeOfExpression(String expression)
170170
return expressionType;
171171
}
172172

173-
public final Option<Class> classFrom(String expression) {
173+
public final Option<Class<?>> classFrom(String expression) {
174174
try {
175175
return some(detectClass(expression));
176176
} catch (Throwable e) {
@@ -247,7 +247,7 @@ private java.lang.reflect.Method detectMethod(String expression) throws Exceptio
247247
return expressionClass.getDeclaredMethods()[0];
248248
}
249249

250-
private Class detectClass(String expression) throws Exception {
250+
private Class<?> detectClass(String expression) throws Exception {
251251
final String className = randomIdentifier("Class");
252252
final File outputJavaFile = file(context.outputDirectory(), className + ".java");
253253

@@ -256,7 +256,7 @@ private Class detectClass(String expression) throws Exception {
256256

257257
compile(outputJavaFile);
258258

259-
Class expressionClass = classLoader.loadClass(className);
259+
Class<?> expressionClass = classLoader.loadClass(className);
260260

261261
return expressionClass.getDeclaredMethods()[0].getReturnType();
262262
}

src/javarepl/ExpressionCompilationException.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616

1717
public class ExpressionCompilationException extends Exception {
1818

19-
public ExpressionCompilationException(File file, Iterable<? extends Diagnostic> diagnostics) {
19+
public ExpressionCompilationException(File file, Iterable<? extends Diagnostic<?>> diagnostics) {
2020
this(diagnosticsAsMessage(file, diagnostics));
2121
}
2222

2323
public ExpressionCompilationException(String message) {
2424
super(message);
2525
}
2626

27-
private static String diagnosticsAsMessage(final File file, final Iterable<? extends Diagnostic> diagnostics) {
27+
private static String diagnosticsAsMessage(final File file, final Iterable<? extends Diagnostic<?>> diagnostics) {
2828
return sequence(diagnostics)
2929
.filter(isError())
3030
.map(diagnosticToMessage(file))
3131
.toString("\n");
3232
}
3333

34-
private static Function1<Diagnostic, String> diagnosticToMessage(final File file) {
34+
private static Function1<Diagnostic<?>, String> diagnosticToMessage(final File file) {
3535
return diagnostic -> {
3636
String line = lines(file).drop((int) diagnostic.getLineNumber() - 1).head();
3737
String marker = repeat(' ').take((int) diagnostic.getColumnNumber() - 1).toString("", "", "^");
@@ -42,7 +42,7 @@ private static Function1<Diagnostic, String> diagnosticToMessage(final File file
4242
};
4343
}
4444

45-
private static Predicate<Diagnostic> isError() {
45+
private static Predicate<Diagnostic<?>> isError() {
4646
return diagnostic -> diagnostic.getKind() == ERROR;
4747
}
4848
}

src/javarepl/Utils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static String randomIdentifier(String prefix) {
4040
}
4141

4242
public static Type extractType(Type type) {
43-
if (type instanceof Class) {
44-
Class clazz = (Class) type;
43+
if (type instanceof Class<?>) {
44+
Class<?> clazz = (Class<?>) type;
4545
if (clazz.isAnonymousClass() || clazz.isSynthetic() || clazz.isMemberClass()) {
4646
if (clazz.getGenericSuperclass().equals(Object.class)) {
4747
return extractType(sequence(clazz.getGenericInterfaces())

src/javarepl/client/EvaluationLog.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
public class EvaluationLog {
44
public static enum Type {
5-
INFO, SUCCESS, ERROR
5+
INFO, SUCCESS, ERROR;
6+
7+
public static Type type(String type) {
8+
return valueOf(type);
9+
}
610
}
711

812
private final Type type;

src/javarepl/client/JavaREPLClient.java

+43-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import com.googlecode.totallylazy.Option;
44
import com.googlecode.totallylazy.Sequence;
5+
import com.googlecode.totallylazy.Sequences;
6+
import com.googlecode.totallylazy.collections.Keyword;
57
import com.googlecode.totallylazy.functions.Function1;
8+
import com.googlecode.totallylazy.numbers.Integers;
9+
import com.googlecode.totallylazy.numbers.Numbers;
610
import com.googlecode.utterlyidle.Response;
711
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
812
import javarepl.completion.CompletionResult;
@@ -14,15 +18,33 @@
1418

1519
import static com.googlecode.totallylazy.Option.none;
1620
import static com.googlecode.totallylazy.Option.some;
17-
import static com.googlecode.totallylazy.Sequences.sequence;
21+
import static com.googlecode.totallylazy.collections.Keyword.keyword;
1822
import static com.googlecode.totallylazy.json.Json.map;
19-
import static com.googlecode.utterlyidle.RequestBuilder.get;
20-
import static com.googlecode.utterlyidle.RequestBuilder.post;
21-
import static javarepl.client.EvaluationLog.Type;
23+
import static com.googlecode.totallylazy.reflection.Types.classOf;
24+
import static com.googlecode.totallylazy.reflection.Types.parameterizedType;
25+
import static com.googlecode.utterlyidle.Request.get;
26+
import static com.googlecode.utterlyidle.Request.post;
27+
import static javarepl.client.EvaluationLog.Type.type;
2228
import static javarepl.completion.CompletionResult.methods.fromJson;
2329
import static javarepl.console.ConsoleStatus.Idle;
30+
import static javarepl.console.ConsoleStatus.consoleStatus;
2431

2532
public final class JavaREPLClient {
33+
34+
public static final Keyword<String> EXPRESSION = keyword("expression", String.class);
35+
public static final Keyword<String> TEMPLATE = keyword("template", String.class);
36+
public static final Keyword<String> TOKEN = keyword("token", String.class);
37+
public static final Keyword<String> TYPE = keyword("type", String.class);
38+
public static final Keyword<String> MESSAGE = keyword("message", String.class);
39+
public static final Keyword<String> STATUS = keyword("status", String.class);
40+
public static final Keyword<String> VERSION = keyword("version", String.class);
41+
public static final Keyword<String> VALUE = keyword("value", String.class);
42+
public static final Keyword<String> POSITION = keyword("position", String.class);
43+
public static final Keyword<List<String>> HISTORY = keyword("history", classOf(parameterizedType(List.class, String.class)));
44+
public static final Keyword<List<String>> FORMS = keyword("forms", classOf(parameterizedType(List.class, String.class)));
45+
public static final Keyword<List<Map<String, Object>>> LOGS = keyword("logs", classOf(parameterizedType(List.class, parameterizedType(Map.class, String.class, Object.class))));
46+
public static final Keyword<List<Map<String, Object>>> CANDIDATES = keyword("candidates", classOf(parameterizedType(List.class, parameterizedType(Map.class, String.class, Object.class))));
47+
2648
private final String hostname;
2749
private final Integer port;
2850
private final ClientHttpHandler client;
@@ -34,52 +56,56 @@ public JavaREPLClient(String hostname, Integer port) {
3456
}
3557

3658
public synchronized ExpressionTemplate template(String expression) throws Exception {
37-
Map<String, Object> model = map(client.handle(get(url("template")).query("expression", expression).build()).entity().toString());
38-
return new ExpressionTemplate(model.get("template").toString(), model.get("token").toString());
59+
Map<String, Object> response = map(client.handle(get(url("template")).query("expression", expression)).entity().toString());
60+
String template = TEMPLATE.call(response);
61+
String token = TOKEN.call(response);
62+
return new ExpressionTemplate(template, token);
3963
}
4064

4165

4266
public synchronized Option<EvaluationResult> execute(String expr) throws Exception {
43-
String json = client.handle(post(url("execute")).form("expression", expr).build()).entity().toString();
67+
String json = client.handle(post(url("execute")).form("expression", expr)).entity().toString();
4468

4569
if (json.isEmpty())
4670
return none();
4771

48-
Map<String, Object> model = map(json);
49-
Sequence<Map<String, Object>> logs = sequence((List<Object>)model.get("logs")).unsafeCast();
50-
String expression = model.get("expression").toString();
72+
Map<String, Object> response = map(json);
73+
Sequence<Map<String, Object>> logs = LOGS.map(Sequences::sequence).call(response);
74+
String expression = EXPRESSION.call(response);
5175

5276
return some(new EvaluationResult(expression, logs.map(modelToEvaluationLog())));
5377
}
5478

5579
public synchronized CompletionResult completions(String expr) throws Exception {
56-
return fromJson(client.handle(get(url("completions")).query("expression", expr).build()).entity().toString());
80+
return fromJson(client.handle(get(url("completions")).query("expression", expr)).entity().toString());
5781
}
5882

5983
public synchronized ConsoleStatus status() {
6084
try {
61-
return ConsoleStatus.valueOf(map(client.handle(get(url("status")).build()).entity().toString()).get("status").toString());
85+
Map<String, Object> response = map(client.handle(get(url("status"))).entity().toString());
86+
return consoleStatus(STATUS.call(response));
6287
} catch (Exception e) {
6388
return Idle;
6489
}
6590
}
6691

6792
public synchronized String version() {
6893
try {
69-
return map(client.handle(get(url("version")).build()).entity().toString()).get("version").toString();
94+
Map<String, Object> response = map(client.handle(get(url("version"))).entity().toString());
95+
return VERSION.call(response);
7096
} catch (Exception e) {
7197
return "[unknown]";
7298
}
7399
}
74100

75101
public synchronized Sequence<String> history() throws Exception {
76-
Response history = client.handle(get(url("history")).build());
77-
Map<String, Object> model = map(history.entity().toString());
78-
return sequence((List<Object>)model.get("history")).safeCast(String.class);
102+
Response history = client.handle(get(url("history")));
103+
Map<String, Object> response = map(history.entity().toString());
104+
return HISTORY.map(Sequences::sequence).call(response);
79105
}
80106

81107
private Function1<Map<String, Object>, EvaluationLog> modelToEvaluationLog() {
82-
return model -> new EvaluationLog(Type.valueOf(model.get("type").toString()), model.get("message").toString());
108+
return response -> new EvaluationLog(type(TYPE.call(response)), MESSAGE.call(response));
83109
}
84110

85111
private String url(String path) {

src/javarepl/completion/CompletionResult.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package javarepl.completion;
22

33
import com.googlecode.totallylazy.Sequence;
4+
import com.googlecode.totallylazy.Sequences;
45
import com.googlecode.totallylazy.json.Json;
6+
import javarepl.client.JavaREPLClient;
57

68
import java.util.List;
79
import java.util.Map;
@@ -10,6 +12,7 @@
1012
import static com.googlecode.totallylazy.collections.PersistentMap.constructors.emptyMap;
1113
import static com.googlecode.totallylazy.collections.PersistentMap.constructors.map;
1214
import static com.googlecode.totallylazy.json.Json.json;
15+
import static javarepl.client.JavaREPLClient.*;
1316

1417

1518
public class CompletionResult {
@@ -68,11 +71,15 @@ public static String toJson(CompletionResult result) {
6871
}
6972

7073
public static CompletionResult fromJson(String json) {
71-
Map<String, Object> model = map(Json.map(json));
72-
return new CompletionResult(model.get("expression").toString(),
73-
Integer.valueOf(model.get("position").toString()),
74-
sequence((List<Map<String, Object>>)model.get("candidates"))
75-
.map(model1 -> new CompletionCandidate(model1.get("value").toString(), sequence((List<String>)model1.get("forms")))));
74+
Map<String, Object> jsonMap = map(Json.map(json));
75+
76+
Sequence<Map<String, Object>> candidates = CANDIDATES.map(Sequences::sequence).apply(jsonMap);
77+
78+
return new CompletionResult(
79+
EXPRESSION.apply(jsonMap),
80+
POSITION.map(Integer::valueOf).apply(jsonMap),
81+
candidates.map(candidate -> new CompletionCandidate(VALUE.apply(candidate), FORMS.map(Sequences::sequence).apply(candidate)))
82+
);
7683
}
7784
}
7885
}

src/javarepl/completion/Completions.java

+13-23
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
public class Completions {
1818

19-
public static Function1<Character, Integer> lastIndexOf(final String string) {
20-
return character -> string.lastIndexOf(character);
21-
}
22-
23-
public static Function1<MemberReflection, String> candidateName() {
24-
return memberReflection -> new match<MemberReflection, String>() {
19+
public static Function1<MemberReflection<?>, String> candidateName() {
20+
return memberReflection -> new match<MemberReflection<?>, String>() {
2521
String value(MethodReflection expr) {
2622
return expr.name() + "(";
2723
}
@@ -30,15 +26,15 @@ String value(ClassReflection expr) {
3026
return expr.member().getSimpleName();
3127
}
3228

33-
String value(MemberReflection expr) {
29+
String value(MemberReflection<?> expr) {
3430
return expr.name();
3531
}
3632
}.apply(memberReflection).get();
3733
}
3834

3935

40-
public static Function1<MemberReflection, String> candidateForm() {
41-
return memberReflection -> new match<MemberReflection, String>() {
36+
public static Function1<MemberReflection<?>, String> candidateForm() {
37+
return memberReflection -> new match<MemberReflection<?>, String>() {
4238
String value(MethodReflection expr) {
4339
return genericMethodSignature(expr.member());
4440
}
@@ -47,37 +43,31 @@ String value(ClassReflection expr) {
4743
return expr.member().getSimpleName();
4844
}
4945

50-
String value(MemberReflection expr) {
46+
String value(MemberReflection<?> expr) {
5147
return expr.name();
5248
}
5349
}.apply(memberReflection).get();
5450
}
5551

56-
57-
public static Function1<Group<String, MemberReflection>, CompletionCandidate> candidate() {
52+
public static Function1<Group<String, MemberReflection<?>>, CompletionCandidate> candidate() {
5853
return group -> new CompletionCandidate(group.key(), group.map(candidateForm()).sort(Comparators.ascending(String.class)));
5954
}
6055

61-
private static Function1<Type, String> asSimpleGenericTypeSignature() {
62-
return type -> renderSimpleGenericType(type);
63-
}
64-
65-
6656
private static String renderSimpleGenericType(Type type) {
6757
return new match<Type, String>() {
6858
String value(ParameterizedType t) {
69-
return renderSimpleGenericType(t.getRawType()) + sequence(t.getActualTypeArguments()).map(asSimpleGenericTypeSignature()).toString("<", ", ", ">");
59+
return renderSimpleGenericType(t.getRawType()) + sequence(t.getActualTypeArguments()).map(Completions::renderSimpleGenericType).toString("<", ", ", ">");
7060
}
7161

72-
String value(TypeVariable t) {
62+
String value(TypeVariable<?> t) {
7363
return t.getName();
7464
}
7565

7666
String value(GenericArrayType t) {
7767
return renderSimpleGenericType(t.getGenericComponentType()) + "[]";
7868
}
7969

80-
String value(Class t) {
70+
String value(Class<?> t) {
8171
return t.getSimpleName();
8272
}
8373

@@ -93,17 +83,17 @@ private static String genericMethodSignature(Method method) {
9383
method.getName() +
9484

9585
sequence(method.getGenericParameterTypes())
96-
.map(asSimpleGenericTypeSignature())
86+
.map(Completions::renderSimpleGenericType)
9787
.toString("(", ", ", ")") +
9888

9989
sequence(method.getGenericExceptionTypes())
100-
.map(asSimpleGenericTypeSignature())
90+
.map(Completions::renderSimpleGenericType)
10191
.toString(method.getGenericExceptionTypes().length > 0 ? " throws " : "", ", ", "");
10292
}
10393

10494
public static int lastIndexOfSeparator(Sequence<Character> characters, String expression) {
10595
return characters
106-
.map(lastIndexOf(expression))
96+
.map(expression::lastIndexOf)
10797
.reduce(maximum())
10898
.intValue();
10999
}

src/javarepl/completion/ConsoleCompleter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.googlecode.totallylazy.functions.Function1;
55
import javarepl.Evaluator;
66
import javarepl.Result;
7+
import javarepl.expressions.Expression;
78
import javarepl.expressions.Import;
89
import javarepl.expressions.Method;
910
import javarepl.expressions.Type;
@@ -12,7 +13,6 @@
1213
import static com.googlecode.totallylazy.predicates.Predicates.in;
1314
import static com.googlecode.totallylazy.predicates.Predicates.where;
1415
import static javarepl.completion.CompletionCandidate.asCompletionCandidate;
15-
import static javarepl.expressions.Expression.functions.key;
1616

1717
public class ConsoleCompleter extends Completer {
1818
private final Evaluator evaluator;
@@ -44,7 +44,7 @@ private Sequence<String> methods() {
4444
}
4545

4646
private Sequence<String> types() {
47-
return evaluator.expressionsOfType(Type.class).map(key());
47+
return evaluator.expressionsOfType(Type.class).map(Expression::key);
4848
}
4949

5050
private Sequence<String> imports() {

0 commit comments

Comments
 (0)