diff --git a/build.gradle b/build.gradle index df0f2cc..082457a 100644 --- a/build.gradle +++ b/build.gradle @@ -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() diff --git a/src/javarepl/EvaluationContext.java b/src/javarepl/EvaluationContext.java index f31051d..3c17d20 100644 --- a/src/javarepl/EvaluationContext.java +++ b/src/javarepl/EvaluationContext.java @@ -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); } } diff --git a/src/javarepl/Evaluator.java b/src/javarepl/Evaluator.java index 2a042c3..8532028 100644 --- a/src/javarepl/Evaluator.java +++ b/src/javarepl/Evaluator.java @@ -170,7 +170,7 @@ public final Option typeOfExpression(String expression) return expressionType; } - public final Option classFrom(String expression) { + public final Option> classFrom(String expression) { try { return some(detectClass(expression)); } catch (Throwable e) { @@ -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"); @@ -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(); } diff --git a/src/javarepl/ExpressionCompilationException.java b/src/javarepl/ExpressionCompilationException.java index c0a4a56..dc1a29b 100644 --- a/src/javarepl/ExpressionCompilationException.java +++ b/src/javarepl/ExpressionCompilationException.java @@ -16,7 +16,7 @@ public class ExpressionCompilationException extends Exception { - public ExpressionCompilationException(File file, Iterable diagnostics) { + public ExpressionCompilationException(File file, Iterable> diagnostics) { this(diagnosticsAsMessage(file, diagnostics)); } @@ -24,14 +24,14 @@ public ExpressionCompilationException(String message) { super(message); } - private static String diagnosticsAsMessage(final File file, final Iterable diagnostics) { + private static String diagnosticsAsMessage(final File file, final Iterable> diagnostics) { return sequence(diagnostics) .filter(isError()) .map(diagnosticToMessage(file)) .toString("\n"); } - private static Function1 diagnosticToMessage(final File file) { + private static Function1, 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("", "", "^"); @@ -42,7 +42,7 @@ private static Function1 diagnosticToMessage(final File file }; } - private static Predicate isError() { + private static Predicate> isError() { return diagnostic -> diagnostic.getKind() == ERROR; } } diff --git a/src/javarepl/Utils.java b/src/javarepl/Utils.java index a141b8d..726bc64 100644 --- a/src/javarepl/Utils.java +++ b/src/javarepl/Utils.java @@ -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()) diff --git a/src/javarepl/client/EvaluationLog.java b/src/javarepl/client/EvaluationLog.java index a8835d1..51fe748 100644 --- a/src/javarepl/client/EvaluationLog.java +++ b/src/javarepl/client/EvaluationLog.java @@ -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; diff --git a/src/javarepl/client/JavaREPLClient.java b/src/javarepl/client/JavaREPLClient.java index cc94b39..4e2eaeb 100644 --- a/src/javarepl/client/JavaREPLClient.java +++ b/src/javarepl/client/JavaREPLClient.java @@ -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; @@ -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 EXPRESSION = keyword("expression", String.class); + public static final Keyword TEMPLATE = keyword("template", String.class); + public static final Keyword TOKEN = keyword("token", String.class); + public static final Keyword TYPE = keyword("type", String.class); + public static final Keyword MESSAGE = keyword("message", String.class); + public static final Keyword STATUS = keyword("status", String.class); + public static final Keyword VERSION = keyword("version", String.class); + public static final Keyword VALUE = keyword("value", String.class); + public static final Keyword POSITION = keyword("position", String.class); + public static final Keyword> HISTORY = keyword("history", classOf(parameterizedType(List.class, String.class))); + public static final Keyword> FORMS = keyword("forms", classOf(parameterizedType(List.class, String.class))); + public static final Keyword>> LOGS = keyword("logs", classOf(parameterizedType(List.class, parameterizedType(Map.class, String.class, Object.class)))); + public static final Keyword>> 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; @@ -34,31 +56,34 @@ public JavaREPLClient(String hostname, Integer port) { } public synchronized ExpressionTemplate template(String expression) throws Exception { - Map 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 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 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 model = map(json); - Sequence> logs = sequence((List)model.get("logs")).unsafeCast(); - String expression = model.get("expression").toString(); + Map response = map(json); + Sequence> 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 response = map(client.handle(get(url("status"))).entity().toString()); + return consoleStatus(STATUS.call(response)); } catch (Exception e) { return Idle; } @@ -66,20 +91,21 @@ public synchronized ConsoleStatus status() { public synchronized String version() { try { - return map(client.handle(get(url("version")).build()).entity().toString()).get("version").toString(); + Map response = map(client.handle(get(url("version"))).entity().toString()); + return VERSION.call(response); } catch (Exception e) { return "[unknown]"; } } public synchronized Sequence history() throws Exception { - Response history = client.handle(get(url("history")).build()); - Map model = map(history.entity().toString()); - return sequence((List)model.get("history")).safeCast(String.class); + Response history = client.handle(get(url("history"))); + Map response = map(history.entity().toString()); + return HISTORY.map(Sequences::sequence).call(response); } private Function1, 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) { diff --git a/src/javarepl/completion/CompletionResult.java b/src/javarepl/completion/CompletionResult.java index be4978d..e7bc5b8 100644 --- a/src/javarepl/completion/CompletionResult.java +++ b/src/javarepl/completion/CompletionResult.java @@ -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; @@ -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 { @@ -68,11 +71,15 @@ public static String toJson(CompletionResult result) { } public static CompletionResult fromJson(String json) { - Map model = map(Json.map(json)); - return new CompletionResult(model.get("expression").toString(), - Integer.valueOf(model.get("position").toString()), - sequence((List>)model.get("candidates")) - .map(model1 -> new CompletionCandidate(model1.get("value").toString(), sequence((List)model1.get("forms"))))); + Map jsonMap = map(Json.map(json)); + + Sequence> 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))) + ); } } } diff --git a/src/javarepl/completion/Completions.java b/src/javarepl/completion/Completions.java index f5751fa..20f67ec 100644 --- a/src/javarepl/completion/Completions.java +++ b/src/javarepl/completion/Completions.java @@ -16,12 +16,8 @@ public class Completions { - public static Function1 lastIndexOf(final String string) { - return character -> string.lastIndexOf(character); - } - - public static Function1 candidateName() { - return memberReflection -> new match() { + public static Function1, String> candidateName() { + return memberReflection -> new match, String>() { String value(MethodReflection expr) { return expr.name() + "("; } @@ -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 candidateForm() { - return memberReflection -> new match() { + public static Function1, String> candidateForm() { + return memberReflection -> new match, String>() { String value(MethodReflection expr) { return genericMethodSignature(expr.member()); } @@ -47,29 +43,23 @@ 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, CompletionCandidate> candidate() { + public static Function1>, CompletionCandidate> candidate() { return group -> new CompletionCandidate(group.key(), group.map(candidateForm()).sort(Comparators.ascending(String.class))); } - private static Function1 asSimpleGenericTypeSignature() { - return type -> renderSimpleGenericType(type); - } - - private static String renderSimpleGenericType(Type type) { return new match() { 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(); } @@ -77,7 +67,7 @@ String value(GenericArrayType t) { return renderSimpleGenericType(t.getGenericComponentType()) + "[]"; } - String value(Class t) { + String value(Class t) { return t.getSimpleName(); } @@ -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 characters, String expression) { return characters - .map(lastIndexOf(expression)) + .map(expression::lastIndexOf) .reduce(maximum()) .intValue(); } diff --git a/src/javarepl/completion/ConsoleCompleter.java b/src/javarepl/completion/ConsoleCompleter.java index b9de00a..62e97aa 100644 --- a/src/javarepl/completion/ConsoleCompleter.java +++ b/src/javarepl/completion/ConsoleCompleter.java @@ -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; @@ -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; @@ -44,7 +44,7 @@ private Sequence methods() { } private Sequence types() { - return evaluator.expressionsOfType(Type.class).map(key()); + return evaluator.expressionsOfType(Type.class).map(Expression::key); } private Sequence imports() { diff --git a/src/javarepl/completion/InstanceMemberCompleter.java b/src/javarepl/completion/InstanceMemberCompleter.java index 869e120..7dbd9c4 100644 --- a/src/javarepl/completion/InstanceMemberCompleter.java +++ b/src/javarepl/completion/InstanceMemberCompleter.java @@ -41,10 +41,11 @@ public CompletionResult call(String expression) throws Exception { if (aClass.isDefined()) { ClassReflection classReflection = reflectionOf(aClass.get()); - Sequence join = Sequences.empty(MemberReflection.class) + Sequence> join = Sequences.empty() .join(classReflection.declaredFields()) .join(classReflection.declaredMethods()) - .unique(); + .unique() + .unsafeCast(); Sequence candidates = join .filter(isPublic().and(not(isStatic()))) diff --git a/src/javarepl/completion/StaticMemberCompleter.java b/src/javarepl/completion/StaticMemberCompleter.java index e66b882..45df947 100644 --- a/src/javarepl/completion/StaticMemberCompleter.java +++ b/src/javarepl/completion/StaticMemberCompleter.java @@ -63,7 +63,7 @@ private Option, String>> completionFor(String expression) { private Option>> parseExpression(Pair> expression) { - Option expressionClass = evaluator.classFrom(expression.first()); + Option> expressionClass = evaluator.classFrom(expression.first()); if (!expressionClass.isEmpty()) { return some(expression); diff --git a/src/javarepl/console/ConsoleConfig.java b/src/javarepl/console/ConsoleConfig.java index c75a13b..355343a 100644 --- a/src/javarepl/console/ConsoleConfig.java +++ b/src/javarepl/console/ConsoleConfig.java @@ -13,6 +13,7 @@ import static com.googlecode.totallylazy.Option.none; import static com.googlecode.totallylazy.Option.option; import static com.googlecode.totallylazy.Sequences.empty; +import static com.googlecode.totallylazy.Sequences.one; import static com.googlecode.totallylazy.Sequences.sequence; public final class ConsoleConfig { @@ -53,8 +54,10 @@ public ConsoleConfig expressions(String... expressions) { return new ConsoleConfig(historyFile, sequence(expressions), logger, commands, results, sandboxed); } - public ConsoleConfig commands(Class... cmds) { - return new ConsoleConfig(historyFile, expressions, logger, sequence(cmds).>unsafeCast(), results, sandboxed); + @SafeVarargs + @SuppressWarnings("varargs") + public final ConsoleConfig commands(Class... cmds) { + return new ConsoleConfig(historyFile, expressions, logger, sequence(cmds).unsafeCast(), results, sandboxed); } public ConsoleConfig results(Result... results) { diff --git a/src/javarepl/console/ConsoleStatus.java b/src/javarepl/console/ConsoleStatus.java index 770e75d..67a3bb8 100644 --- a/src/javarepl/console/ConsoleStatus.java +++ b/src/javarepl/console/ConsoleStatus.java @@ -3,6 +3,10 @@ public enum ConsoleStatus { Idle, Starting, Running, Terminating, Terminated; + public static ConsoleStatus consoleStatus(String consoleStatus) { + return valueOf(consoleStatus); + } + public Boolean isRunning() { return this == Running; } diff --git a/src/javarepl/expressions/Expression.java b/src/javarepl/expressions/Expression.java index 15a7151..bf0be50 100644 --- a/src/javarepl/expressions/Expression.java +++ b/src/javarepl/expressions/Expression.java @@ -34,13 +34,9 @@ public boolean equals(Object other) { public static final class functions { public static Function1 source() { - return value -> value.source; + return Expression::source; } - public static Function1 key() { - return Expression::key; - } } - } diff --git a/src/javarepl/reflection/ClassReflection.java b/src/javarepl/reflection/ClassReflection.java index 5d391d8..e6e2bfb 100644 --- a/src/javarepl/reflection/ClassReflection.java +++ b/src/javarepl/reflection/ClassReflection.java @@ -70,21 +70,23 @@ private Sequence classes() { return sequence(member().getClasses()).map(ClassReflection::new); } - public Sequence declaredMembers() { - return empty(MemberReflection.class) + public Sequence> declaredMembers() { + return empty() .join(declaredClasses()) .join(declaredConstructors()) .join(declaredFields()) .join(declaredMethods()) - .unique(); + .unique() + .unsafeCast(); } - public Sequence members() { - return empty(MemberReflection.class) + public Sequence> members() { + return empty() .join(classes()) .join(constructors()) .join(fields()) .join(methods()) - .unique(); + .unique() + .unsafeCast(); } } diff --git a/src/javarepl/reflection/MemberReflection.java b/src/javarepl/reflection/MemberReflection.java index 8a10f28..72aefbb 100644 --- a/src/javarepl/reflection/MemberReflection.java +++ b/src/javarepl/reflection/MemberReflection.java @@ -4,7 +4,7 @@ public abstract class MemberReflection implements AnnotatedReflection { private final T member; - protected MemberReflection(T member) { + MemberReflection(T member) { this.member = member; } diff --git a/src/javarepl/reflection/MemberReflections.java b/src/javarepl/reflection/MemberReflections.java index 0105023..1121c48 100644 --- a/src/javarepl/reflection/MemberReflections.java +++ b/src/javarepl/reflection/MemberReflections.java @@ -7,7 +7,7 @@ public final class MemberReflections { - public static LogicalPredicate modifier(final int modifier) { + public static > LogicalPredicate modifier(final int modifier) { return new LogicalPredicate() { public boolean matches(T member) { return (member.modifiers() & modifier) != 0; @@ -15,71 +15,71 @@ public boolean matches(T member) { }; } - public static LogicalPredicate isAbstract() { + public static > LogicalPredicate isAbstract() { return modifier(ABSTRACT); } - public static LogicalPredicate isFinal() { + public static > LogicalPredicate isFinal() { return modifier(FINAL); } - public static LogicalPredicate isInterface() { + public static > LogicalPredicate isInterface() { return modifier(INTERFACE); } - public static LogicalPredicate isNative() { + public static > LogicalPredicate isNative() { return modifier(NATIVE); } - public static LogicalPredicate isPrivate() { + public static > LogicalPredicate isPrivate() { return modifier(PRIVATE); } - public static LogicalPredicate isProtected() { + public static > LogicalPredicate isProtected() { return modifier(PROTECTED); } - public static LogicalPredicate isPublic() { + public static > LogicalPredicate isPublic() { return modifier(PUBLIC); } - public static LogicalPredicate isStatic() { + public static > LogicalPredicate isStatic() { return modifier(STATIC); } - public static LogicalPredicate isStrict() { + public static > LogicalPredicate isStrict() { return modifier(STRICT); } - public static LogicalPredicate isSynchronized() { + public static > LogicalPredicate isSynchronized() { return modifier(SYNCHRONIZED); } - public static LogicalPredicate isTransient() { + public static > LogicalPredicate isTransient() { return modifier(TRANSIENT); } - public static LogicalPredicate isVolatile() { + public static > LogicalPredicate isVolatile() { return modifier(VOLATILE); } - public static LogicalPredicate isSynthetic() { + public static > LogicalPredicate isSynthetic() { return modifier(0x00001000); } - public static LogicalPredicate isType() { + public static > LogicalPredicate isType() { return instanceOf(ClassReflection.class); } - public static LogicalPredicate isMethod() { + public static > LogicalPredicate isMethod() { return instanceOf(MethodReflection.class); } - public static LogicalPredicate isField() { + public static > LogicalPredicate isField() { return instanceOf(FieldReflection.class); } - public static LogicalPredicate isConstructor() { + public static > LogicalPredicate isConstructor() { return instanceOf(ConstructorReflection.class); } } diff --git a/src/javarepl/rendering/TypeRenderer.java b/src/javarepl/rendering/TypeRenderer.java index 4cd8341..c82c0d2 100644 --- a/src/javarepl/rendering/TypeRenderer.java +++ b/src/javarepl/rendering/TypeRenderer.java @@ -22,12 +22,12 @@ public static String renderType(Type type) { } @multimethod - private static String renderType(Class type) { + private static String renderType(Class type) { return type.getCanonicalName(); } @multimethod - private static String renderType(TypeVariable typeVariable) { + private static String renderType(TypeVariable typeVariable) { return "Object"; } diff --git a/src/javarepl/web/WebConsoleClientHandler.java b/src/javarepl/web/WebConsoleClientHandler.java index 325aae8..7c78e7c 100644 --- a/src/javarepl/web/WebConsoleClientHandler.java +++ b/src/javarepl/web/WebConsoleClientHandler.java @@ -5,7 +5,6 @@ import com.googlecode.totallylazy.Sequences; import com.googlecode.totallylazy.Strings; import com.googlecode.totallylazy.json.Json; -import com.googlecode.utterlyidle.RequestBuilder; import com.googlecode.utterlyidle.Response; import com.googlecode.utterlyidle.Status; import com.googlecode.utterlyidle.handlers.ClientHttpHandler; @@ -17,7 +16,8 @@ import static com.googlecode.totallylazy.Option.none; import static com.googlecode.totallylazy.Option.some; -import static com.googlecode.utterlyidle.RequestBuilder.get; +import static com.googlecode.utterlyidle.Request.get; +import static com.googlecode.utterlyidle.Request.post; import static com.googlecode.utterlyidle.Response.response; import static com.googlecode.utterlyidle.Status.GATEWAY_TIMEOUT; import static com.googlecode.utterlyidle.Status.INTERNAL_SERVER_ERROR; @@ -62,7 +62,7 @@ private void createProcess() { private ConsoleStatus status() { try { - return ConsoleStatus.valueOf(Json.map(new ClientHttpHandler().handle(get("http://localhost:" + port.get() + "/status").build()).entity().toString()) + return ConsoleStatus.valueOf(Json.map(new ClientHttpHandler().handle(get("http://localhost:" + port.get() + "/status")).entity().toString()) .get("status").toString()); } catch (Exception e) { @@ -91,7 +91,7 @@ public Response execute(String expression) { createProcess(); try { - return reportProcessError(new ClientHttpHandler().handle(RequestBuilder.post("http://localhost:" + port.get() + "/" + "execute").form("expression", expression).build())); + return reportProcessError(new ClientHttpHandler().handle(post("http://localhost:" + port.get() + "/" + "execute").form("expression", expression))); } catch (Exception e) { e.printStackTrace(); return response(INTERNAL_SERVER_ERROR); @@ -102,7 +102,7 @@ public Response readExpression(String line) { createProcess(); try { - return reportProcessError(new ClientHttpHandler().handle(RequestBuilder.post("http://localhost:" + port.get() + "/" + "readExpression").form("line", line).build())); + return reportProcessError(new ClientHttpHandler().handle(post("http://localhost:" + port.get() + "/" + "readExpression").form("line", line))); } catch (Exception e) { e.printStackTrace(); return response(INTERNAL_SERVER_ERROR); @@ -113,7 +113,7 @@ public Response completions(String expression) { createProcess(); try { - return reportProcessError(new ClientHttpHandler().handle(RequestBuilder.get("http://localhost:" + port.get() + "/" + "completions").query("expression", expression).build())); + return reportProcessError(new ClientHttpHandler().handle(get("http://localhost:" + port.get() + "/" + "completions").query("expression", expression))); } catch (Exception e) { e.printStackTrace(); return response(INTERNAL_SERVER_ERROR); diff --git a/src/javarepl/web/WebConsoleResource.java b/src/javarepl/web/WebConsoleResource.java index 535f1ac..81639c6 100644 --- a/src/javarepl/web/WebConsoleResource.java +++ b/src/javarepl/web/WebConsoleResource.java @@ -8,7 +8,6 @@ import com.googlecode.utterlyidle.BaseUri; import com.googlecode.utterlyidle.MediaType; import com.googlecode.utterlyidle.Response; -import com.googlecode.utterlyidle.ResponseBuilder; import com.googlecode.utterlyidle.annotations.*; import java.io.File; @@ -21,6 +20,7 @@ import static com.googlecode.totallylazy.Option.some; import static com.googlecode.totallylazy.collections.PersistentMap.constructors.emptyMap; import static com.googlecode.totallylazy.io.URLs.uri; +import static com.googlecode.utterlyidle.Response.ok; import static com.googlecode.utterlyidle.Response.response; import static com.googlecode.utterlyidle.Response.seeOther; import static com.googlecode.utterlyidle.Status.*; @@ -44,7 +44,7 @@ public WebConsoleResource(WebConsole agent, BaseUri baseUri) { @Path("create") @Produces(MediaType.APPLICATION_JSON) public Map create(@FormParam("expression") Option expression, - @FormParam("snap") Option snap) throws Exception { + @FormParam("snap") Option snap) throws Exception { Option initial = snap.isDefined() @@ -111,12 +111,11 @@ public Response snap(@FormParam("id") String id) { Files.write(clientHandler.get().history().toString("\n").getBytes(), snapFile(snapId)); - return ResponseBuilder - .response() + return ok() .entity(emptyMap(String.class, Object.class) .insert("snap", snapId) .insert("uri", snapUri(snapId).toString()) - ).build(); + ); } else { return response(BAD_REQUEST); @@ -130,9 +129,9 @@ public Response snap(@FormParam("id") String id) { public Response getSnap(@PathParam("id") String id) { File snap = snapFile(id); if (snap.exists()) { - return ResponseBuilder.response().entity(Strings.lines(snap).toString("\n")).build(); + return ok().entity(Strings.lines(snap).toString("\n")); } else { - return ResponseBuilder.response(NOT_FOUND).build(); + return response(NOT_FOUND); } } diff --git a/test/javarepl/EvaluatorTest.java b/test/javarepl/EvaluatorTest.java index 7a6d4e5..11ca596 100644 --- a/test/javarepl/EvaluatorTest.java +++ b/test/javarepl/EvaluatorTest.java @@ -73,8 +73,8 @@ public void shouldReturnTypeOfExpression() { @Test public void shouldReturnClassFromExpression() { - assertThat(evaluator().classFrom("String"), is(Option.some(String.class))); - assertThat(evaluator().classFrom("java.util.Map.Entry"), is(Option.some(java.util.Map.Entry.class))); + assertThat(evaluator().classFrom("String"), is(Option.>some(String.class))); + assertThat(evaluator().classFrom("java.util.Map.Entry"), is(Option.>some(java.util.Map.Entry.class))); assertThat(evaluator().classFrom("invalid"), is(none(Class.class))); } @@ -99,6 +99,7 @@ protected Throwable featureValueOf(Either evalu }; } + @SuppressWarnings("unchecked") private static Matcher> hasResult(T value) { return new FeatureMatcher, T>(is(value), "result value", "result value") { protected T featureValueOf(Either evaluation) { diff --git a/test/javarepl/UtilsTest.java b/test/javarepl/UtilsTest.java index 2b98bc2..9664977 100644 --- a/test/javarepl/UtilsTest.java +++ b/test/javarepl/UtilsTest.java @@ -52,12 +52,14 @@ public void returnsResolvedUrl() { } @Test + @SuppressWarnings("unchecked") public void returnsPowerSetPermutations() { assertThat(powerSetPermutations(sequence(1, 2)), containsInAnyOrder(sequence(1), sequence(2), sequence(1, 2), sequence(2, 1), empty(Integer.class))); } @Test + @SuppressWarnings("unchecked") public void returnsAllPermutationsOfTheSet() { assertThat(permutations(sequence(1, 2, 3)), containsInAnyOrder( diff --git a/test/javarepl/console/rest/RestConsoleTest.java b/test/javarepl/console/rest/RestConsoleTest.java index a5ad1b3..d89fad1 100644 --- a/test/javarepl/console/rest/RestConsoleTest.java +++ b/test/javarepl/console/rest/RestConsoleTest.java @@ -13,9 +13,10 @@ import java.util.Map; import static com.googlecode.totallylazy.collections.PersistentMap.constructors.emptyMap; -import static com.googlecode.utterlyidle.RequestBuilder.get; -import static com.googlecode.utterlyidle.RequestBuilder.post; +import static com.googlecode.utterlyidle.Request.get; +import static com.googlecode.utterlyidle.Request.post; import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static javarepl.Utils.applicationVersion; import static javarepl.Utils.randomServerPort; import static javarepl.console.ConsoleStatus.Running; @@ -44,20 +45,20 @@ public void tearDown() throws Exception { @Test public void shouldReturnResultForGivenExpression() throws Exception { - Response response = client.handle(post(url("execute")).form("expression", "life = 42").build()); + Response response = client.handle(post(url("execute")).form("expression", "life = 42")); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class) .insert("expression", "life = 42") - .insert("logs", asList(emptyMap(String.class, Object.class) + .insert("logs", singletonList(emptyMap(String.class, Object.class) .insert("message", "java.lang.Integer life = 42") .insert("type", "SUCCESS"))))); } @Test public void shouldReturnTemplate() throws Exception { - Response response = client.handle(get(url("template")).query("expression", "life = 42").build()); + Response response = client.handle(get(url("template")).query("expression", "life = 42")); assertThat(response.status(), is(Status.OK)); assertThat(body(response).get("template").toString(), @@ -66,11 +67,11 @@ public void shouldReturnTemplate() throws Exception { @Test public void shouldReturnCompletions() throws Exception { - client.handle(post(url("execute")).form("expression", "expr_1 = 42").build()); - client.handle(post(url("execute")).form("expression", "expr_2 = 21").build()); - client.handle(post(url("execute")).form("expression", "expr_3 = 7").build()); + client.handle(post(url("execute")).form("expression", "expr_1 = 42")); + client.handle(post(url("execute")).form("expression", "expr_2 = 21")); + client.handle(post(url("execute")).form("expression", "expr_3 = 7")); - Response response = client.handle(get(url("completions")).query("expression", "prefix expr_").build()); + Response response = client.handle(get(url("completions")).query("expression", "prefix expr_")); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class) @@ -78,18 +79,18 @@ public void shouldReturnCompletions() throws Exception { .insert("position", "7") .insert("candidates", asList( - emptyMap(String.class, Object.class).insert("value", "expr_1").insert("forms", asList("expr_1")), - emptyMap(String.class, Object.class).insert("value", "expr_2").insert("forms", asList("expr_2")), - emptyMap(String.class, Object.class).insert("value", "expr_3").insert("forms", asList("expr_3"))) + emptyMap(String.class, Object.class).insert("value", "expr_1").insert("forms", singletonList("expr_1")), + emptyMap(String.class, Object.class).insert("value", "expr_2").insert("forms", singletonList("expr_2")), + emptyMap(String.class, Object.class).insert("value", "expr_3").insert("forms", singletonList("expr_3"))) ))); } @Test public void shouldReturnHistory() throws Exception { - client.handle(post(url("execute")).form("expression", "life = 42").build()); - client.handle(post(url("execute")).form("expression", ":help").build()); + client.handle(post(url("execute")).form("expression", "life = 42")); + client.handle(post(url("execute")).form("expression", ":help")); - Response response = client.handle(get(url("history")).build()); + Response response = client.handle(get(url("history"))); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class).insert("history", asList("life = 42", ":help")))); @@ -98,7 +99,7 @@ public void shouldReturnHistory() throws Exception { @Test public void shouldReturnCorrectStatus() throws Exception { - Response response = client.handle(get(url("status")).build()); + Response response = client.handle(get(url("status"))); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class) @@ -108,7 +109,7 @@ public void shouldReturnCorrectStatus() throws Exception { @Test public void shouldReturnCorrectVersion() throws Exception { - Response response = client.handle(get(url("version")).build()); + Response response = client.handle(get(url("version"))); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class) @@ -118,11 +119,11 @@ public void shouldReturnCorrectVersion() throws Exception { @Test public void shouldReadExpression() throws Exception { - Response response = client.handle(post(url("readExpression")).form("line", "{").build()); + Response response = client.handle(post(url("readExpression")).form("line", "{")); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class))); - response = client.handle(post(url("readExpression")).form("line", "}").build()); + response = client.handle(post(url("readExpression")).form("line", "}")); assertThat(response.status(), is(Status.OK)); assertThat(body(response), is(emptyMap(String.class, Object.class).insert("expression", "{\n}"))); diff --git a/test/javarepl/reflection/MemberReflectionsTest.java b/test/javarepl/reflection/MemberReflectionsTest.java index f817e8d..826b7b9 100644 --- a/test/javarepl/reflection/MemberReflectionsTest.java +++ b/test/javarepl/reflection/MemberReflectionsTest.java @@ -34,7 +34,7 @@ public void shouldMatchReflectionType() { assertThat(membersMatching(isConstructor()), hasItem(ModifiersTestClass.class.getName())); } - private Sequence membersMatching(LogicalPredicate predicate) { + private Sequence membersMatching(LogicalPredicate> predicate) { return reflectionOf(new ModifiersTestClass()).declaredMembers() .join(reflectionOf(ModifiersTestClass.InterfaceModifiers.class).declaredMembers()) .filter(predicate)