Use null instead of CompilerMessageLocation.NO_LOCATION in MessageCollector

This commit is contained in:
Alexander Udalov
2017-03-31 17:39:03 +03:00
parent 411a8dc206
commit 861d9a1620
46 changed files with 279 additions and 390 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.cli.common.messages;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Predicate;
@@ -35,9 +36,7 @@ public class FilteringMessageCollector implements MessageCollector {
}
@Override
public void report(
@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location
) {
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
if (!decline.test(severity)) {
messageCollector.report(severity, message, location);
}
@@ -19,14 +19,11 @@ package org.jetbrains.kotlin.cli.common.messages;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
public class GroupingMessageCollector implements MessageCollector {
private final MessageCollector delegate;
// File path (nullable) -> message
@@ -45,25 +42,19 @@ public class GroupingMessageCollector implements MessageCollector {
public void report(
@NotNull CompilerMessageSeverity severity,
@NotNull String message,
@NotNull CompilerMessageLocation location
@Nullable CompilerMessageLocation location
) {
if (CompilerMessageSeverity.VERBOSE.contains(severity)) {
delegate.report(severity, message, location);
}
else {
groupedMessages.put(location.getPath(), new Message(severity, message, location));
groupedMessages.put(location != null ? location.getPath() : null, new Message(severity, message, location));
}
}
@Override
public boolean hasErrors() {
for (Map.Entry<String, Message> entry : groupedMessages.entries()) {
if (entry.getValue().severity.isError()) {
return true;
}
}
return false;
return groupedMessages.entries().stream().anyMatch(entry -> entry.getValue().severity.isError());
}
public void flush() {
@@ -82,14 +73,9 @@ public class GroupingMessageCollector implements MessageCollector {
@NotNull
private Collection<String> sortedKeys() {
List<String> sortedKeys = new ArrayList<>(groupedMessages.keySet());
// ensure that messages with no location i.e. perf, incomplete hierarchy are always reported first
sortedKeys.sort((o1, o2) -> {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
});
List<String> sortedKeys = new ArrayList<>(groupedMessages.keySet());
sortedKeys.sort(Comparator.nullsFirst(Comparator.naturalOrder()));
return sortedKeys;
}
@@ -98,7 +84,7 @@ public class GroupingMessageCollector implements MessageCollector {
private final String message;
private final CompilerMessageLocation location;
private Message(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
private Message(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
this.severity = severity;
this.message = message;
this.location = location;
@@ -109,11 +95,11 @@ public class GroupingMessageCollector implements MessageCollector {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Message message1 = (Message) o;
Message other = (Message) o;
if (!location.equals(message1.location)) return false;
if (!message.equals(message1.message)) return false;
if (severity != message1.severity) return false;
if (!Objects.equals(location, other.location)) return false;
if (!message.equals(other.message)) return false;
if (severity != other.severity) return false;
return true;
}
@@ -122,8 +108,13 @@ public class GroupingMessageCollector implements MessageCollector {
public int hashCode() {
int result = severity.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + location.hashCode();
result = 31 * result + (location != null ? location.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[" + severity + "] " + message + (location != null ? " (at " + location + ")" : " (no location)");
}
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.cli.common.messages;
import org.jetbrains.annotations.NotNull;
public interface MessageCollector {
MessageCollector NONE = new MessageCollector() {
@Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
// Do nothing
}
@Override
public void clear() {
// Do nothing
}
@Override
public boolean hasErrors() {
return false;
}
};
void clear();
void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location);
boolean hasErrors();
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.cli.common.messages
interface MessageCollector {
fun clear()
fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation? = null)
fun hasErrors(): Boolean
companion object {
val NONE: MessageCollector = object : MessageCollector {
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
// Do nothing
}
override fun clear() {
// Do nothing
}
override fun hasErrors(): Boolean = false
}
}
}
@@ -20,7 +20,6 @@ import org.jetbrains.annotations.NotNull;
public class MessageCollectorUtil {
public static void reportException(@NotNull MessageCollector messageCollector, @NotNull Throwable exception) {
messageCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(exception),
CompilerMessageLocation.NO_LOCATION);
messageCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(exception), null);
}
}
@@ -35,7 +35,6 @@ import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.util.List;
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
public class ModuleXmlParser {
@@ -103,7 +102,7 @@ public class ModuleXmlParser {
MessageCollectorUtil.reportException(messageCollector, e);
}
catch (SAXException e) {
messageCollector.report(ERROR, OutputMessageUtil.renderException(e), NO_LOCATION);
messageCollector.report(ERROR, OutputMessageUtil.renderException(e), null);
}
return ModuleScriptData.EMPTY;
}