1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  * 
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */ 
  24 package sun.evtracing.processing;
  25 
  26 import static java.lang.System.err;
  27 
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.Modifier;
  30 import java.util.HashMap;
  31 
  32 public class Sanity {
  33 
  34         private static final boolean PRINT_INDIVIDUAL_WARNINGS = Boolean.parseBoolean(System.getProperty("sun.evtracing.printIndividualWarnings"));
  35 
  36         private static class Counter {
  37                 private int value = 0;
  38                 public void inc() { value++; }
  39                 public int get() { return value; }
  40         }
  41         private static HashMap<String, Counter> WARNINGS;
  42 
  43         public static boolean isEnabled() {
  44                 boolean ea = false;
  45                 assert (ea = true);
  46                 return ea;
  47         }
  48 
  49         /**
  50          * Non-failing assert for conditions that we expect to be true, but might be
  51          * violated in rare cases, especially because of magic happening during VM
  52          * startup.
  53          */
  54         public static void warnIf(boolean condition, String format, Object... args) {
  55                 if (condition) {
  56                         warn(format, args);
  57                 }
  58         }
  59 
  60         /**
  61          * Print a warning if assertions are enabled.
  62          */
  63         public static void warn(String format, Object... args) {
  64                 if (isEnabled()) {
  65                         if (PRINT_INDIVIDUAL_WARNINGS) {
  66                                 err.printf(format, args);
  67                                 err.println();
  68                         } else {
  69                                 if (WARNINGS == null) {
  70                                         WARNINGS = new HashMap<>();
  71                                 }
  72                                 Counter ctr = WARNINGS.computeIfAbsent(format, f -> new Counter());
  73                                 ctr.inc();
  74                         }
  75                 }
  76         }
  77 
  78         public static void printCollectedWarnings() {
  79                 if (WARNINGS != null) {
  80                         WARNINGS.entrySet().stream()
  81                                 .sorted((x, y) -> y.getValue().get() - x.getValue().get())
  82                                 .forEach(e -> err.printf("%6d  %s%n", e.getValue().get(), e.getKey()));
  83                 }
  84         }
  85 
  86         public static void assertNoInheritedAbstractMethods(Class<?> clazz) {
  87                 for (Method m : clazz.getMethods()) {
  88                         if (Modifier.isAbstract(m.getModifiers()) && !clazz.equals(m.getDeclaringClass())) {
  89                                 throw new AssertionError("class must implement all abstract methods from superclasses", null);
  90                         }
  91                 }
  92         }
  93 }