001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package org.eclipse.golo.runtime;
012
013import gololang.Tuple;
014
015import java.util.Arrays;
016import java.util.HashSet;
017
018import static gololang.Messages.message;
019import static gololang.Messages.warning;
020import static org.eclipse.golo.cli.command.Metadata.GUIDE_BASE;
021
022/**
023 * A static class to deal with several kinds of warnings.
024 */
025public final class Warnings {
026  private Warnings() {
027    // utility class
028  }
029
030  private static final boolean NO_PARAMETER_NAMES = load("golo.warnings.no-parameter-names", "true");
031  private static final boolean UNAVAILABLE_CLASS = load("golo.warnings.unavailable-class", "false");
032  private static final boolean DEPRECATED = load("golo.warnings.deprecated", "true");
033  private static final boolean MULTIPLE_PACKAGE_DESCRIPTION = load("golo.warnings.doc.multiple-package-desc", "true");
034  private static final HashSet<Tuple> SEEN_DEPRECATIONS = new HashSet<>();
035
036  private static boolean load(String property, String def) {
037    return Boolean.valueOf(System.getProperty(property, def));
038  }
039
040  public static void multiplePackageDescription(String packageName) {
041    if (MULTIPLE_PACKAGE_DESCRIPTION) {
042      warning(message("multiple_package_desc", packageName, GUIDE_BASE));
043    }
044  }
045
046  public static void noParameterNames(String methodName, String[] argumentNames) {
047    if (NO_PARAMETER_NAMES || gololang.Runtime.debugMode()) {
048      warning(message("no_parameter_names", methodName, Arrays.toString(argumentNames), GUIDE_BASE));
049    }
050  }
051
052  public static void unavailableClass(String className, String callerModule) {
053    if ((UNAVAILABLE_CLASS || gololang.Runtime.debugMode()) && !className.startsWith("java.lang") && !className.startsWith("gololang")) {
054      warning(message("unavailable_class", className, callerModule, GUIDE_BASE));
055    }
056  }
057
058  public static void deprecatedElement(String object, String caller) {
059    if (DEPRECATED) {
060      Tuple seen = new Tuple(object, caller);
061      if (!SEEN_DEPRECATIONS.contains(seen)) {
062        SEEN_DEPRECATIONS.add(seen);
063        warning(message("deprecated_element", object, caller, GUIDE_BASE));
064      }
065    }
066  }
067}