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 java.lang.reflect.InvocationTargetException;
014import java.lang.reflect.Method;
015
016public final class Module {
017
018  private static final Class<?>[] EMPTY_TYPES = new Class<?>[]{};
019  private static final Object[] EMPTY_ARGS = {};
020
021  private Module() {
022    throw new UnsupportedOperationException("Don't instantiate utility classes");
023  }
024
025  static String[] metadata(String name, Class<?> callerClass, Class<?>[] types, Object[] args) {
026    String[] data;
027    try {
028      Method dataMethod = callerClass.getMethod("$" + name, types);
029      data = (String[]) dataMethod.invoke(null, args);
030    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
031      // This can only happen as part of the unit tests, because the lookup does not originate from
032      // a Golo module class, hence it doesn't have a $<name>() static method.
033      data = new String[]{};
034    }
035    return data;
036  }
037
038  public static String[] imports(Class<?> callerClass) {
039    return metadata("imports", callerClass, EMPTY_TYPES, EMPTY_ARGS);
040  }
041
042  public static String[] augmentations(Class<?> callerClass) {
043    return metadata("augmentations", callerClass, EMPTY_TYPES, EMPTY_ARGS);
044  }
045
046  public static String[] augmentationApplications(Class<?> callerClass) {
047    return metadata("augmentationApplications", callerClass, EMPTY_TYPES, EMPTY_ARGS);
048  }
049
050  public static String[] augmentationApplications(Class<?> callerClass, Class<?> receiverClass) {
051    return metadata("augmentationApplications", callerClass,
052        new Class<?>[] {int.class},
053        new Object[]{receiverClass.getName().hashCode()}
054    );
055  }
056}