Skip to content

Commit

Permalink
Fail on warnings when building. Starting to use keywords to extract f…
Browse files Browse the repository at this point in the history
…rom responses
  • Loading branch information
albertlatacz committed Dec 5, 2016
1 parent 4c6b01d commit b9eb480
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 130 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ apply plugin: 'java'
apply plugin: 'maven-publish'

def projectVersion = System.getenv('PROJECT_VERSION') != null ? System.getenv('PROJECT_VERSION') : 'dev'
def projectCompilerArgs = ["-Xlint:all", "-Xlint:-processing", "-Xlint:-serial", "-Werror"]


compileJava {
options.compilerArgs = projectCompilerArgs
}

compileTestJava {
options.compilerArgs = projectCompilerArgs
}

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/javarepl/EvaluationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ public EvaluationContext addExpression(Expression expression) {
}

public EvaluationContext removeExpressionWithKey(String key) {
return new EvaluationContext(outputDirectory, expressions.filter(where(Expression.functions.key(), not(key))), results, lastSource);
return new EvaluationContext(outputDirectory, expressions.filter(where(Expression::key, not(key))), results, lastSource);
}
}
6 changes: 3 additions & 3 deletions src/javarepl/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public final Option<java.lang.reflect.Type> typeOfExpression(String expression)
return expressionType;
}

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

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

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

compile(outputJavaFile);

Class expressionClass = classLoader.loadClass(className);
Class<?> expressionClass = classLoader.loadClass(className);

return expressionClass.getDeclaredMethods()[0].getReturnType();
}
Expand Down
8 changes: 4 additions & 4 deletions src/javarepl/ExpressionCompilationException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@

