/*
* Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
*
* This file is part of the Lock Contention Tracing Subsystem for the HotSpot
* Virtual Machine, which is developed at Christian Doppler Laboratory on
* Monitoring and Evolution of Very-Large-Scale Software Systems. Please
* contact us at if you need additional information
* or have any questions.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work. If not, see .
*
*/
package sun.evtracing.processing;
import static java.lang.System.err;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
public class Sanity {
private static final boolean PRINT_INDIVIDUAL_WARNINGS = Boolean.parseBoolean(System.getProperty("sun.evtracing.printIndividualWarnings"));
private static class Counter {
private int value = 0;
public void inc() { value++; }
public int get() { return value; }
}
private static HashMap WARNINGS;
public static boolean isEnabled() {
boolean ea = false;
assert (ea = true);
return ea;
}
/**
* Non-failing assert for conditions that we expect to be true, but might be
* violated in rare cases, especially because of magic happening during VM
* startup.
*/
public static void warnIf(boolean condition, String format, Object... args) {
if (condition) {
warn(format, args);
}
}
/**
* Print a warning if assertions are enabled.
*/
public static void warn(String format, Object... args) {
if (isEnabled()) {
if (PRINT_INDIVIDUAL_WARNINGS) {
err.printf(format, args);
err.println();
} else {
if (WARNINGS == null) {
WARNINGS = new HashMap<>();
}
Counter ctr = WARNINGS.computeIfAbsent(format, f -> new Counter());
ctr.inc();
}
}
}
public static void printCollectedWarnings() {
if (WARNINGS != null) {
WARNINGS.entrySet().stream()
.sorted((x, y) -> y.getValue().get() - x.getValue().get())
.forEach(e -> err.printf("%6d %s%n", e.getValue().get(), e.getKey()));
}
}
public static void assertNoInheritedAbstractMethods(Class> clazz) {
for (Method m : clazz.getMethods()) {
if (Modifier.isAbstract(m.getModifiers()) && !clazz.equals(m.getDeclaringClass())) {
throw new AssertionError("class must implement all abstract methods from superclasses", null);
}
}
}
}