001/*
002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 */
009
010package org.eclipse.golo.runtime;
011
012import java.lang.reflect.InvocationTargetException;
013import java.lang.reflect.Method;
014
015public final class Module {
016
017  private static final Class<?>[] EMPTY_TYPES = new Class<?>[]{};
018  private static final Object[] EMPTY_ARGS = new Object[]{};
019
020  private Module() {
021    throw new UnsupportedOperationException("Don't instantiate utility classes");
022  }
023
024  static String[] metadata(String name, Class<?> callerClass, Class<?>[] types, Object[] args) {
025    String[] data;
026    try {
027      Method dataMethod = callerClass.getMethod("$" + name, types);
028      data = (String[]) dataMethod.invoke(null, args);
029    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
030      // This can only happen as part of the unit tests, because the lookup does not originate from
031      // a Golo module class, hence it doesn't have a $<name>() static method.
032      data = new String[]{};
033    }
034    return data;
035  }
036
037  public static String[] imports(Class<?> callerClass) {
038    return metadata("imports", callerClass, EMPTY_TYPES, EMPTY_ARGS);
039  }
040
041  public static String[] augmentations(Class<?> callerClass) {
042    return metadata("augmentations", callerClass, EMPTY_TYPES, EMPTY_ARGS);
043  }
044
045  public static String[] augmentationApplications(Class<?> callerClass) {
046    return metadata("augmentationApplications", callerClass, EMPTY_TYPES, EMPTY_ARGS);
047  }
048
049  public static String[] augmentationApplications(Class<?> callerClass, Class<?> receiverClass) {
050    return metadata("augmentationApplications", callerClass,
051        new Class<?>[] {int.class},
052        new Object[]{receiverClass.getName().hashCode()}
053    );
054  }
055}