public class ExpressionCompilationException extends Exception {

public ExpressionCompilationException(File file, Iterable<? extends Diagnostic> diagnostics) {
public ExpressionCompilationException(File file, Iterable<? extends Diagnostic<?>> diagnostics) {
this(diagnosticsAsMessage(file, diagnostics));
}

public ExpressionCompilationException(String message) {
super(message);
}

private static String diagnosticsAsMessage(final File file, final Iterable<? extends Diagnostic> diagnostics) {
private static String diagnosticsAsMessage(final File file, final Iterable<? extends Diagnostic<?>> diagnostics) {
return sequence(diagnostics)
.filter(isError())
.map(diagnosticToMessage(file))
.toString("\n");
}

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

private static Predicate<Diagnostic> isError() {
private static Predicate<Diagnostic<?>> isError() {
return diagnostic -> diagnostic.getKind() == ERROR;
}
}
4 changes: 2 additions & 2 deletions src/javarepl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static String randomIdentifier(String prefix) {
}

public static Type extractType(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
if (type instanceof Class<?>) {
Class<?> clazz = (Class<?>) type;
if (clazz.isAnonymousClass() || clazz.isSynthetic() || clazz.isMemberClass()) {
if (clazz.getGenericSuperclass().equals(Object.class)) {
return extractType(sequence(clazz.getGenericInterfaces())
Expand Down
6 changes: 5 additions & 1 deletion src/javarepl/client/EvaluationLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

public class EvaluationLog {
public static enum Type {
INFO, SUCCESS, ERROR
INFO, SUCCESS, ERROR;

public static Type type(String type) {
return valueOf(type);
}
}

private final Type type;
Expand Down
60 changes: 43 additions & 17 deletions src/javarepl/client/JavaREPLClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import com.googlecode.totallylazy.collections.Keyword;
import com.googlecode.totallylazy.functions.Function1;
import com.googlecode.totallylazy.numbers.Integers;
import com.googlecode.totallylazy.numbers.Numbers;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
import javarepl.completion.CompletionResult;
Expand All @@ -14,15 +18,33 @@

import static com.googlecode.totallylazy.Option.none;
import static com.googlecode.totallylazy.Option.some;
import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.totallylazy.collections.Keyword.keyword;
import static com.googlecode.totallylazy.json.Json.map;
import static com.googlecode.utterlyidle.RequestBuilder.get;
import static com.googlecode.utterlyidle.RequestBuilder.post;
import static javarepl.client.EvaluationLog.Type;
import static com.googlecode.totallylazy.reflection.Types.classOf;
import static com.googlecode.totallylazy.reflection.Types.parameterizedType;
import static com.googlecode.utterlyidle.Request.get;
import static com.googlecode.utterlyidle.Request.post;
import static javarepl.client.EvaluationLog.Type.type;
import static javarepl.completion.CompletionResult.methods.fromJson;
import static javarepl.console.ConsoleStatus.Idle;
import static javarepl.console.ConsoleStatus.consoleStatus;

public final class JavaREPLClient {

public static final Keyword<String> EXPRESSION = keyword("expression", String.class);
public static final Keyword<String> TEMPLATE = keyword("template", String.class);
public static final Keyword<String> TOKEN = keyword("token", String.class);
public static final Keyword<String> TYPE = keyword("type", String.class);
public static final Keyword<String> MESSAGE = keyword("message", String.class);
public static final Keyword<String> STATUS = keyword("status", String.class);
public static final Keyword<String> VERSION = keyword("version", String.class);
public static final Keyword<String> VALUE = keyword("value", String.class);
public static final Keyword<String> POSITION = keyword("position", String.class);
public static final Keyword<List<String>> HISTORY = keyword("history", classOf(parameterizedType(List.class, String.class)));
public static final Keyword<List<String>> FORMS = keyword("forms", classOf(parameterizedType(List.class, String.class)));
public static final Keyword<List<Map<String, Object>>> LOGS = keyword("logs", classOf(parameterizedType(List.class, parameterizedType(Map.class, String.class, Object.class))));
public static final Keyword<List<Map<String, Object>>> CANDIDATES = keyword("candidates", classOf(parameterizedType(List.class, parameterizedType(Map.class, String.class, Object.class))));

private final String hostname;
private final Integer port;
private final ClientHttpHandler client;
Expand All @@ -34,52 +56,56 @@ public JavaREPLClient(String hostname, Integer port) {
}

public synchronized ExpressionTemplate template(String expression) throws Exception {
Map<String, Object> model = map(client.handle(get(url("template")).query("expression", expression).build()).entity().toString());
return new ExpressionTemplate(model.get("template").toString(), model.get("token").toString());
Map<String, Object> response = map(client.handle(get(url("template")).query("expression", expression)).entity().toString());
String template = TEMPLATE.call(response);
String token = TOKEN.call(response);
return new ExpressionTemplate(template, token);
}


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

if (json.isEmpty())
return none();

Map<String, Object> model = map(json);
Sequence<Map<String, Object>> logs = sequence((List<Object>)model.get("logs")).unsafeCast();
String expression = model.get("expression").toString();
Map<String, Object> response = map(json);
Sequence<Map<String, Object>> logs = LOGS.map(Sequences::sequence).call(response);
String expression = EXPRESSION.call(response);

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

public synchronized CompletionResult completions(String expr) throws Exception {
return fromJson(client.handle(get(url("completions")).query("expression", expr).build()).entity().toString());
return fromJson(client.handle(get(url("completions")).query("expression", expr)).entity().toString());
}

public synchronized ConsoleStatus status() {
try {
return ConsoleStatus.valueOf(map(client.handle(get(url("status")).build()).entity().toString()).get("status").toString());
Map<String, Object> response = map(client.handle(get(url("status"))).entity().toString());
return consoleStatus(STATUS.call(response));
} catch (Exception e) {
return Idle;
}
}

public synchronized String version() {
try {
return map(client.handle(get(url("version")).build()).entity().toString()).get("version").toString();
Map<String, Object> response = map(client.handle(get(url("version"))).entity().toString());
return VERSION.call(response);
} catch (Exception e) {
return "[unknown]";
}
}

public synchronized Sequence<String> history() throws Exception {
Response history = client.handle(get(url("history")).build());
Map<String, Object> model = map(history.entity().toString());
return sequence((List<Object>)model.get("history")).safeCast(String.class);
Response history = client.handle(get(url("history")));
Map<String, Object> response = map(history.entity().toString());
return HISTORY.map(Sequences::sequence).call(response);
}

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

private String url(String path) {
Expand Down
17 changes: 12 additions & 5 deletions src/javarepl/completion/CompletionResult.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package javarepl.completion;

import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import com.googlecode.totallylazy.json.Json;
import javarepl.client.JavaREPLClient;

import java.util.List;
import java.util.Map;
Expand All @@ -10,6 +12,7 @@
import static com.googlecode.totallylazy.collections.PersistentMap.constructors.emptyMap;
import static com.googlecode.totallylazy.collections.PersistentMap.constructors.map;
import static com.googlecode.totallylazy.json.Json.json;
import static javarepl.client.JavaREPLClient.*;


public class CompletionResult {
Expand Down Expand Up @@ -68,11 +71,15 @@ public static String toJson(CompletionResult result) {
}

public static CompletionResult fromJson(String json) {
Map<String, Object> model = map(Json.map(json));
return new CompletionResult(model.get("expression").toString(),
Integer.valueOf(model.get("position").toString()),
sequence((List<Map<String, Object>>)model.get("candidates"))
.map(model1 -> new CompletionCandidate(model1.get("value").toString(), sequence((List<String>)model1.get("forms")))));
Map<String, Object> jsonMap = map(Json.map(json));

Sequence<Map<String, Object>> candidates = CANDIDATES.map(Sequences::sequence).apply(jsonMap);

return new CompletionResult(
EXPRESSION.apply(jsonMap),
POSITION.map(Integer::valueOf).apply(jsonMap),
candidates.map(candidate -> new CompletionCandidate(VALUE.apply(candidate), FORMS.map(Sequences::sequence).apply(candidate)))
);
}
}
}
36 changes: 13 additions & 23 deletions src/javarepl/completion/Completions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

public class Completions {

public static Function1<Character, Integer> lastIndexOf(final String string) {
return character -> string.lastIndexOf(character);
}

public static Function1<MemberReflection, String> candidateName() {
return memberReflection -> new match<MemberReflection, String>() {
public static Function1<MemberReflection<?>, String> candidateName() {
return memberReflection -> new match<MemberReflection<?>, String>() {
String value(MethodReflection expr) {
return expr.name() + "(";
}
Expand All @@ -30,15 +26,15 @@ String value(ClassReflection expr) {
return expr.member().getSimpleName();
}

String value(MemberReflection expr) {
String value(MemberReflection<?> expr) {
return expr.name();
}
}.apply(memberReflection).get();
}


public static Function1<MemberReflection, String> candidateForm() {
return memberReflection -> new match<MemberReflection, String>() {
public static Function1<MemberReflection<?>, String> candidateForm() {
return memberReflection -> new match<MemberReflection<?>, String>() {
String value(MethodReflection expr) {
return genericMethodSignature(expr.member());
}
Expand All @@ -47,37 +43,31 @@ String value(ClassReflection expr) {
return expr.member().getSimpleName();
}

String value(MemberReflection expr) {
String value(MemberReflection<?> expr) {
return expr.name();
}
}.apply(memberReflection).get();
}


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

private static Function1<Type, String> asSimpleGenericTypeSignature() {
return type -> renderSimpleGenericType(type);
}


private static String renderSimpleGenericType(Type type) {
return new match<Type, String>() {
String value(ParameterizedType t) {
return renderSimpleGenericType(t.getRawType()) + sequence(t.getActualTypeArguments()).map(asSimpleGenericTypeSignature()).toString("<", ", ", ">");
return renderSimpleGenericType(t.getRawType()) + sequence(t.getActualTypeArguments()).map(Completions::renderSimpleGenericType).toString("<", ", ", ">");
}

String value(TypeVariable t) {
String value(TypeVariable<?> t) {
return t.getName();
}

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

String value(Class t) {
String value(Class<?> t) {
return t.getSimpleName();
}

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

sequence(method.getGenericParameterTypes())
.map(asSimpleGenericTypeSignature())
.map(Completions::renderSimpleGenericType)
.toString("(", ", ", ")") +

sequence(method.getGenericExceptionTypes())
.map(asSimpleGenericTypeSignature())
.map(Completions::renderSimpleGenericType)
.toString(method.getGenericExceptionTypes().length > 0 ? " throws " : "", ", ", "");
}

public static int lastIndexOfSeparator(Sequence<Character> characters, String expression) {
return characters
.map(lastIndexOf(expression))
.map(expression::lastIndexOf)
.reduce(maximum())
.intValue();
}
Expand Down
4 changes: 2 additions & 2 deletions src/javarepl/completion/ConsoleCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.googlecode.totallylazy.functions.Function1;
import javarepl.Evaluator;
import javarepl.Result;
import javarepl.expressions.Expression;
import javarepl.expressions.Import;
import javarepl.expressions.Method;
import javarepl.expressions.Type;
Expand All @@ -12,7 +13,6 @@
import static com.googlecode.totallylazy.predicates.Predicates.in;
import static com.googlecode.totallylazy.predicates.Predicates.where;
import static javarepl.completion.CompletionCandidate.asCompletionCandidate;
import static javarepl.expressions.Expression.functions.key;

public class ConsoleCompleter extends Completer {
private final Evaluator evaluator;
Expand Down Expand Up @@ -44,7 +44,7 @@ private Sequence<String> methods() {
}

private Sequence<String> types() {
return evaluator.expressionsOfType(Type.class).map(key());
return evaluator.expressionsOfType(Type.class).map(Expression::key);
}

private Sequence<String> imports() {
Expand Down
Loading

0 comments on commit b9eb480

Please sign in to comment.